2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
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.
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.
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
20 /** @file lib/unicode.c
21 * @brief Unicode support functions
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.
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
32 * As the code stands this guarantee is not well met!
37 * - @ref utf32iterator
49 /** @defgroup utf32props Unicode Code Point Properties */
52 static const struct unidata *utf32__unidata_hard(uint32_t c);
54 /** @brief Find definition of code point @p c
56 * @return Pointer to @ref unidata structure for @p c
58 * @p c can be any 32-bit value, a sensible value will be returned regardless.
59 * The returned pointer is NOT guaranteed to be unique to @p c.
61 static inline const struct unidata *utf32__unidata(uint32_t c) {
62 /* The bottom half of the table contains almost everything of interest
63 * and we can just return the right thing straight away */
64 if(c < UNICODE_BREAK_START)
65 return &unidata[c / UNICODE_MODULUS][c % UNICODE_MODULUS];
67 return utf32__unidata_hard(c);
70 /** @brief Find definition of code point @p c
72 * @return Pointer to @ref unidata structure for @p c
74 * @p c can be any 32-bit value, a sensible value will be returned regardless.
75 * The returned pointer is NOT guaranteed to be unique to @p c.
77 * Don't use this function (although it will work fine) - use utf32__unidata()
80 static const struct unidata *utf32__unidata_hard(uint32_t c) {
81 if(c < UNICODE_BREAK_START)
82 return &unidata[c / UNICODE_MODULUS][c % UNICODE_MODULUS];
83 /* Within the break everything is unassigned */
84 if(c < UNICODE_BREAK_END)
85 return utf32__unidata(0xFFFF); /* guaranteed to be Cn */
86 /* Planes 15 and 16 are (mostly) private use */
87 if((c >= 0xF0000 && c <= 0xFFFFD)
88 || (c >= 0x100000 && c <= 0x10FFFD))
89 return utf32__unidata(0xE000); /* first Co code point */
90 /* Everything else above the break top is unassigned */
91 if(c >= UNICODE_BREAK_TOP)
92 return utf32__unidata(0xFFFF); /* guaranteed to be Cn */
93 /* Currently the rest is language tags and variation selectors */
94 c -= (UNICODE_BREAK_END - UNICODE_BREAK_START);
95 return &unidata[c / UNICODE_MODULUS][c % UNICODE_MODULUS];
98 /** @brief Return the combining class of @p c
100 * @return Combining class of @p c
102 * @p c can be any 32-bit value, a sensible value will be returned regardless.
104 static inline int utf32__combining_class(uint32_t c) {
105 return utf32__unidata(c)->ccc;
108 /** @brief Return the combining class of @p c
109 * @param c Code point
110 * @return Combining class of @p c
112 * @p c can be any 32-bit value, a sensible value will be returned regardless.
114 int utf32_combining_class(uint32_t c) {
115 return utf32__combining_class(c);
118 /** @brief Return the General_Category value for @p c
119 * @param c Code point
120 * @return General_Category property value
122 * @p c can be any 32-bit value, a sensible value will be returned regardless.
124 static inline enum unicode_General_Category utf32__general_category(uint32_t c) {
125 return utf32__unidata(c)->general_category;
128 /** @brief Determine Grapheme_Break property
129 * @param c Code point
130 * @return Grapheme_Break property value of @p c
132 * @p c can be any 32-bit value, a sensible value will be returned regardless.
134 static inline enum unicode_Grapheme_Break utf32__grapheme_break(uint32_t c) {
135 return utf32__unidata(c)->grapheme_break;
138 /** @brief Determine Word_Break property
139 * @param c Code point
140 * @return Word_Break property value of @p c
142 * @p c can be any 32-bit value, a sensible value will be returned regardless.
144 static inline enum unicode_Word_Break utf32__word_break(uint32_t c) {
145 return utf32__unidata(c)->word_break;
148 /** @brief Determine Sentence_Break property
149 * @param c Code point
150 * @return Word_Break property value of @p c
152 * @p c can be any 32-bit value, a sensible value will be returned regardless.
154 static inline enum unicode_Sentence_Break utf32__sentence_break(uint32_t c) {
155 return utf32__unidata(c)->sentence_break;
158 /** @brief Return true if @p c is ignorable for boundary specifications
159 * @param wb Word break property value
160 * @return non-0 if @p wb is unicode_Word_Break_Extend or unicode_Word_Break_Format
162 static inline int utf32__boundary_ignorable(enum unicode_Word_Break wb) {
163 return (wb == unicode_Word_Break_Extend
164 || wb == unicode_Word_Break_Format);
167 /** @brief Return the canonical decomposition of @p c
168 * @param c Code point
169 * @return 0-terminated canonical decomposition, or 0
171 static inline const uint32_t *utf32__decomposition_canon(uint32_t c) {
172 const struct unidata *const data = utf32__unidata(c);
173 const uint32_t *const decomp = data->decomp;
175 if(decomp && !(data->flags & unicode_compatibility_decomposition))
181 /** @brief Return the compatibility decomposition of @p c
182 * @param c Code point
183 * @return 0-terminated decomposition, or 0
185 static inline const uint32_t *utf32__decomposition_compat(uint32_t c) {
186 return utf32__unidata(c)->decomp;
190 /** @defgroup utftransform Functions that transform between different Unicode encoding forms */
193 /** @brief Convert UTF-32 to UTF-8
194 * @param s Source string
195 * @param ns Length of source string in code points
196 * @param ndp Where to store length of destination string (or NULL)
197 * @return Newly allocated destination string or NULL on error
199 * If the UTF-32 is not valid then NULL is returned. A UTF-32 code point is
201 * - it codes for a UTF-16 surrogate
202 * - it codes for a value outside the unicode code space
204 * The return value is always 0-terminated. The value returned via @p *ndp
205 * does not include the terminator.
207 char *utf32_to_utf8(const uint32_t *s, size_t ns, size_t *ndp) {
215 dynstr_append(&d, c);
216 else if(c < 0x0800) {
217 dynstr_append(&d, 0xC0 | (c >> 6));
218 dynstr_append(&d, 0x80 | (c & 0x3F));
219 } else if(c < 0x10000) {
220 if(c >= 0xD800 && c <= 0xDFFF)
222 dynstr_append(&d, 0xE0 | (c >> 12));
223 dynstr_append(&d, 0x80 | ((c >> 6) & 0x3F));
224 dynstr_append(&d, 0x80 | (c & 0x3F));
225 } else if(c < 0x110000) {
226 dynstr_append(&d, 0xF0 | (c >> 18));
227 dynstr_append(&d, 0x80 | ((c >> 12) & 0x3F));
228 dynstr_append(&d, 0x80 | ((c >> 6) & 0x3F));
229 dynstr_append(&d, 0x80 | (c & 0x3F));
234 dynstr_terminate(&d);
243 /** @brief Convert UTF-8 to UTF-32
244 * @param s Source string
245 * @param ns Length of source string in code points
246 * @param ndp Where to store length of destination string (or NULL)
247 * @return Newly allocated destination string or NULL on error
249 * The return value is always 0-terminated. The value returned via @p *ndp
250 * does not include the terminator.
252 * If the UTF-8 is not valid then NULL is returned. A UTF-8 sequence
253 * for a code point is invalid if:
254 * - it is not the shortest possible sequence for the code point
255 * - it codes for a UTF-16 surrogate
256 * - it codes for a value outside the unicode code space
258 uint32_t *utf8_to_utf32(const char *s, size_t ns, size_t *ndp) {
259 struct dynstr_ucs4 d;
261 const uint8_t *ss = (const uint8_t *)s;
264 dynstr_ucs4_init(&d);
266 const struct unicode_utf8_row *const r = &unicode_utf8_valid[*ss];
273 if(ss[1] < r->min2 || ss[1] > r->max2)
278 if(ss[1] < r->min2 || ss[1] > r->max2)
283 if(ss[1] < r->min2 || ss[1] > r->max2)
292 for(n = 1; n < r->count; ++n) {
293 if(ss[n] < 0x80 || ss[n] > 0xBF)
295 c32 = (c32 << 6) | (ss[n] & 0x3F);
297 dynstr_ucs4_append(&d, c32);
301 dynstr_ucs4_terminate(&d);
310 /** @brief Test whether [s,s+ns) is valid UTF-8
311 * @param s Start of string
312 * @param ns Length of string
313 * @return non-0 if @p s is valid UTF-8, 0 if it is not valid
315 * This function is intended to be much faster than calling utf8_to_utf32() and
316 * throwing away the result.
318 int utf8_valid(const char *s, size_t ns) {
319 const uint8_t *ss = (const uint8_t *)s;
321 const struct unicode_utf8_row *const r = &unicode_utf8_valid[*ss];
327 if(ss[1] < r->min2 || ss[1] > r->max2)
331 if(ss[1] < r->min2 || ss[1] > r->max2)
333 if(ss[2] < 0x80 || ss[2] > 0xBF)
337 if(ss[1] < r->min2 || ss[1] > r->max2)
339 if(ss[2] < 0x80 || ss[2] > 0xBF)
341 if(ss[3] < 0x80 || ss[3] > 0xBF)
356 /** @defgroup utf32iterator UTF-32 string iterators */
359 struct utf32_iterator_data {
360 /** @brief Start of string */
363 /** @brief Length of string */
366 /** @brief Current position */
369 /** @brief Last two non-ignorable characters or (uint32_t)-1
371 * last[1] is the non-Extend/Format character just before position @p n;
372 * last[0] is the one just before that.
374 * Exception 1: if there is no such non-Extend/Format character then an
375 * Extend/Format character is accepted instead.
377 * Exception 2: if there is no such character even taking that into account
378 * the value is (uint32_t)-1.
382 /** @brief Tailoring for Word_Break */
383 unicode_property_tailor *word_break;
386 /** @brief Initialize an internal private iterator
388 * @param s Start of string
389 * @param ns Length of string
390 * @param n Absolute position
392 static void utf32__iterator_init(utf32_iterator it,
393 const uint32_t *s, size_t ns, size_t n) {
397 it->last[0] = it->last[1] = -1;
399 utf32_iterator_set(it, n);
402 /** @brief Create a new iterator pointing at the start of a string
403 * @param s Start of string
404 * @param ns Length of string
405 * @return New iterator
407 utf32_iterator utf32_iterator_new(const uint32_t *s, size_t ns) {
408 utf32_iterator it = xmalloc(sizeof *it);
409 utf32__iterator_init(it, s, ns, 0);
413 /** @brief Tailor this iterator's interpretation of the Word_Break property.
415 * @param pt Property tailor function or NULL
417 * After calling this the iterator will call @p pt to determine the Word_Break
418 * property of each code point. If it returns -1 the default value will be
419 * used otherwise the returned value will be used.
421 * @p pt can be NULL to revert to the default value of the property.
423 * It is safe to call this function at any time; the iterator's internal state
424 * will be reset to suit the new tailoring.
426 void utf32_iterator_tailor_word_break(utf32_iterator it,
427 unicode_property_tailor *pt) {
429 utf32_iterator_set(it, it->n);
432 static inline enum unicode_Word_Break utf32__iterator_word_break(utf32_iterator it,
435 return utf32__word_break(c);
437 const int t = it->word_break(c);
440 return utf32__word_break(c);
446 /** @brief Destroy an iterator
449 void utf32_iterator_destroy(utf32_iterator it) {
453 /** @brief Find the current position of an interator
456 size_t utf32_iterator_where(utf32_iterator it) {
460 /** @brief Set an iterator's absolute position
462 * @param n Absolute position
463 * @return 0 on success, non-0 on error
465 * It is an error to position the iterator outside the string (but acceptable
466 * to point it at the hypothetical post-final character). If an invalid value
467 * of @p n is specified then the iterator is not changed.
469 * This function works by backing up and then advancing to reconstruct the
470 * iterator's internal state for position @p n. The worst case will be O(n)
471 * time complexity (with a worse constant factor that utf32_iterator_advance())
472 * but the typical case is essentially constant-time.
474 int utf32_iterator_set(utf32_iterator it, size_t n) {
475 /* We can't just jump to position @p n; the @p last[] values will be wrong.
476 * What we need is to jump a bit behind @p n and then advance forward,
477 * updating @p last[] along the way. How far back? We need to cross two
478 * non-ignorable code points as we advance forwards, so we'd better pass two
479 * such characters on the way back (if such are available).
483 if(n > it->ns) /* range check */
485 /* Walk backwards skipping ignorable code points */
488 && (utf32__boundary_ignorable(utf32__iterator_word_break(it,
491 /* Either m=0 or s[m-1] is not ignorable */
494 /* s[m] is our first non-ignorable code; look for a second in the same
497 && (utf32__boundary_ignorable(utf32__iterator_word_break(it,
500 /* Either m=0 or s[m-1] is not ignorable */
504 it->last[0] = it->last[1] = -1;
506 return utf32_iterator_advance(it, n - m);
509 /** @brief Advance an iterator
511 * @param count Number of code points to advance by
512 * @return 0 on success, non-0 on error
514 * It is an error to advance an iterator beyond the hypothetical post-final
515 * character of the string. If an invalid value of @p n is specified then the
516 * iterator is not changed.
518 * This function has O(n) time complexity: it works by advancing naively
519 * forwards through the string.
521 int utf32_iterator_advance(utf32_iterator it, size_t count) {
522 if(count <= it->ns - it->n) {
524 const uint32_t c = it->s[it->n];
525 const enum unicode_Word_Break wb = utf32__iterator_word_break(it, c);
526 if(it->last[1] == (uint32_t)-1
527 || !utf32__boundary_ignorable(wb)) {
528 it->last[0] = it->last[1];
539 /** @brief Find the current code point
541 * @return Current code point or 0
543 * If the iterator points at the hypothetical post-final character of the
544 * string then 0 is returned. NB that this doesn't mean that there aren't any
545 * 0 code points inside the string!
547 uint32_t utf32_iterator_code(utf32_iterator it) {
554 /** @brief Test for a grapheme boundary
556 * @return Non-0 if pointing just after a grapheme boundary, otherwise 0
558 * This function identifies default grapheme cluster boundaries as described in
559 * UAX #29 s3. It returns non-0 if @p it points at the code point just after a
560 * grapheme cluster boundary (including the hypothetical code point just after
561 * the end of the string).
563 int utf32_iterator_grapheme_boundary(utf32_iterator it) {
564 uint32_t before, after;
565 enum unicode_Grapheme_Break gbbefore, gbafter;
567 if(it->n == 0 || it->n == it->ns)
569 /* Now we know that s[n-1] and s[n] are safe to inspect */
571 before = it->s[it->n-1];
572 after = it->s[it->n];
573 if(before == 0x000D && after == 0x000A)
575 gbbefore = utf32__grapheme_break(before);
576 gbafter = utf32__grapheme_break(after);
578 if(gbbefore == unicode_Grapheme_Break_Control
583 if(gbafter == unicode_Grapheme_Break_Control
588 if(gbbefore == unicode_Grapheme_Break_L
589 && (gbafter == unicode_Grapheme_Break_L
590 || gbafter == unicode_Grapheme_Break_V
591 || gbafter == unicode_Grapheme_Break_LV
592 || gbafter == unicode_Grapheme_Break_LVT))
595 if((gbbefore == unicode_Grapheme_Break_LV
596 || gbbefore == unicode_Grapheme_Break_V)
597 && (gbafter == unicode_Grapheme_Break_V
598 || gbafter == unicode_Grapheme_Break_T))
601 if((gbbefore == unicode_Grapheme_Break_LVT
602 || gbbefore == unicode_Grapheme_Break_T)
603 && gbafter == unicode_Grapheme_Break_T)
606 if(gbafter == unicode_Grapheme_Break_Extend)
613 /** @brief Test for a word boundary
615 * @return Non-0 if pointing just after a word boundary, otherwise 0
617 * This function identifies default word boundaries as described in UAX #29 s4.
618 * It returns non-0 if @p it points at the code point just after a word
619 * boundary (including the hypothetical code point just after the end of the
620 * string) and 0 otherwise.
622 int utf32_iterator_word_boundary(utf32_iterator it) {
623 enum unicode_Word_Break twobefore, before, after, twoafter;
627 if(it->n == 0 || it->n == it->ns)
630 if(it->s[it->n-1] == 0x000D && it->s[it->n] == 0x000A)
633 /* (!Sep) x (Extend|Format) as in UAX #29 s6.2 */
634 if(utf32__sentence_break(it->s[it->n-1]) != unicode_Sentence_Break_Sep
635 && utf32__boundary_ignorable(utf32__iterator_word_break(it, it->s[it->n])))
637 /* Gather the property values we'll need for the rest of the test taking the
638 * s6.2 changes into account */
639 /* First we look at the code points after the proposed boundary */
640 nn = it->n; /* <it->ns */
641 after = utf32__iterator_word_break(it, it->s[nn++]);
642 if(!utf32__boundary_ignorable(after)) {
643 /* X (Extend|Format)* -> X */
645 && utf32__boundary_ignorable(utf32__iterator_word_break(it,
649 /* It's possible now that nn=ns */
651 twoafter = utf32__iterator_word_break(it, it->s[nn]);
653 twoafter = unicode_Word_Break_Other;
655 /* We've already recorded the non-ignorable code points before the proposed
657 before = utf32__iterator_word_break(it, it->last[1]);
658 twobefore = utf32__iterator_word_break(it, it->last[0]);
661 if(before == unicode_Word_Break_ALetter
662 && after == unicode_Word_Break_ALetter)
665 if(before == unicode_Word_Break_ALetter
666 && after == unicode_Word_Break_MidLetter
667 && twoafter == unicode_Word_Break_ALetter)
670 if(twobefore == unicode_Word_Break_ALetter
671 && before == unicode_Word_Break_MidLetter
672 && after == unicode_Word_Break_ALetter)
675 if(before == unicode_Word_Break_Numeric
676 && after == unicode_Word_Break_Numeric)
679 if(before == unicode_Word_Break_ALetter
680 && after == unicode_Word_Break_Numeric)
683 if(before == unicode_Word_Break_Numeric
684 && after == unicode_Word_Break_ALetter)
687 if(twobefore == unicode_Word_Break_Numeric
688 && before == unicode_Word_Break_MidNum
689 && after == unicode_Word_Break_Numeric)
692 if(before == unicode_Word_Break_Numeric
693 && after == unicode_Word_Break_MidNum
694 && twoafter == unicode_Word_Break_Numeric)
697 if(before == unicode_Word_Break_Katakana
698 && after == unicode_Word_Break_Katakana)
701 if((before == unicode_Word_Break_ALetter
702 || before == unicode_Word_Break_Numeric
703 || before == unicode_Word_Break_Katakana
704 || before == unicode_Word_Break_ExtendNumLet)
705 && after == unicode_Word_Break_ExtendNumLet)
708 if(before == unicode_Word_Break_ExtendNumLet
709 && (after == unicode_Word_Break_ALetter
710 || after == unicode_Word_Break_Numeric
711 || after == unicode_Word_Break_Katakana))
718 /** @defgroup utf32 Functions that operate on UTF-32 strings */
721 /** @brief Return the length of a 0-terminated UTF-32 string
722 * @param s Pointer to 0-terminated string
723 * @return Length of string in code points (excluding terminator)
725 * Unlike the conversion functions no validity checking is done on the string.
727 size_t utf32_len(const uint32_t *s) {
728 const uint32_t *t = s;
732 return (size_t)(t - s);
735 /** @brief Stably sort [s,s+ns) into descending order of combining class
736 * @param s Start of array
737 * @param ns Number of elements, must be at least 1
738 * @param buffer Buffer of at least @p ns elements
740 static void utf32__sort_ccc(uint32_t *s, size_t ns, uint32_t *buffer) {
741 uint32_t *a, *b, *bp;
745 case 1: /* 1-element array is always sorted */
747 case 2: /* 2-element arrays are trivial to sort */
748 if(utf32__combining_class(s[0]) > utf32__combining_class(s[1])) {
755 /* Partition the array */
760 /* Sort the two halves of the array */
761 utf32__sort_ccc(a, na, buffer);
762 utf32__sort_ccc(b, nb, buffer);
763 /* Merge them back into one, via the buffer */
765 while(na > 0 && nb > 0) {
766 /* We want ascending order of combining class (hence <)
767 * and we want stability within combining classes (hence <=)
769 if(utf32__combining_class(*a) <= utf32__combining_class(*b)) {
785 memcpy(s, buffer, ns * sizeof(uint32_t));
790 /** @brief Put combining characters into canonical order
791 * @param s Pointer to UTF-32 string
792 * @param ns Length of @p s
793 * @return 0 on success, non-0 on error
795 * @p s is modified in-place. See Unicode 5.0 s3.11 for details of the
798 * Currently we only support a maximum of 1024 combining characters after each
799 * base character. If this limit is exceeded then a non-0 value is returned.
801 static int utf32__canonical_ordering(uint32_t *s, size_t ns) {
803 uint32_t buffer[1024];
805 /* The ordering amounts to a stable sort of each contiguous group of
806 * characters with non-0 combining class. */
808 /* Skip non-combining characters */
809 if(utf32__combining_class(*s) == 0) {
814 /* We must now have at least one combining character; see how many
816 for(nc = 1; nc < ns && utf32__combining_class(s[nc]) != 0; ++nc)
821 utf32__sort_ccc(s, nc, buffer);
828 /* Magic numbers from UAX #15 s16 */
836 #define NCount (VCount * TCount)
837 #define SCount (LCount * NCount)
839 /** @brief Guts of the decomposition lookup functions */
840 #define utf32__decompose_one_generic(WHICH) do { \
841 const uint32_t *dc = utf32__decomposition_##WHICH(c); \
843 /* Found a canonical decomposition in the table */ \
845 utf32__decompose_one_##WHICH(d, *dc++); \
846 } else if(c >= SBase && c < SBase + SCount) { \
847 /* Mechanically decomposable Hangul syllable (UAX #15 s16) */ \
848 const uint32_t SIndex = c - SBase; \
849 const uint32_t L = LBase + SIndex / NCount; \
850 const uint32_t V = VBase + (SIndex % NCount) / TCount; \
851 const uint32_t T = TBase + SIndex % TCount; \
852 dynstr_ucs4_append(d, L); \
853 dynstr_ucs4_append(d, V); \
855 dynstr_ucs4_append(d, T); \
857 /* Equal to own canonical decomposition */ \
858 dynstr_ucs4_append(d, c); \
861 /** @brief Recursively compute the canonical decomposition of @p c
862 * @param d Dynamic string to store decomposition in
863 * @param c Code point to decompose (must be a valid!)
864 * @return 0 on success, non-0 on error
866 static void utf32__decompose_one_canon(struct dynstr_ucs4 *d, uint32_t c) {
867 utf32__decompose_one_generic(canon);
870 /** @brief Recursively compute the compatibility decomposition of @p c
871 * @param d Dynamic string to store decomposition in
872 * @param c Code point to decompose (must be a valid!)
873 * @return 0 on success, non-0 on error
875 static void utf32__decompose_one_compat(struct dynstr_ucs4 *d, uint32_t c) {
876 utf32__decompose_one_generic(compat);
879 /** @brief Magic utf32__compositions() return value for Hangul Choseong */
880 static const uint32_t utf32__hangul_L[1];
882 /** @brief Return the list of compositions that @p c starts
883 * @param c Starter code point
884 * @return Composition list or NULL
886 * For Hangul leading (Choseong) jamo we return the special value
887 * utf32__hangul_L. These code points are not listed as the targets of
888 * canonical decompositions (make-unidata checks) so there is no confusion with
889 * real decompositions here.
891 static const uint32_t *utf32__compositions(uint32_t c) {
892 const uint32_t *compositions = utf32__unidata(c)->composed;
896 /* Special-casing for Hangul */
897 switch(utf32__grapheme_break(c)) {
900 case unicode_Grapheme_Break_L:
901 return utf32__hangul_L;
905 /** @brief Composition step
906 * @param s Start of string
907 * @param ns Length of string
908 * @return New length of string
910 * This is called from utf32__decompose_generic() to compose the result string
913 static size_t utf32__compose(uint32_t *s, size_t ns) {
914 const uint32_t *compositions;
915 uint32_t *start = s, *t = s, *tt, cc;
918 uint32_t starter = *s++;
919 int block_starters = 0;
921 /* We don't attempt to compose the following things:
922 * - final characters whatever kind they are
923 * - non-starter characters
924 * - starters that don't take part in a canonical decomposition mapping
927 || utf32__combining_class(starter)
928 || !(compositions = utf32__compositions(starter))) {
932 if(compositions != utf32__hangul_L) {
933 /* Where we'll put the eventual starter */
936 /* See if we can find composition of starter+*s */
937 const uint32_t cchar = *s, *cp = compositions;
938 while((cc = *cp++)) {
939 const uint32_t *decomp = utf32__decomposition_canon(cc);
940 /* We know decomp[0] == starter */
941 if(decomp[1] == cchar)
945 /* Found a composition: cc decomposes to starter,*s */
947 compositions = utf32__compositions(starter);
951 /* No composition found. */
952 const int class = utf32__combining_class(*s);
954 /* Transfer the uncomposable combining character to the output */
957 /* All the combining characters of the same class of the
958 * uncomposable character are blocked by it, but there may be
959 * others of higher class later. We eat the uncomposable and
960 * blocked characters and go back round the loop for that higher
962 while(ns > 0 && utf32__combining_class(*s) == class) {
966 /* Block any subsequent starters */
969 /* The uncombinable character is itself a starter, so we don't
970 * transfer it to the output but instead go back round the main
975 /* Keep going while there are still characters and the starter takes
976 * part in some composition */
977 } while(ns > 0 && compositions
978 && (!block_starters || utf32__combining_class(*s)));
979 /* Store any remaining combining characters */
980 while(ns > 0 && utf32__combining_class(*s)) {
984 /* Store the resulting starter */
987 /* Special-casing for Hangul
989 * If there are combining characters between the L and the V then they
990 * will block the V and so no composition happens. Similarly combining
991 * characters between V and T will block the T and so we only get as far
994 if(utf32__grapheme_break(*s) == unicode_Grapheme_Break_V) {
995 const uint32_t V = *s++;
996 const uint32_t LIndex = starter - LBase;
997 const uint32_t VIndex = V - VBase;
1001 && utf32__grapheme_break(*s) == unicode_Grapheme_Break_T) {
1002 /* We have an L V T sequence */
1003 const uint32_t T = *s++;
1009 /* Compose to LVT or LV as appropriate */
1010 starter = (LIndex * VCount + VIndex) * TCount + TIndex + SBase;
1011 } /* else we only have L or LV and no V or T */
1013 /* There could be some combining characters that belong to the V or T.
1014 * These will be treated as non-starter characters at the top of the loop
1015 * and thuss transferred to the output. */
1021 /** @brief Guts of the composition and decomposition functions
1022 * @param WHICH @c canon or @c compat to choose decomposition
1023 * @param COMPOSE @c 0 or @c 1 to compose
1025 #define utf32__decompose_generic(WHICH, COMPOSE) do { \
1026 struct dynstr_ucs4 d; \
1029 dynstr_ucs4_init(&d); \
1032 if((c >= 0xD800 && c <= 0xDFFF) || c > 0x10FFFF) \
1034 utf32__decompose_one_##WHICH(&d, c); \
1037 if(utf32__canonical_ordering(d.vec, d.nvec)) \
1040 d.nvec = utf32__compose(d.vec, d.nvec); \
1041 dynstr_ucs4_terminate(&d); \
1050 /** @brief Canonically decompose @p [s,s+ns)
1051 * @param s Pointer to string
1052 * @param ns Length of string
1053 * @param ndp Where to store length of result
1054 * @return Pointer to result string, or NULL on error
1056 * Computes NFD (Normalization Form D) of the string at @p s. This implies
1057 * performing all canonical decompositions and then normalizing the order of
1058 * combining characters.
1060 * Returns NULL if the string is not valid for either of the following reasons:
1061 * - it codes for a UTF-16 surrogate
1062 * - it codes for a value outside the unicode code space
1065 * - utf32_decompose_compat()
1066 * - utf32_compose_canon()
1068 uint32_t *utf32_decompose_canon(const uint32_t *s, size_t ns, size_t *ndp) {
1069 utf32__decompose_generic(canon, 0);
1072 /** @brief Compatibility decompose @p [s,s+ns)
1073 * @param s Pointer to string
1074 * @param ns Length of string
1075 * @param ndp Where to store length of result
1076 * @return Pointer to result string, or NULL on error
1078 * Computes NFKD (Normalization Form KD) of the string at @p s. This implies
1079 * performing all canonical and compatibility decompositions and then
1080 * normalizing the order of combining characters.
1082 * Returns NULL if the string is not valid for either of the following reasons:
1083 * - it codes for a UTF-16 surrogate
1084 * - it codes for a value outside the unicode code space
1087 * - utf32_decompose_canon()
1088 * - utf32_compose_compat()
1090 uint32_t *utf32_decompose_compat(const uint32_t *s, size_t ns, size_t *ndp) {
1091 utf32__decompose_generic(compat, 0);
1094 /** @brief Canonically compose @p [s,s+ns)
1095 * @param s Pointer to string
1096 * @param ns Length of string
1097 * @param ndp Where to store length of result
1098 * @return Pointer to result string, or NULL on error
1100 * Computes NFC (Normalization Form C) of the string at @p s. This implies
1101 * performing all canonical decompositions, normalizing the order of combining
1102 * characters and then composing all unblocked primary compositables.
1104 * Returns NULL if the string is not valid for either of the following reasons:
1105 * - it codes for a UTF-16 surrogate
1106 * - it codes for a value outside the unicode code space
1109 * - utf32_compose_compat()
1110 * - utf32_decompose_canon()
1112 uint32_t *utf32_compose_canon(const uint32_t *s, size_t ns, size_t *ndp) {
1113 utf32__decompose_generic(canon, 1);
1116 /** @brief Compatibility compose @p [s,s+ns)
1117 * @param s Pointer to string
1118 * @param ns Length of string
1119 * @param ndp Where to store length of result
1120 * @return Pointer to result string, or NULL on error
1122 * Computes NFKC (Normalization Form KC) of the string at @p s. This implies
1123 * performing all canonical and compatibility decompositions, normalizing the
1124 * order of combining characters and then composing all unblocked primary
1127 * Returns NULL if the string is not valid for either of the following reasons:
1128 * - it codes for a UTF-16 surrogate
1129 * - it codes for a value outside the unicode code space
1132 * - utf32_compose_canon()
1133 * - utf32_decompose_compat()
1135 uint32_t *utf32_compose_compat(const uint32_t *s, size_t ns, size_t *ndp) {
1136 utf32__decompose_generic(compat, 1);
1139 /** @brief Single-character case-fold and decompose operation */
1140 #define utf32__casefold_one(WHICH) do { \
1141 const uint32_t *cf = utf32__unidata(c)->casefold; \
1143 /* Found a case-fold mapping in the table */ \
1145 utf32__decompose_one_##WHICH(&d, *cf++); \
1147 utf32__decompose_one_##WHICH(&d, c); \
1150 /** @brief Case-fold @p [s,s+ns)
1151 * @param s Pointer to string
1152 * @param ns Length of string
1153 * @param ndp Where to store length of result
1154 * @return Pointer to result string, or NULL on error
1156 * Case-fold the string at @p s according to full default case-folding rules
1157 * (s3.13) for caseless matching. The result will be in NFD.
1159 * Returns NULL if the string is not valid for either of the following reasons:
1160 * - it codes for a UTF-16 surrogate
1161 * - it codes for a value outside the unicode code space
1163 uint32_t *utf32_casefold_canon(const uint32_t *s, size_t ns, size_t *ndp) {
1164 struct dynstr_ucs4 d;
1169 /* If the canonical decomposition of the string includes any combining
1170 * character that case-folds to a non-combining character then we must
1171 * normalize before we fold. In Unicode 5.0.0 this means 0345 COMBINING
1172 * GREEK YPOGEGRAMMENI in its decomposition and the various characters that
1173 * canonically decompose to it. */
1174 for(n = 0; n < ns; ++n)
1175 if(utf32__unidata(s[n])->flags & unicode_normalize_before_casefold)
1178 /* We need a preliminary decomposition */
1179 if(!(ss = utf32_decompose_canon(s, ns, &ns)))
1183 dynstr_ucs4_init(&d);
1186 if((c >= 0xD800 && c <= 0xDFFF) || c > 0x10FFFF)
1188 utf32__casefold_one(canon);
1191 if(utf32__canonical_ordering(d.vec, d.nvec))
1193 dynstr_ucs4_terminate(&d);
1203 /** @brief Compatibility case-fold @p [s,s+ns)
1204 * @param s Pointer to string
1205 * @param ns Length of string
1206 * @param ndp Where to store length of result
1207 * @return Pointer to result string, or NULL on error
1209 * Case-fold the string at @p s according to full default case-folding rules
1210 * (s3.13) for compatibility caseless matching. The result will be in NFKD.
1212 * Returns NULL if the string is not valid for either of the following reasons:
1213 * - it codes for a UTF-16 surrogate
1214 * - it codes for a value outside the unicode code space
1216 uint32_t *utf32_casefold_compat(const uint32_t *s, size_t ns, size_t *ndp) {
1217 struct dynstr_ucs4 d;
1222 for(n = 0; n < ns; ++n)
1223 if(utf32__unidata(s[n])->flags & unicode_normalize_before_casefold)
1226 /* We need a preliminary _canonical_ decomposition */
1227 if(!(ss = utf32_decompose_canon(s, ns, &ns)))
1231 /* This computes NFKD(toCaseFold(s)) */
1232 #define compat_casefold_middle() do { \
1233 dynstr_ucs4_init(&d); \
1236 if((c >= 0xD800 && c <= 0xDFFF) || c > 0x10FFFF) \
1238 utf32__casefold_one(compat); \
1241 if(utf32__canonical_ordering(d.vec, d.nvec)) \
1244 /* Do the inner (NFKD o toCaseFold) */
1245 compat_casefold_middle();
1246 /* We can do away with the NFD'd copy of the input now */
1250 /* Do the outer (NFKD o toCaseFold) */
1251 compat_casefold_middle();
1253 dynstr_ucs4_terminate(&d);
1263 /** @brief Order a pair of UTF-32 strings
1264 * @param a First 0-terminated string
1265 * @param b Second 0-terminated string
1266 * @return -1, 0 or 1 for a less than, equal to or greater than b
1268 * "Comparable to strcmp() at its best."
1270 int utf32_cmp(const uint32_t *a, const uint32_t *b) {
1271 while(*a && *b && *a == *b) {
1275 return *a < *b ? -1 : (*a > *b ? 1 : 0);
1278 /** @brief Identify a grapheme cluster boundary
1279 * @param s Start of string (must be NFD)
1280 * @param ns Length of string
1281 * @param n Index within string (in [0,ns].)
1282 * @return 1 at a grapheme cluster boundary, 0 otherwise
1284 * This function identifies default grapheme cluster boundaries as described in
1285 * UAX #29 s3. It returns non-0 if @p n points at the code point just after a
1286 * grapheme cluster boundary (including the hypothetical code point just after
1287 * the end of the string).
1289 * This function uses utf32_iterator_set() internally; see that function for
1290 * remarks on performance.
1292 int utf32_is_grapheme_boundary(const uint32_t *s, size_t ns, size_t n) {
1293 struct utf32_iterator_data it[1];
1295 utf32__iterator_init(it, s, ns, n);
1296 return utf32_iterator_grapheme_boundary(it);
1299 /** @brief Identify a word boundary
1300 * @param s Start of string (must be NFD)
1301 * @param ns Length of string
1302 * @param n Index within string (in [0,ns].)
1303 * @return 1 at a word boundary, 0 otherwise
1305 * This function identifies default word boundaries as described in UAX #29 s4.
1306 * It returns non-0 if @p n points at the code point just after a word boundary
1307 * (including the hypothetical code point just after the end of the string).
1309 * This function uses utf32_iterator_set() internally; see that function for
1310 * remarks on performance.
1312 int utf32_is_word_boundary(const uint32_t *s, size_t ns, size_t n) {
1313 struct utf32_iterator_data it[1];
1315 utf32__iterator_init(it, s, ns, n);
1316 return utf32_iterator_word_boundary(it);
1319 /** @brief Split [s,ns) into multiple words
1320 * @param s Pointer to start of string
1321 * @param ns Length of string
1322 * @param nwp Where to store word count, or NULL
1323 * @param wbreak Word_Break property tailor, or NULL
1324 * @return Pointer to array of pointers to words
1326 * The returned array is terminated by a NULL pointer and individual
1327 * strings are 0-terminated.
1329 uint32_t **utf32_word_split(const uint32_t *s, size_t ns, size_t *nwp,
1330 unicode_property_tailor *wbreak) {
1331 struct utf32_iterator_data it[1];
1332 size_t b1 = 0, b2 = 0 ,i;
1334 struct vector32 v32[1];
1338 utf32__iterator_init(it, s, ns, 0);
1339 it->word_break = wbreak;
1340 /* Work our way through the string stopping at each word break. */
1342 if(utf32_iterator_word_boundary(it)) {
1343 /* We've found a new boundary */
1346 /*fprintf(stderr, "[%zu, %zu) is a candidate word\n", b1, b2);*/
1347 /* Inspect the characters between the boundary and form an opinion as to
1348 * whether they are a word or not */
1350 for(i = b1; i < b2; ++i) {
1351 switch(utf32__iterator_word_break(it, it->s[i])) {
1352 case unicode_Word_Break_ALetter:
1353 case unicode_Word_Break_Numeric:
1354 case unicode_Word_Break_Katakana:
1361 /* If it's a word add it to the list of results */
1363 w = xcalloc(b2 - b1 + 1, sizeof(uint32_t));
1364 memcpy(w, it->s + b1, (b2 - b1) * sizeof (uint32_t));
1365 vector32_append(v32, w);
1368 } while(!utf32_iterator_advance(it, 1));
1369 vector32_terminate(v32);
1376 /** @defgroup utf8 Functions that operate on UTF-8 strings */
1379 /** @brief Wrapper to transform a UTF-8 string using the UTF-32 function */
1380 #define utf8__transform(FN) do { \
1381 uint32_t *to32 = 0, *decomp32 = 0; \
1382 size_t nto32, ndecomp32; \
1383 char *decomp8 = 0; \
1385 if(!(to32 = utf8_to_utf32(s, ns, &nto32))) goto error; \
1386 if(!(decomp32 = FN(to32, nto32, &ndecomp32))) goto error; \
1387 decomp8 = utf32_to_utf8(decomp32, ndecomp32, ndp); \
1394 /** @brief Canonically decompose @p [s,s+ns)
1395 * @param s Pointer to string
1396 * @param ns Length of string
1397 * @param ndp Where to store length of result
1398 * @return Pointer to result string, or NULL on error
1400 * Computes NFD (Normalization Form D) of the string at @p s. This implies
1401 * performing all canonical decompositions and then normalizing the order of
1402 * combining characters.
1404 * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why
1408 * - utf32_decompose_canon().
1409 * - utf8_decompose_compat()
1410 * - utf8_compose_canon()
1412 char *utf8_decompose_canon(const char *s, size_t ns, size_t *ndp) {
1413 utf8__transform(utf32_decompose_canon);
1416 /** @brief Compatibility decompose @p [s,s+ns)
1417 * @param s Pointer to string
1418 * @param ns Length of string
1419 * @param ndp Where to store length of result
1420 * @return Pointer to result string, or NULL on error
1422 * Computes NFKD (Normalization Form KD) of the string at @p s. This implies
1423 * performing all canonical and compatibility decompositions and then
1424 * normalizing the order of combining characters.
1426 * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why
1430 * - utf32_decompose_compat().
1431 * - utf8_decompose_canon()
1432 * - utf8_compose_compat()
1434 char *utf8_decompose_compat(const char *s, size_t ns, size_t *ndp) {
1435 utf8__transform(utf32_decompose_compat);
1438 /** @brief Canonically compose @p [s,s+ns)
1439 * @param s Pointer to string
1440 * @param ns Length of string
1441 * @param ndp Where to store length of result
1442 * @return Pointer to result string, or NULL on error
1444 * Computes NFC (Normalization Form C) of the string at @p s. This implies
1445 * performing all canonical decompositions, normalizing the order of combining
1446 * characters and then composing all unblocked primary compositables.
1448 * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why
1452 * - utf32_compose_canon()
1453 * - utf8_compose_compat()
1454 * - utf8_decompose_canon()
1456 char *utf8_compose_canon(const char *s, size_t ns, size_t *ndp) {
1457 utf8__transform(utf32_compose_canon);
1460 /** @brief Compatibility compose @p [s,s+ns)
1461 * @param s Pointer to string
1462 * @param ns Length of string
1463 * @param ndp Where to store length of result
1464 * @return Pointer to result string, or NULL on error
1466 * Computes NFKC (Normalization Form KC) of the string at @p s. This implies
1467 * performing all canonical and compatibility decompositions, normalizing the
1468 * order of combining characters and then composing all unblocked primary
1471 * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why
1475 * - utf32_compose_compat()
1476 * - utf8_compose_canon()
1477 * - utf8_decompose_compat()
1479 char *utf8_compose_compat(const char *s, size_t ns, size_t *ndp) {
1480 utf8__transform(utf32_compose_compat);
1483 /** @brief Case-fold @p [s,s+ns)
1484 * @param s Pointer to string
1485 * @param ns Length of string
1486 * @param ndp Where to store length of result
1487 * @return Pointer to result string, or NULL on error
1489 * Case-fold the string at @p s according to full default case-folding rules
1490 * (s3.13). The result will be in NFD.
1492 * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why
1495 char *utf8_casefold_canon(const char *s, size_t ns, size_t *ndp) {
1496 utf8__transform(utf32_casefold_canon);
1499 /** @brief Compatibility case-fold @p [s,s+ns)
1500 * @param s Pointer to string
1501 * @param ns Length of string
1502 * @param ndp Where to store length of result
1503 * @return Pointer to result string, or NULL on error
1505 * Case-fold the string at @p s according to full default case-folding rules
1506 * (s3.13). The result will be in NFKD.
1508 * Returns NULL if the string is not valid; see utf8_to_utf32() for reasons why
1511 char *utf8_casefold_compat(const char *s, size_t ns, size_t *ndp) {
1512 utf8__transform(utf32_casefold_compat);
1515 /** @brief Split [s,ns) into multiple words
1516 * @param s Pointer to start of string
1517 * @param ns Length of string
1518 * @param nwp Where to store word count, or NULL
1519 * @param wbreak Word_Break property tailor, or NULL
1520 * @return Pointer to array of pointers to words
1522 * The returned array is terminated by a NULL pointer and individual
1523 * strings are 0-terminated.
1525 char **utf8_word_split(const char *s, size_t ns, size_t *nwp,
1526 unicode_property_tailor *wbreak) {
1527 uint32_t *to32 = 0, **v32 = 0;
1528 size_t nto32, nv, n;
1529 char **v8 = 0, **ret = 0;
1531 if(!(to32 = utf8_to_utf32(s, ns, &nto32))) goto error;
1532 if(!(v32 = utf32_word_split(to32, nto32, &nv, wbreak))) goto error;
1533 v8 = xcalloc(sizeof (char *), nv + 1);
1534 for(n = 0; n < nv; ++n)
1535 if(!(v8[n] = utf32_to_utf8(v32[n], utf32_len(v32[n]), 0)))
1539 v8 = 0; /* don't free */
1542 for(n = 0; n < nv; ++n)
1547 for(n = 0; n < nv; ++n)
1563 indent-tabs-mode:nil