utf8proc

annotate utf8proc.c @ 0:a0368662434c

Version 0.1
author jbe
date Fri Jun 02 12:00:00 2006 +0200 (2006-06-02)
parents
children 61a89ecc2fb9
rev   line source
jbe@0 1 /*
jbe@0 2 * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany
jbe@0 3 * Author: Jan Behrens <jan.behrens@flexiguided.de>
jbe@0 4 * All rights reserved.
jbe@0 5 *
jbe@0 6 * Redistribution and use in source and binary forms, with or without
jbe@0 7 * modification, are permitted provided that the following conditions are
jbe@0 8 * met:
jbe@0 9 *
jbe@0 10 * 1. Redistributions of source code must retain the above copyright
jbe@0 11 * notice, this list of conditions and the following disclaimer.
jbe@0 12 * 2. Redistributions in binary form must reproduce the above copyright
jbe@0 13 * notice, this list of conditions and the following disclaimer in the
jbe@0 14 * documentation and/or other materials provided with the distribution.
jbe@0 15 * 3. Neither the name of the FlexiGuided GmbH nor the names of its
jbe@0 16 * contributors may be used to endorse or promote products derived from
jbe@0 17 * this software without specific prior written permission.
jbe@0 18 *
jbe@0 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
jbe@0 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
jbe@0 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
jbe@0 22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
jbe@0 23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
jbe@0 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
jbe@0 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
jbe@0 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
jbe@0 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
jbe@0 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
jbe@0 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jbe@0 30 *
jbe@0 31 *
jbe@0 32 * This library contains derived data from a modified version of the
jbe@0 33 * Unicode data files.
jbe@0 34 *
jbe@0 35 * The original data files are available at
jbe@0 36 * http://www.unicode.org/Public/UNIDATA/
jbe@0 37 *
jbe@0 38 * Please notice the copyright statement in the file "utf8proc_data.c".
jbe@0 39 *
jbe@0 40 */
jbe@0 41
jbe@0 42
jbe@0 43 /*
jbe@0 44 * File name: utf8proc.c
jbe@0 45 * Version: 0.1
jbe@0 46 * Last changed: 2006-05-31
jbe@0 47 *
jbe@0 48 * Description:
jbe@0 49 * Implementation of libutf8proc.
jbe@0 50 */
jbe@0 51
jbe@0 52
jbe@0 53 #include "utf8proc.h"
jbe@0 54 #include "utf8proc_data.c"
jbe@0 55
jbe@0 56
jbe@0 57 const int8_t utf8proc_utf8class[256] = {
jbe@0 58 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
jbe@0 59 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
jbe@0 60 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
jbe@0 61 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
jbe@0 62 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
jbe@0 63 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
jbe@0 64 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
jbe@0 65 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
jbe@0 66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
jbe@0 67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
jbe@0 68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
jbe@0 69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
jbe@0 70 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
jbe@0 71 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
jbe@0 72 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
jbe@0 73 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
jbe@0 74
jbe@0 75 #define UTF8PROC_HANGUL_SBASE 0xAC00
jbe@0 76 #define UTF8PROC_HANGUL_LBASE 0x1100
jbe@0 77 #define UTF8PROC_HANGUL_VBASE 0x1161
jbe@0 78 #define UTF8PROC_HANGUL_TBASE 0x11A7
jbe@0 79 #define UTF8PROC_HANGUL_LCOUNT 19
jbe@0 80 #define UTF8PROC_HANGUL_VCOUNT 21
jbe@0 81 #define UTF8PROC_HANGUL_TCOUNT 28
jbe@0 82 #define UTF8PROC_HANGUL_NCOUNT 588
jbe@0 83 #define UTF8PROC_HANGUL_SCOUNT 11172
jbe@0 84
jbe@0 85
jbe@0 86 const char *utf8proc_errmsg(ssize_t errcode) {
jbe@0 87 switch (errcode) {
jbe@0 88 case UTF8PROC_ERROR_NOMEM:
jbe@0 89 return "Memory for processing UTF-8 data could not be allocated.";
jbe@0 90 case UTF8PROC_ERROR_OVERFLOW:
jbe@0 91 return "UTF-8 string is too long to be processed.";
jbe@0 92 case UTF8PROC_ERROR_INVALIDUTF8:
jbe@0 93 return "Invalid UTF-8 string";
jbe@0 94 case UTF8PROC_ERROR_NOTASSIGNED:
jbe@0 95 return "Unassigned Unicode code point found in UTF-8 string.";
jbe@0 96 default:
jbe@0 97 return "An unknown error occured while processing UTF-8 data.";
jbe@0 98 }
jbe@0 99 }
jbe@0 100
jbe@0 101 ssize_t utf8proc_iterate(uint8_t *str, ssize_t strlen, int32_t *dst) {
jbe@0 102 int length;
jbe@0 103 int i;
jbe@0 104 int32_t uc = -1;
jbe@0 105 *dst = -1;
jbe@0 106 if (!strlen) return 0;
jbe@0 107 length = utf8proc_utf8class[str[0]];
jbe@0 108 if (!length) return UTF8PROC_ERROR_INVALIDUTF8;
jbe@0 109 if (strlen >= 0 && length > strlen) return UTF8PROC_ERROR_INVALIDUTF8;
jbe@0 110 for (i=1; i<length; i++) {
jbe@0 111 if ((str[i] & 0xC0) != 0x80) return UTF8PROC_ERROR_INVALIDUTF8;
jbe@0 112 }
jbe@0 113 switch (length) {
jbe@0 114 case 1:
jbe@0 115 uc = str[0];
jbe@0 116 break;
jbe@0 117 case 2:
jbe@0 118 uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
jbe@0 119 if (uc < 0x80) uc = -1;
jbe@0 120 break;
jbe@0 121 case 3:
jbe@0 122 uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
jbe@0 123 + (str[2] & 0x3F);
jbe@0 124 if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
jbe@0 125 (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
jbe@0 126 break;
jbe@0 127 case 4:
jbe@0 128 uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
jbe@0 129 + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
jbe@0 130 if (uc < 0x10000 || uc >= 0x110000) uc = -1;
jbe@0 131 break;
jbe@0 132 }
jbe@0 133 if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE)) return UTF8PROC_ERROR_INVALIDUTF8;
jbe@0 134 *dst = uc;
jbe@0 135 return length;
jbe@0 136 }
jbe@0 137
jbe@0 138 ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst) {
jbe@0 139 if (uc < 0x00) {
jbe@0 140 return 0;
jbe@0 141 } else if (uc < 0x80) {
jbe@0 142 dst[0] = uc;
jbe@0 143 return 1;
jbe@0 144 } else if (uc < 0x800) {
jbe@0 145 dst[0] = 0xC0 + (uc >> 6);
jbe@0 146 dst[1] = 0x80 + (uc & 0x3F);
jbe@0 147 return 2;
jbe@0 148 } else if (uc < 0x10000) {
jbe@0 149 dst[0] = 0xE0 + (uc >> 12);
jbe@0 150 dst[1] = 0x80 + ((uc >> 6) & 0x3F);
jbe@0 151 dst[2] = 0x80 + (uc & 0x3F);
jbe@0 152 return 3;
jbe@0 153 } else if (uc < 0x110000) {
jbe@0 154 dst[0] = 0xF0 + (uc >> 18);
jbe@0 155 dst[1] = 0x80 + ((uc >> 12) & 0x3F);
jbe@0 156 dst[2] = 0x80 + ((uc >> 6) & 0x3F);
jbe@0 157 dst[3] = 0x80 + (uc & 0x3F);
jbe@0 158 return 4;
jbe@0 159 } else return 0;
jbe@0 160 }
jbe@0 161
jbe@0 162 const utf8proc_property_t *utf8proc_get_property(int32_t uc) {
jbe@0 163 // ASSERT: uc >= 0 && uc < 0x110000
jbe@0 164 return utf8proc_properties + (
jbe@0 165 utf8proc_stage2table[
jbe@0 166 utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
jbe@0 167 ]
jbe@0 168 );
jbe@0 169 }
jbe@0 170
jbe@0 171 ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize,
jbe@0 172 int options) {
jbe@0 173 // ASSERT: uc >= 0 && uc < 0x110000
jbe@0 174 const utf8proc_property_t *property;
jbe@0 175 int32_t hangul_sindex;
jbe@0 176 property = utf8proc_get_property(uc);
jbe@0 177 hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
jbe@0 178 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
jbe@0 179 int32_t hangul_tindex;
jbe@0 180 if (bufsize >= 1) {
jbe@0 181 dst[0] = UTF8PROC_HANGUL_LBASE +
jbe@0 182 hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
jbe@0 183 if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
jbe@0 184 (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
jbe@0 185 }
jbe@0 186 hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
jbe@0 187 if (!hangul_tindex) return 2;
jbe@0 188 if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
jbe@0 189 return 3;
jbe@0 190 } else if ((options & UTF8PROC_REJECTNA) && !property->category) {
jbe@0 191 return UTF8PROC_ERROR_NOTASSIGNED;
jbe@0 192 } else if ((options & UTF8PROC_IGNORE) && property->ignorable) {
jbe@0 193 return 0;
jbe@0 194 } else if ((options & UTF8PROC_CASEFOLD) && property->casefold_mapping) {
jbe@0 195 const int32_t *casefold_entry;
jbe@0 196 ssize_t written = 0;
jbe@0 197 for (casefold_entry = property->casefold_mapping;
jbe@0 198 *casefold_entry >= 0; casefold_entry++) {
jbe@0 199 written += utf8proc_decompose_char(*casefold_entry, dst+written,
jbe@0 200 (bufsize > written) ? (bufsize - written) : 0, options);
jbe@0 201 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
jbe@0 202 }
jbe@0 203 return written;
jbe@0 204 } else if (property->decomp_mapping &&
jbe@0 205 (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
jbe@0 206 const int32_t *decomp_entry;
jbe@0 207 ssize_t written = 0;
jbe@0 208 for (decomp_entry = property->decomp_mapping;
jbe@0 209 *decomp_entry >= 0; decomp_entry++) {
jbe@0 210 written += utf8proc_decompose_char(*decomp_entry, dst+written,
jbe@0 211 (bufsize > written) ? (bufsize - written) : 0, options);
jbe@0 212 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
jbe@0 213 }
jbe@0 214 return written;
jbe@0 215 } else {
jbe@0 216 if (bufsize >= 1) *dst = uc;
jbe@0 217 return 1;
jbe@0 218 }
jbe@0 219 }
jbe@0 220
jbe@0 221 ssize_t utf8proc_decompose(uint8_t *str, ssize_t strlen,
jbe@0 222 int32_t *buffer, ssize_t bufsize, int options) {
jbe@0 223 // strlen will be ignored, if UTF8PROC_NULLTERM is set in options
jbe@0 224 ssize_t wpos = 0;
jbe@0 225 {
jbe@0 226 int32_t uc;
jbe@0 227 ssize_t rpos = 0;
jbe@0 228 ssize_t decomp_result;
jbe@0 229 while (1) {
jbe@0 230 if (options & UTF8PROC_NULLTERM) {
jbe@0 231 rpos += utf8proc_iterate(str + rpos, -1, &uc);
jbe@0 232 // checking of return value is not neccessary,
jbe@0 233 // as 'uc' is < 0 in case of error
jbe@0 234 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
jbe@0 235 if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
jbe@0 236 if (uc == 0) break;
jbe@0 237 } else {
jbe@0 238 if (rpos >= strlen) break;
jbe@0 239 rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
jbe@0 240 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
jbe@0 241 }
jbe@0 242 decomp_result = utf8proc_decompose_char(
jbe@0 243 uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options
jbe@0 244 );
jbe@0 245 if (decomp_result < 0) return decomp_result;
jbe@0 246 wpos += decomp_result;
jbe@0 247 // prohibiting integer overflows due to too long strings:
jbe@0 248 if (wpos < 0 || wpos > SSIZE_MAX/sizeof(int32_t)/2)
jbe@0 249 return UTF8PROC_ERROR_OVERFLOW;
jbe@0 250 }
jbe@0 251 }
jbe@0 252 if (bufsize >= wpos) {
jbe@0 253 ssize_t pos = 0;
jbe@0 254 while (pos < wpos-1) {
jbe@0 255 int32_t uc1, uc2;
jbe@0 256 const utf8proc_property_t *property1, *property2;
jbe@0 257 uc1 = buffer[pos];
jbe@0 258 uc2 = buffer[pos+1];
jbe@0 259 property1 = utf8proc_get_property(uc1);
jbe@0 260 property2 = utf8proc_get_property(uc2);
jbe@0 261 if (property1->combining_class > property2->combining_class &&
jbe@0 262 property2->combining_class > 0) {
jbe@0 263 buffer[pos] = uc2;
jbe@0 264 buffer[pos+1] = uc1;
jbe@0 265 if (pos > 0) pos--; else pos++;
jbe@0 266 } else {
jbe@0 267 pos++;
jbe@0 268 }
jbe@0 269 }
jbe@0 270 }
jbe@0 271 return wpos;
jbe@0 272 }
jbe@0 273
jbe@0 274 ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options) {
jbe@0 275 // UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
jbe@0 276 // ASSERT: 'buffer' has one spare byte of free space at the end!
jbe@0 277 if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
jbe@0 278 ssize_t rpos;
jbe@0 279 ssize_t wpos = 0;
jbe@0 280 int32_t uc;
jbe@0 281 for (rpos = 0; rpos < length; rpos++) {
jbe@0 282 uc = buffer[rpos];
jbe@0 283 if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
jbe@0 284 if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
jbe@0 285 ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
jbe@0 286 if (options & UTF8PROC_NLF2LS) {
jbe@0 287 if (options & UTF8PROC_NLF2PS) {
jbe@0 288 buffer[wpos++] = 0x000A;
jbe@0 289 } else {
jbe@0 290 buffer[wpos++] = 0x2028;
jbe@0 291 }
jbe@0 292 } else {
jbe@0 293 if (options & UTF8PROC_NLF2PS) {
jbe@0 294 buffer[wpos++] = 0x2029;
jbe@0 295 } else {
jbe@0 296 buffer[wpos++] = 0x0020;
jbe@0 297 }
jbe@0 298 }
jbe@0 299 } else if ((options & UTF8PROC_STRIPCC) &&
jbe@0 300 (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
jbe@0 301 if (uc == 0x0009) buffer[wpos++] = 0x0020;
jbe@0 302 } else {
jbe@0 303 buffer[wpos++] = uc;
jbe@0 304 }
jbe@0 305 }
jbe@0 306 length = wpos;
jbe@0 307 }
jbe@0 308 if (options & UTF8PROC_COMPOSE) {
jbe@0 309 int32_t *starter = NULL;
jbe@0 310 int32_t current_char;
jbe@0 311 const utf8proc_property_t *starter_property = NULL, *current_property;
jbe@0 312 int16_t max_combining_class = -1;
jbe@0 313 ssize_t rpos;
jbe@0 314 ssize_t wpos = 0;
jbe@0 315 int32_t composition;
jbe@0 316 for (rpos = 0; rpos < length; rpos++) {
jbe@0 317 current_char = buffer[rpos];
jbe@0 318 current_property = utf8proc_get_property(current_char);
jbe@0 319 if (starter && current_property->combining_class > max_combining_class) {
jbe@0 320 // combination perhaps possible
jbe@0 321 int32_t hangul_lindex;
jbe@0 322 int32_t hangul_sindex;
jbe@0 323 hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
jbe@0 324 if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
jbe@0 325 int32_t hangul_vindex;
jbe@0 326 hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
jbe@0 327 if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
jbe@0 328 *starter = UTF8PROC_HANGUL_SBASE +
jbe@0 329 (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
jbe@0 330 UTF8PROC_HANGUL_TCOUNT;
jbe@0 331 starter_property = NULL;
jbe@0 332 continue;
jbe@0 333 }
jbe@0 334 }
jbe@0 335 hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
jbe@0 336 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
jbe@0 337 (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
jbe@0 338 int32_t hangul_tindex;
jbe@0 339 hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
jbe@0 340 if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
jbe@0 341 *starter += hangul_tindex;
jbe@0 342 starter_property = NULL;
jbe@0 343 continue;
jbe@0 344 }
jbe@0 345 }
jbe@0 346 if (!starter_property) {
jbe@0 347 starter_property = utf8proc_get_property(*starter);
jbe@0 348 }
jbe@0 349 if (starter_property->comb1st_index >= 0 &&
jbe@0 350 current_property->comb2nd_index >= 0) {
jbe@0 351 composition = utf8proc_combinations[
jbe@0 352 starter_property->comb1st_index +
jbe@0 353 current_property->comb2nd_index
jbe@0 354 ];
jbe@0 355 if (composition >= 0 && (!(options & UTF8PROC_STABLE) ||
jbe@0 356 !(utf8proc_get_property(composition)->comp_exclusion))) {
jbe@0 357 *starter = composition;
jbe@0 358 starter_property = NULL;
jbe@0 359 continue;
jbe@0 360 }
jbe@0 361 }
jbe@0 362 }
jbe@0 363 buffer[wpos] = current_char;
jbe@0 364 if (current_property->combining_class) {
jbe@0 365 if (current_property->combining_class > max_combining_class) {
jbe@0 366 max_combining_class = current_property->combining_class;
jbe@0 367 }
jbe@0 368 } else {
jbe@0 369 starter = buffer + wpos;
jbe@0 370 starter_property = NULL;
jbe@0 371 max_combining_class = -1;
jbe@0 372 }
jbe@0 373 wpos++;
jbe@0 374 }
jbe@0 375 length = wpos;
jbe@0 376 }
jbe@0 377 {
jbe@0 378 ssize_t rpos, wpos = 0;
jbe@0 379 int32_t uc;
jbe@0 380 for (rpos = 0; rpos < length; rpos++) {
jbe@0 381 uc = buffer[rpos];
jbe@0 382 wpos += utf8proc_encode_char(uc, ((uint8_t *)buffer) + wpos);
jbe@0 383 }
jbe@0 384 ((uint8_t *)buffer)[wpos] = 0;
jbe@0 385 return wpos;
jbe@0 386 }
jbe@0 387 }
jbe@0 388
jbe@0 389 ssize_t utf8proc_map(uint8_t *str, ssize_t strlen, uint8_t **dstptr,
jbe@0 390 int options) {
jbe@0 391 int32_t *buffer;
jbe@0 392 ssize_t result;
jbe@0 393 *dstptr = NULL;
jbe@0 394 result = utf8proc_decompose(str, strlen, NULL, 0, options);
jbe@0 395 if (result < 0) return result;
jbe@0 396 buffer = malloc(result * sizeof(int32_t) + 1);
jbe@0 397 if (!buffer) return UTF8PROC_ERROR_NOMEM;
jbe@0 398 result = utf8proc_decompose(str, strlen, buffer, result, options);
jbe@0 399 if (result < 0) {
jbe@0 400 free(buffer);
jbe@0 401 return result;
jbe@0 402 }
jbe@0 403 result = utf8proc_reencode(buffer, result, options);
jbe@0 404 if (result < 0) {
jbe@0 405 free(buffer);
jbe@0 406 return result;
jbe@0 407 }
jbe@0 408 {
jbe@0 409 int32_t *newptr;
jbe@0 410 newptr = realloc(buffer, result+1);
jbe@0 411 if (newptr) buffer = newptr;
jbe@0 412 }
jbe@0 413 *dstptr = (uint8_t *)buffer;
jbe@0 414 return result;
jbe@0 415 }
jbe@0 416
jbe@0 417 uint8_t *utf8proc_NFD(uint8_t *str) {
jbe@0 418 uint8_t *retval;
jbe@0 419 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE);
jbe@0 420 return retval;
jbe@0 421 }
jbe@0 422
jbe@0 423 uint8_t *utf8proc_NFC(uint8_t *str) {
jbe@0 424 uint8_t *retval;
jbe@0 425 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
jbe@0 426 UTF8PROC_COMPOSE);
jbe@0 427 return retval;
jbe@0 428 }
jbe@0 429
jbe@0 430 uint8_t *utf8proc_NFKD(uint8_t *str) {
jbe@0 431 uint8_t *retval;
jbe@0 432 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
jbe@0 433 UTF8PROC_COMPAT);
jbe@0 434 return retval;
jbe@0 435 }
jbe@0 436
jbe@0 437 uint8_t *utf8proc_NFKC(uint8_t *str) {
jbe@0 438 uint8_t *retval;
jbe@0 439 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
jbe@0 440 UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
jbe@0 441 return retval;
jbe@0 442 }
jbe@0 443
jbe@0 444

Impressum / About Us