src/unicode.c

filter: Statementstmt 86.3% 120 / 139 Branchbran 83.0% 103 / 124 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.7% 223 / 263 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/string_util.c ↑ Summary src/util.c »
1 /* ------------------------------------------------------------------------
2 @NAME : unicode.c
3 @DESCRIPTION: UTF-8 decoding and the Unicode character classes and case
4 mappings btparse needs. The tables in unidata.h are
5 generated at build time from the building perl's Unicode
6 database (scripts/gen_unidata.pl).
7  
8 Every function here is safe on invalid UTF-8: a byte that
9 does not start a valid sequence is treated as a single
10 character with no properties, and is copied unchanged by
11 the case-mapping functions. This keeps "bytes" mode (where
12 the input may be in any encoding) working.
13 -------------------------------------------------------------------------- */
14  
15 #include <string.h>
16 #include "bt_internal.h"
17 #include "unidata.h"
18  
19 #define NELEM(a) (sizeof (a) / sizeof ((a)[0]))
20  
21 const char *
22 2 bt_unicode_version (void)
23 {
24 2 return BT_UNICODE_VERSION;
25 }
26  
27 size_t
28 2619 bt_utf8_decode (const char *str, size_t n, long *cp)
29 {
30 2619 const unsigned char * s = (const unsigned char *) str;
31 unsigned long c;
32 size_t len, i;
33  
34 2619 *cp = -1;
35 2619 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 2619 2619
36 0 return 0;
37 2619 ▶ if (s[0] < 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] < 0x80) 2272 347 2619
38 {
39 2272 *cp = s[0];
40 2272 return 1;
41 }
42 347 ▶ else if (s[0] >= 0xC2 && s[0] <= 0xDF)
Branch: 2 branches
BranchTrueFalseTotal
else if (s[0] >= 0xC2 && s[0] <= 0xDF) 333 14 347
else if (s[0] >= 0xC2 && s[0] <= 0xDF) 296 37 333
43 {
44 296 len = 2; c = s[0] & 0x1F;
45 }
46 51 ▶ else if (s[0] >= 0xE0 && s[0] <= 0xEF)
Branch: 2 branches
BranchTrueFalseTotal
else if (s[0] >= 0xE0 && s[0] <= 0xEF) 37 14 51
else if (s[0] >= 0xE0 && s[0] <= 0xEF) 29 8 37
47 {
48 29 len = 3; c = s[0] & 0x0F;
49 }
50 22 ▶ else if (s[0] >= 0xF0 && s[0] <= 0xF4)
Branch: 2 branches
BranchTrueFalseTotal
else if (s[0] >= 0xF0 && s[0] <= 0xF4) 8 14 22
else if (s[0] >= 0xF0 && s[0] <= 0xF4) 3 5 8
51 {
52 3 len = 4; c = s[0] & 0x07;
53 }
54 else
55 19 return 1; /* invalid lead byte */
56  
57 328 ▶ if (n < len)
Branch: 1 branch
BranchTrueFalseTotal
if (n < len) 0 328 328
58 0 return 1;
59 675 ▶ for (i = 1; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; i++) 358 317 675
60 {
61 358 ▶ if ((s[i] & 0xC0) != 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if ((s[i] & 0xC0) != 0x80) 11 347 358
62 11 return 1;
63 347 c = (c << 6) | (s[i] & 0x3F);
64 }
65 /* reject overlong forms, surrogates and values beyond U+10FFFF */
66 317 ▶ if ((len == 3 && c < 0x800) || (len == 4 && (c < 0x10000 || c > 0x10FFFF))
Branch: 5 branches
BranchTrueFalseTotal
if ((len == 3 && c < 0x800) || (len == 4 && (c < 0x10000 || c > 0x10FFFF)) 24 293 317
if ((len == 3 && c < 0x800) || (len == 4 && (c < 0x10000 || c > 0x10FFFF)) 24 0 24
if ((len == 3 && c < 0x800) || (len == 4 && (c < 0x10000 || c > 0x10FFFF)) 3 314 317
if ((len == 3 && c < 0x800) || (len == 4 && (c < 0x10000 || c > 0x10FFFF)) 3 0 3
if ((len == 3 && c < 0x800) || (len == 4 && (c < 0x10000 || c > 0x10FFFF)) 3 0 3
67 317 ▶ || (c >= 0xD800 && c <= 0xDFFF))
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= 0xD800 && c <= 0xDFFF)) 3 314 317
|| (c >= 0xD800 && c <= 0xDFFF)) 0 3 3
68 0 return 1;
69 317 *cp = (long) c;
70 317 return len;
71 }
72  
73 size_t
74 23 bt_utf8_encode (unsigned long cp, char *buf)
75 {
76 23 unsigned char * b = (unsigned char *) buf;
77  
78 23 ▶ if (cp < 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x80) 3 20 23
79 {
80 3 b[0] = (unsigned char) cp;
81 3 return 1;
82 }
83 20 ▶ if (cp < 0x800)
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x800) 19 1 20
84 {
85 19 b[0] = 0xC0 | (cp >> 6);
86 19 b[1] = 0x80 | (cp & 0x3F);
87 19 return 2;
88 }
89 1 ▶ if (cp < 0x10000)
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x10000) 1 0 1
90 {
91 1 b[0] = 0xE0 | (cp >> 12);
92 1 b[1] = 0x80 | ((cp >> 6) & 0x3F);
93 1 b[2] = 0x80 | (cp & 0x3F);
94 1 return 3;
95 }
96 0 b[0] = 0xF0 | (cp >> 18);
97 0 b[1] = 0x80 | ((cp >> 12) & 0x3F);
98 0 b[2] = 0x80 | ((cp >> 6) & 0x3F);
99 0 b[3] = 0x80 | (cp & 0x3F);
100 0 return 4;
101 }
102  
103 /* Decode the character at a NUL-terminated string. The NUL terminator
104 * stops a truncated sequence, so we never read past it. */
105 static size_t
106 2471 decode_at (const char *s, long *cp)
107 {
108 2471 size_t n = 0;
109  
110 10641 ▶ while (n < 4 && s[n])
Branch: 2 branches
BranchTrueFalseTotal
while (n < 4 && s[n]) 9010 1631 10641
while (n < 4 && s[n]) 8170 840 9010
111 8170 n++;
112 2471 return bt_utf8_decode (s, n, cp);
113 }
114  
115 size_t
116 2364 bt_utf8_charlen (const char *s)
117 {
118 long cp;
119  
120 2364 ▶ if (*s == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 0) 0 2364 2364
121 0 return 0;
122 2364 return decode_at (s, &cp);
123 }
124  
125 size_t
126 0 bt_utf8_length (const char *s, size_t n)
127 {
128 0 size_t i = 0, count = 0;
129 long cp;
130  
131 0 ▶ while (i < n)
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) 0 0 0
132 {
133 0 i += bt_utf8_decode (s + i, n - i, &cp);
134 0 count++;
135 }
136 0 return count;
137 }
138  
139  
140 /* ----------------------------------------------------------------------
141 * Table lookups
142 */
143  
144 static boolean
145 76 in_invlist (const unsigned long *inv, size_t len, long cp)
146 {
147 76 size_t lo = 0, hi = len;
148  
149 76 ▶ if (cp < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0) 0 76 76
150 0 return FALSE;
151 /* find the number of list elements <= cp; odd means "in the set" */
152 869 ▶ while (lo < hi)
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) 793 76 869
153 {
154 793 size_t mid = (lo + hi) / 2;
155 793 ▶ if (inv[mid] <= (unsigned long) cp)
Branch: 1 branch
BranchTrueFalseTotal
if (inv[mid] <= (unsigned long) cp) 211 582 793
156 211 lo = mid + 1;
157 else
158 582 hi = mid;
159 }
160 76 return (lo & 1) != 0;
161 }
162  
163 static const bt_casemap_entry *
164 31 find_map (const bt_casemap_entry *map, size_t len, long cp)
165 {
166 31 size_t lo = 0, hi = len;
167  
168 318 ▶ while (lo < hi)
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) 307 11 318
169 {
170 307 size_t mid = (lo + hi) / 2;
171 307 ▶ if (map[mid].cp == (unsigned long) cp)
Branch: 1 branch
BranchTrueFalseTotal
if (map[mid].cp == (unsigned long) cp) 20 287 307
172 20 return &map[mid];
173 287 ▶ if (map[mid].cp < (unsigned long) cp)
Branch: 1 branch
BranchTrueFalseTotal
if (map[mid].cp < (unsigned long) cp) 80 207 287
174 80 lo = mid + 1;
175 else
176 207 hi = mid;
177 }
178 11 return NULL;
179 }
180  
181 boolean
182 240 bt_uni_islower (const char *s)
183 {
184 long cp;
185  
186 240 ▶ if (BT_IS_ASCII (*s))
Branch: 1 branch
BranchTrueFalseTotal
if (BT_IS_ASCII (*s)) 218 22 240
187 218 ▶ return BT_IS_LOWER (*s);
Branch: 2 branches
BranchTrueFalseTotal
return BT_IS_LOWER (*s); 61 157 218
return BT_IS_LOWER (*s); 47 14 61
188 22 decode_at (s, &cp);
189 22 return in_invlist (bt_uni_lower_inv, NELEM (bt_uni_lower_inv), cp);
190 }
191  
192 boolean
193 82 bt_uni_isalpha (const char *s)
194 {
195 long cp;
196  
197 82 ▶ if (BT_IS_ASCII (*s))
Branch: 1 branch
BranchTrueFalseTotal
if (BT_IS_ASCII (*s)) 82 0 82
198 82 ▶ return BT_IS_ALPHA (*s);
Branch: 4 branches
BranchTrueFalseTotal
return BT_IS_ALPHA (*s); 50 32 82
return BT_IS_ALPHA (*s); 0 50 50
return BT_IS_ALPHA (*s); 0 32 32
return BT_IS_ALPHA (*s); 0 0 0
199 0 decode_at (s, &cp);
200 0 return in_invlist (bt_uni_alpha_inv, NELEM (bt_uni_alpha_inv), cp);
201 }
202  
203 boolean
204 550 bt_uni_isalnum (const char *s)
205 {
206 long cp;
207  
208 550 ▶ if (BT_IS_ASCII (*s))
Branch: 1 branch
BranchTrueFalseTotal
if (BT_IS_ASCII (*s)) 525 25 550
209 525 ▶ return BT_IS_ALNUM (*s);
Branch: 6 branches
BranchTrueFalseTotal
return BT_IS_ALNUM (*s); 389 136 525
return BT_IS_ALNUM (*s); 0 389 389
return BT_IS_ALNUM (*s); 99 37 136
return BT_IS_ALNUM (*s); 21 78 99
return BT_IS_ALNUM (*s); 39 19 58
return BT_IS_ALNUM (*s); 16 23 39
210 25 decode_at (s, &cp);
211 25 return in_invlist (bt_uni_alnum_inv, NELEM (bt_uni_alnum_inv), cp);
212 }
213  
214 boolean
215 1528 bt_uni_ismark (const char *s)
216 {
217 long cp;
218  
219 1528 ▶ if (BT_IS_ASCII (*s))
Branch: 1 branch
BranchTrueFalseTotal
if (BT_IS_ASCII (*s)) 1499 29 1528
220 1499 return FALSE;
221 29 decode_at (s, &cp);
222 29 return in_invlist (bt_uni_extend_inv, NELEM (bt_uni_extend_inv), cp);
223 }
224  
225 size_t
226 1680 bt_uni_grapheme_len (const char *s)
227 {
228 size_t len;
229  
230 1680 ▶ if (*s == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 0) 0 1680 1680
231 0 return 0;
232 1680 len = bt_utf8_charlen (s);
233 1701 ▶ while (s[len] && bt_uni_ismark (s + len))
Branch: 2 branches
BranchTrueFalseTotal
while (s[len] && bt_uni_ismark (s + len)) 1452 249 1701
while (s[len] && bt_uni_ismark (s + len)) 21 1431 1452
234 21 len += bt_utf8_charlen (s + len);
235 1680 return len;
236 }
237  
238  
239 /* ----------------------------------------------------------------------
240 * Case mapping
241 */
242  
243 size_t
244 6534 bt_uni_map_char (bt_sbuf *out, const char *s, bt_casemap how)
245 {
246 6534 const bt_casemap_entry * e = NULL;
247 size_t len;
248 long cp;
249 int i;
250  
251 6534 ▶ if (BT_IS_ASCII (*s))
Branch: 1 branch
BranchTrueFalseTotal
if (BT_IS_ASCII (*s)) 6503 31 6534
252 {
253 6503 char c = *s;
254 12974 ▶ bt_sbuf_putc (out, (how == BT_CASE_UPPER || how == BT_CASE_TITLE)
Branch: 4 branches
BranchTrueFalseTotal
bt_sbuf_putc (out, (how == BT_CASE_UPPER || how == BT_CASE_TITLE) 6494 9 6503
bt_sbuf_putc (out, (how == BT_CASE_UPPER || how == BT_CASE_TITLE) 6 6488 6494
bt_sbuf_putc (out, (how == BT_CASE_UPPER || how == BT_CASE_TITLE) 9 6 15
bt_sbuf_putc (out, (how == BT_CASE_UPPER || how == BT_CASE_TITLE) 6462 26 6488
255 6471 ▶ ? BT_TOUPPER (c) : BT_TOLOWER (c));
Branch: 2 branches
BranchTrueFalseTotal
? BT_TOUPPER (c) : BT_TOLOWER (c)); 9 0 9
? BT_TOUPPER (c) : BT_TOLOWER (c)); 604 5858 6462
256 6503 return 1;
257 }
258  
259 31 len = decode_at (s, &cp);
260 31 ▶ if (cp >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cp >= 0) 31 0 31
261 {
262 31 switch (how)
263 {
264 10 case BT_CASE_LOWER:
265 10 e = find_map (bt_uni_lc_map, NELEM (bt_uni_lc_map), cp); break;
266 12 case BT_CASE_UPPER:
267 12 e = find_map (bt_uni_uc_map, NELEM (bt_uni_uc_map), cp); break;
268 3 case BT_CASE_TITLE:
269 3 e = find_map (bt_uni_tc_map, NELEM (bt_uni_tc_map), cp); break;
270 6 case BT_CASE_FOLD:
271 6 e = find_map (bt_uni_fc_map, NELEM (bt_uni_fc_map), cp); break;
272 }
273 }
274  
275 31 ▶ if (e == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (e == NULL) 11 20 31
276 {
277 11 bt_sbuf_putn (out, s, len);
278 11 return len;
279 }
280 43 ▶ for (i = 0; i < 3 && e->map[i]; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < 3 && e->map[i]; i++) 43 0 43
for (i = 0; i < 3 && e->map[i]; i++) 23 20 43
281 {
282 char buf[4];
283 23 bt_sbuf_putn (out, buf, bt_utf8_encode (e->map[i], buf));
284 }
285 20 return len;
286 }
287  
288 char *
289 1160 bt_uni_map_string (const char *s, bt_casemap how)
290 {
291 bt_sbuf out;
292  
293 1160 bt_sbuf_init (&out);
294 1160 bt_sbuf_grow (&out, strlen (s));
295 7391 ▶ while (*s)
Branch: 1 branch
BranchTrueFalseTotal
while (*s) 6231 1160 7391
296 6231 s += bt_uni_map_char (&out, s, how);
297 1160 return bt_sbuf_steal (&out);
298 }
« src/string_util.c ↑ Summary src/util.c »