2 * This file is part of DisOrde
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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file lib/unicode.h
19 * @brief Unicode support functions
25 /** @brief Smart pointer into a string
27 * Iterators can be efficiently moved either forwards or back to the start of
28 * the string. They cannot (currently) efficiently be moved backwards. Their
29 * advantage is that they remember internal state to speed up boundary
32 * Iterators can point to any code point of the string, or to a hypothetical
33 * post-final code point of value 0, but not outside the string.
35 typedef struct utf32_iterator_data *utf32_iterator;
37 /** @brief Property tailor function
39 * @return Tailored property or -1 to use standard value
41 * See also utf32_iterator_tailor_word_break().
43 typedef int unicode_property_tailor(uint32_t c);
45 char *utf32_to_utf8(const uint32_t *s, size_t ns, size_t *nd);
46 uint32_t *utf8_to_utf32(const char *s, size_t ns, size_t *nd);
47 int utf8_valid(const char *s, size_t ns);
49 int utf32_combining_class(uint32_t c);
51 size_t utf32_len(const uint32_t *s);
52 int utf32_cmp(const uint32_t *a, const uint32_t *b);
54 uint32_t *utf32_decompose_canon(const uint32_t *s, size_t ns, size_t *ndp);
55 char *utf8_decompose_canon(const char *s, size_t ns, size_t *ndp);
57 uint32_t *utf32_decompose_compat(const uint32_t *s, size_t ns, size_t *ndp);
58 char *utf8_decompose_compat(const char *s, size_t ns, size_t *ndp);
60 uint32_t *utf32_compose_canon(const uint32_t *s, size_t ns, size_t *ndp);
61 char *utf8_compose_canon(const char *s, size_t ns, size_t *ndp);
63 uint32_t *utf32_compose_compat(const uint32_t *s, size_t ns, size_t *ndp);
64 char *utf8_compose_compat(const char *s, size_t ns, size_t *ndp);
66 uint32_t *utf32_casefold_canon(const uint32_t *s, size_t ns, size_t *ndp);
67 char *utf8_casefold_canon(const char *s, size_t ns, size_t *ndp);
69 uint32_t *utf32_casefold_compat(const uint32_t *s, size_t ns, size_t *ndp);
70 char *utf8_casefold_compat(const char *s, size_t ns, size_t *ndp);
72 int utf32_is_grapheme_boundary(const uint32_t *s, size_t ns, size_t n);
73 int utf32_is_word_boundary(const uint32_t *s, size_t ns, size_t n);
75 utf32_iterator utf32_iterator_new(const uint32_t *s, size_t ns);
76 void utf32_iterator_destroy(utf32_iterator it);
78 size_t utf32_iterator_where(utf32_iterator it);
79 int utf32_iterator_set(utf32_iterator it, size_t n);
80 int utf32_iterator_advance(utf32_iterator it, size_t n);
81 uint32_t utf32_iterator_code(utf32_iterator it);
82 int utf32_iterator_grapheme_boundary(utf32_iterator it);
83 int utf32_iterator_word_boundary(utf32_iterator it);
84 void utf32_iterator_tailor_word_break(utf32_iterator it,
85 unicode_property_tailor *pt);
87 uint32_t **utf32_word_split(const uint32_t *s, size_t ns, size_t *nwp,
88 unicode_property_tailor *wbreak);
89 char **utf8_word_split(const char *s, size_t ns, size_t *nwp,
90 unicode_property_tailor *wbreak);
92 /** @brief Convert 0-terminated UTF-32 to UTF-8
93 * @param s 0-terminated UTF-32 string
94 * @return 0-terminated UTF-8 string or 0 on error
96 * See utf32_to_utf8() for possible causes of errors.
98 static inline char *utf32nt_to_utf8(const uint32_t *s) {
99 return utf32_to_utf8(s, utf32_len(s), 0);
102 /** @brief Convert 0-terminated UTF-8 to UTF-32
103 * @param s 0-terminated UTF-8 string
104 * @return 0-terminated UTF-32 string or 0 on error
106 * See utf8_to_utf32() for possible causes of errors.
108 static inline uint32_t *utf8nt_to_utf32(const char *s) {
109 return utf8_to_utf32(s, strlen(s), 0);
112 #endif /* UNICODE_H */