utf8proc

view utf8proc.c @ 5:c18366878af9

Version 1.0.2

- included a check in Integer#utf8, which raises an exception, if the given code-point is invalid because of being too high (this was missing yet)
- added support for PostgreSQL version 8.2
author jbe
date Tue Dec 26 12:00:00 2006 +0100 (2006-12-26)
parents 4ee0d5f54af1
children fcfd8c836c64
line source
1 /*
2 * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany
3 * Author: Jan Behrens <jan.behrens@flexiguided.de>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the FlexiGuided GmbH nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * This library contains derived data from a modified version of the
33 * Unicode data files.
34 *
35 * The original data files are available at
36 * http://www.unicode.org/Public/UNIDATA/
37 *
38 * Please notice the copyright statement in the file "utf8proc_data.c".
39 *
40 */
43 /*
44 * File name: utf8proc.c
45 * Version: 1.0
46 * Last changed: 2006-09-17
47 *
48 * Description:
49 * Implementation of libutf8proc.
50 */
53 #include "utf8proc.h"
54 #include "utf8proc_data.c"
57 const 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 };
75 #define UTF8PROC_HANGUL_SBASE 0xAC00
76 #define UTF8PROC_HANGUL_LBASE 0x1100
77 #define UTF8PROC_HANGUL_VBASE 0x1161
78 #define UTF8PROC_HANGUL_TBASE 0x11A7
79 #define UTF8PROC_HANGUL_LCOUNT 19
80 #define UTF8PROC_HANGUL_VCOUNT 21
81 #define UTF8PROC_HANGUL_TCOUNT 28
82 #define UTF8PROC_HANGUL_NCOUNT 588
83 #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
96 #define UTF8PROC_BOUNDCLASS_START 0
97 #define UTF8PROC_BOUNDCLASS_OTHER 1
98 #define UTF8PROC_BOUNDCLASS_CR 2
99 #define UTF8PROC_BOUNDCLASS_LF 3
100 #define UTF8PROC_BOUNDCLASS_CONTROL 4
101 #define UTF8PROC_BOUNDCLASS_EXTEND 5
102 #define UTF8PROC_BOUNDCLASS_L 6
103 #define UTF8PROC_BOUNDCLASS_V 7
104 #define UTF8PROC_BOUNDCLASS_T 8
105 #define UTF8PROC_BOUNDCLASS_LV 9
106 #define UTF8PROC_BOUNDCLASS_LVT 10
109 const char *utf8proc_errmsg(ssize_t errcode) {
110 switch (errcode) {
111 case UTF8PROC_ERROR_NOMEM:
112 return "Memory for processing UTF-8 data could not be allocated.";
113 case UTF8PROC_ERROR_OVERFLOW:
114 return "UTF-8 string is too long to be processed.";
115 case UTF8PROC_ERROR_INVALIDUTF8:
116 return "Invalid UTF-8 string";
117 case UTF8PROC_ERROR_NOTASSIGNED:
118 return "Unassigned Unicode code point found in UTF-8 string.";
119 case UTF8PROC_ERROR_INVALIDOPTS:
120 return "Invalid options for UTF-8 processing chosen.";
121 default:
122 return "An unknown error occured while processing UTF-8 data.";
123 }
124 }
126 ssize_t utf8proc_iterate(uint8_t *str, ssize_t strlen, int32_t *dst) {
127 int length;
128 int i;
129 int32_t uc = -1;
130 *dst = -1;
131 if (!strlen) return 0;
132 length = utf8proc_utf8class[str[0]];
133 if (!length) return UTF8PROC_ERROR_INVALIDUTF8;
134 if (strlen >= 0 && length > strlen) return UTF8PROC_ERROR_INVALIDUTF8;
135 for (i=1; i<length; i++) {
136 if ((str[i] & 0xC0) != 0x80) return UTF8PROC_ERROR_INVALIDUTF8;
137 }
138 switch (length) {
139 case 1:
140 uc = str[0];
141 break;
142 case 2:
143 uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
144 if (uc < 0x80) uc = -1;
145 break;
146 case 3:
147 uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
148 + (str[2] & 0x3F);
149 if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
150 (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
151 break;
152 case 4:
153 uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
154 + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
155 if (uc < 0x10000 || uc >= 0x110000) uc = -1;
156 break;
157 }
158 if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE)) return UTF8PROC_ERROR_INVALIDUTF8;
159 *dst = uc;
160 return length;
161 }
163 ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst) {
164 if (uc < 0x00) {
165 return 0;
166 } else if (uc < 0x80) {
167 dst[0] = uc;
168 return 1;
169 } else if (uc < 0x800) {
170 dst[0] = 0xC0 + (uc >> 6);
171 dst[1] = 0x80 + (uc & 0x3F);
172 return 2;
173 } else if (uc == 0xFFFF) {
174 dst[0] = 0xFF;
175 return 1;
176 } else if (uc == 0xFFFE) {
177 dst[0] = 0xFE;
178 return 1;
179 } else if (uc < 0x10000) {
180 dst[0] = 0xE0 + (uc >> 12);
181 dst[1] = 0x80 + ((uc >> 6) & 0x3F);
182 dst[2] = 0x80 + (uc & 0x3F);
183 return 3;
184 } else if (uc < 0x110000) {
185 dst[0] = 0xF0 + (uc >> 18);
186 dst[1] = 0x80 + ((uc >> 12) & 0x3F);
187 dst[2] = 0x80 + ((uc >> 6) & 0x3F);
188 dst[3] = 0x80 + (uc & 0x3F);
189 return 4;
190 } else return 0;
191 }
193 const utf8proc_property_t *utf8proc_get_property(int32_t uc) {
194 // ASSERT: uc >= 0 && uc < 0x110000
195 return utf8proc_properties + (
196 utf8proc_stage2table[
197 utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
198 ]
199 );
200 }
202 #define utf8proc_decompose_lump(replacement_uc) \
203 return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
204 options & ~UTF8PROC_LUMP, last_boundclass)
206 ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize,
207 int options, int *last_boundclass) {
208 // ASSERT: uc >= 0 && uc < 0x110000
209 const utf8proc_property_t *property;
210 utf8proc_propval_t category;
211 int32_t hangul_sindex;
212 property = utf8proc_get_property(uc);
213 category = property->category;
214 hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
215 if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
216 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
217 int32_t hangul_tindex;
218 if (bufsize >= 1) {
219 dst[0] = UTF8PROC_HANGUL_LBASE +
220 hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
221 if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
222 (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
223 }
224 hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
225 if (!hangul_tindex) return 2;
226 if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
227 return 3;
228 }
229 }
230 if (options & UTF8PROC_REJECTNA) {
231 if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
232 }
233 if (options & UTF8PROC_IGNORE) {
234 if (property->ignorable) return 0;
235 }
236 if (options & UTF8PROC_LUMP) {
237 if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
238 if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
239 utf8proc_decompose_lump(0x0027);
240 if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
241 utf8proc_decompose_lump(0x002D);
242 if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
243 if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
244 if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
245 utf8proc_decompose_lump(0x003C);
246 if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
247 utf8proc_decompose_lump(0x003E);
248 if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
249 if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
250 utf8proc_decompose_lump(0x005E);
251 if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
252 utf8proc_decompose_lump(0x005F);
253 if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
254 if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
255 if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
256 if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
257 if (category == UTF8PROC_CATEGORY_ZL ||
258 category == UTF8PROC_CATEGORY_ZP)
259 utf8proc_decompose_lump(0x000A);
260 }
261 }
262 if (options & UTF8PROC_STRIPMARK) {
263 if (category == UTF8PROC_CATEGORY_MN ||
264 category == UTF8PROC_CATEGORY_MC ||
265 category == UTF8PROC_CATEGORY_ME) return 0;
266 }
267 if (options & UTF8PROC_CASEFOLD) {
268 if (property->casefold_mapping) {
269 const int32_t *casefold_entry;
270 ssize_t written = 0;
271 for (casefold_entry = property->casefold_mapping;
272 *casefold_entry >= 0; casefold_entry++) {
273 written += utf8proc_decompose_char(*casefold_entry, dst+written,
274 (bufsize > written) ? (bufsize - written) : 0, options,
275 last_boundclass);
276 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
277 }
278 return written;
279 }
280 }
281 if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
282 if (property->decomp_mapping &&
283 (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
284 const int32_t *decomp_entry;
285 ssize_t written = 0;
286 for (decomp_entry = property->decomp_mapping;
287 *decomp_entry >= 0; decomp_entry++) {
288 written += utf8proc_decompose_char(*decomp_entry, dst+written,
289 (bufsize > written) ? (bufsize - written) : 0, options,
290 last_boundclass);
291 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
292 }
293 return written;
294 }
295 }
296 if (options & UTF8PROC_CHARBOUND) {
297 bool boundary;
298 int tbc, lbc;
299 tbc =
300 (uc == 0x000D) ? UTF8PROC_BOUNDCLASS_CR :
301 (uc == 0x000A) ? UTF8PROC_BOUNDCLASS_LF :
302 ((category == UTF8PROC_CATEGORY_ZL ||
303 category == UTF8PROC_CATEGORY_ZP ||
304 category == UTF8PROC_CATEGORY_CC ||
305 category == UTF8PROC_CATEGORY_CF) &&
306 !(uc == 0x200C || uc == 0x200D)) ? UTF8PROC_BOUNDCLASS_CONTROL :
307 property->extend ? UTF8PROC_BOUNDCLASS_EXTEND :
308 ((uc >= UTF8PROC_HANGUL_L_START && uc < UTF8PROC_HANGUL_L_END) ||
309 uc == UTF8PROC_HANGUL_L_FILLER) ? UTF8PROC_BOUNDCLASS_L :
310 (uc >= UTF8PROC_HANGUL_V_START && uc < UTF8PROC_HANGUL_V_END) ?
311 UTF8PROC_BOUNDCLASS_V :
312 (uc >= UTF8PROC_HANGUL_T_START && uc < UTF8PROC_HANGUL_T_END) ?
313 UTF8PROC_BOUNDCLASS_T :
314 (uc >= UTF8PROC_HANGUL_S_START && uc < UTF8PROC_HANGUL_S_END) ? (
315 ((uc-UTF8PROC_HANGUL_SBASE) % UTF8PROC_HANGUL_TCOUNT == 0) ?
316 UTF8PROC_BOUNDCLASS_LV : UTF8PROC_BOUNDCLASS_LVT
317 ) :
318 UTF8PROC_BOUNDCLASS_OTHER;
319 lbc = *last_boundclass;
320 boundary =
321 (tbc == UTF8PROC_BOUNDCLASS_EXTEND) ? false :
322 (lbc == UTF8PROC_BOUNDCLASS_START) ? true :
323 (lbc == UTF8PROC_BOUNDCLASS_CR &&
324 tbc == UTF8PROC_BOUNDCLASS_LF) ? false :
325 (lbc == UTF8PROC_BOUNDCLASS_CONTROL) ? true :
326 (tbc == UTF8PROC_BOUNDCLASS_CONTROL) ? true :
327 (lbc == UTF8PROC_BOUNDCLASS_L &&
328 (tbc == UTF8PROC_BOUNDCLASS_L ||
329 tbc == UTF8PROC_BOUNDCLASS_V ||
330 tbc == UTF8PROC_BOUNDCLASS_LV ||
331 tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false :
332 ((lbc == UTF8PROC_BOUNDCLASS_LV ||
333 lbc == UTF8PROC_BOUNDCLASS_V) &&
334 (tbc == UTF8PROC_BOUNDCLASS_V ||
335 tbc == UTF8PROC_BOUNDCLASS_T)) ? false :
336 ((lbc == UTF8PROC_BOUNDCLASS_LVT ||
337 lbc == UTF8PROC_BOUNDCLASS_T) &&
338 tbc == UTF8PROC_BOUNDCLASS_T) ? false :
339 true;
340 *last_boundclass = tbc;
341 if (boundary) {
342 if (bufsize >= 1) dst[0] = 0xFFFF;
343 if (bufsize >= 2) dst[1] = uc;
344 return 2;
345 }
346 }
347 if (bufsize >= 1) *dst = uc;
348 return 1;
349 }
351 ssize_t utf8proc_decompose(uint8_t *str, ssize_t strlen,
352 int32_t *buffer, ssize_t bufsize, int options) {
353 // strlen will be ignored, if UTF8PROC_NULLTERM is set in options
354 ssize_t wpos = 0;
355 if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
356 return UTF8PROC_ERROR_INVALIDOPTS;
357 if ((options & UTF8PROC_STRIPMARK) &&
358 !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
359 return UTF8PROC_ERROR_INVALIDOPTS;
360 {
361 int32_t uc;
362 ssize_t rpos = 0;
363 ssize_t decomp_result;
364 int boundclass = UTF8PROC_BOUNDCLASS_START;
365 while (1) {
366 if (options & UTF8PROC_NULLTERM) {
367 rpos += utf8proc_iterate(str + rpos, -1, &uc);
368 // checking of return value is not neccessary,
369 // as 'uc' is < 0 in case of error
370 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
371 if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
372 if (uc == 0) break;
373 } else {
374 if (rpos >= strlen) break;
375 rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
376 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
377 }
378 decomp_result = utf8proc_decompose_char(
379 uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
380 &boundclass
381 );
382 if (decomp_result < 0) return decomp_result;
383 wpos += decomp_result;
384 // prohibiting integer overflows due to too long strings:
385 if (wpos < 0 || wpos > SSIZE_MAX/sizeof(int32_t)/2)
386 return UTF8PROC_ERROR_OVERFLOW;
387 }
388 }
389 if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
390 ssize_t pos = 0;
391 while (pos < wpos-1) {
392 int32_t uc1, uc2;
393 const utf8proc_property_t *property1, *property2;
394 uc1 = buffer[pos];
395 uc2 = buffer[pos+1];
396 property1 = utf8proc_get_property(uc1);
397 property2 = utf8proc_get_property(uc2);
398 if (property1->combining_class > property2->combining_class &&
399 property2->combining_class > 0) {
400 buffer[pos] = uc2;
401 buffer[pos+1] = uc1;
402 if (pos > 0) pos--; else pos++;
403 } else {
404 pos++;
405 }
406 }
407 }
408 return wpos;
409 }
411 ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options) {
412 // UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
413 // ASSERT: 'buffer' has one spare byte of free space at the end!
414 if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
415 ssize_t rpos;
416 ssize_t wpos = 0;
417 int32_t uc;
418 for (rpos = 0; rpos < length; rpos++) {
419 uc = buffer[rpos];
420 if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
421 if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
422 ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
423 if (options & UTF8PROC_NLF2LS) {
424 if (options & UTF8PROC_NLF2PS) {
425 buffer[wpos++] = 0x000A;
426 } else {
427 buffer[wpos++] = 0x2028;
428 }
429 } else {
430 if (options & UTF8PROC_NLF2PS) {
431 buffer[wpos++] = 0x2029;
432 } else {
433 buffer[wpos++] = 0x0020;
434 }
435 }
436 } else if ((options & UTF8PROC_STRIPCC) &&
437 (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
438 if (uc == 0x0009) buffer[wpos++] = 0x0020;
439 } else {
440 buffer[wpos++] = uc;
441 }
442 }
443 length = wpos;
444 }
445 if (options & UTF8PROC_COMPOSE) {
446 int32_t *starter = NULL;
447 int32_t current_char;
448 const utf8proc_property_t *starter_property = NULL, *current_property;
449 utf8proc_propval_t max_combining_class = -1;
450 ssize_t rpos;
451 ssize_t wpos = 0;
452 int32_t composition;
453 for (rpos = 0; rpos < length; rpos++) {
454 current_char = buffer[rpos];
455 current_property = utf8proc_get_property(current_char);
456 if (starter && current_property->combining_class > max_combining_class) {
457 // combination perhaps possible
458 int32_t hangul_lindex;
459 int32_t hangul_sindex;
460 hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
461 if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
462 int32_t hangul_vindex;
463 hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
464 if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
465 *starter = UTF8PROC_HANGUL_SBASE +
466 (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
467 UTF8PROC_HANGUL_TCOUNT;
468 starter_property = NULL;
469 continue;
470 }
471 }
472 hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
473 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
474 (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
475 int32_t hangul_tindex;
476 hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
477 if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
478 *starter += hangul_tindex;
479 starter_property = NULL;
480 continue;
481 }
482 }
483 if (!starter_property) {
484 starter_property = utf8proc_get_property(*starter);
485 }
486 if (starter_property->comb1st_index >= 0 &&
487 current_property->comb2nd_index >= 0) {
488 composition = utf8proc_combinations[
489 starter_property->comb1st_index +
490 current_property->comb2nd_index
491 ];
492 if (composition >= 0 && (!(options & UTF8PROC_STABLE) ||
493 !(utf8proc_get_property(composition)->comp_exclusion))) {
494 *starter = composition;
495 starter_property = NULL;
496 continue;
497 }
498 }
499 }
500 buffer[wpos] = current_char;
501 if (current_property->combining_class) {
502 if (current_property->combining_class > max_combining_class) {
503 max_combining_class = current_property->combining_class;
504 }
505 } else {
506 starter = buffer + wpos;
507 starter_property = NULL;
508 max_combining_class = -1;
509 }
510 wpos++;
511 }
512 length = wpos;
513 }
514 {
515 ssize_t rpos, wpos = 0;
516 int32_t uc;
517 for (rpos = 0; rpos < length; rpos++) {
518 uc = buffer[rpos];
519 wpos += utf8proc_encode_char(uc, ((uint8_t *)buffer) + wpos);
520 }
521 ((uint8_t *)buffer)[wpos] = 0;
522 return wpos;
523 }
524 }
526 ssize_t utf8proc_map(uint8_t *str, ssize_t strlen, uint8_t **dstptr,
527 int options) {
528 int32_t *buffer;
529 ssize_t result;
530 *dstptr = NULL;
531 result = utf8proc_decompose(str, strlen, NULL, 0, options);
532 if (result < 0) return result;
533 buffer = malloc(result * sizeof(int32_t) + 1);
534 if (!buffer) return UTF8PROC_ERROR_NOMEM;
535 result = utf8proc_decompose(str, strlen, buffer, result, options);
536 if (result < 0) {
537 free(buffer);
538 return result;
539 }
540 result = utf8proc_reencode(buffer, result, options);
541 if (result < 0) {
542 free(buffer);
543 return result;
544 }
545 {
546 int32_t *newptr;
547 newptr = realloc(buffer, result+1);
548 if (newptr) buffer = newptr;
549 }
550 *dstptr = (uint8_t *)buffer;
551 return result;
552 }
554 uint8_t *utf8proc_NFD(uint8_t *str) {
555 uint8_t *retval;
556 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
557 UTF8PROC_DECOMPOSE);
558 return retval;
559 }
561 uint8_t *utf8proc_NFC(uint8_t *str) {
562 uint8_t *retval;
563 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
564 UTF8PROC_COMPOSE);
565 return retval;
566 }
568 uint8_t *utf8proc_NFKD(uint8_t *str) {
569 uint8_t *retval;
570 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
571 UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
572 return retval;
573 }
575 uint8_t *utf8proc_NFKC(uint8_t *str) {
576 uint8_t *retval;
577 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
578 UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
579 return retval;
580 }

Impressum / About Us