utf8proc

view utf8proc.c @ 7:fcfd8c836c64

Version 1.1.1

- Added a new PostgreSQL function 'unistrip', which behaves like 'unifold', but also removes all character marks (e.g. accents).
- Changed license from BSD to MIT style.
- Added a new function 'utf8proc_codepoint_valid' to the C library.
- Changed compiler flags in Makefile from -g -O0 to -O2
- The ruby script, which was used to build the utf8proc_data.c file, is now included in the distribution.
author jbe
date Sun Jul 22 12:00:00 2007 +0200 (2007-07-22)
parents 4ee0d5f54af1
children 951e73a98021
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_errmsg(ssize_t errcode) {
101 switch (errcode) {
102 case UTF8PROC_ERROR_NOMEM:
103 return "Memory for processing UTF-8 data could not be allocated.";
104 case UTF8PROC_ERROR_OVERFLOW:
105 return "UTF-8 string is too long to be processed.";
106 case UTF8PROC_ERROR_INVALIDUTF8:
107 return "Invalid UTF-8 string";
108 case UTF8PROC_ERROR_NOTASSIGNED:
109 return "Unassigned Unicode code point found in UTF-8 string.";
110 case UTF8PROC_ERROR_INVALIDOPTS:
111 return "Invalid options for UTF-8 processing chosen.";
112 default:
113 return "An unknown error occured while processing UTF-8 data.";
114 }
115 }
117 ssize_t utf8proc_iterate(
118 const uint8_t *str, ssize_t strlen, int32_t *dst
119 ) {
120 int length;
121 int i;
122 int32_t uc = -1;
123 *dst = -1;
124 if (!strlen) return 0;
125 length = utf8proc_utf8class[str[0]];
126 if (!length) return UTF8PROC_ERROR_INVALIDUTF8;
127 if (strlen >= 0 && length > strlen) return UTF8PROC_ERROR_INVALIDUTF8;
128 for (i=1; i<length; i++) {
129 if ((str[i] & 0xC0) != 0x80) return UTF8PROC_ERROR_INVALIDUTF8;
130 }
131 switch (length) {
132 case 1:
133 uc = str[0];
134 break;
135 case 2:
136 uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
137 if (uc < 0x80) uc = -1;
138 break;
139 case 3:
140 uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
141 + (str[2] & 0x3F);
142 if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
143 (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
144 break;
145 case 4:
146 uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
147 + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
148 if (uc < 0x10000 || uc >= 0x110000) uc = -1;
149 break;
150 }
151 if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE))
152 return UTF8PROC_ERROR_INVALIDUTF8;
153 *dst = uc;
154 return length;
155 }
157 bool utf8proc_codepoint_valid(int32_t uc) {
158 if (uc < 0 || uc >= 0x110000 ||
159 ((uc & 0xFFFF) >= 0xFFFE) || (uc >= 0xD800 && uc < 0xE000) ||
160 (uc >= 0xFDD0 && uc < 0xFDF0)) return false;
161 else return true;
162 }
164 ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst) {
165 if (uc < 0x00) {
166 return 0;
167 } else if (uc < 0x80) {
168 dst[0] = uc;
169 return 1;
170 } else if (uc < 0x800) {
171 dst[0] = 0xC0 + (uc >> 6);
172 dst[1] = 0x80 + (uc & 0x3F);
173 return 2;
174 } else if (uc == 0xFFFF) {
175 dst[0] = 0xFF;
176 return 1;
177 } else if (uc == 0xFFFE) {
178 dst[0] = 0xFE;
179 return 1;
180 } else if (uc < 0x10000) {
181 dst[0] = 0xE0 + (uc >> 12);
182 dst[1] = 0x80 + ((uc >> 6) & 0x3F);
183 dst[2] = 0x80 + (uc & 0x3F);
184 return 3;
185 } else if (uc < 0x110000) {
186 dst[0] = 0xF0 + (uc >> 18);
187 dst[1] = 0x80 + ((uc >> 12) & 0x3F);
188 dst[2] = 0x80 + ((uc >> 6) & 0x3F);
189 dst[3] = 0x80 + (uc & 0x3F);
190 return 4;
191 } else return 0;
192 }
194 const utf8proc_property_t *utf8proc_get_property(int32_t uc) {
195 // ASSERT: uc >= 0 && uc < 0x110000
196 return utf8proc_properties + (
197 utf8proc_stage2table[
198 utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
199 ]
200 );
201 }
203 #define utf8proc_decompose_lump(replacement_uc) \
204 return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
205 options & ~UTF8PROC_LUMP, last_boundclass)
207 ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize,
208 int options, int *last_boundclass) {
209 // ASSERT: uc >= 0 && uc < 0x110000
210 const utf8proc_property_t *property;
211 utf8proc_propval_t category;
212 int32_t hangul_sindex;
213 property = utf8proc_get_property(uc);
214 category = property->category;
215 hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
216 if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
217 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
218 int32_t hangul_tindex;
219 if (bufsize >= 1) {
220 dst[0] = UTF8PROC_HANGUL_LBASE +
221 hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
222 if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
223 (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
224 }
225 hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
226 if (!hangul_tindex) return 2;
227 if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
228 return 3;
229 }
230 }
231 if (options & UTF8PROC_REJECTNA) {
232 if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
233 }
234 if (options & UTF8PROC_IGNORE) {
235 if (property->ignorable) return 0;
236 }
237 if (options & UTF8PROC_LUMP) {
238 if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
239 if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
240 utf8proc_decompose_lump(0x0027);
241 if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
242 utf8proc_decompose_lump(0x002D);
243 if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
244 if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
245 if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
246 utf8proc_decompose_lump(0x003C);
247 if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
248 utf8proc_decompose_lump(0x003E);
249 if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
250 if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
251 utf8proc_decompose_lump(0x005E);
252 if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
253 utf8proc_decompose_lump(0x005F);
254 if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
255 if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
256 if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
257 if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
258 if (category == UTF8PROC_CATEGORY_ZL ||
259 category == UTF8PROC_CATEGORY_ZP)
260 utf8proc_decompose_lump(0x000A);
261 }
262 }
263 if (options & UTF8PROC_STRIPMARK) {
264 if (category == UTF8PROC_CATEGORY_MN ||
265 category == UTF8PROC_CATEGORY_MC ||
266 category == UTF8PROC_CATEGORY_ME) return 0;
267 }
268 if (options & UTF8PROC_CASEFOLD) {
269 if (property->casefold_mapping) {
270 const int32_t *casefold_entry;
271 ssize_t written = 0;
272 for (casefold_entry = property->casefold_mapping;
273 *casefold_entry >= 0; casefold_entry++) {
274 written += utf8proc_decompose_char(*casefold_entry, dst+written,
275 (bufsize > written) ? (bufsize - written) : 0, options,
276 last_boundclass);
277 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
278 }
279 return written;
280 }
281 }
282 if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
283 if (property->decomp_mapping &&
284 (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
285 const int32_t *decomp_entry;
286 ssize_t written = 0;
287 for (decomp_entry = property->decomp_mapping;
288 *decomp_entry >= 0; decomp_entry++) {
289 written += utf8proc_decompose_char(*decomp_entry, dst+written,
290 (bufsize > written) ? (bufsize - written) : 0, options,
291 last_boundclass);
292 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
293 }
294 return written;
295 }
296 }
297 if (options & UTF8PROC_CHARBOUND) {
298 bool boundary;
299 int tbc, lbc;
300 tbc =
301 (uc == 0x000D) ? UTF8PROC_BOUNDCLASS_CR :
302 (uc == 0x000A) ? UTF8PROC_BOUNDCLASS_LF :
303 ((category == UTF8PROC_CATEGORY_ZL ||
304 category == UTF8PROC_CATEGORY_ZP ||
305 category == UTF8PROC_CATEGORY_CC ||
306 category == UTF8PROC_CATEGORY_CF) &&
307 !(uc == 0x200C || uc == 0x200D)) ? UTF8PROC_BOUNDCLASS_CONTROL :
308 property->extend ? UTF8PROC_BOUNDCLASS_EXTEND :
309 ((uc >= UTF8PROC_HANGUL_L_START && uc < UTF8PROC_HANGUL_L_END) ||
310 uc == UTF8PROC_HANGUL_L_FILLER) ? UTF8PROC_BOUNDCLASS_L :
311 (uc >= UTF8PROC_HANGUL_V_START && uc < UTF8PROC_HANGUL_V_END) ?
312 UTF8PROC_BOUNDCLASS_V :
313 (uc >= UTF8PROC_HANGUL_T_START && uc < UTF8PROC_HANGUL_T_END) ?
314 UTF8PROC_BOUNDCLASS_T :
315 (uc >= UTF8PROC_HANGUL_S_START && uc < UTF8PROC_HANGUL_S_END) ? (
316 ((uc-UTF8PROC_HANGUL_SBASE) % UTF8PROC_HANGUL_TCOUNT == 0) ?
317 UTF8PROC_BOUNDCLASS_LV : UTF8PROC_BOUNDCLASS_LVT
318 ) :
319 UTF8PROC_BOUNDCLASS_OTHER;
320 lbc = *last_boundclass;
321 boundary =
322 (tbc == UTF8PROC_BOUNDCLASS_EXTEND) ? false :
323 (lbc == UTF8PROC_BOUNDCLASS_START) ? true :
324 (lbc == UTF8PROC_BOUNDCLASS_CR &&
325 tbc == UTF8PROC_BOUNDCLASS_LF) ? false :
326 (lbc == UTF8PROC_BOUNDCLASS_CONTROL) ? true :
327 (tbc == UTF8PROC_BOUNDCLASS_CONTROL) ? true :
328 (lbc == UTF8PROC_BOUNDCLASS_L &&
329 (tbc == UTF8PROC_BOUNDCLASS_L ||
330 tbc == UTF8PROC_BOUNDCLASS_V ||
331 tbc == UTF8PROC_BOUNDCLASS_LV ||
332 tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false :
333 ((lbc == UTF8PROC_BOUNDCLASS_LV ||
334 lbc == UTF8PROC_BOUNDCLASS_V) &&
335 (tbc == UTF8PROC_BOUNDCLASS_V ||
336 tbc == UTF8PROC_BOUNDCLASS_T)) ? false :
337 ((lbc == UTF8PROC_BOUNDCLASS_LVT ||
338 lbc == UTF8PROC_BOUNDCLASS_T) &&
339 tbc == UTF8PROC_BOUNDCLASS_T) ? false :
340 true;
341 *last_boundclass = tbc;
342 if (boundary) {
343 if (bufsize >= 1) dst[0] = 0xFFFF;
344 if (bufsize >= 2) dst[1] = uc;
345 return 2;
346 }
347 }
348 if (bufsize >= 1) *dst = uc;
349 return 1;
350 }
352 ssize_t utf8proc_decompose(
353 const uint8_t *str, ssize_t strlen,
354 int32_t *buffer, ssize_t bufsize, int options
355 ) {
356 // strlen will be ignored, if UTF8PROC_NULLTERM is set in options
357 ssize_t wpos = 0;
358 if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
359 return UTF8PROC_ERROR_INVALIDOPTS;
360 if ((options & UTF8PROC_STRIPMARK) &&
361 !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
362 return UTF8PROC_ERROR_INVALIDOPTS;
363 {
364 int32_t uc;
365 ssize_t rpos = 0;
366 ssize_t decomp_result;
367 int boundclass = UTF8PROC_BOUNDCLASS_START;
368 while (1) {
369 if (options & UTF8PROC_NULLTERM) {
370 rpos += utf8proc_iterate(str + rpos, -1, &uc);
371 // checking of return value is not neccessary,
372 // as 'uc' is < 0 in case of error
373 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
374 if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
375 if (uc == 0) break;
376 } else {
377 if (rpos >= strlen) break;
378 rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
379 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
380 }
381 decomp_result = utf8proc_decompose_char(
382 uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
383 &boundclass
384 );
385 if (decomp_result < 0) return decomp_result;
386 wpos += decomp_result;
387 // prohibiting integer overflows due to too long strings:
388 if (wpos < 0 || wpos > SSIZE_MAX/sizeof(int32_t)/2)
389 return UTF8PROC_ERROR_OVERFLOW;
390 }
391 }
392 if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
393 ssize_t pos = 0;
394 while (pos < wpos-1) {
395 int32_t uc1, uc2;
396 const utf8proc_property_t *property1, *property2;
397 uc1 = buffer[pos];
398 uc2 = buffer[pos+1];
399 property1 = utf8proc_get_property(uc1);
400 property2 = utf8proc_get_property(uc2);
401 if (property1->combining_class > property2->combining_class &&
402 property2->combining_class > 0) {
403 buffer[pos] = uc2;
404 buffer[pos+1] = uc1;
405 if (pos > 0) pos--; else pos++;
406 } else {
407 pos++;
408 }
409 }
410 }
411 return wpos;
412 }
414 ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options) {
415 // UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
416 // ASSERT: 'buffer' has one spare byte of free space at the end!
417 if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
418 ssize_t rpos;
419 ssize_t wpos = 0;
420 int32_t uc;
421 for (rpos = 0; rpos < length; rpos++) {
422 uc = buffer[rpos];
423 if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
424 if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
425 ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
426 if (options & UTF8PROC_NLF2LS) {
427 if (options & UTF8PROC_NLF2PS) {
428 buffer[wpos++] = 0x000A;
429 } else {
430 buffer[wpos++] = 0x2028;
431 }
432 } else {
433 if (options & UTF8PROC_NLF2PS) {
434 buffer[wpos++] = 0x2029;
435 } else {
436 buffer[wpos++] = 0x0020;
437 }
438 }
439 } else if ((options & UTF8PROC_STRIPCC) &&
440 (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
441 if (uc == 0x0009) buffer[wpos++] = 0x0020;
442 } else {
443 buffer[wpos++] = uc;
444 }
445 }
446 length = wpos;
447 }
448 if (options & UTF8PROC_COMPOSE) {
449 int32_t *starter = NULL;
450 int32_t current_char;
451 const utf8proc_property_t *starter_property = NULL, *current_property;
452 utf8proc_propval_t max_combining_class = -1;
453 ssize_t rpos;
454 ssize_t wpos = 0;
455 int32_t composition;
456 for (rpos = 0; rpos < length; rpos++) {
457 current_char = buffer[rpos];
458 current_property = utf8proc_get_property(current_char);
459 if (starter && current_property->combining_class > max_combining_class) {
460 // combination perhaps possible
461 int32_t hangul_lindex;
462 int32_t hangul_sindex;
463 hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
464 if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
465 int32_t hangul_vindex;
466 hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
467 if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
468 *starter = UTF8PROC_HANGUL_SBASE +
469 (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
470 UTF8PROC_HANGUL_TCOUNT;
471 starter_property = NULL;
472 continue;
473 }
474 }
475 hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
476 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
477 (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
478 int32_t hangul_tindex;
479 hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
480 if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
481 *starter += hangul_tindex;
482 starter_property = NULL;
483 continue;
484 }
485 }
486 if (!starter_property) {
487 starter_property = utf8proc_get_property(*starter);
488 }
489 if (starter_property->comb1st_index >= 0 &&
490 current_property->comb2nd_index >= 0) {
491 composition = utf8proc_combinations[
492 starter_property->comb1st_index +
493 current_property->comb2nd_index
494 ];
495 if (composition >= 0 && (!(options & UTF8PROC_STABLE) ||
496 !(utf8proc_get_property(composition)->comp_exclusion))) {
497 *starter = composition;
498 starter_property = NULL;
499 continue;
500 }
501 }
502 }
503 buffer[wpos] = current_char;
504 if (current_property->combining_class) {
505 if (current_property->combining_class > max_combining_class) {
506 max_combining_class = current_property->combining_class;
507 }
508 } else {
509 starter = buffer + wpos;
510 starter_property = NULL;
511 max_combining_class = -1;
512 }
513 wpos++;
514 }
515 length = wpos;
516 }
517 {
518 ssize_t rpos, wpos = 0;
519 int32_t uc;
520 for (rpos = 0; rpos < length; rpos++) {
521 uc = buffer[rpos];
522 wpos += utf8proc_encode_char(uc, ((uint8_t *)buffer) + wpos);
523 }
524 ((uint8_t *)buffer)[wpos] = 0;
525 return wpos;
526 }
527 }
529 ssize_t utf8proc_map(
530 const uint8_t *str, ssize_t strlen, uint8_t **dstptr, int options
531 ) {
532 int32_t *buffer;
533 ssize_t result;
534 *dstptr = NULL;
535 result = utf8proc_decompose(str, strlen, NULL, 0, options);
536 if (result < 0) return result;
537 buffer = malloc(result * sizeof(int32_t) + 1);
538 if (!buffer) return UTF8PROC_ERROR_NOMEM;
539 result = utf8proc_decompose(str, strlen, buffer, result, options);
540 if (result < 0) {
541 free(buffer);
542 return result;
543 }
544 result = utf8proc_reencode(buffer, result, options);
545 if (result < 0) {
546 free(buffer);
547 return result;
548 }
549 {
550 int32_t *newptr;
551 newptr = realloc(buffer, result+1);
552 if (newptr) buffer = newptr;
553 }
554 *dstptr = (uint8_t *)buffer;
555 return result;
556 }
558 uint8_t *utf8proc_NFD(const uint8_t *str) {
559 uint8_t *retval;
560 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
561 UTF8PROC_DECOMPOSE);
562 return retval;
563 }
565 uint8_t *utf8proc_NFC(const uint8_t *str) {
566 uint8_t *retval;
567 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
568 UTF8PROC_COMPOSE);
569 return retval;
570 }
572 uint8_t *utf8proc_NFKD(const uint8_t *str) {
573 uint8_t *retval;
574 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
575 UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
576 return retval;
577 }
579 uint8_t *utf8proc_NFKC(const uint8_t *str) {
580 uint8_t *retval;
581 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
582 UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
583 return retval;
584 }

Impressum / About Us