Commit | Line | Data |
---|---|---|
e5a5a138 RK |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2007 Richard Kettlewell | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
20 | /** @file lib/unicode.c | |
21 | * @brief Unicode support functions | |
22 | * | |
23 | * Here by UTF-8 and UTF-8 we mean the encoding forms of those names (not the | |
35b651f0 RK |
24 | * encoding schemes). The primary encoding form is UTF-32 but convenience |
25 | * wrappers using UTF-8 are provided for a number of functions. | |
e5a5a138 RK |
26 | * |
27 | * The idea is that all the strings that hit the database will be in a | |
28 | * particular normalization form, and for the search and tags database | |
29 | * in case-folded form, so they can be naively compared within the | |
30 | * database code. | |
31 | * | |
32 | * As the code stands this guarantee is not well met! | |
0ae60b83 RK |
33 | * |
34 | * Subpages: | |
35 | * - @ref utf32props | |
36 | * - @ref utftransform | |
37 | * - @ref utf32iterator | |
38 | * - @ref utf32 | |
39 | * - @ref utf8 | |
e5a5a138 RK |
40 | */ |
41 | ||
42 | #include <config.h> | |
43 | #include "types.h" | |
44 | ||
45 | #include <string.h> | |
46 | #include <stdio.h> /* TODO */ | |
47 | ||
48 | #include "mem.h" | |
49 | #include "vector.h" | |
50 | #include "unicode.h" | |
51 | #include "unidata.h" | |
52 | ||
092f426f RK |
53 | /** @defgroup utf32props Unicode Code Point Properties */ |
54 | /*@{*/ | |
55 | ||
56 | static const struct unidata *utf32__unidata_hard(uint32_t c); | |
57 | ||
58 | /** @brief Find definition of code point @p c | |
59 | * @param c Code point | |
60 | * @return Pointer to @ref unidata structure for @p c | |
61 | * | |
62 | * @p c can be any 32-bit value, a sensible value will be returned regardless. | |
63 | * The returned pointer is NOT guaranteed to be unique to @p c. | |
64 | */ | |
65 | static inline const struct unidata *utf32__unidata(uint32_t c) { | |
66 | /* The bottom half of the table contains almost everything of interest | |
67 | * and we can just return the right thing straight away */ | |
68 | if(c < UNICODE_BREAK_START) | |
69 | return &unidata[c / UNICODE_MODULUS][c % UNICODE_MODULUS]; | |
70 | else | |
71 | return utf32__unidata_hard(c); | |
72 | } | |
73 | ||
74 | /** @brief Find definition of code point @p c | |
75 | * @param c Code point | |
76 | * @return Pointer to @ref unidata structure for @p c | |
77 | * | |
78 | * @p c can be any 32-bit value, a sensible value will be returned regardless. | |
79 | * The returned pointer is NOT guaranteed to be unique to @p c. | |
80 | * | |
81 | * Don't use this function (although it will work fine) - use utf32__unidata() | |
82 | * instead. | |
83 | */ | |
84 | static const struct unidata *utf32__unidata_hard(uint32_t c) { | |
85 | if(c < UNICODE_BREAK_START) | |
86 | return &unidata[c / UNICODE_MODULUS][c % UNICODE_MODULUS]; | |
87 | /* Within the break everything is unassigned */ | |
88 | if(c < UNICODE_BREAK_END) | |
89 | return utf32__unidata(0xFFFF); /* guaranteed to be Cn */ | |
90 | /* Planes 15 and 16 are (mostly) private use */ | |
91 | if((c >= 0xF0000 && c <= 0xFFFFD) | |
92 | || (c >= 0x100000 && c <= 0x10FFFD)) | |
93 | return utf32__unidata(0xE000); /* first Co code point */ | |
94 | /* Everything else above the break top is unassigned */ | |
95 | if(c >= UNICODE_BREAK_TOP) | |
96 | return utf32__unidata(0xFFFF); /* guaranteed to be Cn */ | |
97 | /* Currently the rest is language tags and variation selectors */ | |
98 | c -= (UNICODE_BREAK_END - UNICODE_BREAK_START); | |
99 | return &unidata[c / UNICODE_MODULUS][c % UNICODE_MODULUS]; | |
100 | } | |
101 | ||
102 | /** @brief Return the combining class of @p c | |
103 | * @param c Code point | |
104 | * @return Combining class of @p c | |
105 | * | |
106 | * @p c can be any 32-bit value, a sensible value will be returned regardless. | |
107 | */ | |
108 | static inline int utf32__combining_class(uint32_t c) { | |
109 | return utf32__unidata(c)->ccc; | |
110 | } | |
111 | ||
112 | /** @brief Return the General_Category value for @p c | |
0ae60b83 | 113 | * @param c Code point |
092f426f RK |
114 | * @return General_Category property value |
115 | * | |
116 | * @p c can be any 32-bit value, a sensible value will be returned regardless. | |
117 | */ | |
118 | static inline enum unicode_General_Category utf32__general_category(uint32_t c) { | |
119 | return utf32__unidata(c)->general_category; | |
120 | } | |
121 | ||
122 | /** @brief Determine Grapheme_Break property | |
123 | * @param c Code point | |
124 | * @return Grapheme_Break property value of @p c | |
125 | * | |
126 | * @p c can be any 32-bit value, a sensible value will be returned regardless. | |
127 | */ | |
128 | static inline enum unicode_Grapheme_Break utf32__grapheme_break(uint32_t c) { | |
129 | return utf32__unidata(c)->grapheme_break; | |
130 | } | |
131 | ||
132 | /** @brief Determine Word_Break property | |
133 | * @param c Code point | |
134 | * @return Word_Break property value of @p c | |
135 | * | |
136 | * @p c can be any 32-bit value, a sensible value will be returned regardless. | |
137 | */ | |
138 | static inline enum unicode_Word_Break utf32__word_break(uint32_t c) { | |
139 | return utf32__unidata(c)->word_break; | |
140 | } | |
141 | ||
142 | /** @brief Determine Sentence_Break property | |
143 | * @param c Code point | |
144 | * @return Word_Break property value of @p c | |
145 | * | |
146 | * @p c can be any 32-bit value, a sensible value will be returned regardless. | |
147 | */ | |
148 | static inline enum unicode_Sentence_Break utf32__sentence_break(uint32_t c) { | |
149 | return utf32__unidata(c)->sentence_break; | |
150 | } | |
151 | ||
152 | /** @brief Return true if @p c is ignorable for boundary specifications | |
153 | * @param wb Word break property value | |
154 | * @return non-0 if @p wb is unicode_Word_Break_Extend or unicode_Word_Break_Format | |
155 | */ | |
156 | static inline int utf32__boundary_ignorable(enum unicode_Word_Break wb) { | |
157 | return (wb == unicode_Word_Break_Extend | |
158 | || wb == unicode_Word_Break_Format); | |
159 | } | |
160 | ||
f98fcddb RK |
161 | /** @brief Return the canonical decomposition of @p c |
162 | * @param c Code point | |
163 | * @return 0-terminated canonical decomposition, or 0 | |
164 | */ | |
165 | static inline const uint32_t *utf32__decomposition_canon(uint32_t c) { | |
166 | const struct unidata *const data = utf32__unidata(c); | |
167 | const uint32_t *const decomp = data->decomp; | |
168 | ||
169 | if(decomp && !(data->flags & unicode_compatibility_decomposition)) | |
170 | return decomp; | |
171 | else | |
172 | return 0; | |
173 | } | |
174 | ||
175 | /** @brief Return the compatibility decomposition of @p c | |
176 | * @param c Code point | |
177 | * @return 0-terminated decomposition, or 0 | |
178 | */ | |
179 | static inline const uint32_t *utf32__decomposition_compat(uint32_t c) { | |
180 | return utf32__unidata(c)->decomp; | |
181 | } | |
182 | ||
092f426f | 183 | /*@}*/ |
e5a5a138 RK |
184 | /** @defgroup utftransform Functions that transform between different Unicode encoding forms */ |
185 | /*@{*/ | |
186 | ||
187 | /** @brief Convert UTF-32 to UTF-8 | |
188 | * @param s Source string | |
189 | * @param ns Length of source string in code points | |
190 | * @param ndp Where to store length of destination string (or NULL) | |
191 | * @return Newly allocated destination string or NULL on error | |
192 | * | |
56fd389c RK |
193 | * If the UTF-32 is not valid then NULL is returned. A UTF-32 code point is |
194 | * invalid if: | |
e5a5a138 RK |
195 | * - it codes for a UTF-16 surrogate |
196 | * - it codes for a value outside the unicode code space | |
197 | * | |
56fd389c RK |
198 | * The return value is always 0-terminated. The value returned via @p *ndp |
199 | * does not include the terminator. | |
e5a5a138 RK |
200 | */ |
201 | char *utf32_to_utf8(const uint32_t *s, size_t ns, size_t *ndp) { | |
202 | struct dynstr d; | |
203 | uint32_t c; | |
204 | ||
205 | dynstr_init(&d); | |
206 | while(ns > 0) { | |
207 | c = *s++; | |
208 | if(c < 0x80) | |
209 | dynstr_append(&d, c); | |
210 | else if(c < 0x0800) { | |
211 | dynstr_append(&d, 0xC0 | (c >> 6)); | |
212 | dynstr_append(&d, 0x80 | (c & 0x3F)); | |
213 | } else if(c < 0x10000) { | |
56fd389c | 214 | if(c >= 0xD800 && c <= 0xDFFF) |
e5a5a138 RK |
215 | goto error; |
216 | dynstr_append(&d, 0xE0 | (c >> 12)); | |
217 | dynstr_append(&d, 0x80 | ((c >> 6) & 0x3F)); | |
218 | dynstr_append(&d, 0x80 | (c & 0x3F)); | |
219 | } else if(c < 0x110000) { | |
220 | dynstr_append(&d, 0xF0 | (c >> 18)); | |
221 | dynstr_append(&d, 0x80 | ((c >> 12) & 0x3F)); | |
222 | dynstr_append(&d, 0x80 | ((c >> 6) & 0x3F)); | |
223 | dynstr_append(&d, 0x80 | (c & 0x3F)); | |
224 | } else | |
225 | goto error; | |
226 | --ns; | |
227 | } | |
228 | dynstr_terminate(&d); | |
229 | if(ndp) | |
230 | *ndp = d.nvec; | |
231 | return d.vec; | |
232 | error: | |
233 | xfree(d.vec); | |
234 | return 0; | |
235 | } | |
236 | ||
237 | /** @brief Convert UTF-8 to UTF-32 | |
238 | * @param s Source string | |
239 | * @param ns Length of source string in code points | |
240 | * @param ndp Where to store length of destination string (or NULL) | |
f98fcddb | 241 | * @return Newly allocated destination string or NULL on error |
e5a5a138 | 242 | * |
56fd389c RK |
243 | * The return value is always 0-terminated. The value returned via @p *ndp |
244 | * does not include the terminator. | |
e5a5a138 RK |
245 | * |
246 | * If the UTF-8 is not valid then NULL is returned. A UTF-8 sequence | |
247 | * for a code point is invalid if: | |
248 | * - it is not the shortest possible sequence for the code point | |
249 | * - it codes for a UTF-16 surrogate | |
250 | * - it codes for a value outside the unicode code space | |
251 | */ | |
252 | uint32_t *utf8_to_utf32(const char *s, size_t ns, size_t *ndp) { | |
253 | struct dynstr_ucs4 d; | |
32b158f2 | 254 | uint32_t c32; |
e5a5a138 | 255 | const uint8_t *ss = (const uint8_t *)s; |
32b158f2 | 256 | int n; |
e5a5a138 RK |
257 | |
258 | dynstr_ucs4_init(&d); | |
259 | while(ns > 0) { | |
32b158f2 RK |
260 | const struct unicode_utf8_row *const r = &unicode_utf8_valid[*ss]; |
261 | if(r->count <= ns) { | |
262 | switch(r->count) { | |
263 | case 1: | |
264 | c32 = *ss; | |
265 | break; | |
266 | case 2: | |
267 | if(ss[1] < r->min2 || ss[1] > r->max2) | |
268 | goto error; | |
269 | c32 = *ss & 0x1F; | |
270 | break; | |
271 | case 3: | |
272 | if(ss[1] < r->min2 || ss[1] > r->max2) | |
273 | goto error; | |
274 | c32 = *ss & 0x0F; | |
275 | break; | |
276 | case 4: | |
277 | if(ss[1] < r->min2 || ss[1] > r->max2) | |
278 | goto error; | |
279 | c32 = *ss & 0x07; | |
280 | break; | |
281 | default: | |
282 | goto error; | |
283 | } | |
e5a5a138 RK |
284 | } else |
285 | goto error; | |
32b158f2 RK |
286 | for(n = 1; n < r->count; ++n) { |
287 | if(ss[n] < 0x80 || ss[n] > 0xBF) | |
288 | goto error; | |
289 | c32 = (c32 << 6) | (ss[n] & 0x3F); | |
290 | } | |
e5a5a138 | 291 | dynstr_ucs4_append(&d, c32); |
32b158f2 RK |
292 | ss += r->count; |
293 | ns -= r->count; | |
e5a5a138 RK |
294 | } |
295 | dynstr_ucs4_terminate(&d); | |
296 | if(ndp) | |
297 | *ndp = d.nvec; | |
298 | return d.vec; | |
299 | error: | |
300 | xfree(d.vec); | |
301 | return 0; | |
302 | } | |
303 | ||
18cda350 RK |
304 | /** @brief Test whether [s,s+ns) is valid UTF-8 |
305 | * @param s Start of string | |
306 | * @param ns Length of string | |
307 | * @return non-0 if @p s is valid UTF-8, 0 if it is not valid | |
308 | * | |
309 | * This function is intended to be much faster than calling utf8_to_utf32() and | |
310 | * throwing away the result. | |
311 | */ | |
312 | int utf8_valid(const char *s, size_t ns) { | |
313 | const uint8_t *ss = (const uint8_t *)s; | |
314 | while(ns > 0) { | |
315 | const struct unicode_utf8_row *const r = &unicode_utf8_valid[*ss]; | |
316 | if(r->count <= ns) { | |
317 | switch(r->count) { | |
318 | case 1: | |
319 | break; | |
320 | case 2: | |
321 | if(ss[1] < r->min2 || ss[1] > r->max2) | |
322 | return 0; | |
323 | break; | |
324 | case 3: | |
325 | if(ss[1] < r->min2 || ss[1] > r->max2) | |
326 | return 0; | |
327 | if(ss[2] < 0x80 || ss[2] > 0xBF) | |
328 | return 0; | |
329 | break; | |
330 | case 4: | |
331 | if(ss[1] < r->min2 || ss[1] > r->max2) | |
332 | return 0; | |
333 | if(ss[2] < 0x80 || ss[2] > 0xBF) | |
334 | return 0; | |
335 | if(ss[3] < 0x80 || ss[3] > 0xBF) | |
336 | return 0; | |
337 | break; | |
338 | default: | |
339 | return 0; | |
340 | } | |
341 | } else | |
342 | return 0; | |
343 | ss += r->count; | |
344 | ns -= r->count; | |
345 | } | |
346 | return 1; | |
347 | } | |
348 | ||
092f426f RK |
349 | /*@}*/ |
350 | /** @defgroup utf32iterator UTF-32 string iterators */ | |
351 | /*@{*/ | |
352 | ||
353 | struct utf32_iterator_data { | |
354 | /** @brief Start of string */ | |
355 | const uint32_t *s; | |
356 | ||
357 | /** @brief Length of string */ | |
358 | size_t ns; | |
359 | ||
360 | /** @brief Current position */ | |
361 | size_t n; | |
362 | ||
363 | /** @brief Last two non-ignorable characters or (uint32_t)-1 | |
364 | * | |
365 | * last[1] is the non-Extend/Format character just before position @p n; | |
366 | * last[0] is the one just before that. | |
367 | * | |
368 | * Exception 1: if there is no such non-Extend/Format character then an | |
369 | * Extend/Format character is accepted instead. | |
370 | * | |
371 | * Exception 2: if there is no such character even taking that into account | |
372 | * the value is (uint32_t)-1. | |
373 | */ | |
374 | uint32_t last[2]; | |
092f426f | 375 | |
c85b7022 RK |
376 | /** @brief Tailoring for Word_Break */ |
377 | unicode_property_tailor *word_break; | |
378 | }; | |
092f426f RK |
379 | |
380 | /** @brief Initialize an internal private iterator | |
381 | * @param it Iterator | |
382 | * @param s Start of string | |
383 | * @param ns Length of string | |
384 | * @param n Absolute position | |
385 | */ | |
386 | static void utf32__iterator_init(utf32_iterator it, | |
387 | const uint32_t *s, size_t ns, size_t n) { | |
388 | it->s = s; | |
389 | it->ns = ns; | |
390 | it->n = 0; | |
391 | it->last[0] = it->last[1] = -1; | |
c85b7022 | 392 | it->word_break = 0; |
b21a155c | 393 | utf32_iterator_set(it, n); |
092f426f RK |
394 | } |
395 | ||
c85b7022 RK |
396 | /** @brief Create a new iterator pointing at the start of a string |
397 | * @param s Start of string | |
398 | * @param ns Length of string | |
399 | * @return New iterator | |
400 | */ | |
401 | utf32_iterator utf32_iterator_new(const uint32_t *s, size_t ns) { | |
402 | utf32_iterator it = xmalloc(sizeof *it); | |
403 | utf32__iterator_init(it, s, ns, 0); | |
404 | return it; | |
405 | } | |
406 | ||
407 | /** @brief Tailor this iterator's interpretation of the Word_Break property. | |
408 | * @param it Iterator | |
409 | * @param pt Property tailor function or NULL | |
410 | * | |
411 | * After calling this the iterator will call @p pt to determine the Word_Break | |
412 | * property of each code point. If it returns -1 the default value will be | |
413 | * used otherwise the returned value will be used. | |
414 | * | |
415 | * @p pt can be NULL to revert to the default value of the property. | |
416 | * | |
417 | * It is safe to call this function at any time; the iterator's internal state | |
418 | * will be reset to suit the new tailoring. | |
419 | */ | |
420 | void utf32_iterator_tailor_word_break(utf32_iterator it, | |
421 | unicode_property_tailor *pt) { | |
422 | it->word_break = pt; | |
423 | utf32_iterator_set(it, it->n); | |
424 | } | |
425 | ||
426 | static inline enum unicode_Word_Break utf32__iterator_word_break(utf32_iterator it, | |
427 | uint32_t c) { | |
428 | if(!it->word_break) | |
429 | return utf32__word_break(c); | |
430 | else { | |
431 | const int t = it->word_break(c); | |
432 | ||
433 | if(t < 0) | |
434 | return utf32__word_break(c); | |
435 | else | |
436 | return t; | |
437 | } | |
438 | } | |
439 | ||
092f426f RK |
440 | /** @brief Destroy an iterator |
441 | * @param it Iterator | |
442 | */ | |
443 | void utf32_iterator_destroy(utf32_iterator it) { | |
444 | xfree(it); | |
445 | } | |
446 | ||
447 | /** @brief Find the current position of an interator | |
448 | * @param it Iterator | |
449 | */ | |
450 | size_t utf32_iterator_where(utf32_iterator it) { | |
451 | return it->n; | |
452 | } | |
453 | ||
454 | /** @brief Set an iterator's absolute position | |
455 | * @param it Iterator | |
456 | * @param n Absolute position | |
457 | * @return 0 on success, non-0 on error | |
458 | * | |
459 | * It is an error to position the iterator outside the string (but acceptable | |
460 | * to point it at the hypothetical post-final character). If an invalid value | |
461 | * of @p n is specified then the iterator is not changed. | |
f98fcddb RK |
462 | * |
463 | * This function works by backing up and then advancing to reconstruct the | |
464 | * iterator's internal state for position @p n. The worst case will be O(n) | |
465 | * time complexity (with a worse constant factor that utf32_iterator_advance()) | |
466 | * but the typical case is essentially constant-time. | |
092f426f RK |
467 | */ |
468 | int utf32_iterator_set(utf32_iterator it, size_t n) { | |
5617aaff RK |
469 | /* We can't just jump to position @p n; the @p last[] values will be wrong. |
470 | * What we need is to jump a bit behind @p n and then advance forward, | |
471 | * updating @p last[] along the way. How far back? We need to cross two | |
472 | * non-ignorable code points as we advance forwards, so we'd better pass two | |
473 | * such characters on the way back (if such are available). | |
474 | */ | |
b21a155c | 475 | size_t m; |
5617aaff RK |
476 | |
477 | if(n > it->ns) /* range check */ | |
092f426f | 478 | return -1; |
b21a155c RK |
479 | /* Walk backwards skipping ignorable code points */ |
480 | m = n; | |
c85b7022 RK |
481 | while(m > 0 |
482 | && (utf32__boundary_ignorable(utf32__iterator_word_break(it, | |
483 | it->s[m-1])))) | |
b21a155c RK |
484 | --m; |
485 | /* Either m=0 or s[m-1] is not ignorable */ | |
486 | if(m > 0) { | |
487 | --m; | |
488 | /* s[m] is our first non-ignorable code; look for a second in the same | |
489 | way **/ | |
c85b7022 RK |
490 | while(m > 0 |
491 | && (utf32__boundary_ignorable(utf32__iterator_word_break(it, | |
492 | it->s[m-1])))) | |
5617aaff | 493 | --m; |
b21a155c RK |
494 | /* Either m=0 or s[m-1] is not ignorable */ |
495 | if(m > 0) | |
496 | --m; | |
497 | } | |
498 | it->last[0] = it->last[1] = -1; | |
5617aaff RK |
499 | it->n = m; |
500 | return utf32_iterator_advance(it, n - m); | |
092f426f RK |
501 | } |
502 | ||
503 | /** @brief Advance an iterator | |
504 | * @param it Iterator | |
505 | * @param count Number of code points to advance by | |
506 | * @return 0 on success, non-0 on error | |
507 | * | |
508 | * It is an error to advance an iterator beyond the hypothetical post-final | |
509 | * character of the string. If an invalid value of @p n is specified then the | |
510 | * iterator is not changed. | |
511 | * | |
512 | * This function has O(n) time complexity: it works by advancing naively | |
513 | * forwards through the string. | |
514 | */ | |
515 | int utf32_iterator_advance(utf32_iterator it, size_t count) { | |
516 | if(count <= it->ns - it->n) { | |
517 | while(count > 0) { | |
518 | const uint32_t c = it->s[it->n]; | |
c85b7022 | 519 | const enum unicode_Word_Break wb = utf32__iterator_word_break(it, c); |
092f426f RK |
520 | if(it->last[1] == (uint32_t)-1 |
521 | || !utf32__boundary_ignorable(wb)) { | |
522 | it->last[0] = it->last[1]; | |
523 | it->last[1] = c; | |
524 | } | |
525 | ++it->n; | |
526 | --count; | |
527 | } | |
528 | return 0; | |
529 | } else | |
530 | return -1; | |
531 | } | |
532 | ||
533 | /** @brief Find the current code point | |
534 | * @param it Iterator | |
535 | * @return Current code point or 0 | |
536 | * | |
537 | * If the iterator points at the hypothetical post-final character of the | |
538 | * string then 0 is returned. NB that this doesn't mean that there aren't any | |
539 | * 0 code points inside the string! | |
540 | */ | |
541 | uint32_t utf32_iterator_code(utf32_iterator it) { | |
542 | if(it->n < it->ns) | |
543 | return it->s[it->n]; | |
544 | else | |
545 | return 0; | |
546 | } | |
547 | ||
548 | /** @brief Test for a grapheme boundary | |
549 | * @param it Iterator | |
550 | * @return Non-0 if pointing just after a grapheme boundary, otherwise 0 | |
f98fcddb RK |
551 | * |
552 | * This function identifies default grapheme cluster boundaries as described in | |
553 | * UAX #29 s3. It returns non-0 if @p it points at the code point just after a | |
554 | * grapheme cluster boundary (including the hypothetical code point just after | |
555 | * the end of the string). | |
092f426f RK |
556 | */ |
557 | int utf32_iterator_grapheme_boundary(utf32_iterator it) { | |
558 | uint32_t before, after; | |
559 | enum unicode_Grapheme_Break gbbefore, gbafter; | |
560 | /* GB1 and GB2 */ | |
561 | if(it->n == 0 || it->n == it->ns) | |
562 | return 1; | |
563 | /* Now we know that s[n-1] and s[n] are safe to inspect */ | |
564 | /* GB3 */ | |
565 | before = it->s[it->n-1]; | |
566 | after = it->s[it->n]; | |
567 | if(before == 0x000D && after == 0x000A) | |
568 | return 0; | |
569 | gbbefore = utf32__grapheme_break(before); | |
570 | gbafter = utf32__grapheme_break(after); | |
571 | /* GB4 */ | |
572 | if(gbbefore == unicode_Grapheme_Break_Control | |
573 | || before == 0x000D | |
574 | || before == 0x000A) | |
575 | return 1; | |
576 | /* GB5 */ | |
577 | if(gbafter == unicode_Grapheme_Break_Control | |
578 | || after == 0x000D | |
579 | || after == 0x000A) | |
580 | return 1; | |
581 | /* GB6 */ | |
582 | if(gbbefore == unicode_Grapheme_Break_L | |
583 | && (gbafter == unicode_Grapheme_Break_L | |
584 | || gbafter == unicode_Grapheme_Break_V | |
585 | || gbafter == unicode_Grapheme_Break_LV | |
586 | || gbafter == unicode_Grapheme_Break_LVT)) | |
587 | return 0; | |
588 | /* GB7 */ | |
589 | if((gbbefore == unicode_Grapheme_Break_LV | |
590 | || gbbefore == unicode_Grapheme_Break_V) | |
591 | && (gbafter == unicode_Grapheme_Break_V | |
592 | || gbafter == unicode_Grapheme_Break_T)) | |
593 | return 0; | |
594 | /* GB8 */ | |
595 | if((gbbefore == unicode_Grapheme_Break_LVT | |
596 | || gbbefore == unicode_Grapheme_Break_T) | |
597 | && gbafter == unicode_Grapheme_Break_T) | |
598 | return 0; | |
599 | /* GB9 */ | |
600 | if(gbafter == unicode_Grapheme_Break_Extend) | |
601 | return 0; | |
602 | /* GB10 */ | |
603 | return 1; | |
604 | ||
605 | } | |
606 | ||
607 | /** @brief Test for a word boundary | |
608 | * @param it Iterator | |
609 | * @return Non-0 if pointing just after a word boundary, otherwise 0 | |
f98fcddb RK |
610 | * |
611 | * This function identifies default word boundaries as described in UAX #29 s4. | |
612 | * It returns non-0 if @p it points at the code point just after a word | |
613 | * boundary (including the hypothetical code point just after the end of the | |
614 | * string) and 0 otherwise. | |
092f426f RK |
615 | */ |
616 | int utf32_iterator_word_boundary(utf32_iterator it) { | |
617 | enum unicode_Word_Break twobefore, before, after, twoafter; | |
618 | size_t nn; | |
619 | ||
620 | /* WB1 and WB2 */ | |
621 | if(it->n == 0 || it->n == it->ns) | |
622 | return 1; | |
623 | /* WB3 */ | |
624 | if(it->s[it->n-1] == 0x000D && it->s[it->n] == 0x000A) | |
625 | return 0; | |
626 | /* WB4 */ | |
627 | /* (!Sep) x (Extend|Format) as in UAX #29 s6.2 */ | |
628 | if(utf32__sentence_break(it->s[it->n-1]) != unicode_Sentence_Break_Sep | |
c85b7022 | 629 | && utf32__boundary_ignorable(utf32__iterator_word_break(it, it->s[it->n]))) |
092f426f RK |
630 | return 0; |
631 | /* Gather the property values we'll need for the rest of the test taking the | |
632 | * s6.2 changes into account */ | |
633 | /* First we look at the code points after the proposed boundary */ | |
634 | nn = it->n; /* <it->ns */ | |
c85b7022 | 635 | after = utf32__iterator_word_break(it, it->s[nn++]); |
092f426f RK |
636 | if(!utf32__boundary_ignorable(after)) { |
637 | /* X (Extend|Format)* -> X */ | |
638 | while(nn < it->ns | |
c85b7022 RK |
639 | && utf32__boundary_ignorable(utf32__iterator_word_break(it, |
640 | it->s[nn]))) | |
092f426f RK |
641 | ++nn; |
642 | } | |
643 | /* It's possible now that nn=ns */ | |
644 | if(nn < it->ns) | |
c85b7022 | 645 | twoafter = utf32__iterator_word_break(it, it->s[nn]); |
092f426f RK |
646 | else |
647 | twoafter = unicode_Word_Break_Other; | |
648 | ||
649 | /* We've already recorded the non-ignorable code points before the proposed | |
650 | * boundary */ | |
c85b7022 RK |
651 | before = utf32__iterator_word_break(it, it->last[1]); |
652 | twobefore = utf32__iterator_word_break(it, it->last[0]); | |
092f426f RK |
653 | |
654 | /* WB5 */ | |
655 | if(before == unicode_Word_Break_ALetter | |
656 | && after == unicode_Word_Break_ALetter) | |
657 | return 0; | |
658 | /* WB6 */ | |
659 | if(before == unicode_Word_Break_ALetter | |
660 | && after == unicode_Word_Break_MidLetter | |
661 | && twoafter == unicode_Word_Break_ALetter) | |
662 | return 0; | |
663 | /* WB7 */ | |
664 | if(twobefore == unicode_Word_Break_ALetter | |
665 | && before == unicode_Word_Break_MidLetter | |
666 | && after == unicode_Word_Break_ALetter) | |
667 | return 0; | |
c85b7022 | 668 | /* WB8 */ |
092f426f RK |
669 | if(before == unicode_Word_Break_Numeric |
670 | && after == unicode_Word_Break_Numeric) | |
671 | return 0; | |
672 | /* WB9 */ | |
673 | if(before == unicode_Word_Break_ALetter | |
674 | && after == unicode_Word_Break_Numeric) | |
675 | return 0; | |
676 | /* WB10 */ | |
677 | if(before == unicode_Word_Break_Numeric | |
678 | && after == unicode_Word_Break_ALetter) | |
679 | return 0; | |
680 | /* WB11 */ | |
681 | if(twobefore == unicode_Word_Break_Numeric | |
682 | && before == unicode_Word_Break_MidNum | |
683 | && after == unicode_Word_Break_Numeric) | |
684 | return 0; | |
685 | /* WB12 */ | |
686 | if(before == unicode_Word_Break_Numeric | |
687 | && after == unicode_Word_Break_MidNum | |
688 | && twoafter == unicode_Word_Break_Numeric) | |
689 | return 0; | |
690 | /* WB13 */ | |
691 | if(before == unicode_Word_Break_Katakana | |
692 | && after == unicode_Word_Break_Katakana) | |
693 | return 0; | |
694 | /* WB13a */ | |
695 | if((before == unicode_Word_Break_ALetter | |
696 | || before == unicode_Word_Break_Numeric | |
697 | || before == unicode_Word_Break_Katakana | |
698 | || before == unicode_Word_Break_ExtendNumLet) | |
699 | && after == unicode_Word_Break_ExtendNumLet) | |
700 | return 0; | |
701 | /* WB13b */ | |
702 | if(before == unicode_Word_Break_ExtendNumLet | |
703 | && (after == unicode_Word_Break_ALetter | |
704 | || after == unicode_Word_Break_Numeric | |
705 | || after == unicode_Word_Break_Katakana)) | |
706 | return 0; | |
707 | /* WB14 */ | |
708 | return 1; | |
709 | } | |
710 | ||
e5a5a138 RK |
711 | /*@}*/ |
712 | /** @defgroup utf32 Functions that operate on UTF-32 strings */ | |
713 | /*@{*/ | |
714 | ||
715 | /** @brief Return the length of a 0-terminated UTF-32 string | |
716 | * @param s Pointer to 0-terminated string | |
717 | * @return Length of string in code points (excluding terminator) | |
718 | * | |
56fd389c | 719 | * Unlike the conversion functions no validity checking is done on the string. |
e5a5a138 RK |
720 | */ |
721 | size_t utf32_len(const uint32_t *s) { | |
722 | const uint32_t *t = s; | |
723 | ||
724 | while(*t) | |
725 | ++t; | |
726 | return (size_t)(t - s); | |
727 | } | |
728 | ||
e5a5a138 RK |
729 | /** @brief Stably sort [s,s+ns) into descending order of combining class |
730 | * @param s Start of array | |
731 | * @param ns Number of elements, must be at least 1 | |
732 | * @param buffer Buffer of at least @p ns elements | |
733 | */ | |
734 | static void utf32__sort_ccc(uint32_t *s, size_t ns, uint32_t *buffer) { | |
735 | uint32_t *a, *b, *bp; | |
736 | size_t na, nb; | |
737 | ||
738 | switch(ns) { | |
739 | case 1: /* 1-element array is always sorted */ | |
740 | return; | |
741 | case 2: /* 2-element arrays are trivial to sort */ | |
742 | if(utf32__combining_class(s[0]) > utf32__combining_class(s[1])) { | |
743 | uint32_t tmp = s[0]; | |
744 | s[0] = s[1]; | |
745 | s[1] = tmp; | |
746 | } | |
747 | return; | |
748 | default: | |
749 | /* Partition the array */ | |
750 | na = ns / 2; | |
751 | nb = ns - na; | |
752 | a = s; | |
753 | b = s + na; | |
754 | /* Sort the two halves of the array */ | |
755 | utf32__sort_ccc(a, na, buffer); | |
756 | utf32__sort_ccc(b, nb, buffer); | |
757 | /* Merge them back into one, via the buffer */ | |
758 | bp = buffer; | |
759 | while(na > 0 && nb > 0) { | |
16506c9d | 760 | /* We want ascending order of combining class (hence <) |
e5a5a138 RK |
761 | * and we want stability within combining classes (hence <=) |
762 | */ | |
763 | if(utf32__combining_class(*a) <= utf32__combining_class(*b)) { | |
764 | *bp++ = *a++; | |
765 | --na; | |
766 | } else { | |
767 | *bp++ = *b++; | |
768 | --nb; | |
769 | } | |
770 | } | |
771 | while(na > 0) { | |
772 | *bp++ = *a++; | |
773 | --na; | |
774 | } | |
775 | while(nb > 0) { | |
776 | *bp++ = *b++; | |
777 | --nb; | |
778 | } | |
779 | memcpy(s, buffer, ns * sizeof(uint32_t)); | |
780 | return; | |
781 | } | |
782 | } | |
783 | ||
784 | /** @brief Put combining characters into canonical order | |
785 | * @param s Pointer to UTF-32 string | |
786 | * @param ns Length of @p s | |
f98fcddb | 787 | * @return 0 on success, non-0 on error |
e5a5a138 | 788 | * |
56fd389c RK |
789 | * @p s is modified in-place. See Unicode 5.0 s3.11 for details of the |
790 | * ordering. | |
e5a5a138 | 791 | * |
56fd389c | 792 | * Currently we only support a maximum of 1024 combining characters after each |
f98fcddb | 793 | * base character. If this limit is exceeded then a non-0 value is returned. |
e5a5a138 RK |
794 | */ |
795 | static int utf32__canonical_ordering(uint32_t *s, size_t ns) { | |
796 | size_t nc; | |
797 | uint32_t buffer[1024]; | |
798 | ||
799 | /* The ordering amounts to a stable sort of each contiguous group of | |
800 | * characters with non-0 combining class. */ | |
801 | while(ns > 0) { | |
802 | /* Skip non-combining characters */ | |
803 | if(utf32__combining_class(*s) == 0) { | |
804 | ++s; | |
805 | --ns; | |
806 | continue; | |
807 | } | |
808 | /* We must now have at least one combining character; see how many | |
809 | * there are */ | |
810 | for(nc = 1; nc < ns && utf32__combining_class(s[nc]) != 0; ++nc) | |
811 | ; | |
812 | if(nc > 1024) | |
813 | return -1; | |
814 | /* Sort the array */ | |
815 | utf32__sort_ccc(s, nc, buffer); | |
816 | s += nc; | |
817 | ns -= nc; | |
818 | } | |
819 | return 0; | |
820 | } | |
821 | ||
822 | /* Magic numbers from UAX #15 s16 */ | |
823 | #define SBase 0xAC00 | |
824 | #define LBase 0x1100 | |
825 | #define VBase 0x1161 | |
826 | #define TBase 0x11A7 | |
827 | #define LCount 19 | |
828 | #define VCount 21 | |
829 | #define TCount 28 | |
830 | #define NCount (VCount * TCount) | |
831 | #define SCount (LCount * NCount) | |
832 | ||
833 | /** @brief Guts of the decomposition lookup functions */ | |
834 | #define utf32__decompose_one_generic(WHICH) do { \ | |
f98fcddb | 835 | const uint32_t *dc = utf32__decomposition_##WHICH(c); \ |
e5a5a138 RK |
836 | if(dc) { \ |
837 | /* Found a canonical decomposition in the table */ \ | |
838 | while(*dc) \ | |
839 | utf32__decompose_one_##WHICH(d, *dc++); \ | |
840 | } else if(c >= SBase && c < SBase + SCount) { \ | |
841 | /* Mechanically decomposable Hangul syllable (UAX #15 s16) */ \ | |
842 | const uint32_t SIndex = c - SBase; \ | |
843 | const uint32_t L = LBase + SIndex / NCount; \ | |
844 | const uint32_t V = VBase + (SIndex % NCount) / TCount; \ | |
845 | const uint32_t T = TBase + SIndex % TCount; \ | |
846 | dynstr_ucs4_append(d, L); \ | |
847 | dynstr_ucs4_append(d, V); \ | |
848 | if(T != TBase) \ | |
849 | dynstr_ucs4_append(d, T); \ | |
850 | } else \ | |
851 | /* Equal to own canonical decomposition */ \ | |
852 | dynstr_ucs4_append(d, c); \ | |
853 | } while(0) | |
854 | ||
855 | /** @brief Recursively compute the canonical decomposition of @p c | |
856 | * @param d Dynamic string to store decomposition in | |
857 | * @param c Code point to decompose (must be a valid!) | |
f98fcddb | 858 | * @return 0 on success, non-0 on error |
e5a5a138 RK |
859 | */ |
860 | static void utf32__decompose_one_canon(struct dynstr_ucs4 *d, uint32_t c) { | |
861 | utf32__decompose_one_generic(canon); | |
862 | } | |
863 | ||
864 | /** @brief Recursively compute the compatibility decomposition of @p c | |
865 | * @param d Dynamic string to store decomposition in | |
866 | * @param c Code point to decompose (must be a valid!) | |
f98fcddb | 867 | * @return 0 on success, non-0 on error |
e5a5a138 RK |
868 | */ |
869 | static void utf32__decompose_one_compat(struct dynstr_ucs4 *d, uint32_t c) { | |
870 | utf32__decompose_one_generic(compat); | |
871 | } | |
872 | ||
16506c9d RK |
873 | /** @brief Magic utf32__compositions() return value for Hangul Choseong */ |
874 | static const uint32_t utf32__hangul_L[1]; | |
875 | ||
876 | /** @brief Return the list of compositions that @p c starts | |
877 | * @param c Starter code point | |
878 | * @return Composition list or NULL | |
879 | * | |
880 | * For Hangul leading (Choseong) jamo we return the special value | |
881 | * utf32__hangul_L. These code points are not listed as the targets of | |
882 | * canonical decompositions (make-unidata checks) so there is no confusion with | |
883 | * real decompositions here. | |
884 | */ | |
885 | static const uint32_t *utf32__compositions(uint32_t c) { | |
886 | const uint32_t *compositions = utf32__unidata(c)->composed; | |
887 | ||
888 | if(compositions) | |
889 | return compositions; | |
890 | /* Special-casing for Hangul */ | |
891 | switch(utf32__grapheme_break(c)) { | |
892 | default: | |
893 | return 0; | |
894 | case unicode_Grapheme_Break_L: | |
895 | return utf32__hangul_L; | |
896 | } | |
897 | } | |
898 | ||
899 | /** @brief Composition step | |
900 | * @param s Start of string | |
901 | * @param ns Length of string | |
902 | * @return New length of string | |
903 | * | |
904 | * This is called from utf32__decompose_generic() to compose the result string | |
905 | * in place. | |
906 | */ | |
907 | static size_t utf32__compose(uint32_t *s, size_t ns) { | |
908 | const uint32_t *compositions; | |
909 | uint32_t *start = s, *t = s, *tt, cc; | |
910 | ||
911 | while(ns > 0) { | |
912 | uint32_t starter = *s++; | |
913 | int block_starters = 0; | |
914 | --ns; | |
915 | /* We don't attempt to compose the following things: | |
916 | * - final characters whatever kind they are | |
917 | * - non-starter characters | |
918 | * - starters that don't take part in a canonical decomposition mapping | |
919 | */ | |
920 | if(ns == 0 | |
921 | || utf32__combining_class(starter) | |
922 | || !(compositions = utf32__compositions(starter))) { | |
923 | *t++ = starter; | |
924 | continue; | |
925 | } | |
926 | if(compositions != utf32__hangul_L) { | |
927 | /* Where we'll put the eventual starter */ | |
928 | tt = t++; | |
929 | do { | |
930 | /* See if we can find composition of starter+*s */ | |
931 | const uint32_t cchar = *s, *cp = compositions; | |
932 | while((cc = *cp++)) { | |
933 | const uint32_t *decomp = utf32__decomposition_canon(cc); | |
934 | /* We know decomp[0] == starter */ | |
935 | if(decomp[1] == cchar) | |
936 | break; | |
937 | } | |
938 | if(cc) { | |
939 | /* Found a composition: cc decomposes to starter,*s */ | |
940 | starter = cc; | |
941 | compositions = utf32__compositions(starter); | |
942 | ++s; | |
943 | --ns; | |
944 | } else { | |
945 | /* No composition found. */ | |
946 | const int class = utf32__combining_class(*s); | |
947 | if(class) { | |
948 | /* Transfer the uncomposable combining character to the output */ | |
949 | *t++ = *s++; | |
950 | --ns; | |
951 | /* All the combining characters of the same class of the | |
952 | * uncomposable character are blocked by it, but there may be | |
953 | * others of higher class later. We eat the uncomposable and | |
954 | * blocked characters and go back round the loop for that higher | |
955 | * class. */ | |
956 | while(ns > 0 && utf32__combining_class(*s) == class) { | |
957 | *t++ = *s++; | |
958 | --ns; | |
959 | } | |
960 | /* Block any subsequent starters */ | |
961 | block_starters = 1; | |
962 | } else { | |
963 | /* The uncombinable character is itself a starter, so we don't | |
964 | * transfer it to the output but instead go back round the main | |
965 | * loop. */ | |
966 | break; | |
967 | } | |
968 | } | |
969 | /* Keep going while there are still characters and the starter takes | |
970 | * part in some composition */ | |
971 | } while(ns > 0 && compositions | |
972 | && (!block_starters || utf32__combining_class(*s))); | |
973 | /* Store any remaining combining characters */ | |
974 | while(ns > 0 && utf32__combining_class(*s)) { | |
975 | *t++ = *s++; | |
976 | --ns; | |
977 | } | |
978 | /* Store the resulting starter */ | |
979 | *tt = starter; | |
980 | } else { | |
981 | /* Special-casing for Hangul | |
982 | * | |
983 | * If there are combining characters between the L and the V then they | |
984 | * will block the V and so no composition happens. Similarly combining | |
985 | * characters between V and T will block the T and so we only get as far | |
986 | * as LV. | |
987 | */ | |
988 | if(utf32__grapheme_break(*s) == unicode_Grapheme_Break_V) { | |
989 | const uint32_t V = *s++; | |
990 | const uint32_t LIndex = starter - LBase; | |
991 | const uint32_t VIndex = V - VBase; | |
992 | uint32_t TIndex; | |
993 | --ns; | |
994 | if(ns > 0 | |
995 | && utf32__grapheme_break(*s) == unicode_Grapheme_Break_T) { | |
996 | /* We have an L V T sequence */ | |
997 | const uint32_t T = *s++; | |
998 | TIndex = T - TBase; | |
999 | --ns; | |
1000 | } else | |
1001 | /* It's just L V */ | |
1002 | TIndex = 0; | |
1003 | /* Compose to LVT or LV as appropriate */ | |
1004 | starter = (LIndex * VCount + VIndex) * TCount + TIndex + SBase; | |
1005 | } /* else we only have L or LV and no V or T */ | |
1006 | *t++ = starter; | |
1007 | /* There could be some combining characters that belong to the V or T. | |
1008 | * These will be treated as non-starter characters at the top of the loop | |
1009 | * and thuss transferred to the output. */ | |
1010 | } | |
1011 | } | |
1012 | return t - start; | |
1013 | } | |
1014 | ||
1015 | /** @brief Guts of the composition and decomposition functions | |
1016 | * @param WHICH @c canon or @c compat to choose decomposition | |
1017 | * @param COMPOSE @c 0 or @c 1 to compose | |
1018 | */ | |
1019 | #define utf32__decompose_generic(WHICH, COMPOSE) do { \ | |
e5a5a138 RK |
1020 | struct dynstr_ucs4 d; \ |
1021 | uint32_t c; \ | |
1022 | \ | |
1023 | dynstr_ucs4_init(&d); \ | |
1024 | while(ns) { \ | |
1025 | c = *s++; \ | |
56fd389c | 1026 | if((c >= 0xD800 && c <= 0xDFFF) || c > 0x10FFFF) \ |
e5a5a138 RK |
1027 | goto error; \ |
1028 | utf32__decompose_one_##WHICH(&d, c); \ | |
1029 | --ns; \ | |
1030 | } \ | |
1031 | if(utf32__canonical_ordering(d.vec, d.nvec)) \ | |
1032 | goto error; \ | |
16506c9d RK |
1033 | if(COMPOSE) \ |
1034 | d.nvec = utf32__compose(d.vec, d.nvec); \ | |
e5a5a138 RK |
1035 | dynstr_ucs4_terminate(&d); \ |
1036 | if(ndp) \ | |
1037 | *ndp = d.nvec; \ | |
1038 | return d.vec; \ | |
1039 | error: \ | |
1040 | xfree(d.vec); \ | |
1041 | return 0; \ | |
1042 | } while(0) | |
1043 | ||
1044 | /** @brief Canonically decompose @p [s,s+ns) | |
1045 | * @param s Pointer to string | |
1046 | * @param ns Length of string | |
1047 | * @param ndp Where to store length of result | |
f98fcddb | 1048 | * @return Pointer to result string, or NULL on error |
e5a5a138 | 1049 | * |
16506c9d RK |
1050 | * Computes NFD (Normalization Form D) of the string at @p s. This implies |
1051 | * performing all canonical decompositions and then normalizing the order of | |
1052 | * combining characters. | |
e5a5a138 | 1053 | * |
56fd389c | 1054 | * Returns NULL if the string is not valid for either of the following reasons: |
e5a5a138 RK |
1055 | * - it codes for a UTF-16 surrogate |
1056 | * - it codes for a value outside the unicode code space | |
16506c9d RK |
1057 | * |
1058 | * See also: | |
1059 | * - utf32_decompose_compat() | |
1060 | * - utf32_compose_canon() | |
e5a5a138 RK |
1061 | */ |
1062 | uint32_t *utf32_decompose_canon(const uint32_t *s, size_t ns, size_t *ndp) { | |
16506c9d | 1063 | utf32__decompose_generic(canon, 0); |
e5a5a138 RK |
1064 | } |
1065 | ||
1066 | /** @brief Compatibility decompose @p [s,s+ns) | |
1067 | * @param s Pointer to string | |
1068 | * @param ns Length of string | |
1069 | * @param ndp Where to store length of result | |
f98fcddb | 1070 | * @return Pointer to result string, or NULL on error |
e5a5a138 | 1071 | * |
16506c9d RK |
1072 | * Computes NFKD (Normalization Form KD) of the string at @p s. This implies |
1073 | * performing all canonical and compatibility decompositions and then | |
1074 | * normalizing the order of combining characters. | |
e5a5a138 | 1075 | * |
56fd389c | 1076 | * Returns NULL if the string is not valid for either of the following reasons: |
e5a5a138 RK |
1077 | * - it codes for a UTF-16 surrogate |
1078 | * - it codes for a value outside the unicode code space | |
16506c9d RK |
1079 | * |
1080 | * See also: | |
1081 | * - utf32_decompose_canon() | |
1082 | * - utf32_compose_compat() | |
e5a5a138 RK |
1083 | */ |
1084 | uint32_t *utf32_decompose_compat(const uint32_t *s, size_t ns, size_t *ndp) { | |
16506c9d RK |
1085 | utf32__decompose_generic(compat, 0); |
1086 | } | |
1087 | ||
1088 | /** @brief Canonically compose @p [s,s+ns) | |
1089 | * @param s Pointer to string | |
1090 | * @param ns Length of string | |
1091 | * @param ndp Where to store length of result | |
1092 | * @return Pointer to result string, or NULL on error | |
1093 | * | |
1094 | * Computes NFC (Normalization Form C) of the string at @p s. This implies | |
1095 | * performing all canonical decompositions, normalizing the order of combining | |
1096 | * characters and then composing all unblocked primary compositables. | |
1097 | * | |
1098 | * Returns NULL if the string is not valid for either of the following reasons: | |
1099 | * - it codes for a UTF-16 surrogate | |
1100 | * - it codes for a value outside the unicode code space | |
1101 | * | |
1102 | * See also: | |
1103 | * - utf32_compose_compat() | |
1104 | * - utf32_decompose_canon() | |
1105 | */ | |
1106 | uint32_t *utf32_compose_canon(const uint32_t *s, size_t ns, size_t *ndp) { | |
1107 | utf32__decompose_generic(canon, 1); | |
1108 | } | |
1109 | ||
1110 | /** @brief Compatibility compose @p [s,s+ns) | |
1111 | * @param s Pointer to string | |
1112 | * @param ns Length of string | |
1113 | * @param ndp Where to store length of result | |
1114 | * @return Pointer to result string, or NULL on error | |
1115 | * | |
1116 | * Computes NFKC (Normalization Form KC) of the string at @p s. This implies | |
1117 | * performing all canonical and compatibility decompositions, normalizing the | |
1118 | * order of combining characters and then composing all unblocked primary | |
1119 | * compositables. | |
1120 | * | |
1121 | * Returns NULL if the string is not valid for either of the following reasons: | |
1122 | * - it codes for a UTF-16 surrogate | |
1123 | * - it codes for a value outside the unicode code space | |
1124 | * | |
1125 | * See also: | |
1126 | * - utf32_compose_canon() | |
1127 | * - utf32_decompose_compat() | |
1128 | */ | |
1129 | uint32_t *utf32_compose_compat(const uint32_t *s, size_t ns, size_t *ndp) { | |
1130 | utf32__decompose_generic(compat, 1); | |
e5a5a138 RK |
1131 | } |
1132 | ||
56fd389c RK |
1133 | /** @brief Single-character case-fold and decompose operation */ |
1134 | #define utf32__casefold_one(WHICH) do { \ | |
bcf9ed7f | 1135 | const uint32_t *cf = utf32__unidata(c)->casefold; \ |
56fd389c RK |
1136 | if(cf) { \ |
1137 | /* Found a case-fold mapping in the table */ \ | |
1138 | while(*cf) \ | |
1139 | utf32__decompose_one_##WHICH(&d, *cf++); \ | |
1140 | } else \ | |
1141 | utf32__decompose_one_##WHICH(&d, c); \ | |
1142 | } while(0) | |
e5a5a138 RK |
1143 | |
1144 | /** @brief Case-fold @p [s,s+ns) | |
1145 | * @param s Pointer to string | |
1146 | * @param ns Length of string | |
1147 | * @param ndp Where to store length of result | |
f98fcddb | 1148 | * @return Pointer to result string, or NULL on error |
e5a5a138 RK |
1149 | * |
1150 | * Case-fold the string at @p s according to full default case-folding rules | |
56fd389c | 1151 | * (s3.13) for caseless matching. The result will be in NFD. |
e5a5a138 | 1152 | * |
56fd389c | 1153 | * Returns NULL if the string is not valid for either of the following reasons: |
e5a5a138 RK |
1154 | * - it codes for a UTF-16 surrogate |
1155 | * - it codes for a value outside the unicode code space | |
1156 | */ | |
1157 | uint32_t *utf32_casefold_canon(const uint32_t *s, size_t ns, size_t *ndp) { | |
1158 | struct dynstr_ucs4 d; | |
1159 | uint32_t c; | |
1160 | size_t n; | |
1161 | uint32_t *ss = 0; | |
1162 | ||
1163 | /* If the canonical decomposition of the string includes any combining | |
1164 | * character that case-folds to a non-combining character then we must | |
1165 | * normalize before we fold. In Unicode 5.0.0 this means 0345 COMBINING | |
1166 | * GREEK YPOGEGRAMMENI in its decomposition and the various characters that | |
1167 | * canonically decompose to it. */ | |
bcf9ed7f RK |
1168 | for(n = 0; n < ns; ++n) |
1169 | if(utf32__unidata(s[n])->flags & unicode_normalize_before_casefold) | |
e5a5a138 | 1170 | break; |
e5a5a138 RK |
1171 | if(n < ns) { |
1172 | /* We need a preliminary decomposition */ | |
1173 | if(!(ss = utf32_decompose_canon(s, ns, &ns))) | |
1174 | return 0; | |
1175 | s = ss; | |
1176 | } | |
1177 | dynstr_ucs4_init(&d); | |
1178 | while(ns) { | |
1179 | c = *s++; | |
56fd389c | 1180 | if((c >= 0xD800 && c <= 0xDFFF) || c > 0x10FFFF) |
e5a5a138 | 1181 | goto error; |
56fd389c | 1182 | utf32__casefold_one(canon); |
e5a5a138 RK |
1183 | --ns; |
1184 | } | |
1185 | if(utf32__canonical_ordering(d.vec, d.nvec)) | |
1186 | goto error; | |
1187 | dynstr_ucs4_terminate(&d); | |
1188 | if(ndp) | |
1189 | *ndp = d.nvec; | |
1190 | return d.vec; | |
1191 | error: | |
1192 | xfree(d.vec); | |
1193 | xfree(ss); | |
1194 | return 0; | |
1195 | } | |
1196 | ||
f98fcddb | 1197 | /** @brief Compatibility case-fold @p [s,s+ns) |
56fd389c RK |
1198 | * @param s Pointer to string |
1199 | * @param ns Length of string | |
1200 | * @param ndp Where to store length of result | |
f98fcddb | 1201 | * @return Pointer to result string, or NULL on error |
56fd389c RK |
1202 | * |
1203 | * Case-fold the string at @p s according to full default case-folding rules | |
1204 | * (s3.13) for compatibility caseless matching. The result will be in NFKD. | |
1205 | * | |
1206 | * Returns NULL if the string is not valid for either of the following reasons: | |
1207 | * - it codes for a UTF-16 surrogate | |
1208 | * - it codes for a value outside the unicode code space | |
1209 | */ | |
1210 | uint32_t *utf32_casefold_compat(const uint32_t *s, size_t ns, size_t *ndp) { | |
1211 | struct dynstr_ucs4 d; | |
1212 | uint32_t c; | |
1213 | size_t n; | |
1214 | uint32_t *ss = 0; | |
1215 | ||
bcf9ed7f RK |
1216 | for(n = 0; n < ns; ++n) |
1217 | if(utf32__unidata(s[n])->flags & unicode_normalize_before_casefold) | |
56fd389c | 1218 | break; |
56fd389c RK |
1219 | if(n < ns) { |
1220 | /* We need a preliminary _canonical_ decomposition */ | |
1221 | if(!(ss = utf32_decompose_canon(s, ns, &ns))) | |
1222 | return 0; | |
1223 | s = ss; | |
1224 | } | |
1225 | /* This computes NFKD(toCaseFold(s)) */ | |
1226 | #define compat_casefold_middle() do { \ | |
1227 | dynstr_ucs4_init(&d); \ | |
1228 | while(ns) { \ | |
1229 | c = *s++; \ | |
1230 | if((c >= 0xD800 && c <= 0xDFFF) || c > 0x10FFFF) \ | |
1231 | goto error; \ | |
1232 | utf32__casefold_one(compat); \ | |
1233 | --ns; \ | |
1234 | } \ | |
1235 | if(utf32__canonical_ordering(d.vec, d.nvec)) \ | |
1236 | goto error; \ | |
1237 | } while(0) | |
1238 | /* Do the inner (NFKD o toCaseFold) */ | |
1239 | compat_casefold_middle(); | |
1240 | /* We can do away with the NFD'd copy of the input now */ | |
1241 | xfree(ss); | |
1242 | s = ss = d.vec; | |
1243 | ns = d.nvec; | |
1244 | /* Do the outer (NFKD o toCaseFold) */ | |
1245 | compat_casefold_middle(); | |
1246 | /* That's all */ | |
1247 | dynstr_ucs4_terminate(&d); | |
1248 | if(ndp) | |
1249 | *ndp = d.nvec; | |
1250 | return d.vec; | |
1251 | error: | |
1252 | xfree(d.vec); | |
1253 | xfree(ss); | |
1254 | return 0; | |
1255 | } | |
1256 | ||
e5a5a138 RK |
1257 | /** @brief Order a pair of UTF-32 strings |
1258 | * @param a First 0-terminated string | |
1259 | * @param b Second 0-terminated string | |
1260 | * @return -1, 0 or 1 for a less than, equal to or greater than b | |
1261 | * | |
1262 | * "Comparable to strcmp() at its best." | |
1263 | */ | |
1264 | int utf32_cmp(const uint32_t *a, const uint32_t *b) { | |
1265 | while(*a && *b && *a == *b) { | |
1266 | ++a; | |
1267 | ++b; | |
1268 | } | |
1269 | return *a < *b ? -1 : (*a > *b ? 1 : 0); | |
1270 | } | |
1271 | ||
35b651f0 RK |
1272 | /** @brief Identify a grapheme cluster boundary |
1273 | * @param s Start of string (must be NFD) | |
1274 | * @param ns Length of string | |
1275 | * @param n Index within string (in [0,ns].) | |
1276 | * @return 1 at a grapheme cluster boundary, 0 otherwise | |
1277 | * | |
1278 | * This function identifies default grapheme cluster boundaries as described in | |
f98fcddb | 1279 | * UAX #29 s3. It returns non-0 if @p n points at the code point just after a |
35b651f0 RK |
1280 | * grapheme cluster boundary (including the hypothetical code point just after |
1281 | * the end of the string). | |
f98fcddb RK |
1282 | * |
1283 | * This function uses utf32_iterator_set() internally; see that function for | |
1284 | * remarks on performance. | |
35b651f0 | 1285 | */ |
1625e11a | 1286 | int utf32_is_grapheme_boundary(const uint32_t *s, size_t ns, size_t n) { |
092f426f | 1287 | struct utf32_iterator_data it[1]; |
35b651f0 | 1288 | |
092f426f RK |
1289 | utf32__iterator_init(it, s, ns, n); |
1290 | return utf32_iterator_grapheme_boundary(it); | |
0b7052da RK |
1291 | } |
1292 | ||
1293 | /** @brief Identify a word boundary | |
1294 | * @param s Start of string (must be NFD) | |
1295 | * @param ns Length of string | |
1296 | * @param n Index within string (in [0,ns].) | |
1297 | * @return 1 at a word boundary, 0 otherwise | |
1298 | * | |
1299 | * This function identifies default word boundaries as described in UAX #29 s4. | |
f98fcddb | 1300 | * It returns non-0 if @p n points at the code point just after a word boundary |
0b7052da | 1301 | * (including the hypothetical code point just after the end of the string). |
f98fcddb RK |
1302 | * |
1303 | * This function uses utf32_iterator_set() internally; see that function for | |
1304 | * remarks on performance. | |
0b7052da RK |
1305 | */ |
1306 | int utf32_is_word_boundary(const uint32_t *s, size_t ns, size_t n) { | |
092f426f | 1307 | struct utf32_iterator_data it[1]; |
0b7052da | 1308 | |
092f426f RK |
1309 | utf32__iterator_init(it, s, ns, n); |
1310 | return utf32_iterator_word_boundary(it); | |
0b7052da RK |
1311 | } |
1312 | ||
8818b7fc RK |
1313 | /** @brief Split [s,ns) into multiple words |
1314 | * @param s Pointer to start of string | |
1315 | * @param ns Length of string | |
1316 | * @param nwp Where to store word count, or NULL | |
c85b7022 | 1317 | * @param wbreak Word_Break property tailor, or NULL |
8818b7fc RK |
1318 | * @return Pointer to array of pointers to words |
1319 | * | |
1320 | * The returned array is terminated by a NULL pointer and individual | |
1321 | * strings are 0-terminated. | |
1322 | */ | |
c85b7022 RK |
1323 | uint32_t **utf32_word_split(const uint32_t *s, size_t ns, size_t *nwp, |
1324 | unicode_property_tailor *wbreak) { | |
8818b7fc RK |
1325 | struct utf32_iterator_data it[1]; |
1326 | size_t b1 = 0, b2 = 0 ,i; | |
1327 | int isword; | |
1328 | struct vector32 v32[1]; | |
1329 | uint32_t *w; | |
1330 | ||
1331 | vector32_init(v32); | |
1332 | utf32__iterator_init(it, s, ns, 0); | |
c85b7022 | 1333 | it->word_break = wbreak; |
8818b7fc RK |
1334 | /* Work our way through the string stopping at each word break. */ |
1335 | do { | |
1336 | if(utf32_iterator_word_boundary(it)) { | |
1337 | /* We've found a new boundary */ | |
1338 | b1 = b2; | |
1339 | b2 = it->n; | |
1340 | /*fprintf(stderr, "[%zu, %zu) is a candidate word\n", b1, b2);*/ | |
1341 | /* Inspect the characters between the boundary and form an opinion as to | |
1342 | * whether they are a word or not */ | |
1343 | isword = 0; | |
1344 | for(i = b1; i < b2; ++i) { | |
c85b7022 | 1345 | switch(utf32__iterator_word_break(it, it->s[i])) { |
8818b7fc RK |
1346 | case unicode_Word_Break_ALetter: |
1347 | case unicode_Word_Break_Numeric: | |
1348 | case unicode_Word_Break_Katakana: | |
1349 | isword = 1; | |
1350 | break; | |
1351 | default: | |
1352 | break; | |
1353 | } | |
1354 | } | |
1355 | /* If it's a word add it to the list of results */ | |
1356 | if(isword) { | |
1357 | w = xcalloc(b2 - b1 + 1, sizeof(uint32_t)); | |
1358 | memcpy(w, it->s + b1, (b2 - b1) * sizeof (uint32_t)); | |
1359 | vector32_append(v32, w); | |
1360 | } | |
1361 | } | |
1362 | } while(!utf32_iterator_advance(it, 1)); | |
1363 | vector32_terminate(v32); | |
1364 | if(nwp) | |
1365 | *nwp = v32->nvec; | |
1366 | return v32->vec; | |
1367 | } | |
1368 | ||
e5a5a138 | 1369 | /*@}*/ |
349b7b74 | 1370 | /** @defgroup utf8 Functions that operate on UTF-8 strings */ |
e5a5a138 RK |
1371 | /*@{*/ |
1372 | ||
1373 | /** @brief Wrapper to transform a UTF-8 string using the UTF-32 function */ | |
1374 | #define utf8__transform(FN) do { \ | |
1375 | uint32_t *to32 = 0, *decomp32 = 0; \ | |
1376 | size_t nto32, ndecomp32; \ | |
1377 | char *decomp8 = 0; \ | |
1378 | \ | |
1379 | if(!(to32 = utf8_to_utf32(s, ns, &nto32))) goto error; \ | |
1380 | if(!(decomp32 = FN(to32, nto32, &ndecomp32))) goto error; \ | |
1381 | decomp8 = utf32_to_utf8(decomp32, ndecomp32, ndp); \ | |
1382 | error: \ | |
1383 | xfree(to32); \ | |
1384 | xfree(decomp32); \ | |
1385 | return decomp8; \ | |
1386 | } while(0) | |
1387 | ||
1388 | /** @brief Canonically decompose @p [s,s+ns) | |
1389 | * @param s Pointer to string | |
1390 | * @param ns Length of string | |
1391 | * @param ndp Where to store length of result | |
f98fcddb | 1392 | * @return Pointer to result string, or NULL on error |
e5a5a138 | 1393 | * |
0ae60b83 RK |
1394 | * Computes NFD (Normalization Form D) of the string at @p s. This implies |
1395 | * performing all canonical decompositions and then normalizing the order of | |
1396 | * combining characters. | |
e5a5a138 RK |
1397 | * |
1398 | * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why | |
1399 | * this might be. | |
1400 | * | |
0ae60b83 RK |
1401 | * See also: |
1402 | * - utf32_decompose_canon(). | |
1403 | * - utf8_decompose_compat() | |
1404 | * - utf8_compose_canon() | |
e5a5a138 RK |
1405 | */ |
1406 | char *utf8_decompose_canon(const char *s, size_t ns, size_t *ndp) { | |
1407 | utf8__transform(utf32_decompose_canon); | |
1408 | } | |
1409 | ||
1410 | /** @brief Compatibility decompose @p [s,s+ns) | |
1411 | * @param s Pointer to string | |
1412 | * @param ns Length of string | |
1413 | * @param ndp Where to store length of result | |
f98fcddb | 1414 | * @return Pointer to result string, or NULL on error |
e5a5a138 | 1415 | * |
0ae60b83 RK |
1416 | * Computes NFKD (Normalization Form KD) of the string at @p s. This implies |
1417 | * performing all canonical and compatibility decompositions and then | |
1418 | * normalizing the order of combining characters. | |
e5a5a138 RK |
1419 | * |
1420 | * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why | |
1421 | * this might be. | |
1422 | * | |
0ae60b83 RK |
1423 | * See also: |
1424 | * - utf32_decompose_compat(). | |
1425 | * - utf8_decompose_canon() | |
1426 | * - utf8_compose_compat() | |
e5a5a138 RK |
1427 | */ |
1428 | char *utf8_decompose_compat(const char *s, size_t ns, size_t *ndp) { | |
1429 | utf8__transform(utf32_decompose_compat); | |
1430 | } | |
1431 | ||
0ae60b83 RK |
1432 | /** @brief Canonically compose @p [s,s+ns) |
1433 | * @param s Pointer to string | |
1434 | * @param ns Length of string | |
1435 | * @param ndp Where to store length of result | |
1436 | * @return Pointer to result string, or NULL on error | |
1437 | * | |
1438 | * Computes NFC (Normalization Form C) of the string at @p s. This implies | |
1439 | * performing all canonical decompositions, normalizing the order of combining | |
1440 | * characters and then composing all unblocked primary compositables. | |
1441 | * | |
1442 | * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why | |
1443 | * this might be. | |
1444 | * | |
1445 | * See also: | |
1446 | * - utf32_compose_canon() | |
1447 | * - utf8_compose_compat() | |
1448 | * - utf8_decompose_canon() | |
1449 | */ | |
1450 | char *utf8_compose_canon(const char *s, size_t ns, size_t *ndp) { | |
1451 | utf8__transform(utf32_compose_canon); | |
1452 | } | |
1453 | ||
1454 | /** @brief Compatibility compose @p [s,s+ns) | |
1455 | * @param s Pointer to string | |
1456 | * @param ns Length of string | |
1457 | * @param ndp Where to store length of result | |
1458 | * @return Pointer to result string, or NULL on error | |
1459 | * | |
1460 | * Computes NFKC (Normalization Form KC) of the string at @p s. This implies | |
1461 | * performing all canonical and compatibility decompositions, normalizing the | |
1462 | * order of combining characters and then composing all unblocked primary | |
1463 | * compositables. | |
1464 | * | |
1465 | * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why | |
1466 | * this might be. | |
1467 | * | |
1468 | * See also: | |
1469 | * - utf32_compose_compat() | |
1470 | * - utf8_compose_canon() | |
1471 | * - utf8_decompose_compat() | |
1472 | */ | |
1473 | char *utf8_compose_compat(const char *s, size_t ns, size_t *ndp) { | |
1474 | utf8__transform(utf32_compose_compat); | |
1475 | } | |
1476 | ||
e5a5a138 RK |
1477 | /** @brief Case-fold @p [s,s+ns) |
1478 | * @param s Pointer to string | |
1479 | * @param ns Length of string | |
1480 | * @param ndp Where to store length of result | |
f98fcddb | 1481 | * @return Pointer to result string, or NULL on error |
e5a5a138 RK |
1482 | * |
1483 | * Case-fold the string at @p s according to full default case-folding rules | |
1484 | * (s3.13). The result will be in NFD. | |
1485 | * | |
1486 | * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why | |
1487 | * this might be. | |
1488 | */ | |
1489 | char *utf8_casefold_canon(const char *s, size_t ns, size_t *ndp) { | |
1490 | utf8__transform(utf32_casefold_canon); | |
1491 | } | |
1492 | ||
1493 | /** @brief Compatibility case-fold @p [s,s+ns) | |
1494 | * @param s Pointer to string | |
1495 | * @param ns Length of string | |
1496 | * @param ndp Where to store length of result | |
f98fcddb | 1497 | * @return Pointer to result string, or NULL on error |
e5a5a138 RK |
1498 | * |
1499 | * Case-fold the string at @p s according to full default case-folding rules | |
1500 | * (s3.13). The result will be in NFKD. | |
1501 | * | |
1502 | * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why | |
1503 | * this might be. | |
1504 | */ | |
e5a5a138 RK |
1505 | char *utf8_casefold_compat(const char *s, size_t ns, size_t *ndp) { |
1506 | utf8__transform(utf32_casefold_compat); | |
1507 | } | |
e5a5a138 | 1508 | |
8818b7fc RK |
1509 | /** @brief Split [s,ns) into multiple words |
1510 | * @param s Pointer to start of string | |
1511 | * @param ns Length of string | |
1512 | * @param nwp Where to store word count, or NULL | |
c85b7022 | 1513 | * @param wbreak Word_Break property tailor, or NULL |
8818b7fc RK |
1514 | * @return Pointer to array of pointers to words |
1515 | * | |
1516 | * The returned array is terminated by a NULL pointer and individual | |
1517 | * strings are 0-terminated. | |
1518 | */ | |
c85b7022 RK |
1519 | char **utf8_word_split(const char *s, size_t ns, size_t *nwp, |
1520 | unicode_property_tailor *wbreak) { | |
8818b7fc RK |
1521 | uint32_t *to32 = 0, **v32 = 0; |
1522 | size_t nto32, nv, n; | |
1523 | char **v8 = 0, **ret = 0; | |
c85b7022 | 1524 | |
8818b7fc | 1525 | if(!(to32 = utf8_to_utf32(s, ns, &nto32))) goto error; |
c85b7022 | 1526 | if(!(v32 = utf32_word_split(to32, nto32, &nv, wbreak))) goto error; |
8818b7fc RK |
1527 | v8 = xcalloc(sizeof (char *), nv + 1); |
1528 | for(n = 0; n < nv; ++n) | |
1529 | if(!(v8[n] = utf32_to_utf8(v32[n], utf32_len(v32[n]), 0))) | |
1530 | goto error; | |
1531 | ret = v8; | |
1532 | *nwp = nv; | |
1533 | v8 = 0; /* don't free */ | |
c85b7022 | 1534 | error: |
8818b7fc RK |
1535 | if(v8) { |
1536 | for(n = 0; n < nv; ++n) | |
1537 | xfree(v8[n]); | |
1538 | xfree(v8); | |
1539 | } | |
1540 | if(v32) { | |
1541 | for(n = 0; n < nv; ++n) | |
1542 | xfree(v32[n]); | |
1543 | xfree(v32); | |
1544 | } | |
1545 | xfree(to32); | |
1546 | return ret; | |
1547 | } | |
1548 | ||
1549 | ||
e5a5a138 RK |
1550 | /*@}*/ |
1551 | ||
1552 | /* | |
1553 | Local Variables: | |
1554 | c-basic-offset:2 | |
1555 | comment-column:40 | |
1556 | fill-column:79 | |
1557 | indent-tabs-mode:nil | |
1558 | End: | |
1559 | */ |