utf8proc
view utf8proc.c @ 9:951e73a98021
Version 1.1.3
- Added a function utf8proc_version returning a string containing the version number of the library.
- Included a target libutf8proc.dylib for MacOSX.
- PostgreSQL 8.3 compatibility (use of SET_VARSIZE macro)
- Added a function utf8proc_version returning a string containing the version number of the library.
- Included a target libutf8proc.dylib for MacOSX.
- PostgreSQL 8.3 compatibility (use of SET_VARSIZE macro)
| author | jbe |
|---|---|
| date | Fri May 01 12:00:00 2009 +0200 (2009-05-01) |
| parents | fcfd8c836c64 |
| children | 00d2bcbdc945 |
line source
1 /*
2 * Copyright (c) 2006-2007 Jan Behrens, FlexiGuided GmbH, Berlin
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
23 /*
24 * This library contains derived data from a modified version of the
25 * Unicode data files.
26 *
27 * The original data files are available at
28 * http://www.unicode.org/Public/UNIDATA/
29 *
30 * Please notice the copyright statement in the file "utf8proc_data.c".
31 */
34 /*
35 * File name: utf8proc.c
36 * Version: 1.1.1
37 * Last changed: 2007-07-22
38 *
39 * Description:
40 * Implementation of libutf8proc.
41 */
44 #include "utf8proc.h"
45 #include "utf8proc_data.c"
48 const int8_t utf8proc_utf8class[256] = {
49 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
53 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
54 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
55 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
62 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
63 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
64 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
66 #define UTF8PROC_HANGUL_SBASE 0xAC00
67 #define UTF8PROC_HANGUL_LBASE 0x1100
68 #define UTF8PROC_HANGUL_VBASE 0x1161
69 #define UTF8PROC_HANGUL_TBASE 0x11A7
70 #define UTF8PROC_HANGUL_LCOUNT 19
71 #define UTF8PROC_HANGUL_VCOUNT 21
72 #define UTF8PROC_HANGUL_TCOUNT 28
73 #define UTF8PROC_HANGUL_NCOUNT 588
74 #define UTF8PROC_HANGUL_SCOUNT 11172
75 // END is exclusive
76 #define UTF8PROC_HANGUL_L_START 0x1100
77 #define UTF8PROC_HANGUL_L_END 0x115A
78 #define UTF8PROC_HANGUL_L_FILLER 0x115F
79 #define UTF8PROC_HANGUL_V_START 0x1160
80 #define UTF8PROC_HANGUL_V_END 0x11A3
81 #define UTF8PROC_HANGUL_T_START 0x11A8
82 #define UTF8PROC_HANGUL_T_END 0x11FA
83 #define UTF8PROC_HANGUL_S_START 0xAC00
84 #define UTF8PROC_HANGUL_S_END 0xD7A4
87 #define UTF8PROC_BOUNDCLASS_START 0
88 #define UTF8PROC_BOUNDCLASS_OTHER 1
89 #define UTF8PROC_BOUNDCLASS_CR 2
90 #define UTF8PROC_BOUNDCLASS_LF 3
91 #define UTF8PROC_BOUNDCLASS_CONTROL 4
92 #define UTF8PROC_BOUNDCLASS_EXTEND 5
93 #define UTF8PROC_BOUNDCLASS_L 6
94 #define UTF8PROC_BOUNDCLASS_V 7
95 #define UTF8PROC_BOUNDCLASS_T 8
96 #define UTF8PROC_BOUNDCLASS_LV 9
97 #define UTF8PROC_BOUNDCLASS_LVT 10
100 const char *utf8proc_version(void) {
101 return "1.1.3";
102 }
104 const char *utf8proc_errmsg(ssize_t errcode) {
105 switch (errcode) {
106 case UTF8PROC_ERROR_NOMEM:
107 return "Memory for processing UTF-8 data could not be allocated.";
108 case UTF8PROC_ERROR_OVERFLOW:
109 return "UTF-8 string is too long to be processed.";
110 case UTF8PROC_ERROR_INVALIDUTF8:
111 return "Invalid UTF-8 string";
112 case UTF8PROC_ERROR_NOTASSIGNED:
113 return "Unassigned Unicode code point found in UTF-8 string.";
114 case UTF8PROC_ERROR_INVALIDOPTS:
115 return "Invalid options for UTF-8 processing chosen.";
116 default:
117 return "An unknown error occured while processing UTF-8 data.";
118 }
119 }
121 ssize_t utf8proc_iterate(
122 const uint8_t *str, ssize_t strlen, int32_t *dst
123 ) {
124 int length;
125 int i;
126 int32_t uc = -1;
127 *dst = -1;
128 if (!strlen) return 0;
129 length = utf8proc_utf8class[str[0]];
130 if (!length) return UTF8PROC_ERROR_INVALIDUTF8;
131 if (strlen >= 0 && length > strlen) return UTF8PROC_ERROR_INVALIDUTF8;
132 for (i=1; i<length; i++) {
133 if ((str[i] & 0xC0) != 0x80) return UTF8PROC_ERROR_INVALIDUTF8;
134 }
135 switch (length) {
136 case 1:
137 uc = str[0];
138 break;
139 case 2:
140 uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
141 if (uc < 0x80) uc = -1;
142 break;
143 case 3:
144 uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
145 + (str[2] & 0x3F);
146 if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
147 (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
148 break;
149 case 4:
150 uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
151 + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
152 if (uc < 0x10000 || uc >= 0x110000) uc = -1;
153 break;
154 }
155 if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE))
156 return UTF8PROC_ERROR_INVALIDUTF8;
157 *dst = uc;
158 return length;
159 }
161 bool utf8proc_codepoint_valid(int32_t uc) {
162 if (uc < 0 || uc >= 0x110000 ||
163 ((uc & 0xFFFF) >= 0xFFFE) || (uc >= 0xD800 && uc < 0xE000) ||
164 (uc >= 0xFDD0 && uc < 0xFDF0)) return false;
165 else return true;
166 }
168 ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst) {
169 if (uc < 0x00) {
170 return 0;
171 } else if (uc < 0x80) {
172 dst[0] = uc;
173 return 1;
174 } else if (uc < 0x800) {
175 dst[0] = 0xC0 + (uc >> 6);
176 dst[1] = 0x80 + (uc & 0x3F);
177 return 2;
178 } else if (uc == 0xFFFF) {
179 dst[0] = 0xFF;
180 return 1;
181 } else if (uc == 0xFFFE) {
182 dst[0] = 0xFE;
183 return 1;
184 } else if (uc < 0x10000) {
185 dst[0] = 0xE0 + (uc >> 12);
186 dst[1] = 0x80 + ((uc >> 6) & 0x3F);
187 dst[2] = 0x80 + (uc & 0x3F);
188 return 3;
189 } else if (uc < 0x110000) {
190 dst[0] = 0xF0 + (uc >> 18);
191 dst[1] = 0x80 + ((uc >> 12) & 0x3F);
192 dst[2] = 0x80 + ((uc >> 6) & 0x3F);
193 dst[3] = 0x80 + (uc & 0x3F);
194 return 4;
195 } else return 0;
196 }
198 const utf8proc_property_t *utf8proc_get_property(int32_t uc) {
199 // ASSERT: uc >= 0 && uc < 0x110000
200 return utf8proc_properties + (
201 utf8proc_stage2table[
202 utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
203 ]
204 );
205 }
207 #define utf8proc_decompose_lump(replacement_uc) \
208 return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
209 options & ~UTF8PROC_LUMP, last_boundclass)
211 ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize,
212 int options, int *last_boundclass) {
213 // ASSERT: uc >= 0 && uc < 0x110000
214 const utf8proc_property_t *property;
215 utf8proc_propval_t category;
216 int32_t hangul_sindex;
217 property = utf8proc_get_property(uc);
218 category = property->category;
219 hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
220 if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
221 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
222 int32_t hangul_tindex;
223 if (bufsize >= 1) {
224 dst[0] = UTF8PROC_HANGUL_LBASE +
225 hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
226 if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
227 (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
228 }
229 hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
230 if (!hangul_tindex) return 2;
231 if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
232 return 3;
233 }
234 }
235 if (options & UTF8PROC_REJECTNA) {
236 if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
237 }
238 if (options & UTF8PROC_IGNORE) {
239 if (property->ignorable) return 0;
240 }
241 if (options & UTF8PROC_LUMP) {
242 if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
243 if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
244 utf8proc_decompose_lump(0x0027);
245 if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
246 utf8proc_decompose_lump(0x002D);
247 if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
248 if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
249 if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
250 utf8proc_decompose_lump(0x003C);
251 if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
252 utf8proc_decompose_lump(0x003E);
253 if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
254 if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
255 utf8proc_decompose_lump(0x005E);
256 if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
257 utf8proc_decompose_lump(0x005F);
258 if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
259 if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
260 if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
261 if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
262 if (category == UTF8PROC_CATEGORY_ZL ||
263 category == UTF8PROC_CATEGORY_ZP)
264 utf8proc_decompose_lump(0x000A);
265 }
266 }
267 if (options & UTF8PROC_STRIPMARK) {
268 if (category == UTF8PROC_CATEGORY_MN ||
269 category == UTF8PROC_CATEGORY_MC ||
270 category == UTF8PROC_CATEGORY_ME) return 0;
271 }
272 if (options & UTF8PROC_CASEFOLD) {
273 if (property->casefold_mapping) {
274 const int32_t *casefold_entry;
275 ssize_t written = 0;
276 for (casefold_entry = property->casefold_mapping;
277 *casefold_entry >= 0; casefold_entry++) {
278 written += utf8proc_decompose_char(*casefold_entry, dst+written,
279 (bufsize > written) ? (bufsize - written) : 0, options,
280 last_boundclass);
281 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
282 }
283 return written;
284 }
285 }
286 if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
287 if (property->decomp_mapping &&
288 (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
289 const int32_t *decomp_entry;
290 ssize_t written = 0;
291 for (decomp_entry = property->decomp_mapping;
292 *decomp_entry >= 0; decomp_entry++) {
293 written += utf8proc_decompose_char(*decomp_entry, dst+written,
294 (bufsize > written) ? (bufsize - written) : 0, options,
295 last_boundclass);
296 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
297 }
298 return written;
299 }
300 }
301 if (options & UTF8PROC_CHARBOUND) {
302 bool boundary;
303 int tbc, lbc;
304 tbc =
305 (uc == 0x000D) ? UTF8PROC_BOUNDCLASS_CR :
306 (uc == 0x000A) ? UTF8PROC_BOUNDCLASS_LF :
307 ((category == UTF8PROC_CATEGORY_ZL ||
308 category == UTF8PROC_CATEGORY_ZP ||
309 category == UTF8PROC_CATEGORY_CC ||
310 category == UTF8PROC_CATEGORY_CF) &&
311 !(uc == 0x200C || uc == 0x200D)) ? UTF8PROC_BOUNDCLASS_CONTROL :
312 property->extend ? UTF8PROC_BOUNDCLASS_EXTEND :
313 ((uc >= UTF8PROC_HANGUL_L_START && uc < UTF8PROC_HANGUL_L_END) ||
314 uc == UTF8PROC_HANGUL_L_FILLER) ? UTF8PROC_BOUNDCLASS_L :
315 (uc >= UTF8PROC_HANGUL_V_START && uc < UTF8PROC_HANGUL_V_END) ?
316 UTF8PROC_BOUNDCLASS_V :
317 (uc >= UTF8PROC_HANGUL_T_START && uc < UTF8PROC_HANGUL_T_END) ?
318 UTF8PROC_BOUNDCLASS_T :
319 (uc >= UTF8PROC_HANGUL_S_START && uc < UTF8PROC_HANGUL_S_END) ? (
320 ((uc-UTF8PROC_HANGUL_SBASE) % UTF8PROC_HANGUL_TCOUNT == 0) ?
321 UTF8PROC_BOUNDCLASS_LV : UTF8PROC_BOUNDCLASS_LVT
322 ) :
323 UTF8PROC_BOUNDCLASS_OTHER;
324 lbc = *last_boundclass;
325 boundary =
326 (tbc == UTF8PROC_BOUNDCLASS_EXTEND) ? false :
327 (lbc == UTF8PROC_BOUNDCLASS_START) ? true :
328 (lbc == UTF8PROC_BOUNDCLASS_CR &&
329 tbc == UTF8PROC_BOUNDCLASS_LF) ? false :
330 (lbc == UTF8PROC_BOUNDCLASS_CONTROL) ? true :
331 (tbc == UTF8PROC_BOUNDCLASS_CONTROL) ? true :
332 (lbc == UTF8PROC_BOUNDCLASS_L &&
333 (tbc == UTF8PROC_BOUNDCLASS_L ||
334 tbc == UTF8PROC_BOUNDCLASS_V ||
335 tbc == UTF8PROC_BOUNDCLASS_LV ||
336 tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false :
337 ((lbc == UTF8PROC_BOUNDCLASS_LV ||
338 lbc == UTF8PROC_BOUNDCLASS_V) &&
339 (tbc == UTF8PROC_BOUNDCLASS_V ||
340 tbc == UTF8PROC_BOUNDCLASS_T)) ? false :
341 ((lbc == UTF8PROC_BOUNDCLASS_LVT ||
342 lbc == UTF8PROC_BOUNDCLASS_T) &&
343 tbc == UTF8PROC_BOUNDCLASS_T) ? false :
344 true;
345 *last_boundclass = tbc;
346 if (boundary) {
347 if (bufsize >= 1) dst[0] = 0xFFFF;
348 if (bufsize >= 2) dst[1] = uc;
349 return 2;
350 }
351 }
352 if (bufsize >= 1) *dst = uc;
353 return 1;
354 }
356 ssize_t utf8proc_decompose(
357 const uint8_t *str, ssize_t strlen,
358 int32_t *buffer, ssize_t bufsize, int options
359 ) {
360 // strlen will be ignored, if UTF8PROC_NULLTERM is set in options
361 ssize_t wpos = 0;
362 if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
363 return UTF8PROC_ERROR_INVALIDOPTS;
364 if ((options & UTF8PROC_STRIPMARK) &&
365 !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
366 return UTF8PROC_ERROR_INVALIDOPTS;
367 {
368 int32_t uc;
369 ssize_t rpos = 0;
370 ssize_t decomp_result;
371 int boundclass = UTF8PROC_BOUNDCLASS_START;
372 while (1) {
373 if (options & UTF8PROC_NULLTERM) {
374 rpos += utf8proc_iterate(str + rpos, -1, &uc);
375 // checking of return value is not neccessary,
376 // as 'uc' is < 0 in case of error
377 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
378 if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
379 if (uc == 0) break;
380 } else {
381 if (rpos >= strlen) break;
382 rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
383 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
384 }
385 decomp_result = utf8proc_decompose_char(
386 uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
387 &boundclass
388 );
389 if (decomp_result < 0) return decomp_result;
390 wpos += decomp_result;
391 // prohibiting integer overflows due to too long strings:
392 if (wpos < 0 || wpos > SSIZE_MAX/sizeof(int32_t)/2)
393 return UTF8PROC_ERROR_OVERFLOW;
394 }
395 }
396 if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
397 ssize_t pos = 0;
398 while (pos < wpos-1) {
399 int32_t uc1, uc2;
400 const utf8proc_property_t *property1, *property2;
401 uc1 = buffer[pos];
402 uc2 = buffer[pos+1];
403 property1 = utf8proc_get_property(uc1);
404 property2 = utf8proc_get_property(uc2);
405 if (property1->combining_class > property2->combining_class &&
406 property2->combining_class > 0) {
407 buffer[pos] = uc2;
408 buffer[pos+1] = uc1;
409 if (pos > 0) pos--; else pos++;
410 } else {
411 pos++;
412 }
413 }
414 }
415 return wpos;
416 }
418 ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options) {
419 // UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
420 // ASSERT: 'buffer' has one spare byte of free space at the end!
421 if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
422 ssize_t rpos;
423 ssize_t wpos = 0;
424 int32_t uc;
425 for (rpos = 0; rpos < length; rpos++) {
426 uc = buffer[rpos];
427 if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
428 if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
429 ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
430 if (options & UTF8PROC_NLF2LS) {
431 if (options & UTF8PROC_NLF2PS) {
432 buffer[wpos++] = 0x000A;
433 } else {
434 buffer[wpos++] = 0x2028;
435 }
436 } else {
437 if (options & UTF8PROC_NLF2PS) {
438 buffer[wpos++] = 0x2029;
439 } else {
440 buffer[wpos++] = 0x0020;
441 }
442 }
443 } else if ((options & UTF8PROC_STRIPCC) &&
444 (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
445 if (uc == 0x0009) buffer[wpos++] = 0x0020;
446 } else {
447 buffer[wpos++] = uc;
448 }
449 }
450 length = wpos;
451 }
452 if (options & UTF8PROC_COMPOSE) {
453 int32_t *starter = NULL;
454 int32_t current_char;
455 const utf8proc_property_t *starter_property = NULL, *current_property;
456 utf8proc_propval_t max_combining_class = -1;
457 ssize_t rpos;
458 ssize_t wpos = 0;
459 int32_t composition;
460 for (rpos = 0; rpos < length; rpos++) {
461 current_char = buffer[rpos];
462 current_property = utf8proc_get_property(current_char);
463 if (starter && current_property->combining_class > max_combining_class) {
464 // combination perhaps possible
465 int32_t hangul_lindex;
466 int32_t hangul_sindex;
467 hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
468 if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
469 int32_t hangul_vindex;
470 hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
471 if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
472 *starter = UTF8PROC_HANGUL_SBASE +
473 (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
474 UTF8PROC_HANGUL_TCOUNT;
475 starter_property = NULL;
476 continue;
477 }
478 }
479 hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
480 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
481 (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
482 int32_t hangul_tindex;
483 hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
484 if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
485 *starter += hangul_tindex;
486 starter_property = NULL;
487 continue;
488 }
489 }
490 if (!starter_property) {
491 starter_property = utf8proc_get_property(*starter);
492 }
493 if (starter_property->comb1st_index >= 0 &&
494 current_property->comb2nd_index >= 0) {
495 composition = utf8proc_combinations[
496 starter_property->comb1st_index +
497 current_property->comb2nd_index
498 ];
499 if (composition >= 0 && (!(options & UTF8PROC_STABLE) ||
500 !(utf8proc_get_property(composition)->comp_exclusion))) {
501 *starter = composition;
502 starter_property = NULL;
503 continue;
504 }
505 }
506 }
507 buffer[wpos] = current_char;
508 if (current_property->combining_class) {
509 if (current_property->combining_class > max_combining_class) {
510 max_combining_class = current_property->combining_class;
511 }
512 } else {
513 starter = buffer + wpos;
514 starter_property = NULL;
515 max_combining_class = -1;
516 }
517 wpos++;
518 }
519 length = wpos;
520 }
521 {
522 ssize_t rpos, wpos = 0;
523 int32_t uc;
524 for (rpos = 0; rpos < length; rpos++) {
525 uc = buffer[rpos];
526 wpos += utf8proc_encode_char(uc, ((uint8_t *)buffer) + wpos);
527 }
528 ((uint8_t *)buffer)[wpos] = 0;
529 return wpos;
530 }
531 }
533 ssize_t utf8proc_map(
534 const uint8_t *str, ssize_t strlen, uint8_t **dstptr, int options
535 ) {
536 int32_t *buffer;
537 ssize_t result;
538 *dstptr = NULL;
539 result = utf8proc_decompose(str, strlen, NULL, 0, options);
540 if (result < 0) return result;
541 buffer = malloc(result * sizeof(int32_t) + 1);
542 if (!buffer) return UTF8PROC_ERROR_NOMEM;
543 result = utf8proc_decompose(str, strlen, buffer, result, options);
544 if (result < 0) {
545 free(buffer);
546 return result;
547 }
548 result = utf8proc_reencode(buffer, result, options);
549 if (result < 0) {
550 free(buffer);
551 return result;
552 }
553 {
554 int32_t *newptr;
555 newptr = realloc(buffer, result+1);
556 if (newptr) buffer = newptr;
557 }
558 *dstptr = (uint8_t *)buffer;
559 return result;
560 }
562 uint8_t *utf8proc_NFD(const uint8_t *str) {
563 uint8_t *retval;
564 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
565 UTF8PROC_DECOMPOSE);
566 return retval;
567 }
569 uint8_t *utf8proc_NFC(const uint8_t *str) {
570 uint8_t *retval;
571 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
572 UTF8PROC_COMPOSE);
573 return retval;
574 }
576 uint8_t *utf8proc_NFKD(const uint8_t *str) {
577 uint8_t *retval;
578 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
579 UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
580 return retval;
581 }
583 uint8_t *utf8proc_NFKC(const uint8_t *str) {
584 uint8_t *retval;
585 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
586 UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
587 return retval;
588 }
