+/** @brief Compatibilit case-fold @p [s,s+ns)
+ * @param s Pointer to string
+ * @param ns Length of string
+ * @param ndp Where to store length of result
+ * @return Pointer to result string, or NULL
+ *
+ * Case-fold the string at @p s according to full default case-folding rules
+ * (s3.13) for compatibility caseless matching. The result will be in NFKD.
+ *
+ * Returns NULL if the string is not valid for either of the following reasons:
+ * - it codes for a UTF-16 surrogate
+ * - it codes for a value outside the unicode code space
+ */
+uint32_t *utf32_casefold_compat(const uint32_t *s, size_t ns, size_t *ndp) {
+ struct dynstr_ucs4 d;
+ uint32_t c;
+ size_t n;
+ uint32_t *ss = 0;
+
+ for(n = 0; n < ns; ++n) {
+ c = s[n];
+ if(c < UNICODE_NCHARS
+ && (unidata[c / UNICODE_MODULUS][c % UNICODE_MODULUS].flags
+ & unicode_normalize_before_casefold))
+ break;
+ }
+ if(n < ns) {
+ /* We need a preliminary _canonical_ decomposition */
+ if(!(ss = utf32_decompose_canon(s, ns, &ns)))
+ return 0;
+ s = ss;
+ }
+ /* This computes NFKD(toCaseFold(s)) */
+#define compat_casefold_middle() do { \
+ dynstr_ucs4_init(&d); \
+ while(ns) { \
+ c = *s++; \
+ if((c >= 0xD800 && c <= 0xDFFF) || c > 0x10FFFF) \
+ goto error; \
+ utf32__casefold_one(compat); \
+ --ns; \
+ } \
+ if(utf32__canonical_ordering(d.vec, d.nvec)) \
+ goto error; \
+} while(0)
+ /* Do the inner (NFKD o toCaseFold) */
+ compat_casefold_middle();
+ /* We can do away with the NFD'd copy of the input now */
+ xfree(ss);
+ s = ss = d.vec;
+ ns = d.nvec;
+ /* Do the outer (NFKD o toCaseFold) */
+ compat_casefold_middle();
+ /* That's all */
+ dynstr_ucs4_terminate(&d);
+ if(ndp)
+ *ndp = d.nvec;
+ return d.vec;
+error:
+ xfree(d.vec);
+ xfree(ss);
+ return 0;
+}
+