/src/duckdb/third_party/utf8proc/utf8proc.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*- */ |
2 | | /* |
3 | | * Copyright (c) 2014-2021 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other contributors. |
4 | | * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a |
7 | | * copy of this software and associated documentation files (the "Software"), |
8 | | * to deal in the Software without restriction, including without limitation |
9 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
10 | | * and/or sell copies of the Software, and to permit persons to whom the |
11 | | * Software is furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
22 | | * DEALINGS IN THE SOFTWARE. |
23 | | */ |
24 | | |
25 | | /* |
26 | | * This library contains derived data from a modified version of the |
27 | | * Unicode data files. |
28 | | * |
29 | | * The original data files are available at |
30 | | * https://www.unicode.org/Public/UNIDATA/ |
31 | | * |
32 | | * Please notice the copyright statement in the file "utf8proc_data.c". |
33 | | */ |
34 | | |
35 | | |
36 | | /* |
37 | | * File name: utf8proc.c |
38 | | * |
39 | | * Description: |
40 | | * Implementation of libutf8proc. |
41 | | */ |
42 | | |
43 | | |
44 | | #include "utf8proc.hpp" |
45 | | |
46 | | namespace duckdb { |
47 | | #ifndef SSIZE_MAX |
48 | | #define SSIZE_MAX ((size_t)SIZE_MAX/2) |
49 | | #endif |
50 | | #ifndef UINT16_MAX |
51 | | # define UINT16_MAX 65535U |
52 | | #endif |
53 | | |
54 | | #include "utf8proc_data.cpp" |
55 | | |
56 | | |
57 | | UTF8PROC_DLLEXPORT const utf8proc_int8_t utf8proc_utf8class[256] = { |
58 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
59 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
60 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
61 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
62 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
63 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
64 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
65 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
66 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
67 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
68 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
69 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
70 | | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
71 | | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
72 | | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
73 | | 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 }; |
74 | | |
75 | 0 | #define UTF8PROC_HANGUL_SBASE 0xAC00 |
76 | 0 | #define UTF8PROC_HANGUL_LBASE 0x1100 |
77 | 0 | #define UTF8PROC_HANGUL_VBASE 0x1161 |
78 | 0 | #define UTF8PROC_HANGUL_TBASE 0x11A7 |
79 | 0 | #define UTF8PROC_HANGUL_LCOUNT 19 |
80 | 0 | #define UTF8PROC_HANGUL_VCOUNT 21 |
81 | 0 | #define UTF8PROC_HANGUL_TCOUNT 28 |
82 | 0 | #define UTF8PROC_HANGUL_NCOUNT 588 |
83 | 0 | #define UTF8PROC_HANGUL_SCOUNT 11172 |
84 | | /* END is exclusive */ |
85 | | #define UTF8PROC_HANGUL_L_START 0x1100 |
86 | | #define UTF8PROC_HANGUL_L_END 0x115A |
87 | | #define UTF8PROC_HANGUL_L_FILLER 0x115F |
88 | | #define UTF8PROC_HANGUL_V_START 0x1160 |
89 | | #define UTF8PROC_HANGUL_V_END 0x11A3 |
90 | | #define UTF8PROC_HANGUL_T_START 0x11A8 |
91 | | #define UTF8PROC_HANGUL_T_END 0x11FA |
92 | | #define UTF8PROC_HANGUL_S_START 0xAC00 |
93 | | #define UTF8PROC_HANGUL_S_END 0xD7A4 |
94 | | |
95 | | /* Should follow semantic-versioning rules (semver.org) based on API |
96 | | compatibility. (Note that the shared-library version number will |
97 | | be different, being based on ABI compatibility.): */ |
98 | 0 | #define STRINGIZEx(x) #x |
99 | 0 | #define STRINGIZE(x) STRINGIZEx(x) |
100 | 0 | UTF8PROC_DLLEXPORT const char *utf8proc_version(void) { |
101 | 0 | return STRINGIZE(UTF8PROC_VERSION_MAJOR) "." STRINGIZE(UTF8PROC_VERSION_MINOR) "." STRINGIZE(UTF8PROC_VERSION_PATCH) ""; |
102 | 0 | } |
103 | | |
104 | 0 | UTF8PROC_DLLEXPORT const char *utf8proc_unicode_version(void) { |
105 | 0 | return "15.1.0"; |
106 | 0 | } |
107 | | |
108 | 0 | UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode) { |
109 | 0 | switch (errcode) { |
110 | 0 | case UTF8PROC_ERROR_NOMEM: |
111 | 0 | return "Memory for processing UTF-8 data could not be allocated."; |
112 | 0 | case UTF8PROC_ERROR_OVERFLOW: |
113 | 0 | return "UTF-8 string is too long to be processed."; |
114 | 0 | case UTF8PROC_ERROR_INVALIDUTF8: |
115 | 0 | return "Invalid UTF-8 string"; |
116 | 0 | case UTF8PROC_ERROR_NOTASSIGNED: |
117 | 0 | return "Unassigned Unicode code point found in UTF-8 string."; |
118 | 0 | case UTF8PROC_ERROR_INVALIDOPTS: |
119 | 0 | return "Invalid options for UTF-8 processing chosen."; |
120 | 0 | default: |
121 | 0 | return "An unknown error occurred while processing UTF-8 data."; |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | 0 | #define utf_cont(ch) (((ch) & 0xc0) == 0x80) |
126 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate( |
127 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst |
128 | 0 | ) { |
129 | 0 | utf8proc_int32_t uc; |
130 | 0 | const utf8proc_uint8_t *end; |
131 | |
|
132 | 0 | *dst = -1; |
133 | 0 | if (!strlen) return 0; |
134 | 0 | end = str + ((strlen < 0) ? 4 : strlen); |
135 | 0 | uc = *str++; |
136 | 0 | if (uc < 0x80) { |
137 | 0 | *dst = uc; |
138 | 0 | return 1; |
139 | 0 | } |
140 | | // Must be between 0xc2 and 0xf4 inclusive to be valid |
141 | 0 | if ((utf8proc_uint32_t)(uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8; |
142 | 0 | if (uc < 0xe0) { // 2-byte sequence |
143 | | // Must have valid continuation character |
144 | 0 | if (str >= end || !utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8; |
145 | 0 | *dst = ((uc & 0x1f)<<6) | (*str & 0x3f); |
146 | 0 | return 2; |
147 | 0 | } |
148 | 0 | if (uc < 0xf0) { // 3-byte sequence |
149 | 0 | if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1])) |
150 | 0 | return UTF8PROC_ERROR_INVALIDUTF8; |
151 | | // Check for surrogate chars |
152 | 0 | if (uc == 0xed && *str > 0x9f) |
153 | 0 | return UTF8PROC_ERROR_INVALIDUTF8; |
154 | 0 | uc = ((uc & 0xf)<<12) | ((*str & 0x3f)<<6) | (str[1] & 0x3f); |
155 | 0 | if (uc < 0x800) |
156 | 0 | return UTF8PROC_ERROR_INVALIDUTF8; |
157 | 0 | *dst = uc; |
158 | 0 | return 3; |
159 | 0 | } |
160 | | // 4-byte sequence |
161 | | // Must have 3 valid continuation characters |
162 | 0 | if ((str + 2 >= end) || !utf_cont(*str) || !utf_cont(str[1]) || !utf_cont(str[2])) |
163 | 0 | return UTF8PROC_ERROR_INVALIDUTF8; |
164 | | // Make sure in correct range (0x10000 - 0x10ffff) |
165 | 0 | if (uc == 0xf0) { |
166 | 0 | if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8; |
167 | 0 | } else if (uc == 0xf4) { |
168 | 0 | if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8; |
169 | 0 | } |
170 | 0 | *dst = ((uc & 7)<<18) | ((*str & 0x3f)<<12) | ((str[1] & 0x3f)<<6) | (str[2] & 0x3f); |
171 | 0 | return 4; |
172 | 0 | } |
173 | | |
174 | 0 | UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t uc) { |
175 | 0 | return (((utf8proc_uint32_t)uc)-0xd800 > 0x07ff) && ((utf8proc_uint32_t)uc < 0x110000); |
176 | 0 | } |
177 | | |
178 | 0 | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) { |
179 | 0 | if (uc < 0x00) { |
180 | 0 | return 0; |
181 | 0 | } else if (uc < 0x80) { |
182 | 0 | dst[0] = (utf8proc_uint8_t) uc; |
183 | 0 | return 1; |
184 | 0 | } else if (uc < 0x800) { |
185 | 0 | dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6)); |
186 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
187 | 0 | return 2; |
188 | | // Note: we allow encoding 0xd800-0xdfff here, so as not to change |
189 | | // the API, however, these are actually invalid in UTF-8 |
190 | 0 | } else if (uc < 0x10000) { |
191 | 0 | dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12)); |
192 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F)); |
193 | 0 | dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
194 | 0 | return 3; |
195 | 0 | } else if (uc < 0x110000) { |
196 | 0 | dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18)); |
197 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F)); |
198 | 0 | dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F)); |
199 | 0 | dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
200 | 0 | return 4; |
201 | 0 | } else return 0; |
202 | 0 | } |
203 | | |
204 | | /* internal version used for inserting 0xff bytes between graphemes */ |
205 | 0 | static utf8proc_ssize_t charbound_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) { |
206 | 0 | if (uc < 0x00) { |
207 | 0 | if (uc == -1) { /* internal value used for grapheme breaks */ |
208 | 0 | dst[0] = (utf8proc_uint8_t)0xFF; |
209 | 0 | return 1; |
210 | 0 | } |
211 | 0 | return 0; |
212 | 0 | } else if (uc < 0x80) { |
213 | 0 | dst[0] = (utf8proc_uint8_t)uc; |
214 | 0 | return 1; |
215 | 0 | } else if (uc < 0x800) { |
216 | 0 | dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6)); |
217 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
218 | 0 | return 2; |
219 | 0 | } else if (uc < 0x10000) { |
220 | 0 | dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12)); |
221 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F)); |
222 | 0 | dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
223 | 0 | return 3; |
224 | 0 | } else if (uc < 0x110000) { |
225 | 0 | dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18)); |
226 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F)); |
227 | 0 | dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F)); |
228 | 0 | dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
229 | 0 | return 4; |
230 | 0 | } else return 0; |
231 | 0 | } |
232 | | |
233 | | /* internal "unsafe" version that does not check whether uc is in range */ |
234 | 99.3M | static const utf8proc_property_t *unsafe_get_property(utf8proc_int32_t uc) { |
235 | | /* ASSERT: uc >= 0 && uc < 0x110000 */ |
236 | 99.3M | return utf8proc_properties + ( |
237 | 99.3M | utf8proc_stage2table[ |
238 | 99.3M | utf8proc_stage1table[uc >> 8] + (uc & 0xFF) |
239 | 99.3M | ] |
240 | 99.3M | ); |
241 | 99.3M | } |
242 | | |
243 | 99.3M | UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t uc) { |
244 | 99.3M | return uc < 0 || uc >= 0x110000 ? utf8proc_properties : unsafe_get_property(uc); |
245 | 99.3M | } |
246 | | |
247 | | /* return whether there is a grapheme break between boundclasses lbc and tbc |
248 | | (according to the definition of extended grapheme clusters) |
249 | | |
250 | | Rule numbering refers to TR29 Version 29 (Unicode 9.0.0): |
251 | | http://www.unicode.org/reports/tr29/tr29-29.html |
252 | | |
253 | | CAVEATS: |
254 | | Please note that evaluation of GB10 (grapheme breaks between emoji zwj sequences) |
255 | | and GB 12/13 (regional indicator code points) require knowledge of previous characters |
256 | | and are thus not handled by this function. This may result in an incorrect break before |
257 | | an E_Modifier class codepoint and an incorrectly missing break between two |
258 | | REGIONAL_INDICATOR class code points if such support does not exist in the caller. |
259 | | |
260 | | See the special support in grapheme_break_extended, for required bookkeeping by the caller. |
261 | | */ |
262 | 32.1M | static utf8proc_bool grapheme_break_simple(int lbc, int tbc) { |
263 | 32.1M | return |
264 | 32.1M | (lbc == UTF8PROC_BOUNDCLASS_START) ? true : // GB1 |
265 | 32.1M | (lbc == UTF8PROC_BOUNDCLASS_CR && // GB3 |
266 | 32.1M | tbc == UTF8PROC_BOUNDCLASS_LF) ? false : // --- |
267 | 32.1M | (lbc >= UTF8PROC_BOUNDCLASS_CR && lbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB4 |
268 | 32.1M | (tbc >= UTF8PROC_BOUNDCLASS_CR && tbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB5 |
269 | 32.1M | (lbc == UTF8PROC_BOUNDCLASS_L && // GB6 |
270 | 32.1M | (tbc == UTF8PROC_BOUNDCLASS_L || // --- |
271 | 102 | tbc == UTF8PROC_BOUNDCLASS_V || // --- |
272 | 102 | tbc == UTF8PROC_BOUNDCLASS_LV || // --- |
273 | 102 | tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false : // --- |
274 | 32.1M | ((lbc == UTF8PROC_BOUNDCLASS_LV || // GB7 |
275 | 32.1M | lbc == UTF8PROC_BOUNDCLASS_V) && // --- |
276 | 32.1M | (tbc == UTF8PROC_BOUNDCLASS_V || // --- |
277 | 376 | tbc == UTF8PROC_BOUNDCLASS_T)) ? false : // --- |
278 | 32.1M | ((lbc == UTF8PROC_BOUNDCLASS_LVT || // GB8 |
279 | 32.1M | lbc == UTF8PROC_BOUNDCLASS_T) && // --- |
280 | 32.1M | tbc == UTF8PROC_BOUNDCLASS_T) ? false : // --- |
281 | 32.1M | (tbc == UTF8PROC_BOUNDCLASS_EXTEND || // GB9 |
282 | 32.1M | tbc == UTF8PROC_BOUNDCLASS_ZWJ || // --- |
283 | 32.1M | tbc == UTF8PROC_BOUNDCLASS_SPACINGMARK || // GB9a |
284 | 32.1M | lbc == UTF8PROC_BOUNDCLASS_PREPEND) ? false : // GB9b |
285 | 32.1M | (lbc == UTF8PROC_BOUNDCLASS_E_ZWG && // GB11 (requires additional handling below) |
286 | 32.1M | tbc == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) ? false : // ---- |
287 | 32.1M | (lbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR && // GB12/13 (requires additional handling below) |
288 | 32.1M | tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) ? false : // ---- |
289 | 32.1M | true; // GB999 |
290 | 32.1M | } |
291 | | |
292 | | static utf8proc_bool grapheme_break_extended(int lbc, int tbc, int licb, int ticb, utf8proc_int32_t *state) |
293 | 32.1M | { |
294 | 32.1M | if (state) { |
295 | 32.1M | int state_bc, state_icb; /* boundclass and indic_conjunct_break state */ |
296 | 32.1M | if (*state == 0) { /* state initialization */ |
297 | 32.1M | state_bc = lbc; |
298 | 32.1M | state_icb = licb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT ? licb : UTF8PROC_INDIC_CONJUNCT_BREAK_NONE; |
299 | 32.1M | } |
300 | 845 | else { /* lbc and licb are already encoded in *state */ |
301 | 845 | state_bc = *state & 0xff; // 1st byte of state is bound class |
302 | 845 | state_icb = *state >> 8; // 2nd byte of state is indic conjunct break |
303 | 845 | } |
304 | | |
305 | 32.1M | utf8proc_bool break_permitted = grapheme_break_simple(state_bc, tbc) && |
306 | 32.1M | !(state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER |
307 | 32.1M | && ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT); // GB9c |
308 | | |
309 | | // Special support for GB9c. Don't break between two consonants |
310 | | // separated 1+ linker characters and 0+ extend characters in any order. |
311 | | // After a consonant, we enter LINKER state after at least one linker. |
312 | 32.1M | if (ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT |
313 | 32.1M | || state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT |
314 | 32.1M | || state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_EXTEND) |
315 | 26 | state_icb = ticb; |
316 | 32.1M | else if (state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER) |
317 | 0 | state_icb = ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_EXTEND ? |
318 | 0 | UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER : ticb; |
319 | | |
320 | | // Special support for GB 12/13 made possible by GB999. After two RI |
321 | | // class codepoints we want to force a break. Do this by resetting the |
322 | | // second RI's bound class to UTF8PROC_BOUNDCLASS_OTHER, to force a break |
323 | | // after that character according to GB999 (unless of course such a break is |
324 | | // forbidden by a different rule such as GB9). |
325 | 32.1M | if (state_bc == tbc && tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) |
326 | 7 | state_bc = UTF8PROC_BOUNDCLASS_OTHER; |
327 | | // Special support for GB11 (emoji extend* zwj / emoji) |
328 | 32.1M | else if (state_bc == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) { |
329 | 58 | if (tbc == UTF8PROC_BOUNDCLASS_EXTEND) // fold EXTEND codepoints into emoji |
330 | 5 | state_bc = UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC; |
331 | 53 | else if (tbc == UTF8PROC_BOUNDCLASS_ZWJ) |
332 | 35 | state_bc = UTF8PROC_BOUNDCLASS_E_ZWG; // state to record emoji+zwg combo |
333 | 18 | else |
334 | 18 | state_bc = tbc; |
335 | 58 | } |
336 | 32.1M | else |
337 | 32.1M | state_bc = tbc; |
338 | | |
339 | 32.1M | *state = state_bc + (state_icb << 8); |
340 | 32.1M | return break_permitted; |
341 | 32.1M | } |
342 | 0 | else |
343 | 0 | return grapheme_break_simple(lbc, tbc); |
344 | 32.1M | } |
345 | | |
346 | | UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful( |
347 | 32.1M | utf8proc_int32_t c1, utf8proc_int32_t c2, utf8proc_int32_t *state) { |
348 | | |
349 | 32.1M | const utf8proc_property_t *p1 = utf8proc_get_property(c1); |
350 | 32.1M | const utf8proc_property_t *p2 = utf8proc_get_property(c2); |
351 | 32.1M | return grapheme_break_extended(p1->boundclass, |
352 | 32.1M | p2->boundclass, |
353 | 32.1M | p1->indic_conjunct_break, |
354 | 32.1M | p2->indic_conjunct_break, |
355 | 32.1M | state); |
356 | 32.1M | } |
357 | | |
358 | | |
359 | | UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break( |
360 | 0 | utf8proc_int32_t c1, utf8proc_int32_t c2) { |
361 | 0 | return utf8proc_grapheme_break_stateful(c1, c2, NULL); |
362 | 0 | } |
363 | | |
364 | | static utf8proc_int32_t seqindex_decode_entry(const utf8proc_uint16_t **entry) |
365 | 0 | { |
366 | 0 | utf8proc_int32_t entry_cp = **entry; |
367 | 0 | if ((entry_cp & 0xF800) == 0xD800) { |
368 | 0 | *entry = *entry + 1; |
369 | 0 | entry_cp = ((entry_cp & 0x03FF) << 10) | (**entry & 0x03FF); |
370 | 0 | entry_cp += 0x10000; |
371 | 0 | } |
372 | 0 | return entry_cp; |
373 | 0 | } |
374 | | |
375 | | static utf8proc_int32_t seqindex_decode_index(const utf8proc_uint32_t seqindex) |
376 | 0 | { |
377 | 0 | const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex]; |
378 | 0 | return seqindex_decode_entry(&entry); |
379 | 0 | } |
380 | | |
381 | 0 | static utf8proc_ssize_t seqindex_write_char_decomposed(utf8proc_uint16_t seqindex, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) { |
382 | 0 | utf8proc_ssize_t written = 0; |
383 | 0 | const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex & 0x3FFF]; |
384 | 0 | int len = seqindex >> 14; |
385 | 0 | if (len >= 3) { |
386 | 0 | len = *entry; |
387 | 0 | entry++; |
388 | 0 | } |
389 | 0 | for (; len >= 0; entry++, len--) { |
390 | 0 | utf8proc_int32_t entry_cp = seqindex_decode_entry(&entry); |
391 | |
|
392 | 0 | written += utf8proc_decompose_char(entry_cp, dst ? dst+written : nullptr, |
393 | 0 | (bufsize > written) ? (bufsize - written) : 0, options, |
394 | 0 | last_boundclass); |
395 | 0 | if (written < 0) return UTF8PROC_ERROR_OVERFLOW; |
396 | 0 | } |
397 | 0 | return written; |
398 | 0 | } |
399 | | |
400 | | UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c) |
401 | 0 | { |
402 | 0 | utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_seqindex; |
403 | 0 | return cl != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cl) : c; |
404 | 0 | } |
405 | | |
406 | | UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c) |
407 | 0 | { |
408 | 0 | utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_seqindex; |
409 | 0 | return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c; |
410 | 0 | } |
411 | | |
412 | | UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c) |
413 | 0 | { |
414 | 0 | utf8proc_int32_t cu = utf8proc_get_property(c)->titlecase_seqindex; |
415 | 0 | return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c; |
416 | 0 | } |
417 | | |
418 | | UTF8PROC_DLLEXPORT int utf8proc_islower(utf8proc_int32_t c) |
419 | 0 | { |
420 | 0 | const utf8proc_property_t *p = utf8proc_get_property(c); |
421 | 0 | return p->lowercase_seqindex != p->uppercase_seqindex && p->lowercase_seqindex == UINT16_MAX; |
422 | 0 | } |
423 | | |
424 | | UTF8PROC_DLLEXPORT int utf8proc_isupper(utf8proc_int32_t c) |
425 | 0 | { |
426 | 0 | const utf8proc_property_t *p = utf8proc_get_property(c); |
427 | 0 | return p->lowercase_seqindex != p->uppercase_seqindex && p->uppercase_seqindex == UINT16_MAX && p->category != UTF8PROC_CATEGORY_LT; |
428 | 0 | } |
429 | | |
430 | | /* return a character width analogous to wcwidth (except portable and |
431 | | hopefully less buggy than most system wcwidth functions). */ |
432 | 0 | UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t c) { |
433 | 0 | return utf8proc_get_property(c)->charwidth; |
434 | 0 | } |
435 | | |
436 | 0 | UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t c) { |
437 | 0 | return (utf8proc_category_t) utf8proc_get_property(c)->category; |
438 | 0 | } |
439 | | |
440 | 0 | UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) { |
441 | 0 | static const char s[][3] = {"Cn","Lu","Ll","Lt","Lm","Lo","Mn","Mc","Me","Nd","Nl","No","Pc","Pd","Ps","Pe","Pi","Pf","Po","Sm","Sc","Sk","So","Zs","Zl","Zp","Cc","Cf","Cs","Co"}; |
442 | 0 | return s[utf8proc_category(c)]; |
443 | 0 | } |
444 | | |
445 | 0 | #define utf8proc_decompose_lump(replacement_uc) \ |
446 | 0 | return utf8proc_decompose_char((replacement_uc), dst, bufsize, \ |
447 | 0 | (utf8proc_option_t)(options & ~(unsigned int)UTF8PROC_LUMP), last_boundclass) |
448 | | |
449 | 0 | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(utf8proc_int32_t uc, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) { |
450 | 0 | const utf8proc_property_t *property; |
451 | 0 | utf8proc_propval_t category; |
452 | 0 | utf8proc_int32_t hangul_sindex; |
453 | 0 | if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED; |
454 | 0 | property = unsafe_get_property(uc); |
455 | 0 | category = property->category; |
456 | 0 | hangul_sindex = uc - UTF8PROC_HANGUL_SBASE; |
457 | 0 | if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) { |
458 | 0 | if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) { |
459 | 0 | utf8proc_int32_t hangul_tindex; |
460 | 0 | if (bufsize >= 1) { |
461 | 0 | dst[0] = UTF8PROC_HANGUL_LBASE + |
462 | 0 | hangul_sindex / UTF8PROC_HANGUL_NCOUNT; |
463 | 0 | if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE + |
464 | 0 | (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT; |
465 | 0 | } |
466 | 0 | hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT; |
467 | 0 | if (!hangul_tindex) return 2; |
468 | 0 | if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex; |
469 | 0 | return 3; |
470 | 0 | } |
471 | 0 | } |
472 | 0 | if (options & UTF8PROC_REJECTNA) { |
473 | 0 | if (!category) return UTF8PROC_ERROR_NOTASSIGNED; |
474 | 0 | } |
475 | 0 | if (options & UTF8PROC_IGNORE) { |
476 | 0 | if (property->ignorable) return 0; |
477 | 0 | } |
478 | 0 | if (options & UTF8PROC_STRIPNA) { |
479 | 0 | if (!category) return 0; |
480 | 0 | } |
481 | 0 | if (options & UTF8PROC_LUMP) { |
482 | 0 | if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020); |
483 | 0 | if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8) |
484 | 0 | utf8proc_decompose_lump(0x0027); |
485 | 0 | if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212) |
486 | 0 | utf8proc_decompose_lump(0x002D); |
487 | 0 | if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F); |
488 | 0 | if (uc == 0x2236) utf8proc_decompose_lump(0x003A); |
489 | 0 | if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008) |
490 | 0 | utf8proc_decompose_lump(0x003C); |
491 | 0 | if (uc == 0x203A || uc == 0x232A || uc == 0x3009) |
492 | 0 | utf8proc_decompose_lump(0x003E); |
493 | 0 | if (uc == 0x2216) utf8proc_decompose_lump(0x005C); |
494 | 0 | if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303) |
495 | 0 | utf8proc_decompose_lump(0x005E); |
496 | 0 | if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD) |
497 | 0 | utf8proc_decompose_lump(0x005F); |
498 | 0 | if (uc == 0x02CB) utf8proc_decompose_lump(0x0060); |
499 | 0 | if (uc == 0x2223) utf8proc_decompose_lump(0x007C); |
500 | 0 | if (uc == 0x223C) utf8proc_decompose_lump(0x007E); |
501 | 0 | if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) { |
502 | 0 | if (category == UTF8PROC_CATEGORY_ZL || |
503 | 0 | category == UTF8PROC_CATEGORY_ZP) |
504 | 0 | utf8proc_decompose_lump(0x000A); |
505 | 0 | } |
506 | 0 | } |
507 | 0 | if (options & UTF8PROC_STRIPMARK) { |
508 | 0 | if (category == UTF8PROC_CATEGORY_MN || |
509 | 0 | category == UTF8PROC_CATEGORY_MC || |
510 | 0 | category == UTF8PROC_CATEGORY_ME) return 0; |
511 | 0 | } |
512 | 0 | if (options & UTF8PROC_CASEFOLD) { |
513 | 0 | if (property->casefold_seqindex != UINT16_MAX) { |
514 | 0 | return seqindex_write_char_decomposed(property->casefold_seqindex, dst, bufsize, options, last_boundclass); |
515 | 0 | } |
516 | 0 | } |
517 | 0 | if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) { |
518 | 0 | if (property->decomp_seqindex != UINT16_MAX && |
519 | 0 | (!property->decomp_type || (options & UTF8PROC_COMPAT))) { |
520 | 0 | return seqindex_write_char_decomposed(property->decomp_seqindex, dst, bufsize, options, last_boundclass); |
521 | 0 | } |
522 | 0 | } |
523 | 0 | if (options & UTF8PROC_CHARBOUND) { |
524 | 0 | utf8proc_bool boundary; |
525 | 0 | boundary = grapheme_break_extended(0, property->boundclass, 0, property->indic_conjunct_break, |
526 | 0 | last_boundclass); |
527 | 0 | if (boundary) { |
528 | 0 | if (bufsize >= 1) dst[0] = -1; /* sentinel value for grapheme break */ |
529 | 0 | if (bufsize >= 2) dst[1] = uc; |
530 | 0 | return 2; |
531 | 0 | } |
532 | 0 | } |
533 | 0 | if (bufsize >= 1) *dst = uc; |
534 | 0 | return 1; |
535 | 0 | } |
536 | | |
537 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose( |
538 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, |
539 | | utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options |
540 | 0 | ) { |
541 | 0 | return utf8proc_decompose_custom(str, strlen, buffer, bufsize, options, NULL, NULL); |
542 | 0 | } |
543 | | |
544 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom( |
545 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, |
546 | | utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options, |
547 | | utf8proc_custom_func custom_func, void *custom_data |
548 | 0 | ) { |
549 | | /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */ |
550 | 0 | utf8proc_ssize_t wpos = 0; |
551 | 0 | if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE)) |
552 | 0 | return UTF8PROC_ERROR_INVALIDOPTS; |
553 | 0 | if ((options & UTF8PROC_STRIPMARK) && |
554 | 0 | !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE)) |
555 | 0 | return UTF8PROC_ERROR_INVALIDOPTS; |
556 | 0 | { |
557 | 0 | utf8proc_int32_t uc; |
558 | 0 | utf8proc_ssize_t rpos = 0; |
559 | 0 | utf8proc_ssize_t decomp_result; |
560 | 0 | int boundclass = UTF8PROC_BOUNDCLASS_START; |
561 | 0 | while (1) { |
562 | 0 | if (options & UTF8PROC_NULLTERM) { |
563 | 0 | rpos += utf8proc_iterate(str + rpos, -1, &uc); |
564 | | /* checking of return value is not necessary, |
565 | | as 'uc' is < 0 in case of error */ |
566 | 0 | if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8; |
567 | 0 | if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW; |
568 | 0 | if (uc == 0) break; |
569 | 0 | } else { |
570 | 0 | if (rpos >= strlen) break; |
571 | 0 | rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc); |
572 | 0 | if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8; |
573 | 0 | } |
574 | 0 | if (custom_func != NULL) { |
575 | 0 | uc = custom_func(uc, custom_data); /* user-specified custom mapping */ |
576 | 0 | } |
577 | 0 | decomp_result = utf8proc_decompose_char( |
578 | 0 | uc, buffer ? buffer + wpos : nullptr, (bufsize > wpos) ? (bufsize - wpos) : 0, options, |
579 | 0 | &boundclass |
580 | 0 | ); |
581 | 0 | if (decomp_result < 0) return decomp_result; |
582 | 0 | wpos += decomp_result; |
583 | | /* prohibiting integer overflows due to too long strings: */ |
584 | 0 | if (wpos < 0 || |
585 | 0 | wpos > (utf8proc_ssize_t)(SSIZE_MAX/sizeof(utf8proc_int32_t)/2)) |
586 | 0 | return UTF8PROC_ERROR_OVERFLOW; |
587 | 0 | } |
588 | 0 | } |
589 | 0 | if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) { |
590 | 0 | utf8proc_ssize_t pos = 0; |
591 | 0 | while (pos < wpos-1) { |
592 | 0 | utf8proc_int32_t uc1, uc2; |
593 | 0 | const utf8proc_property_t *property1, *property2; |
594 | 0 | uc1 = buffer[pos]; |
595 | 0 | uc2 = buffer[pos+1]; |
596 | 0 | property1 = unsafe_get_property(uc1); |
597 | 0 | property2 = unsafe_get_property(uc2); |
598 | 0 | if (property1->combining_class > property2->combining_class && |
599 | 0 | property2->combining_class > 0) { |
600 | 0 | buffer[pos] = uc2; |
601 | 0 | buffer[pos+1] = uc1; |
602 | 0 | if (pos > 0) pos--; else pos++; |
603 | 0 | } else { |
604 | 0 | pos++; |
605 | 0 | } |
606 | 0 | } |
607 | 0 | } |
608 | 0 | return wpos; |
609 | 0 | } |
610 | | |
611 | 0 | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) { |
612 | | /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored */ |
613 | 0 | if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) { |
614 | 0 | utf8proc_ssize_t rpos; |
615 | 0 | utf8proc_ssize_t wpos = 0; |
616 | 0 | utf8proc_int32_t uc; |
617 | 0 | for (rpos = 0; rpos < length; rpos++) { |
618 | 0 | uc = buffer[rpos]; |
619 | 0 | if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++; |
620 | 0 | if (uc == 0x000A || uc == 0x000D || uc == 0x0085 || |
621 | 0 | ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) { |
622 | 0 | if (options & UTF8PROC_NLF2LS) { |
623 | 0 | if (options & UTF8PROC_NLF2PS) { |
624 | 0 | buffer[wpos++] = 0x000A; |
625 | 0 | } else { |
626 | 0 | buffer[wpos++] = 0x2028; |
627 | 0 | } |
628 | 0 | } else { |
629 | 0 | if (options & UTF8PROC_NLF2PS) { |
630 | 0 | buffer[wpos++] = 0x2029; |
631 | 0 | } else { |
632 | 0 | buffer[wpos++] = 0x0020; |
633 | 0 | } |
634 | 0 | } |
635 | 0 | } else if ((options & UTF8PROC_STRIPCC) && |
636 | 0 | (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) { |
637 | 0 | if (uc == 0x0009) buffer[wpos++] = 0x0020; |
638 | 0 | } else { |
639 | 0 | buffer[wpos++] = uc; |
640 | 0 | } |
641 | 0 | } |
642 | 0 | length = wpos; |
643 | 0 | } |
644 | 0 | if (options & UTF8PROC_COMPOSE) { |
645 | 0 | utf8proc_int32_t *starter = NULL; |
646 | 0 | utf8proc_int32_t current_char; |
647 | 0 | const utf8proc_property_t *starter_property = NULL, *current_property; |
648 | 0 | utf8proc_propval_t max_combining_class = -1; |
649 | 0 | utf8proc_ssize_t rpos; |
650 | 0 | utf8proc_ssize_t wpos = 0; |
651 | 0 | utf8proc_int32_t composition; |
652 | 0 | for (rpos = 0; rpos < length; rpos++) { |
653 | 0 | current_char = buffer[rpos]; |
654 | 0 | current_property = unsafe_get_property(current_char); |
655 | 0 | if (starter && current_property->combining_class > max_combining_class) { |
656 | | /* combination perhaps possible */ |
657 | 0 | utf8proc_int32_t hangul_lindex; |
658 | 0 | utf8proc_int32_t hangul_sindex; |
659 | 0 | hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE; |
660 | 0 | if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) { |
661 | 0 | utf8proc_int32_t hangul_vindex; |
662 | 0 | hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE; |
663 | 0 | if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) { |
664 | 0 | *starter = UTF8PROC_HANGUL_SBASE + |
665 | 0 | (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) * |
666 | 0 | UTF8PROC_HANGUL_TCOUNT; |
667 | 0 | starter_property = NULL; |
668 | 0 | continue; |
669 | 0 | } |
670 | 0 | } |
671 | 0 | hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE; |
672 | 0 | if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT && |
673 | 0 | (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) { |
674 | 0 | utf8proc_int32_t hangul_tindex; |
675 | 0 | hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE; |
676 | 0 | if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) { |
677 | 0 | *starter += hangul_tindex; |
678 | 0 | starter_property = NULL; |
679 | 0 | continue; |
680 | 0 | } |
681 | 0 | } |
682 | 0 | if (!starter_property) { |
683 | 0 | starter_property = unsafe_get_property(*starter); |
684 | 0 | } |
685 | 0 | if (starter_property->comb_index < 0x8000 && |
686 | 0 | current_property->comb_index != UINT16_MAX && |
687 | 0 | current_property->comb_index >= 0x8000) { |
688 | 0 | int sidx = starter_property->comb_index; |
689 | 0 | int idx = current_property->comb_index & 0x3FFF; |
690 | 0 | if (idx >= utf8proc_combinations[sidx] && idx <= utf8proc_combinations[sidx + 1] ) { |
691 | 0 | idx += sidx + 2 - utf8proc_combinations[sidx]; |
692 | 0 | if (current_property->comb_index & 0x4000) { |
693 | 0 | composition = (utf8proc_combinations[idx] << 16) | utf8proc_combinations[idx+1]; |
694 | 0 | } else |
695 | 0 | composition = utf8proc_combinations[idx]; |
696 | |
|
697 | 0 | if (composition > 0 && (!(options & UTF8PROC_STABLE) || |
698 | 0 | !(unsafe_get_property(composition)->comp_exclusion))) { |
699 | 0 | *starter = composition; |
700 | 0 | starter_property = NULL; |
701 | 0 | continue; |
702 | 0 | } |
703 | 0 | } |
704 | 0 | } |
705 | 0 | } |
706 | 0 | buffer[wpos] = current_char; |
707 | 0 | if (current_property->combining_class) { |
708 | 0 | if (current_property->combining_class > max_combining_class) { |
709 | 0 | max_combining_class = current_property->combining_class; |
710 | 0 | } |
711 | 0 | } else { |
712 | 0 | starter = buffer + wpos; |
713 | 0 | starter_property = NULL; |
714 | 0 | max_combining_class = -1; |
715 | 0 | } |
716 | 0 | wpos++; |
717 | 0 | } |
718 | 0 | length = wpos; |
719 | 0 | } |
720 | 0 | return length; |
721 | 0 | } |
722 | | |
723 | 0 | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) { |
724 | | /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored |
725 | | ASSERT: 'buffer' has one spare byte of free space at the end! */ |
726 | 0 | length = utf8proc_normalize_utf32(buffer, length, options); |
727 | 0 | if (length < 0) return length; |
728 | 0 | { |
729 | 0 | utf8proc_ssize_t rpos, wpos = 0; |
730 | 0 | utf8proc_int32_t uc; |
731 | 0 | if (options & UTF8PROC_CHARBOUND) { |
732 | 0 | for (rpos = 0; rpos < length; rpos++) { |
733 | 0 | uc = buffer[rpos]; |
734 | 0 | wpos += charbound_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos); |
735 | 0 | } |
736 | 0 | } else { |
737 | 0 | for (rpos = 0; rpos < length; rpos++) { |
738 | 0 | uc = buffer[rpos]; |
739 | 0 | wpos += utf8proc_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos); |
740 | 0 | } |
741 | 0 | } |
742 | 0 | ((utf8proc_uint8_t *)buffer)[wpos] = 0; |
743 | 0 | return wpos; |
744 | 0 | } |
745 | 0 | } |
746 | | |
747 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map( |
748 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options |
749 | 0 | ) { |
750 | 0 | return utf8proc_map_custom(str, strlen, dstptr, options, NULL, NULL); |
751 | 0 | } |
752 | | |
753 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom( |
754 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options, |
755 | | utf8proc_custom_func custom_func, void *custom_data |
756 | 0 | ) { |
757 | 0 | utf8proc_int32_t *buffer; |
758 | 0 | utf8proc_ssize_t result; |
759 | 0 | *dstptr = NULL; |
760 | 0 | result = utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data); |
761 | 0 | if (result < 0) return result; |
762 | 0 | buffer = (utf8proc_int32_t *) malloc(((utf8proc_size_t)result) * sizeof(utf8proc_int32_t) + 1); |
763 | 0 | if (!buffer) return UTF8PROC_ERROR_NOMEM; |
764 | 0 | result = utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data); |
765 | 0 | if (result < 0) { |
766 | 0 | free(buffer); |
767 | 0 | return result; |
768 | 0 | } |
769 | 0 | result = utf8proc_reencode(buffer, result, options); |
770 | 0 | if (result < 0) { |
771 | 0 | free(buffer); |
772 | 0 | return result; |
773 | 0 | } |
774 | 0 | { |
775 | 0 | utf8proc_int32_t *newptr; |
776 | 0 | newptr = (utf8proc_int32_t *) realloc(buffer, (size_t)result+1); |
777 | 0 | if (newptr) buffer = newptr; |
778 | 0 | } |
779 | 0 | *dstptr = (utf8proc_uint8_t *)buffer; |
780 | 0 | return result; |
781 | 0 | } |
782 | | |
783 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen) { |
784 | 0 | utf8proc_uint8_t *retval; |
785 | 0 | utf8proc_map(str, strlen, &retval, utf8proc_option_t(UTF8PROC_STABLE | |
786 | 0 | UTF8PROC_DECOMPOSE)); |
787 | 0 | return retval; |
788 | 0 | } |
789 | | |
790 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen) { |
791 | 0 | utf8proc_uint8_t *retval; |
792 | 0 | utf8proc_map(str, strlen, &retval, utf8proc_option_t(UTF8PROC_STABLE | |
793 | 0 | UTF8PROC_COMPOSE)); |
794 | 0 | return retval; |
795 | 0 | } |
796 | | |
797 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen) { |
798 | 0 | utf8proc_uint8_t *retval; |
799 | 0 | utf8proc_map(str, strlen, &retval, utf8proc_option_t(UTF8PROC_STABLE | |
800 | 0 | UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT)); |
801 | 0 | return retval; |
802 | 0 | } |
803 | | |
804 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_remove_accents(const utf8proc_uint8_t *str, utf8proc_ssize_t len) { |
805 | 0 | utf8proc_uint8_t *retval; |
806 | 0 | utf8proc_map(str, len, &retval, (utf8proc_option_t)(UTF8PROC_STABLE | |
807 | 0 | UTF8PROC_COMPOSE | UTF8PROC_STRIPMARK)); |
808 | 0 | return retval; |
809 | 0 | } |
810 | | |
811 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen) { |
812 | 0 | utf8proc_uint8_t *retval; |
813 | 0 | utf8proc_map(str, strlen, &retval, utf8proc_option_t(UTF8PROC_STABLE | |
814 | 0 | UTF8PROC_COMPOSE | UTF8PROC_COMPAT)); |
815 | 0 | return retval; |
816 | 0 | } |
817 | | |
818 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen) { |
819 | 0 | utf8proc_uint8_t *retval; |
820 | 0 | utf8proc_map(str, strlen, &retval, utf8proc_option_t(UTF8PROC_STABLE | |
821 | 0 | UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_CASEFOLD | UTF8PROC_IGNORE)); |
822 | 0 | return retval; |
823 | 0 | } |
824 | | |
825 | | } |