chiark / gitweb /
disorder.h: more consistent approach to function attributes
[disorder] / lib / unicode.h
CommitLineData
e5a5a138
RK
1/*
2 * This file is part of DisOrde
bb5c7798 3 * Copyright (C) 2007, 2008, 2013 Richard Kettlewell
e5a5a138 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
e5a5a138 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
e5a5a138 8 * (at your option) any later version.
e7eb3a27
RK
9 *
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.
14 *
e5a5a138 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e5a5a138
RK
17 */
18/** @file lib/unicode.h
19 * @brief Unicode support functions
20 */
21
22#ifndef UNICODE_H
23#define UNICODE_H
24
092f426f
RK
25/** @brief Smart pointer into a string
26 *
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
30 * detection.
31 *
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.
34 */
35typedef struct utf32_iterator_data *utf32_iterator;
36
c85b7022
RK
37/** @brief Property tailor function
38 * @param c Code point
39 * @return Tailored property or -1 to use standard value
40 *
41 * See also utf32_iterator_tailor_word_break().
42 */
43typedef int unicode_property_tailor(uint32_t c);
44
e5a5a138
RK
45char *utf32_to_utf8(const uint32_t *s, size_t ns, size_t *nd);
46uint32_t *utf8_to_utf32(const char *s, size_t ns, size_t *nd);
bb5c7798
RK
47char *utf16_to_utf8(const uint16_t *s, size_t ns, size_t *nd);
48uint16_t *utf8_to_utf16(const char *s, size_t ns, size_t *nd);
18cda350 49int utf8_valid(const char *s, size_t ns);
e5a5a138 50
3c82b504
RK
51int utf32_combining_class(uint32_t c);
52
e5a5a138 53size_t utf32_len(const uint32_t *s);
bb5c7798 54size_t utf16_len(const uint16_t *s);
e5a5a138
RK
55int utf32_cmp(const uint32_t *a, const uint32_t *b);
56
57uint32_t *utf32_decompose_canon(const uint32_t *s, size_t ns, size_t *ndp);
58char *utf8_decompose_canon(const char *s, size_t ns, size_t *ndp);
59
60uint32_t *utf32_decompose_compat(const uint32_t *s, size_t ns, size_t *ndp);
61char *utf8_decompose_compat(const char *s, size_t ns, size_t *ndp);
62
16506c9d
RK
63uint32_t *utf32_compose_canon(const uint32_t *s, size_t ns, size_t *ndp);
64char *utf8_compose_canon(const char *s, size_t ns, size_t *ndp);
65
66uint32_t *utf32_compose_compat(const uint32_t *s, size_t ns, size_t *ndp);
67char *utf8_compose_compat(const char *s, size_t ns, size_t *ndp);
68
e5a5a138
RK
69uint32_t *utf32_casefold_canon(const uint32_t *s, size_t ns, size_t *ndp);
70char *utf8_casefold_canon(const char *s, size_t ns, size_t *ndp);
71
72uint32_t *utf32_casefold_compat(const uint32_t *s, size_t ns, size_t *ndp);
73char *utf8_casefold_compat(const char *s, size_t ns, size_t *ndp);
74
1625e11a 75int utf32_is_grapheme_boundary(const uint32_t *s, size_t ns, size_t n);
0b7052da 76int utf32_is_word_boundary(const uint32_t *s, size_t ns, size_t n);
e5a5a138 77
092f426f
RK
78utf32_iterator utf32_iterator_new(const uint32_t *s, size_t ns);
79void utf32_iterator_destroy(utf32_iterator it);
80
81size_t utf32_iterator_where(utf32_iterator it);
82int utf32_iterator_set(utf32_iterator it, size_t n);
83int utf32_iterator_advance(utf32_iterator it, size_t n);
84uint32_t utf32_iterator_code(utf32_iterator it);
85int utf32_iterator_grapheme_boundary(utf32_iterator it);
86int utf32_iterator_word_boundary(utf32_iterator it);
c85b7022
RK
87void utf32_iterator_tailor_word_break(utf32_iterator it,
88 unicode_property_tailor *pt);
092f426f 89
c85b7022
RK
90uint32_t **utf32_word_split(const uint32_t *s, size_t ns, size_t *nwp,
91 unicode_property_tailor *wbreak);
92char **utf8_word_split(const char *s, size_t ns, size_t *nwp,
93 unicode_property_tailor *wbreak);
8818b7fc 94
caecd4f4
RK
95/** @brief Convert 0-terminated UTF-32 to UTF-8
96 * @param s 0-terminated UTF-32 string
97 * @return 0-terminated UTF-8 string or 0 on error
98 *
99 * See utf32_to_utf8() for possible causes of errors.
100 */
101static inline char *utf32nt_to_utf8(const uint32_t *s) {
102 return utf32_to_utf8(s, utf32_len(s), 0);
103}
104
bb5c7798
RK
105/** @brief Convert 0-terminated UTF-16 to UTF-8
106 * @param s 0-terminated UTF-16 string
107 * @return 0-terminated UTF-8 string or 0 on error
108 *
109 * See utf16_to_utf8() for possible causes of errors.
110 */
111static inline char *utf16nt_to_utf8(const uint16_t *s) {
112 return utf16_to_utf8(s, utf16_len(s), 0);
113}
114
caecd4f4
RK
115/** @brief Convert 0-terminated UTF-8 to UTF-32
116 * @param s 0-terminated UTF-8 string
117 * @return 0-terminated UTF-32 string or 0 on error
118 *
119 * See utf8_to_utf32() for possible causes of errors.
120 */
121static inline uint32_t *utf8nt_to_utf32(const char *s) {
122 return utf8_to_utf32(s, strlen(s), 0);
123}
124
bb5c7798
RK
125/** @brief Convert 0-terminated UTF-8 to UTF-16
126 * @param s 0-terminated UTF-8 string
127 * @return 0-terminated UTF-16 string or 0 on error
128 *
129 * See utf8_to_utf16() for possible causes of errors.
130 */
131static inline uint16_t *utf8nt_to_utf16(const char *s) {
132 return utf8_to_utf16(s, strlen(s), 0);
133}
134
9e42afcd
RK
135#if _WIN32
136static inline wchar_t *utf8nt_to_wchar(const char *s) {
137 return (wchar_t *)utf8nt_to_utf16(s);
138}
139
140static inline char *wcharnt_to_utf8(const wchar_t *s) {
141 return utf16nt_to_utf8((const uint16_t *)s);
142}
143#else
bb5c7798
RK
144static inline wchar_t *utf8nt_to_wchar(const char *s) {
145 return (wchar_t *)utf8nt_to_utf32(s);
146}
147
148static inline char *wcharnt_to_utf8(const wchar_t *s) {
149 return utf32nt_to_utf8((const uint32_t *)s);
150}
9e42afcd 151#endif
bb5c7798 152
e5a5a138
RK
153#endif /* UNICODE_H */
154
155/*
156Local Variables:
157c-basic-offset:2
158comment-column:40
159fill-column:79
160indent-tabs-mode:nil
161End:
162*/