/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-2019 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 | | * http://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 | | |
48 | | #ifndef SSIZE_MAX |
49 | | #define SSIZE_MAX ((size_t)SIZE_MAX/2) |
50 | | #endif |
51 | | #ifndef UINT16_MAX |
52 | | # define UINT16_MAX 65535U |
53 | | #endif |
54 | | |
55 | | #include "utf8proc_data.cpp" |
56 | | |
57 | | |
58 | | // UTF8PROC_DLLEXPORT const utf8proc_int8_t utf8proc_utf8class[256] = { |
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 | | // 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
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 | | // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
71 | | // 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
72 | | // 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
73 | | // 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
74 | | // 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 }; |
75 | | |
76 | 0 | #define UTF8PROC_HANGUL_SBASE 0xAC00 |
77 | 0 | #define UTF8PROC_HANGUL_LBASE 0x1100 |
78 | 0 | #define UTF8PROC_HANGUL_VBASE 0x1161 |
79 | 0 | #define UTF8PROC_HANGUL_TBASE 0x11A7 |
80 | 0 | #define UTF8PROC_HANGUL_LCOUNT 19 |
81 | 0 | #define UTF8PROC_HANGUL_VCOUNT 21 |
82 | 0 | #define UTF8PROC_HANGUL_TCOUNT 28 |
83 | 0 | #define UTF8PROC_HANGUL_NCOUNT 588 |
84 | 0 | #define UTF8PROC_HANGUL_SCOUNT 11172 |
85 | | /* END is exclusive */ |
86 | | #define UTF8PROC_HANGUL_L_START 0x1100 |
87 | | #define UTF8PROC_HANGUL_L_END 0x115A |
88 | | #define UTF8PROC_HANGUL_L_FILLER 0x115F |
89 | | #define UTF8PROC_HANGUL_V_START 0x1160 |
90 | | #define UTF8PROC_HANGUL_V_END 0x11A3 |
91 | | #define UTF8PROC_HANGUL_T_START 0x11A8 |
92 | | #define UTF8PROC_HANGUL_T_END 0x11FA |
93 | | #define UTF8PROC_HANGUL_S_START 0xAC00 |
94 | | #define UTF8PROC_HANGUL_S_END 0xD7A4 |
95 | | |
96 | | /* Should follow semantic-versioning rules (semver.org) based on API |
97 | | compatibility. (Note that the shared-library version number will |
98 | | be different, being based on ABI compatibility.): */ |
99 | 0 | #define STRINGIZEx(x) #x |
100 | 0 | #define STRINGIZE(x) STRINGIZEx(x) |
101 | 0 | UTF8PROC_DLLEXPORT const char *utf8proc_version(void) { |
102 | 0 | return STRINGIZE(UTF8PROC_VERSION_MAJOR) "." STRINGIZE(UTF8PROC_VERSION_MINOR) "." STRINGIZE(UTF8PROC_VERSION_PATCH) ""; |
103 | 0 | } |
104 | | |
105 | 0 | UTF8PROC_DLLEXPORT const char *utf8proc_unicode_version(void) { |
106 | 0 | return "12.1.0"; |
107 | 0 | } |
108 | | |
109 | 0 | UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode) { |
110 | 0 | switch (errcode) { |
111 | 0 | case UTF8PROC_ERROR_NOMEM: |
112 | 0 | return "Memory for processing UTF-8 data could not be allocated."; |
113 | 0 | case UTF8PROC_ERROR_OVERFLOW: |
114 | 0 | return "UTF-8 string is too long to be processed."; |
115 | 0 | case UTF8PROC_ERROR_INVALIDUTF8: |
116 | 0 | return "Invalid UTF-8 string"; |
117 | 0 | case UTF8PROC_ERROR_NOTASSIGNED: |
118 | 0 | return "Unassigned Unicode code point found in UTF-8 string."; |
119 | 0 | case UTF8PROC_ERROR_INVALIDOPTS: |
120 | 0 | return "Invalid options for UTF-8 processing chosen."; |
121 | 0 | default: |
122 | 0 | return "An unknown error occurred while processing UTF-8 data."; |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | #define utf_cont(ch) (((ch) & 0xc0) == 0x80) |
127 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate( |
128 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst |
129 | 0 | ) { |
130 | 0 | utf8proc_uint32_t uc; |
131 | 0 | const utf8proc_uint8_t *end; |
132 | |
|
133 | 0 | *dst = -1; |
134 | 0 | if (!strlen) return 0; |
135 | 0 | end = str + ((strlen < 0) ? 4 : strlen); |
136 | 0 | uc = *str++; |
137 | 0 | if (uc < 0x80) { |
138 | 0 | *dst = uc; |
139 | 0 | return 1; |
140 | 0 | } |
141 | | // Must be between 0xc2 and 0xf4 inclusive to be valid |
142 | 0 | if ((uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8; |
143 | 0 | if (uc < 0xe0) { // 2-byte sequence |
144 | | // Must have valid continuation character |
145 | 0 | if (str >= end || !utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8; |
146 | 0 | *dst = ((uc & 0x1f)<<6) | (*str & 0x3f); |
147 | 0 | return 2; |
148 | 0 | } |
149 | 0 | if (uc < 0xf0) { // 3-byte sequence |
150 | 0 | if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1])) |
151 | 0 | return UTF8PROC_ERROR_INVALIDUTF8; |
152 | | // Check for surrogate chars |
153 | 0 | if (uc == 0xed && *str > 0x9f) |
154 | 0 | return UTF8PROC_ERROR_INVALIDUTF8; |
155 | 0 | uc = ((uc & 0xf)<<12) | ((*str & 0x3f)<<6) | (str[1] & 0x3f); |
156 | 0 | if (uc < 0x800) |
157 | 0 | return UTF8PROC_ERROR_INVALIDUTF8; |
158 | 0 | *dst = uc; |
159 | 0 | return 3; |
160 | 0 | } |
161 | | // 4-byte sequence |
162 | | // Must have 3 valid continuation characters |
163 | 0 | if ((str + 2 >= end) || !utf_cont(*str) || !utf_cont(str[1]) || !utf_cont(str[2])) |
164 | 0 | return UTF8PROC_ERROR_INVALIDUTF8; |
165 | | // Make sure in correct range (0x10000 - 0x10ffff) |
166 | 0 | if (uc == 0xf0) { |
167 | 0 | if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8; |
168 | 0 | } else if (uc == 0xf4) { |
169 | 0 | if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8; |
170 | 0 | } |
171 | 0 | *dst = ((uc & 7)<<18) | ((*str & 0x3f)<<12) | ((str[1] & 0x3f)<<6) | (str[2] & 0x3f); |
172 | 0 | return 4; |
173 | 0 | } |
174 | | |
175 | 0 | UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t uc) { |
176 | 0 | return (((utf8proc_uint32_t)uc)-0xd800 > 0x07ff) && ((utf8proc_uint32_t)uc < 0x110000); |
177 | 0 | } |
178 | | |
179 | 0 | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) { |
180 | 0 | if (uc < 0x00) { |
181 | 0 | return 0; |
182 | 0 | } else if (uc < 0x80) { |
183 | 0 | dst[0] = (utf8proc_uint8_t) uc; |
184 | 0 | return 1; |
185 | 0 | } else if (uc < 0x800) { |
186 | 0 | dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6)); |
187 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
188 | 0 | return 2; |
189 | | // Note: we allow encoding 0xd800-0xdfff here, so as not to change |
190 | | // the API, however, these are actually invalid in UTF-8 |
191 | 0 | } else if (uc < 0x10000) { |
192 | 0 | dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12)); |
193 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F)); |
194 | 0 | dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
195 | 0 | return 3; |
196 | 0 | } else if (uc < 0x110000) { |
197 | 0 | dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18)); |
198 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F)); |
199 | 0 | dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F)); |
200 | 0 | dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
201 | 0 | return 4; |
202 | 0 | } else return 0; |
203 | 0 | } |
204 | | |
205 | | /* internal version used for inserting 0xff bytes between graphemes */ |
206 | 0 | static utf8proc_ssize_t charbound_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) { |
207 | 0 | if (uc < 0x00) { |
208 | 0 | if (uc == -1) { /* internal value used for grapheme breaks */ |
209 | 0 | dst[0] = (utf8proc_uint8_t)0xFF; |
210 | 0 | return 1; |
211 | 0 | } |
212 | 0 | return 0; |
213 | 0 | } else if (uc < 0x80) { |
214 | 0 | dst[0] = (utf8proc_uint8_t)uc; |
215 | 0 | return 1; |
216 | 0 | } else if (uc < 0x800) { |
217 | 0 | dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6)); |
218 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
219 | 0 | return 2; |
220 | 0 | } else if (uc < 0x10000) { |
221 | 0 | dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12)); |
222 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F)); |
223 | 0 | dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
224 | 0 | return 3; |
225 | 0 | } else if (uc < 0x110000) { |
226 | 0 | dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18)); |
227 | 0 | dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F)); |
228 | 0 | dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F)); |
229 | 0 | dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F)); |
230 | 0 | return 4; |
231 | 0 | } else return 0; |
232 | 0 | } |
233 | | |
234 | | /* internal "unsafe" version that does not check whether uc is in range */ |
235 | 11 | static const utf8proc_property_t *unsafe_get_property(utf8proc_int32_t uc) { |
236 | | /* ASSERT: uc >= 0 && uc < 0x110000 */ |
237 | 11 | return utf8proc_properties + ( |
238 | 11 | utf8proc_stage2table[ |
239 | 11 | utf8proc_stage1table[uc >> 8] + (uc & 0xFF) |
240 | 11 | ] |
241 | 11 | ); |
242 | 11 | } |
243 | | |
244 | 11 | UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t uc) { |
245 | 11 | return uc < 0 || uc >= 0x110000 ? utf8proc_properties : unsafe_get_property(uc); |
246 | 11 | } |
247 | | |
248 | | /* return whether there is a grapheme break between boundclasses lbc and tbc |
249 | | (according to the definition of extended grapheme clusters) |
250 | | |
251 | | Rule numbering refers to TR29 Version 29 (Unicode 9.0.0): |
252 | | http://www.unicode.org/reports/tr29/tr29-29.html |
253 | | |
254 | | CAVEATS: |
255 | | Please note that evaluation of GB10 (grapheme breaks between emoji zwj sequences) |
256 | | and GB 12/13 (regional indicator code points) require knowledge of previous characters |
257 | | and are thus not handled by this function. This may result in an incorrect break before |
258 | | an E_Modifier class codepoint and an incorrectly missing break between two |
259 | | REGIONAL_INDICATOR class code points if such support does not exist in the caller. |
260 | | |
261 | | See the special support in grapheme_break_extended, for required bookkeeping by the caller. |
262 | | */ |
263 | 7 | static utf8proc_bool grapheme_break_simple(int lbc, int tbc) { |
264 | 7 | return |
265 | 7 | (lbc == UTF8PROC_BOUNDCLASS_START) ? true : // GB1 |
266 | 7 | (lbc == UTF8PROC_BOUNDCLASS_CR && // GB3 |
267 | 3 | tbc == UTF8PROC_BOUNDCLASS_LF) ? false : // --- |
268 | 3 | (lbc >= UTF8PROC_BOUNDCLASS_CR && lbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB4 |
269 | 3 | (tbc >= UTF8PROC_BOUNDCLASS_CR && tbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB5 |
270 | 2 | (lbc == UTF8PROC_BOUNDCLASS_L && // GB6 |
271 | 1 | (tbc == UTF8PROC_BOUNDCLASS_L || // --- |
272 | 0 | tbc == UTF8PROC_BOUNDCLASS_V || // --- |
273 | 0 | tbc == UTF8PROC_BOUNDCLASS_LV || // --- |
274 | 0 | tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false : // --- |
275 | 1 | ((lbc == UTF8PROC_BOUNDCLASS_LV || // GB7 |
276 | 1 | lbc == UTF8PROC_BOUNDCLASS_V) && // --- |
277 | 1 | (tbc == UTF8PROC_BOUNDCLASS_V || // --- |
278 | 0 | tbc == UTF8PROC_BOUNDCLASS_T)) ? false : // --- |
279 | 1 | ((lbc == UTF8PROC_BOUNDCLASS_LVT || // GB8 |
280 | 1 | lbc == UTF8PROC_BOUNDCLASS_T) && // --- |
281 | 1 | tbc == UTF8PROC_BOUNDCLASS_T) ? false : // --- |
282 | 1 | (tbc == UTF8PROC_BOUNDCLASS_EXTEND || // GB9 |
283 | 1 | tbc == UTF8PROC_BOUNDCLASS_ZWJ || // --- |
284 | 1 | tbc == UTF8PROC_BOUNDCLASS_SPACINGMARK || // GB9a |
285 | 1 | lbc == UTF8PROC_BOUNDCLASS_PREPEND) ? false : // GB9b |
286 | 1 | (lbc == UTF8PROC_BOUNDCLASS_E_ZWG && // GB11 (requires additional handling below) |
287 | 1 | tbc == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) ? false : // ---- |
288 | 1 | (lbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR && // GB12/13 (requires additional handling below) |
289 | 1 | tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) ? false : // ---- |
290 | 1 | true; // GB999 |
291 | 7 | } |
292 | | |
293 | | utf8proc_bool grapheme_break_extended(int lbc, int tbc, utf8proc_int32_t *state) |
294 | 7 | { |
295 | 7 | int lbc_override = ((state && *state != UTF8PROC_BOUNDCLASS_START) |
296 | 7 | ? *state : lbc); |
297 | 7 | utf8proc_bool break_permitted = grapheme_break_simple(lbc_override, tbc); |
298 | 7 | if (state) { |
299 | | // Special support for GB 12/13 made possible by GB999. After two RI |
300 | | // class codepoints we want to force a break. Do this by resetting the |
301 | | // second RI's bound class to UTF8PROC_BOUNDCLASS_OTHER, to force a break |
302 | | // after that character according to GB999 (unless of course such a break is |
303 | | // forbidden by a different rule such as GB9). |
304 | 7 | if (*state == tbc && tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) |
305 | 0 | *state = UTF8PROC_BOUNDCLASS_OTHER; |
306 | | // Special support for GB11 (emoji extend* zwj / emoji) |
307 | 7 | else if (*state == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) { |
308 | 0 | if (tbc == UTF8PROC_BOUNDCLASS_EXTEND) // fold EXTEND codepoints into emoji |
309 | 0 | *state = UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC; |
310 | 0 | else if (tbc == UTF8PROC_BOUNDCLASS_ZWJ) |
311 | 0 | *state = UTF8PROC_BOUNDCLASS_E_ZWG; // state to record emoji+zwg combo |
312 | 0 | else |
313 | 0 | *state = tbc; |
314 | 0 | } |
315 | 7 | else |
316 | 7 | *state = tbc; |
317 | 7 | } |
318 | 7 | return break_permitted; |
319 | 7 | } |
320 | | |
321 | | UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful( |
322 | 0 | utf8proc_int32_t c1, utf8proc_int32_t c2, utf8proc_int32_t *state) { |
323 | |
|
324 | 0 | return grapheme_break_extended(utf8proc_get_property(c1)->boundclass, |
325 | 0 | utf8proc_get_property(c2)->boundclass, |
326 | 0 | state); |
327 | 0 | } |
328 | | |
329 | | |
330 | | UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break( |
331 | 0 | utf8proc_int32_t c1, utf8proc_int32_t c2) { |
332 | 0 | return utf8proc_grapheme_break_stateful(c1, c2, NULL); |
333 | 0 | } |
334 | | |
335 | | // from http://www.zedwood.com/article/cpp-utf8-char-to-codepoint |
336 | 11 | UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_codepoint(const char *u_input, int &sz) { |
337 | 11 | auto u = (const unsigned char *) u_input; |
338 | 11 | unsigned char u0 = u[0]; |
339 | 11 | if (u0<=127) { |
340 | 11 | sz = 1; |
341 | 11 | return u0; |
342 | 11 | } |
343 | 0 | unsigned char u1 = u[1]; |
344 | 0 | if (u0>=192 && u0<=223) { |
345 | 0 | sz = 2; |
346 | 0 | return (u0-192)*64 + (u1-128); |
347 | 0 | } |
348 | 0 | if (u[0]==0xed && (u[1] & 0xa0) == 0xa0) { |
349 | 0 | return -1; //code points, 0xd800 to 0xdfff |
350 | 0 | } |
351 | 0 | unsigned char u2 = u[2]; |
352 | 0 | if (u0>=224 && u0<=239) { |
353 | 0 | sz = 3; |
354 | 0 | return (u0-224)*4096 + (u1-128)*64 + (u2-128); |
355 | 0 | } |
356 | 0 | unsigned char u3 = u[3]; |
357 | 0 | if (u0>=240 && u0<=247) { |
358 | 0 | sz = 4; |
359 | 0 | return (u0-240)*262144 + (u1-128)*4096 + (u2-128)*64 + (u3-128); |
360 | 0 | } |
361 | 0 | return -1; |
362 | 0 | } |
363 | | |
364 | 0 | bool utf8proc_codepoint_to_utf8(int cp, int &sz, char *c) { |
365 | 0 | if (cp<=0x7F) { |
366 | 0 | sz = 1; |
367 | 0 | c[0] = cp; |
368 | 0 | } else if(cp<=0x7FF) { |
369 | 0 | sz = 2; |
370 | 0 | c[0] = (cp>>6)+192; |
371 | 0 | c[1] = (cp&63)+128; |
372 | 0 | } else if(0xd800<=cp && cp<=0xdfff) { |
373 | 0 | sz = -1; |
374 | | // invalid block of utf |
375 | 0 | return false; |
376 | 0 | } else if(cp<=0xFFFF) { |
377 | 0 | sz = 3; |
378 | 0 | c[0] = (cp>>12)+224; |
379 | 0 | c[1]= ((cp>>6)&63)+128; |
380 | 0 | c[2]=(cp&63)+128; |
381 | 0 | } else if(cp<=0x10FFFF) { |
382 | 0 | sz = 4; |
383 | 0 | c[0] = (cp>>18)+240; |
384 | 0 | c[1] = ((cp>>12)&63)+128; |
385 | 0 | c[2] = ((cp>>6)&63)+128; |
386 | 0 | c[3]=(cp&63)+128; |
387 | 0 | } else { |
388 | 0 | sz = -1; |
389 | 0 | return false; |
390 | 0 | } |
391 | 0 | return true; |
392 | 0 | } |
393 | | |
394 | 0 | int utf8proc_codepoint_length(int cp) { |
395 | 0 | if (cp<=0x7F) { |
396 | 0 | return 1; |
397 | 0 | } else if(cp<=0x7FF) { |
398 | 0 | return 2; |
399 | 0 | } else if(0xd800<=cp && cp<=0xdfff) { |
400 | 0 | return -1; |
401 | 0 | } else if(cp<=0xFFFF) { |
402 | 0 | return 3; |
403 | 0 | } else if(cp<=0x10FFFF) { |
404 | 0 | return 4; |
405 | 0 | } |
406 | 0 | return -1; |
407 | 0 | } |
408 | | |
409 | 4 | size_t utf8proc_next_grapheme(const char *s, size_t len, size_t cpos) { |
410 | 4 | int sz; |
411 | 4 | int boundclass = UTF8PROC_BOUNDCLASS_START; |
412 | 4 | int initial = utf8proc_get_property(utf8proc_codepoint(s + cpos, sz))->boundclass; |
413 | 4 | grapheme_break_extended(boundclass, initial, &boundclass); |
414 | 4 | while(true) { |
415 | 4 | cpos += sz; |
416 | 4 | if (cpos >= len) { |
417 | 1 | return cpos; |
418 | 1 | } |
419 | 3 | int next = utf8proc_get_property(utf8proc_codepoint(s + cpos, sz))->boundclass; |
420 | 3 | if (grapheme_break_extended(boundclass, next, &boundclass)) { |
421 | 3 | return cpos; |
422 | 3 | } |
423 | 3 | } |
424 | 4 | } |
425 | | |
426 | | static utf8proc_int32_t seqindex_decode_entry(const utf8proc_uint16_t **entry) |
427 | 0 | { |
428 | 0 | utf8proc_int32_t entry_cp = **entry; |
429 | 0 | if ((entry_cp & 0xF800) == 0xD800) { |
430 | 0 | *entry = *entry + 1; |
431 | 0 | entry_cp = ((entry_cp & 0x03FF) << 10) | (**entry & 0x03FF); |
432 | 0 | entry_cp += 0x10000; |
433 | 0 | } |
434 | 0 | return entry_cp; |
435 | 0 | } |
436 | | |
437 | | static utf8proc_int32_t seqindex_decode_index(const utf8proc_uint32_t seqindex) |
438 | 0 | { |
439 | 0 | const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex]; |
440 | 0 | return seqindex_decode_entry(&entry); |
441 | 0 | } |
442 | | |
443 | 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) { |
444 | 0 | utf8proc_ssize_t written = 0; |
445 | 0 | const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex & 0x1FFF]; |
446 | 0 | int len = seqindex >> 13; |
447 | 0 | if (len >= 7) { |
448 | 0 | len = *entry; |
449 | 0 | entry++; |
450 | 0 | } |
451 | 0 | for (; len >= 0; entry++, len--) { |
452 | 0 | utf8proc_int32_t entry_cp = seqindex_decode_entry(&entry); |
453 | 0 | utf8proc_int32_t *dst_ptr = dst ? dst + written : nullptr; |
454 | 0 | written += utf8proc_decompose_char(entry_cp, dst_ptr, |
455 | 0 | (bufsize > written) ? (bufsize - written) : 0, options, |
456 | 0 | last_boundclass); |
457 | 0 | if (written < 0) return UTF8PROC_ERROR_OVERFLOW; |
458 | 0 | } |
459 | 0 | return written; |
460 | 0 | } |
461 | | |
462 | | UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c) |
463 | 0 | { |
464 | 0 | utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_seqindex; |
465 | 0 | return cl != UINT16_MAX ? seqindex_decode_index(cl) : c; |
466 | 0 | } |
467 | | |
468 | | UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c) |
469 | 0 | { |
470 | 0 | utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_seqindex; |
471 | 0 | return cu != UINT16_MAX ? seqindex_decode_index(cu) : c; |
472 | 0 | } |
473 | | |
474 | | UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c) |
475 | 0 | { |
476 | 0 | utf8proc_int32_t cu = utf8proc_get_property(c)->titlecase_seqindex; |
477 | 0 | return cu != UINT16_MAX ? seqindex_decode_index(cu) : c; |
478 | 0 | } |
479 | | |
480 | | /* return a character width analogous to wcwidth (except portable and |
481 | | hopefully less buggy than most system wcwidth functions). */ |
482 | 0 | UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t c) { |
483 | 0 | return utf8proc_get_property(c)->charwidth; |
484 | 0 | } |
485 | | |
486 | 0 | UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t c) { |
487 | 0 | return (utf8proc_category_t)utf8proc_get_property(c)->category; |
488 | 0 | } |
489 | | |
490 | 0 | UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) { |
491 | 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"}; |
492 | 0 | return s[utf8proc_category(c)]; |
493 | 0 | } |
494 | | |
495 | | #define utf8proc_decompose_lump(replacement_uc) \ |
496 | 0 | return utf8proc_decompose_char((replacement_uc), dst, bufsize, \ |
497 | 0 | (utf8proc_option_t) (options & ~UTF8PROC_LUMP), last_boundclass) |
498 | | |
499 | 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) { |
500 | 0 | const utf8proc_property_t *property; |
501 | 0 | utf8proc_propval_t category; |
502 | 0 | utf8proc_int32_t hangul_sindex; |
503 | 0 | if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED; |
504 | 0 | property = unsafe_get_property(uc); |
505 | 0 | category = property->category; |
506 | 0 | hangul_sindex = uc - UTF8PROC_HANGUL_SBASE; |
507 | 0 | if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) { |
508 | 0 | if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) { |
509 | 0 | utf8proc_int32_t hangul_tindex; |
510 | 0 | if (bufsize >= 1) { |
511 | 0 | dst[0] = UTF8PROC_HANGUL_LBASE + |
512 | 0 | hangul_sindex / UTF8PROC_HANGUL_NCOUNT; |
513 | 0 | if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE + |
514 | 0 | (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT; |
515 | 0 | } |
516 | 0 | hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT; |
517 | 0 | if (!hangul_tindex) return 2; |
518 | 0 | if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex; |
519 | 0 | return 3; |
520 | 0 | } |
521 | 0 | } |
522 | 0 | if (options & UTF8PROC_REJECTNA) { |
523 | 0 | if (!category) return UTF8PROC_ERROR_NOTASSIGNED; |
524 | 0 | } |
525 | 0 | if (options & UTF8PROC_IGNORE) { |
526 | 0 | if (property->ignorable) return 0; |
527 | 0 | } |
528 | 0 | if (options & UTF8PROC_STRIPNA) { |
529 | 0 | if (!category) return 0; |
530 | 0 | } |
531 | 0 | if (options & UTF8PROC_LUMP) { |
532 | 0 | if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020); |
533 | 0 | if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8) |
534 | 0 | utf8proc_decompose_lump(0x0027); |
535 | 0 | if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212) |
536 | 0 | utf8proc_decompose_lump(0x002D); |
537 | 0 | if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F); |
538 | 0 | if (uc == 0x2236) utf8proc_decompose_lump(0x003A); |
539 | 0 | if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008) |
540 | 0 | utf8proc_decompose_lump(0x003C); |
541 | 0 | if (uc == 0x203A || uc == 0x232A || uc == 0x3009) |
542 | 0 | utf8proc_decompose_lump(0x003E); |
543 | 0 | if (uc == 0x2216) utf8proc_decompose_lump(0x005C); |
544 | 0 | if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303) |
545 | 0 | utf8proc_decompose_lump(0x005E); |
546 | 0 | if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD) |
547 | 0 | utf8proc_decompose_lump(0x005F); |
548 | 0 | if (uc == 0x02CB) utf8proc_decompose_lump(0x0060); |
549 | 0 | if (uc == 0x2223) utf8proc_decompose_lump(0x007C); |
550 | 0 | if (uc == 0x223C) utf8proc_decompose_lump(0x007E); |
551 | 0 | if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) { |
552 | 0 | if (category == UTF8PROC_CATEGORY_ZL || |
553 | 0 | category == UTF8PROC_CATEGORY_ZP) |
554 | 0 | utf8proc_decompose_lump(0x000A); |
555 | 0 | } |
556 | 0 | } |
557 | 0 | if (options & UTF8PROC_STRIPMARK) { |
558 | 0 | if (category == UTF8PROC_CATEGORY_MN || |
559 | 0 | category == UTF8PROC_CATEGORY_MC || |
560 | 0 | category == UTF8PROC_CATEGORY_ME) return 0; |
561 | 0 | } |
562 | 0 | if (options & UTF8PROC_CASEFOLD) { |
563 | 0 | if (property->casefold_seqindex != UINT16_MAX) { |
564 | 0 | return seqindex_write_char_decomposed(property->casefold_seqindex, dst, bufsize, options, last_boundclass); |
565 | 0 | } |
566 | 0 | } |
567 | 0 | if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) { |
568 | 0 | if (property->decomp_seqindex != UINT16_MAX && |
569 | 0 | (!property->decomp_type || (options & UTF8PROC_COMPAT))) { |
570 | 0 | return seqindex_write_char_decomposed(property->decomp_seqindex, dst, bufsize, options, last_boundclass); |
571 | 0 | } |
572 | 0 | } |
573 | 0 | if (options & UTF8PROC_CHARBOUND) { |
574 | 0 | utf8proc_bool boundary; |
575 | 0 | int tbc = property->boundclass; |
576 | 0 | boundary = grapheme_break_extended(*last_boundclass, tbc, last_boundclass); |
577 | 0 | if (boundary) { |
578 | 0 | if (bufsize >= 1) dst[0] = -1; /* sentinel value for grapheme break */ |
579 | 0 | if (bufsize >= 2) dst[1] = uc; |
580 | 0 | return 2; |
581 | 0 | } |
582 | 0 | } |
583 | 0 | if (bufsize >= 1) *dst = uc; |
584 | 0 | return 1; |
585 | 0 | } |
586 | | |
587 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose( |
588 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, |
589 | | utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options |
590 | 0 | ) { |
591 | 0 | return utf8proc_decompose_custom(str, strlen, buffer, bufsize, options, NULL, NULL); |
592 | 0 | } |
593 | | |
594 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom( |
595 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, |
596 | | utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options, |
597 | | utf8proc_custom_func custom_func, void *custom_data |
598 | 0 | ) { |
599 | | /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */ |
600 | 0 | utf8proc_ssize_t wpos = 0; |
601 | 0 | if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE)) |
602 | 0 | return UTF8PROC_ERROR_INVALIDOPTS; |
603 | 0 | if ((options & UTF8PROC_STRIPMARK) && |
604 | 0 | !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE)) |
605 | 0 | return UTF8PROC_ERROR_INVALIDOPTS; |
606 | 0 | { |
607 | 0 | utf8proc_int32_t uc; |
608 | 0 | utf8proc_ssize_t rpos = 0; |
609 | 0 | utf8proc_ssize_t decomp_result; |
610 | 0 | int boundclass = UTF8PROC_BOUNDCLASS_START; |
611 | 0 | while (1) { |
612 | 0 | if (options & UTF8PROC_NULLTERM) { |
613 | 0 | rpos += utf8proc_iterate(str + rpos, -1, &uc); |
614 | | /* checking of return value is not necessary, |
615 | | as 'uc' is < 0 in case of error */ |
616 | 0 | if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8; |
617 | 0 | if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW; |
618 | 0 | if (uc == 0) break; |
619 | 0 | } else { |
620 | 0 | if (rpos >= strlen) break; |
621 | 0 | rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc); |
622 | 0 | if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8; |
623 | 0 | } |
624 | 0 | if (custom_func != NULL) { |
625 | 0 | uc = custom_func(uc, custom_data); /* user-specified custom mapping */ |
626 | 0 | } |
627 | 0 | utf8proc_int32_t *target_buffer = buffer ? buffer + wpos : nullptr; |
628 | 0 | decomp_result = utf8proc_decompose_char( |
629 | 0 | uc, target_buffer, (bufsize > wpos) ? (bufsize - wpos) : 0, options, |
630 | 0 | &boundclass |
631 | 0 | ); |
632 | 0 | if (decomp_result < 0) return decomp_result; |
633 | 0 | wpos += decomp_result; |
634 | | /* prohibiting integer overflows due to too long strings: */ |
635 | 0 | if (wpos < 0 || |
636 | 0 | wpos > (utf8proc_ssize_t)(SSIZE_MAX/sizeof(utf8proc_int32_t)/2)) |
637 | 0 | return UTF8PROC_ERROR_OVERFLOW; |
638 | 0 | } |
639 | 0 | } |
640 | 0 | if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) { |
641 | 0 | utf8proc_ssize_t pos = 0; |
642 | 0 | while (pos < wpos-1) { |
643 | 0 | utf8proc_int32_t uc1, uc2; |
644 | 0 | const utf8proc_property_t *property1, *property2; |
645 | 0 | uc1 = buffer[pos]; |
646 | 0 | uc2 = buffer[pos+1]; |
647 | 0 | property1 = unsafe_get_property(uc1); |
648 | 0 | property2 = unsafe_get_property(uc2); |
649 | 0 | if (property1->combining_class > property2->combining_class && |
650 | 0 | property2->combining_class > 0) { |
651 | 0 | buffer[pos] = uc2; |
652 | 0 | buffer[pos+1] = uc1; |
653 | 0 | if (pos > 0) pos--; else pos++; |
654 | 0 | } else { |
655 | 0 | pos++; |
656 | 0 | } |
657 | 0 | } |
658 | 0 | } |
659 | 0 | return wpos; |
660 | 0 | } |
661 | | |
662 | 0 | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) { |
663 | | /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored */ |
664 | 0 | if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) { |
665 | 0 | utf8proc_ssize_t rpos; |
666 | 0 | utf8proc_ssize_t wpos = 0; |
667 | 0 | utf8proc_int32_t uc; |
668 | 0 | for (rpos = 0; rpos < length; rpos++) { |
669 | 0 | uc = buffer[rpos]; |
670 | 0 | if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++; |
671 | 0 | if (uc == 0x000A || uc == 0x000D || uc == 0x0085 || |
672 | 0 | ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) { |
673 | 0 | if (options & UTF8PROC_NLF2LS) { |
674 | 0 | if (options & UTF8PROC_NLF2PS) { |
675 | 0 | buffer[wpos++] = 0x000A; |
676 | 0 | } else { |
677 | 0 | buffer[wpos++] = 0x2028; |
678 | 0 | } |
679 | 0 | } else { |
680 | 0 | if (options & UTF8PROC_NLF2PS) { |
681 | 0 | buffer[wpos++] = 0x2029; |
682 | 0 | } else { |
683 | 0 | buffer[wpos++] = 0x0020; |
684 | 0 | } |
685 | 0 | } |
686 | 0 | } else if ((options & UTF8PROC_STRIPCC) && |
687 | 0 | (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) { |
688 | 0 | if (uc == 0x0009) buffer[wpos++] = 0x0020; |
689 | 0 | } else { |
690 | 0 | buffer[wpos++] = uc; |
691 | 0 | } |
692 | 0 | } |
693 | 0 | length = wpos; |
694 | 0 | } |
695 | 0 | if (options & UTF8PROC_COMPOSE) { |
696 | 0 | utf8proc_int32_t *starter = NULL; |
697 | 0 | utf8proc_int32_t current_char; |
698 | 0 | const utf8proc_property_t *starter_property = NULL, *current_property; |
699 | 0 | utf8proc_propval_t max_combining_class = -1; |
700 | 0 | utf8proc_ssize_t rpos; |
701 | 0 | utf8proc_ssize_t wpos = 0; |
702 | 0 | utf8proc_int32_t composition; |
703 | 0 | for (rpos = 0; rpos < length; rpos++) { |
704 | 0 | current_char = buffer[rpos]; |
705 | 0 | current_property = unsafe_get_property(current_char); |
706 | 0 | if (starter && current_property->combining_class > max_combining_class) { |
707 | | /* combination perhaps possible */ |
708 | 0 | utf8proc_int32_t hangul_lindex; |
709 | 0 | utf8proc_int32_t hangul_sindex; |
710 | 0 | hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE; |
711 | 0 | if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) { |
712 | 0 | utf8proc_int32_t hangul_vindex; |
713 | 0 | hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE; |
714 | 0 | if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) { |
715 | 0 | *starter = UTF8PROC_HANGUL_SBASE + |
716 | 0 | (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) * |
717 | 0 | UTF8PROC_HANGUL_TCOUNT; |
718 | 0 | starter_property = NULL; |
719 | 0 | continue; |
720 | 0 | } |
721 | 0 | } |
722 | 0 | hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE; |
723 | 0 | if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT && |
724 | 0 | (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) { |
725 | 0 | utf8proc_int32_t hangul_tindex; |
726 | 0 | hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE; |
727 | 0 | if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) { |
728 | 0 | *starter += hangul_tindex; |
729 | 0 | starter_property = NULL; |
730 | 0 | continue; |
731 | 0 | } |
732 | 0 | } |
733 | 0 | if (!starter_property) { |
734 | 0 | starter_property = unsafe_get_property(*starter); |
735 | 0 | } |
736 | 0 | if (starter_property->comb_index < 0x8000 && |
737 | 0 | current_property->comb_index != UINT16_MAX && |
738 | 0 | current_property->comb_index >= 0x8000) { |
739 | 0 | int sidx = starter_property->comb_index; |
740 | 0 | int idx = current_property->comb_index & 0x3FFF; |
741 | 0 | if (idx >= utf8proc_combinations[sidx] && idx <= utf8proc_combinations[sidx + 1] ) { |
742 | 0 | idx += sidx + 2 - utf8proc_combinations[sidx]; |
743 | 0 | if (current_property->comb_index & 0x4000) { |
744 | 0 | composition = (utf8proc_combinations[idx] << 16) | utf8proc_combinations[idx+1]; |
745 | 0 | } else |
746 | 0 | composition = utf8proc_combinations[idx]; |
747 | |
|
748 | 0 | if (composition > 0 && (!(options & UTF8PROC_STABLE) || |
749 | 0 | !(unsafe_get_property(composition)->comp_exclusion))) { |
750 | 0 | *starter = composition; |
751 | 0 | starter_property = NULL; |
752 | 0 | continue; |
753 | 0 | } |
754 | 0 | } |
755 | 0 | } |
756 | 0 | } |
757 | 0 | buffer[wpos] = current_char; |
758 | 0 | if (current_property->combining_class) { |
759 | 0 | if (current_property->combining_class > max_combining_class) { |
760 | 0 | max_combining_class = current_property->combining_class; |
761 | 0 | } |
762 | 0 | } else { |
763 | 0 | starter = buffer + wpos; |
764 | 0 | starter_property = NULL; |
765 | 0 | max_combining_class = -1; |
766 | 0 | } |
767 | 0 | wpos++; |
768 | 0 | } |
769 | 0 | length = wpos; |
770 | 0 | } |
771 | 0 | return length; |
772 | 0 | } |
773 | | |
774 | 0 | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) { |
775 | | /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored |
776 | | ASSERT: 'buffer' has one spare byte of free space at the end! */ |
777 | 0 | length = utf8proc_normalize_utf32(buffer, length, options); |
778 | 0 | if (length < 0) return length; |
779 | 0 | { |
780 | 0 | utf8proc_ssize_t rpos, wpos = 0; |
781 | 0 | utf8proc_int32_t uc; |
782 | 0 | if (options & UTF8PROC_CHARBOUND) { |
783 | 0 | for (rpos = 0; rpos < length; rpos++) { |
784 | 0 | uc = buffer[rpos]; |
785 | 0 | wpos += charbound_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos); |
786 | 0 | } |
787 | 0 | } else { |
788 | 0 | for (rpos = 0; rpos < length; rpos++) { |
789 | 0 | uc = buffer[rpos]; |
790 | 0 | wpos += utf8proc_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos); |
791 | 0 | } |
792 | 0 | } |
793 | 0 | ((utf8proc_uint8_t *)buffer)[wpos] = 0; |
794 | 0 | return wpos; |
795 | 0 | } |
796 | 0 | } |
797 | | |
798 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map( |
799 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options |
800 | 0 | ) { |
801 | 0 | return utf8proc_map_custom(str, strlen, dstptr, options, NULL, NULL); |
802 | 0 | } |
803 | | |
804 | | UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom( |
805 | | const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options, |
806 | | utf8proc_custom_func custom_func, void *custom_data |
807 | 0 | ) { |
808 | 0 | utf8proc_int32_t *buffer; |
809 | 0 | utf8proc_ssize_t result; |
810 | 0 | *dstptr = NULL; |
811 | 0 | result = utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data); |
812 | 0 | if (result < 0) return result; |
813 | 0 | buffer = (utf8proc_int32_t *) malloc(result * sizeof(utf8proc_int32_t) + 1); |
814 | 0 | if (!buffer) return UTF8PROC_ERROR_NOMEM; |
815 | 0 | result = utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data); |
816 | 0 | if (result < 0) { |
817 | 0 | free(buffer); |
818 | 0 | return result; |
819 | 0 | } |
820 | 0 | result = utf8proc_reencode(buffer, result, options); |
821 | 0 | if (result < 0) { |
822 | 0 | free(buffer); |
823 | 0 | return result; |
824 | 0 | } |
825 | 0 | { |
826 | 0 | utf8proc_int32_t *newptr; |
827 | 0 | newptr = (utf8proc_int32_t *) realloc(buffer, (size_t)result+1); |
828 | 0 | if (newptr) buffer = newptr; |
829 | 0 | } |
830 | 0 | *dstptr = (utf8proc_uint8_t *)buffer; |
831 | 0 | return result; |
832 | 0 | } |
833 | | |
834 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str, utf8proc_ssize_t len) { |
835 | 0 | utf8proc_uint8_t *retval; |
836 | 0 | utf8proc_map(str, len, &retval, (utf8proc_option_t)(UTF8PROC_STABLE | |
837 | 0 | UTF8PROC_DECOMPOSE)); |
838 | 0 | return retval; |
839 | 0 | } |
840 | | |
841 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str, utf8proc_ssize_t len) { |
842 | 0 | utf8proc_uint8_t *retval; |
843 | 0 | utf8proc_map(str, len, &retval, (utf8proc_option_t)(UTF8PROC_STABLE | |
844 | 0 | UTF8PROC_COMPOSE)); |
845 | 0 | return retval; |
846 | 0 | } |
847 | | |
848 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_remove_accents(const utf8proc_uint8_t *str, utf8proc_ssize_t len) { |
849 | 0 | utf8proc_uint8_t *retval; |
850 | 0 | utf8proc_map(str, len, &retval, (utf8proc_option_t)(UTF8PROC_STABLE | |
851 | 0 | UTF8PROC_COMPOSE | UTF8PROC_STRIPMARK)); |
852 | 0 | return retval; |
853 | 0 | } |
854 | | |
855 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str, utf8proc_ssize_t len) { |
856 | 0 | utf8proc_uint8_t *retval; |
857 | 0 | utf8proc_map(str, len, &retval, (utf8proc_option_t)(UTF8PROC_STABLE | |
858 | 0 | UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT)); |
859 | 0 | return retval; |
860 | 0 | } |
861 | | |
862 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str, utf8proc_ssize_t len) { |
863 | 0 | utf8proc_uint8_t *retval; |
864 | 0 | utf8proc_map(str, len, &retval, (utf8proc_option_t)(UTF8PROC_STABLE | |
865 | 0 | UTF8PROC_COMPOSE | UTF8PROC_COMPAT)); |
866 | 0 | return retval; |
867 | 0 | } |
868 | | |
869 | 0 | UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str, utf8proc_ssize_t len) { |
870 | 0 | utf8proc_uint8_t *retval; |
871 | 0 | utf8proc_map(str, len, &retval, (utf8proc_option_t)(UTF8PROC_STABLE | |
872 | 0 | UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_CASEFOLD | UTF8PROC_IGNORE)); |
873 | 0 | return retval; |
874 | 0 | } |
875 | | |
876 | | } |