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