chiark / gitweb /
Split base64 code into a separate .c so that the server doesn't pull
[disorder] / lib / unicode.h
index 08812b35b5cbd9fa96c2f17baa025521693095f1..982921b33bdef432293c80cad21a2b3136f0540c 100644 (file)
 #ifndef UNICODE_H
 #define UNICODE_H
 
+/** @brief Smart pointer into a string
+ *
+ * Iterators can be efficiently moved either forwards or back to the start of
+ * the string.  They cannot (currently) efficiently be moved backwards.  Their
+ * advantage is that they remember internal state to speed up boundary
+ * detection.
+ *
+ * Iterators can point to any code point of the string, or to a hypothetical
+ * post-final code point of value 0, but not outside the string.
+ */
+typedef struct utf32_iterator_data *utf32_iterator;
+
+/** @brief Property tailor function
+ * @param c Code point
+ * @return Tailored property or -1 to use standard value
+ *
+ * See also utf32_iterator_tailor_word_break().
+ */
+typedef int unicode_property_tailor(uint32_t c);
+
 char *utf32_to_utf8(const uint32_t *s, size_t ns, size_t *nd);
 uint32_t *utf8_to_utf32(const char *s, size_t ns, size_t *nd);
+int utf8_valid(const char *s, size_t ns);
+
+int utf32_combining_class(uint32_t c);
 
 size_t utf32_len(const uint32_t *s);
 int utf32_cmp(const uint32_t *a, const uint32_t *b);
@@ -36,15 +59,58 @@ char *utf8_decompose_canon(const char *s, size_t ns, size_t *ndp);
 uint32_t *utf32_decompose_compat(const uint32_t *s, size_t ns, size_t *ndp);
 char *utf8_decompose_compat(const char *s, size_t ns, size_t *ndp);
 
+uint32_t *utf32_compose_canon(const uint32_t *s, size_t ns, size_t *ndp);
+char *utf8_compose_canon(const char *s, size_t ns, size_t *ndp);
+
+uint32_t *utf32_compose_compat(const uint32_t *s, size_t ns, size_t *ndp);
+char *utf8_compose_compat(const char *s, size_t ns, size_t *ndp);
+
 uint32_t *utf32_casefold_canon(const uint32_t *s, size_t ns, size_t *ndp);
 char *utf8_casefold_canon(const char *s, size_t ns, size_t *ndp);
 
 uint32_t *utf32_casefold_compat(const uint32_t *s, size_t ns, size_t *ndp);
 char *utf8_casefold_compat(const char *s, size_t ns, size_t *ndp);
 
-int utf32_is_gcb(const uint32_t *s, size_t ns, size_t n);
+int utf32_is_grapheme_boundary(const uint32_t *s, size_t ns, size_t n);
 int utf32_is_word_boundary(const uint32_t *s, size_t ns, size_t n);
 
+utf32_iterator utf32_iterator_new(const uint32_t *s, size_t ns);
+void utf32_iterator_destroy(utf32_iterator it);
+
+size_t utf32_iterator_where(utf32_iterator it);
+int utf32_iterator_set(utf32_iterator it, size_t n);
+int utf32_iterator_advance(utf32_iterator it, size_t n);
+uint32_t utf32_iterator_code(utf32_iterator it);
+int utf32_iterator_grapheme_boundary(utf32_iterator it);
+int utf32_iterator_word_boundary(utf32_iterator it);
+void utf32_iterator_tailor_word_break(utf32_iterator it,
+                                      unicode_property_tailor *pt);
+
+uint32_t **utf32_word_split(const uint32_t *s, size_t ns, size_t *nwp,
+                            unicode_property_tailor *wbreak);
+char **utf8_word_split(const char *s, size_t ns, size_t *nwp,
+                            unicode_property_tailor *wbreak);
+
+/** @brief Convert 0-terminated UTF-32 to UTF-8
+ * @param s 0-terminated UTF-32 string
+ * @return 0-terminated UTF-8 string or 0 on error
+ *
+ * See utf32_to_utf8() for possible causes of errors.
+ */
+static inline char *utf32nt_to_utf8(const uint32_t *s) {
+  return utf32_to_utf8(s, utf32_len(s), 0);
+}
+
+/** @brief Convert 0-terminated UTF-8 to UTF-32
+ * @param s 0-terminated UTF-8 string
+ * @return 0-terminated UTF-32 string or 0 on error
+ *
+ * See utf8_to_utf32() for possible causes of errors.
+ */
+static inline uint32_t *utf8nt_to_utf32(const char *s) {
+  return utf8_to_utf32(s, strlen(s), 0);
+}
+
 #endif /* UNICODE_H */
 
 /*