Blob


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"
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;
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 }
46 void
47 error(char *s)
48 {
49 fprint(2, "rio: %s: %r\n", s);
50 if(errorshouldabort)
51 abort();
52 threadexitsall("error");
53 }
55 void*
56 erealloc(void *p, uint n)
57 {
58 p = realloc(p, n);
59 if(p == nil)
60 error("realloc failed");
61 return p;
62 }
64 void*
65 emalloc(uint n)
66 {
67 void *p;
69 p = malloc(n);
70 if(p == nil)
71 error("malloc failed");
72 memset(p, 0, n);
73 return p;
74 }
76 char*
77 estrdup(char *s)
78 {
79 char *p;
81 p = malloc(strlen(s)+1);
82 if(p == nil)
83 error("strdup failed");
84 strcpy(p, s);
85 return p;
86 }
88 int
89 isalnum(Rune c)
90 {
91 /*
92 * Hard to get absolutely right. Use what we know about ASCII
93 * and assume anything above the Latin control characters is
94 * potentially an alphanumeric.
95 */
96 if(c <= ' ')
97 return FALSE;
98 if(0x7F<=c && c<=0xA0)
99 return FALSE;
100 if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
101 return FALSE;
102 return TRUE;
105 Rune*
106 strrune(Rune *s, Rune c)
108 Rune c1;
110 if(c == 0) {
111 while(*s++)
113 return s-1;
116 while(c1 = *s++)
117 if(c1 == c)
118 return s-1;
119 return nil;
122 int
123 min(int a, int b)
125 if(a < b)
126 return a;
127 return b;
130 int
131 max(int a, int b)
133 if(a > b)
134 return a;
135 return b;
138 char*
139 runetobyte(Rune *r, int n, int *ip)
141 char *s;
142 int m;
144 s = emalloc(n*UTFmax+1);
145 m = snprint(s, n*UTFmax+1, "%.*S", n, r);
146 *ip = m;
147 return s;