]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/rio/util.c
tweak selection criteria
[plan9front.git] / sys / src / cmd / rio / util.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include "dat.h"
11 #include "fns.h"
12
13 void
14 cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
15 {
16         uchar *q;
17         Rune *s;
18         int j, w;
19
20         /*
21          * Always guaranteed that n bytes may be interpreted
22          * without worrying about partial runes.  This may mean
23          * reading up to UTFmax-1 more bytes than n; the caller
24          * knows this.  If n is a firm limit, the caller should
25          * set p[n] = 0.
26          */
27         q = (uchar*)p;
28         s = r;
29         for(j=0; j<n; j+=w){
30                 if(*q < Runeself){
31                         w = 1;
32                         *s = *q++;
33                 }else{
34                         w = chartorune(s, (char*)q);
35                         q += w;
36                 }
37                 if(*s)
38                         s++;
39                 else if(nulls)
40                                 *nulls = TRUE;
41         }
42         *nb = (char*)q-p;
43         *nr = s-r;
44 }
45
46 void
47 error(char *s)
48 {
49         fprint(2, "rio: %s: %r\n", s);
50         if(errorshouldabort)
51                 abort();
52         threadexitsall("error");
53 }
54
55 void*
56 erealloc(void *p, uint n)
57 {
58         p = realloc(p, n);
59         if(p == nil)
60                 error("realloc failed");
61         setrealloctag(p, getcallerpc(&p));
62         return p;
63 }
64
65 void*
66 emalloc(uint n)
67 {
68         void *p;
69
70         p = malloc(n);
71         if(p == nil)
72                 error("malloc failed");
73         setmalloctag(p, getcallerpc(&n));
74         memset(p, 0, n);
75         return p;
76 }
77
78 char*
79 estrdup(char *s)
80 {
81         char *p;
82
83         p = malloc(strlen(s)+1);
84         if(p == nil)
85                 error("strdup failed");
86         setmalloctag(p, getcallerpc(&s));
87         strcpy(p, s);
88         return p;
89 }
90
91 int
92 isalnum(Rune c)
93 {
94         /*
95          * Hard to get absolutely right.  Use what we know about ASCII
96          * and assume anything above the Latin control characters is
97          * potentially an alphanumeric.
98          */
99         if(c <= ' ')
100                 return FALSE;
101         if(0x7F<=c && c<=0xA0)
102                 return FALSE;
103         if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
104                 return FALSE;
105         return TRUE;
106 }
107
108 int
109 isspace(Rune c)
110 {
111         return c == 0 || c == ' ' || c == '\t' ||
112                 c == '\n' || c == '\r' || c == '\v';
113 }
114
115 Rune*
116 strrune(Rune *s, Rune c)
117 {
118         Rune c1;
119
120         if(c == 0) {
121                 while(*s++)
122                         ;
123                 return s-1;
124         }
125
126         while(c1 = *s++)
127                 if(c1 == c)
128                         return s-1;
129         return nil;
130 }
131
132 int
133 min(int a, int b)
134 {
135         if(a < b)
136                 return a;
137         return b;
138 }
139
140 int
141 max(int a, int b)
142 {
143         if(a > b)
144                 return a;
145         return b;
146 }
147
148 char*
149 runetobyte(Rune *r, int n, int *ip)
150 {
151         char *s;
152         int m;
153
154         s = emalloc(n*UTFmax+1);
155         m = snprint(s, n*UTFmax+1, "%.*S", n, r);
156         *ip = m;
157         return s;
158 }
159