Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * The authors of this software are Rob Pike and Ken Thompson.  | 
3  |  |  *              Copyright (c) 2002 by Lucent Technologies.  | 
4  |  |  * Permission to use, copy, modify, and distribute this software for any  | 
5  |  |  * purpose without fee is hereby granted, provided that this entire notice  | 
6  |  |  * is included in all copies of any software which is or includes a copy  | 
7  |  |  * or modification of this software and in all copies of the supporting  | 
8  |  |  * documentation for such software.  | 
9  |  |  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED  | 
10  |  |  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY  | 
11  |  |  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY  | 
12  |  |  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.  | 
13  |  |  */  | 
14  |  |  | 
15  |  | #include <stdarg.h>  | 
16  |  | #include <string.h>  | 
17  |  |  | 
18  |  | #include "util/utf.h"  | 
19  |  |  | 
20  |  | namespace re2 { | 
21  |  |  | 
22  |  | enum  | 
23  |  | { | 
24  |  |   Bit1  = 7,  | 
25  |  |   Bitx  = 6,  | 
26  |  |   Bit2  = 5,  | 
27  |  |   Bit3  = 4,  | 
28  |  |   Bit4  = 3,  | 
29  |  |   Bit5  = 2,   | 
30  |  |  | 
31  |  |   T1  = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */  | 
32  |  |   Tx  = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */  | 
33  |  |   T2  = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */  | 
34  |  |   T3  = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */  | 
35  |  |   T4  = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */  | 
36  |  |   T5  = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */  | 
37  |  |  | 
38  |  |   Rune1 = (1<<(Bit1+0*Bitx))-1,   /* 0000 0000 0111 1111 */  | 
39  |  |   Rune2 = (1<<(Bit2+1*Bitx))-1,   /* 0000 0111 1111 1111 */  | 
40  |  |   Rune3 = (1<<(Bit3+2*Bitx))-1,   /* 1111 1111 1111 1111 */  | 
41  |  |   Rune4 = (1<<(Bit4+3*Bitx))-1,  | 
42  |  |                                         /* 0001 1111 1111 1111 1111 1111 */  | 
43  |  |  | 
44  |  |   Maskx = (1<<Bitx)-1,      /* 0011 1111 */  | 
45  |  |   Testx = Maskx ^ 0xFF,     /* 1100 0000 */  | 
46  |  |  | 
47  |  |   Bad = Runeerror,  | 
48  |  | };  | 
49  |  |  | 
50  |  | int  | 
51  |  | chartorune(Rune *rune, const char *str)  | 
52  | 507k  | { | 
53  | 507k  |   int c, c1, c2, c3;  | 
54  | 507k  |   Rune l;  | 
55  |  |  | 
56  |  |   /*  | 
57  |  |    * one character sequence  | 
58  |  |    *  00000-0007F => T1  | 
59  |  |    */  | 
60  | 507k  |   c = *(unsigned char*)str;  | 
61  | 507k  |   if(c < Tx) { | 
62  | 420k  |     *rune = c;  | 
63  | 420k  |     return 1;  | 
64  | 420k  |   }  | 
65  |  |  | 
66  |  |   /*  | 
67  |  |    * two character sequence  | 
68  |  |    *  0080-07FF => T2 Tx  | 
69  |  |    */  | 
70  | 86.7k  |   c1 = *(unsigned char*)(str+1) ^ Tx;  | 
71  | 86.7k  |   if(c1 & Testx)  | 
72  | 795  |     goto bad;  | 
73  | 85.9k  |   if(c < T3) { | 
74  | 85.4k  |     if(c < T2)  | 
75  | 63  |       goto bad;  | 
76  | 85.3k  |     l = ((c << Bitx) | c1) & Rune2;  | 
77  | 85.3k  |     if(l <= Rune1)  | 
78  | 8  |       goto bad;  | 
79  | 85.3k  |     *rune = l;  | 
80  | 85.3k  |     return 2;  | 
81  | 85.3k  |   }  | 
82  |  |  | 
83  |  |   /*  | 
84  |  |    * three character sequence  | 
85  |  |    *  0800-FFFF => T3 Tx Tx  | 
86  |  |    */  | 
87  | 469  |   c2 = *(unsigned char*)(str+2) ^ Tx;  | 
88  | 469  |   if(c2 & Testx)  | 
89  | 25  |     goto bad;  | 
90  | 444  |   if(c < T4) { | 
91  | 164  |     l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;  | 
92  | 164  |     if(l <= Rune2)  | 
93  | 4  |       goto bad;  | 
94  | 160  |     *rune = l;  | 
95  | 160  |     return 3;  | 
96  | 164  |   }  | 
97  |  |  | 
98  |  |   /*  | 
99  |  |    * four character sequence (21-bit value)  | 
100  |  |    *  10000-1FFFFF => T4 Tx Tx Tx  | 
101  |  |    */  | 
102  | 280  |   c3 = *(unsigned char*)(str+3) ^ Tx;  | 
103  | 280  |   if (c3 & Testx)  | 
104  | 9  |     goto bad;  | 
105  | 271  |   if (c < T5) { | 
106  | 267  |     l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;  | 
107  | 267  |     if (l <= Rune3)  | 
108  | 7  |       goto bad;  | 
109  | 260  |     *rune = l;  | 
110  | 260  |     return 4;  | 
111  | 267  |   }  | 
112  |  |  | 
113  |  |   /*  | 
114  |  |    * Support for 5-byte or longer UTF-8 would go here, but  | 
115  |  |    * since we don't have that, we'll just fall through to bad.  | 
116  |  |    */  | 
117  |  |  | 
118  |  |   /*  | 
119  |  |    * bad decoding  | 
120  |  |    */  | 
121  | 915  | bad:  | 
122  | 915  |   *rune = Bad;  | 
123  | 915  |   return 1;  | 
124  | 271  | }  | 
125  |  |  | 
126  |  | int  | 
127  |  | runetochar(char *str, const Rune *rune)  | 
128  | 3.95M  | { | 
129  |  |   /* Runes are signed, so convert to unsigned for range check. */  | 
130  | 3.95M  |   unsigned int c;  | 
131  |  |  | 
132  |  |   /*  | 
133  |  |    * one character sequence  | 
134  |  |    *  00000-0007F => 00-7F  | 
135  |  |    */  | 
136  | 3.95M  |   c = *rune;  | 
137  | 3.95M  |   if(c <= Rune1) { | 
138  | 487k  |     str[0] = static_cast<char>(c);  | 
139  | 487k  |     return 1;  | 
140  | 487k  |   }  | 
141  |  |  | 
142  |  |   /*  | 
143  |  |    * two character sequence  | 
144  |  |    *  0080-07FF => T2 Tx  | 
145  |  |    */  | 
146  | 3.46M  |   if(c <= Rune2) { | 
147  | 630k  |     str[0] = T2 | static_cast<char>(c >> 1*Bitx);  | 
148  | 630k  |     str[1] = Tx | (c & Maskx);  | 
149  | 630k  |     return 2;  | 
150  | 630k  |   }  | 
151  |  |  | 
152  |  |   /*  | 
153  |  |    * If the Rune is out of range, convert it to the error rune.  | 
154  |  |    * Do this test here because the error rune encodes to three bytes.  | 
155  |  |    * Doing it earlier would duplicate work, since an out of range  | 
156  |  |    * Rune wouldn't have fit in one or two bytes.  | 
157  |  |    */  | 
158  | 2.83M  |   if (c > Runemax)  | 
159  | 0  |     c = Runeerror;  | 
160  |  |  | 
161  |  |   /*  | 
162  |  |    * three character sequence  | 
163  |  |    *  0800-FFFF => T3 Tx Tx  | 
164  |  |    */  | 
165  | 2.83M  |   if (c <= Rune3) { | 
166  | 1.60M  |     str[0] = T3 | static_cast<char>(c >> 2*Bitx);  | 
167  | 1.60M  |     str[1] = Tx | ((c >> 1*Bitx) & Maskx);  | 
168  | 1.60M  |     str[2] = Tx | (c & Maskx);  | 
169  | 1.60M  |     return 3;  | 
170  | 1.60M  |   }  | 
171  |  |  | 
172  |  |   /*  | 
173  |  |    * four character sequence (21-bit value)  | 
174  |  |    *     10000-1FFFFF => T4 Tx Tx Tx  | 
175  |  |    */  | 
176  | 1.22M  |   str[0] = T4 | static_cast<char>(c >> 3*Bitx);  | 
177  | 1.22M  |   str[1] = Tx | ((c >> 2*Bitx) & Maskx);  | 
178  | 1.22M  |   str[2] = Tx | ((c >> 1*Bitx) & Maskx);  | 
179  | 1.22M  |   str[3] = Tx | (c & Maskx);  | 
180  | 1.22M  |   return 4;  | 
181  | 2.83M  | }  | 
182  |  |  | 
183  |  | int  | 
184  |  | runelen(Rune rune)  | 
185  | 0  | { | 
186  | 0  |   char str[10];  | 
187  |  | 
  | 
188  | 0  |   return runetochar(str, &rune);  | 
189  | 0  | }  | 
190  |  |  | 
191  |  | int  | 
192  |  | fullrune(const char *str, int n)  | 
193  | 507k  | { | 
194  | 507k  |   if (n > 0) { | 
195  | 507k  |     int c = *(unsigned char*)str;  | 
196  | 507k  |     if (c < Tx)  | 
197  | 420k  |       return 1;  | 
198  | 86.7k  |     if (n > 1) { | 
199  | 86.7k  |       if (c < T3)  | 
200  | 85.8k  |         return 1;  | 
201  | 933  |       if (n > 2) { | 
202  | 916  |         if (c < T4 || n > 3)  | 
203  | 886  |           return 1;  | 
204  | 916  |       }  | 
205  | 933  |     }  | 
206  | 86.7k  |   }  | 
207  | 97  |   return 0;  | 
208  | 507k  | }  | 
209  |  |  | 
210  |  |  | 
211  |  | int  | 
212  |  | utflen(const char *s)  | 
213  | 0  | { | 
214  | 0  |   int c;  | 
215  | 0  |   int n;  | 
216  | 0  |   Rune rune;  | 
217  |  | 
  | 
218  | 0  |   n = 0;  | 
219  | 0  |   for(;;) { | 
220  | 0  |     c = *(unsigned char*)s;  | 
221  | 0  |     if(c < Runeself) { | 
222  | 0  |       if(c == 0)  | 
223  | 0  |         return n;  | 
224  | 0  |       s++;  | 
225  | 0  |     } else  | 
226  | 0  |       s += chartorune(&rune, s);  | 
227  | 0  |     n++;  | 
228  | 0  |   }  | 
229  | 0  |   return 0;  | 
230  | 0  | }  | 
231  |  |  | 
232  |  | char*  | 
233  |  | utfrune(const char *s, Rune c)  | 
234  | 0  | { | 
235  | 0  |   int c1;  | 
236  | 0  |   Rune r;  | 
237  | 0  |   int n;  | 
238  |  | 
  | 
239  | 0  |   if(c < Runesync)   /* not part of utf sequence */  | 
240  | 0  |     return strchr((char*)s, c);  | 
241  |  |  | 
242  | 0  |   for(;;) { | 
243  | 0  |     c1 = *(unsigned char*)s;  | 
244  | 0  |     if(c1 < Runeself) { /* one byte rune */ | 
245  | 0  |       if(c1 == 0)  | 
246  | 0  |         return 0;  | 
247  | 0  |       if(c1 == c)  | 
248  | 0  |         return (char*)s;  | 
249  | 0  |       s++;  | 
250  | 0  |       continue;  | 
251  | 0  |     }  | 
252  | 0  |     n = chartorune(&r, s);  | 
253  | 0  |     if(r == c)  | 
254  | 0  |       return (char*)s;  | 
255  | 0  |     s += n;  | 
256  | 0  |   }  | 
257  | 0  |   return 0;  | 
258  | 0  | }  | 
259  |  |  | 
260  |  | }  // namespace re2  |