+/** @brief Test whether [s,s+ns) is valid UTF-8
+ * @param s Start of string
+ * @param ns Length of string
+ * @return non-0 if @p s is valid UTF-8, 0 if it is not valid
+ *
+ * This function is intended to be much faster than calling utf8_to_utf32() and
+ * throwing away the result.
+ */
+int utf8_valid(const char *s, size_t ns) {
+ const uint8_t *ss = (const uint8_t *)s;
+ while(ns > 0) {
+ const struct unicode_utf8_row *const r = &unicode_utf8_valid[*ss];
+ if(r->count <= ns) {
+ switch(r->count) {
+ case 1:
+ break;
+ case 2:
+ if(ss[1] < r->min2 || ss[1] > r->max2)
+ return 0;
+ break;
+ case 3:
+ if(ss[1] < r->min2 || ss[1] > r->max2)
+ return 0;
+ if(ss[2] < 0x80 || ss[2] > 0xBF)
+ return 0;
+ break;
+ case 4:
+ if(ss[1] < r->min2 || ss[1] > r->max2)
+ return 0;
+ if(ss[2] < 0x80 || ss[2] > 0xBF)
+ return 0;
+ if(ss[3] < 0x80 || ss[3] > 0xBF)
+ return 0;
+ break;
+ default:
+ return 0;
+ }
+ } else
+ return 0;
+ ss += r->count;
+ ns -= r->count;
+ }
+ return 1;
+}
+
+/*@}*/
+/** @defgroup utf32iterator UTF-32 string iterators */
+/*@{*/
+
+struct utf32_iterator_data {
+ /** @brief Start of string */
+ const uint32_t *s;
+
+ /** @brief Length of string */
+ size_t ns;
+
+ /** @brief Current position */
+ size_t n;
+
+ /** @brief Last two non-ignorable characters or (uint32_t)-1
+ *
+ * last[1] is the non-Extend/Format character just before position @p n;
+ * last[0] is the one just before that.
+ *
+ * Exception 1: if there is no such non-Extend/Format character then an
+ * Extend/Format character is accepted instead.
+ *
+ * Exception 2: if there is no such character even taking that into account
+ * the value is (uint32_t)-1.
+ */
+ uint32_t last[2];
+
+ /** @brief Tailoring for Word_Break */
+ unicode_property_tailor *word_break;
+};
+
+/** @brief Initialize an internal private iterator
+ * @param it Iterator
+ * @param s Start of string
+ * @param ns Length of string
+ * @param n Absolute position
+ */
+static void utf32__iterator_init(utf32_iterator it,
+ const uint32_t *s, size_t ns, size_t n) {
+ it->s = s;
+ it->ns = ns;
+ it->n = 0;
+ it->last[0] = it->last[1] = -1;
+ it->word_break = 0;
+ utf32_iterator_set(it, n);
+}
+
+/** @brief Create a new iterator pointing at the start of a string
+ * @param s Start of string
+ * @param ns Length of string
+ * @return New iterator
+ */
+utf32_iterator utf32_iterator_new(const uint32_t *s, size_t ns) {
+ utf32_iterator it = xmalloc(sizeof *it);
+ utf32__iterator_init(it, s, ns, 0);
+ return it;
+}
+
+/** @brief Tailor this iterator's interpretation of the Word_Break property.
+ * @param it Iterator
+ * @param pt Property tailor function or NULL
+ *
+ * After calling this the iterator will call @p pt to determine the Word_Break
+ * property of each code point. If it returns -1 the default value will be
+ * used otherwise the returned value will be used.
+ *
+ * @p pt can be NULL to revert to the default value of the property.
+ *
+ * It is safe to call this function at any time; the iterator's internal state
+ * will be reset to suit the new tailoring.
+ */
+void utf32_iterator_tailor_word_break(utf32_iterator it,
+ unicode_property_tailor *pt) {
+ it->word_break = pt;
+ utf32_iterator_set(it, it->n);
+}
+
+static inline enum unicode_Word_Break utf32__iterator_word_break(utf32_iterator it,
+ uint32_t c) {
+ if(!it->word_break)
+ return utf32__word_break(c);
+ else {
+ const int t = it->word_break(c);
+
+ if(t < 0)
+ return utf32__word_break(c);
+ else
+ return t;
+ }
+}
+
+/** @brief Destroy an iterator
+ * @param it Iterator
+ */
+void utf32_iterator_destroy(utf32_iterator it) {
+ xfree(it);
+}
+
+/** @brief Find the current position of an interator
+ * @param it Iterator
+ */
+size_t utf32_iterator_where(utf32_iterator it) {
+ return it->n;
+}
+
+/** @brief Set an iterator's absolute position
+ * @param it Iterator
+ * @param n Absolute position
+ * @return 0 on success, non-0 on error
+ *
+ * It is an error to position the iterator outside the string (but acceptable
+ * to point it at the hypothetical post-final character). If an invalid value
+ * of @p n is specified then the iterator is not changed.
+ *
+ * This function works by backing up and then advancing to reconstruct the
+ * iterator's internal state for position @p n. The worst case will be O(n)
+ * time complexity (with a worse constant factor that utf32_iterator_advance())
+ * but the typical case is essentially constant-time.
+ */
+int utf32_iterator_set(utf32_iterator it, size_t n) {
+ /* We can't just jump to position @p n; the @p last[] values will be wrong.
+ * What we need is to jump a bit behind @p n and then advance forward,
+ * updating @p last[] along the way. How far back? We need to cross two
+ * non-ignorable code points as we advance forwards, so we'd better pass two
+ * such characters on the way back (if such are available).
+ */
+ size_t m;
+
+ if(n > it->ns) /* range check */
+ return -1;
+ /* Walk backwards skipping ignorable code points */
+ m = n;
+ while(m > 0
+ && (utf32__boundary_ignorable(utf32__iterator_word_break(it,
+ it->s[m-1]))))
+ --m;
+ /* Either m=0 or s[m-1] is not ignorable */
+ if(m > 0) {
+ --m;
+ /* s[m] is our first non-ignorable code; look for a second in the same
+ way **/
+ while(m > 0
+ && (utf32__boundary_ignorable(utf32__iterator_word_break(it,
+ it->s[m-1]))))
+ --m;
+ /* Either m=0 or s[m-1] is not ignorable */
+ if(m > 0)
+ --m;
+ }
+ it->last[0] = it->last[1] = -1;
+ it->n = m;
+ return utf32_iterator_advance(it, n - m);
+}
+
+/** @brief Advance an iterator
+ * @param it Iterator
+ * @param count Number of code points to advance by
+ * @return 0 on success, non-0 on error
+ *
+ * It is an error to advance an iterator beyond the hypothetical post-final
+ * character of the string. If an invalid value of @p n is specified then the
+ * iterator is not changed.
+ *
+ * This function has O(n) time complexity: it works by advancing naively
+ * forwards through the string.
+ */
+int utf32_iterator_advance(utf32_iterator it, size_t count) {
+ if(count <= it->ns - it->n) {
+ while(count > 0) {
+ const uint32_t c = it->s[it->n];
+ const enum unicode_Word_Break wb = utf32__iterator_word_break(it, c);
+ if(it->last[1] == (uint32_t)-1
+ || !utf32__boundary_ignorable(wb)) {
+ it->last[0] = it->last[1];
+ it->last[1] = c;
+ }
+ ++it->n;
+ --count;
+ }
+ return 0;
+ } else
+ return -1;
+}
+
+/** @brief Find the current code point
+ * @param it Iterator
+ * @return Current code point or 0
+ *
+ * If the iterator points at the hypothetical post-final character of the
+ * string then 0 is returned. NB that this doesn't mean that there aren't any
+ * 0 code points inside the string!
+ */
+uint32_t utf32_iterator_code(utf32_iterator it) {
+ if(it->n < it->ns)
+ return it->s[it->n];
+ else
+ return 0;
+}
+
+/** @brief Test for a grapheme boundary
+ * @param it Iterator
+ * @return Non-0 if pointing just after a grapheme boundary, otherwise 0
+ *
+ * This function identifies default grapheme cluster boundaries as described in
+ * UAX #29 s3. It returns non-0 if @p it points at the code point just after a
+ * grapheme cluster boundary (including the hypothetical code point just after
+ * the end of the string).
+ */
+int utf32_iterator_grapheme_boundary(utf32_iterator it) {
+ uint32_t before, after;
+ enum unicode_Grapheme_Break gbbefore, gbafter;
+ /* GB1 and GB2 */
+ if(it->n == 0 || it->n == it->ns)
+ return 1;
+ /* Now we know that s[n-1] and s[n] are safe to inspect */
+ /* GB3 */
+ before = it->s[it->n-1];
+ after = it->s[it->n];
+ if(before == 0x000D && after == 0x000A)
+ return 0;
+ gbbefore = utf32__grapheme_break(before);
+ gbafter = utf32__grapheme_break(after);
+ /* GB4 */
+ if(gbbefore == unicode_Grapheme_Break_Control
+ || before == 0x000D
+ || before == 0x000A)
+ return 1;
+ /* GB5 */
+ if(gbafter == unicode_Grapheme_Break_Control
+ || after == 0x000D
+ || after == 0x000A)
+ return 1;
+ /* GB6 */
+ if(gbbefore == unicode_Grapheme_Break_L
+ && (gbafter == unicode_Grapheme_Break_L
+ || gbafter == unicode_Grapheme_Break_V
+ || gbafter == unicode_Grapheme_Break_LV
+ || gbafter == unicode_Grapheme_Break_LVT))
+ return 0;
+ /* GB7 */
+ if((gbbefore == unicode_Grapheme_Break_LV
+ || gbbefore == unicode_Grapheme_Break_V)
+ && (gbafter == unicode_Grapheme_Break_V
+ || gbafter == unicode_Grapheme_Break_T))
+ return 0;
+ /* GB8 */
+ if((gbbefore == unicode_Grapheme_Break_LVT
+ || gbbefore == unicode_Grapheme_Break_T)
+ && gbafter == unicode_Grapheme_Break_T)
+ return 0;
+ /* GB9 */
+ if(gbafter == unicode_Grapheme_Break_Extend)
+ return 0;
+ /* GB9a */
+ if(gbafter == unicode_Grapheme_Break_SpacingMark)
+ return 0;
+ /* GB9b */
+ if(gbbefore == unicode_Grapheme_Break_Prepend)
+ return 0;
+ /* GB10 */
+ return 1;
+
+}
+
+/** @brief Test for a word boundary
+ * @param it Iterator
+ * @return Non-0 if pointing just after a word boundary, otherwise 0
+ *
+ * This function identifies default word boundaries as described in UAX #29 s4.
+ * It returns non-0 if @p it points at the code point just after a word
+ * boundary (including the hypothetical code point just after the end of the
+ * string) and 0 otherwise.
+ */
+int utf32_iterator_word_boundary(utf32_iterator it) {
+ uint32_t before, after;
+ enum unicode_Word_Break wbtwobefore, wbbefore, wbafter, wbtwoafter;
+ size_t nn;
+
+ /* WB1 and WB2 */
+ if(it->n == 0 || it->n == it->ns)
+ return 1;
+ before = it->s[it->n-1];
+ after = it->s[it->n];
+ /* WB3 */
+ if(before == 0x000D && after == 0x000A)
+ return 0;
+ /* WB3a */
+ if(utf32__iterator_word_break(it, before) == unicode_Word_Break_Newline
+ || before == 0x000D
+ || before == 0x000A)
+ return 1;
+ /* WB3b */
+ if(utf32__iterator_word_break(it, after) == unicode_Word_Break_Newline
+ || after == 0x000D
+ || after == 0x000A)
+ return 1;
+ /* WB4 */
+ /* (!Sep) x (Extend|Format) as in UAX #29 s6.2 */
+ if(utf32__sentence_break(before) != unicode_Sentence_Break_Sep
+ && utf32__boundary_ignorable(utf32__iterator_word_break(it, after)))
+ return 0;
+ /* Gather the property values we'll need for the rest of the test taking the
+ * s6.2 changes into account */
+ /* First we look at the code points after the proposed boundary */
+ nn = it->n; /* <it->ns */
+ wbafter = utf32__iterator_word_break(it, it->s[nn++]);
+ if(!utf32__boundary_ignorable(wbafter)) {
+ /* X (Extend|Format)* -> X */
+ while(nn < it->ns
+ && utf32__boundary_ignorable(utf32__iterator_word_break(it,
+ it->s[nn])))
+ ++nn;
+ }
+ /* It's possible now that nn=ns */
+ if(nn < it->ns)
+ wbtwoafter = utf32__iterator_word_break(it, it->s[nn]);
+ else
+ wbtwoafter = unicode_Word_Break_Other;
+
+ /* We've already recorded the non-ignorable code points before the proposed
+ * boundary */
+ wbbefore = utf32__iterator_word_break(it, it->last[1]);
+ wbtwobefore = utf32__iterator_word_break(it, it->last[0]);
+
+ /* WB5 */
+ if(wbbefore == unicode_Word_Break_ALetter
+ && wbafter == unicode_Word_Break_ALetter)
+ return 0;
+ /* WB6 */
+ if(wbbefore == unicode_Word_Break_ALetter
+ && (wbafter == unicode_Word_Break_MidLetter
+ || wbafter == unicode_Word_Break_MidNumLet)
+ && wbtwoafter == unicode_Word_Break_ALetter)
+ return 0;
+ /* WB7 */
+ if(wbtwobefore == unicode_Word_Break_ALetter
+ && (wbbefore == unicode_Word_Break_MidLetter
+ || wbbefore == unicode_Word_Break_MidNumLet)
+ && wbafter == unicode_Word_Break_ALetter)
+ return 0;
+ /* WB8 */
+ if(wbbefore == unicode_Word_Break_Numeric
+ && wbafter == unicode_Word_Break_Numeric)
+ return 0;
+ /* WB9 */
+ if(wbbefore == unicode_Word_Break_ALetter
+ && wbafter == unicode_Word_Break_Numeric)
+ return 0;
+ /* WB10 */
+ if(wbbefore == unicode_Word_Break_Numeric
+ && wbafter == unicode_Word_Break_ALetter)
+ return 0;
+ /* WB11 */
+ if(wbtwobefore == unicode_Word_Break_Numeric
+ && (wbbefore == unicode_Word_Break_MidNum
+ || wbbefore == unicode_Word_Break_MidNumLet)
+ && wbafter == unicode_Word_Break_Numeric)
+ return 0;
+ /* WB12 */
+ if(wbbefore == unicode_Word_Break_Numeric
+ && (wbafter == unicode_Word_Break_MidNum
+ || wbafter == unicode_Word_Break_MidNumLet)
+ && wbtwoafter == unicode_Word_Break_Numeric)
+ return 0;
+ /* WB13 */
+ if(wbbefore == unicode_Word_Break_Katakana
+ && wbafter == unicode_Word_Break_Katakana)
+ return 0;
+ /* WB13a */
+ if((wbbefore == unicode_Word_Break_ALetter
+ || wbbefore == unicode_Word_Break_Numeric
+ || wbbefore == unicode_Word_Break_Katakana
+ || wbbefore == unicode_Word_Break_ExtendNumLet)
+ && wbafter == unicode_Word_Break_ExtendNumLet)
+ return 0;
+ /* WB13b */
+ if(wbbefore == unicode_Word_Break_ExtendNumLet
+ && (wbafter == unicode_Word_Break_ALetter
+ || wbafter == unicode_Word_Break_Numeric
+ || wbafter == unicode_Word_Break_Katakana))
+ return 0;
+ /* WB14 */
+ return 1;
+}
+