[unicode] Upgrading to latest utf8proc from JuliaLang (Unicode 8)

This commit is contained in:
Al
2015-12-23 19:30:52 -05:00
parent 3fbb3c587a
commit 5e6d24ff7e
3 changed files with 11377 additions and 10672 deletions

View File

@@ -1,4 +1,6 @@
/* -*- mode: c; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*- */
/* /*
* Copyright (c) 2015 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other contributors.
* Copyright (c) 2009 Public Software Group e. V., Berlin, Germany * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
@@ -43,7 +45,7 @@
#include "utf8proc_data.c" #include "utf8proc_data.c"
DLLEXPORT const int8_t utf8proc_utf8class[256] = { UTF8PROC_DLLEXPORT const utf8proc_int8_t utf8proc_utf8class[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -84,11 +86,13 @@ DLLEXPORT const int8_t utf8proc_utf8class[256] = {
/* Should follow semantic-versioning rules (semver.org) based on API /* Should follow semantic-versioning rules (semver.org) based on API
compatibility. (Note that the shared-library version number will compatibility. (Note that the shared-library version number will
be different, being based on ABI compatibility.): */ be different, being based on ABI compatibility.): */
DLLEXPORT const char *utf8proc_version(void) { #define STRINGIZEx(x) #x
return "1.2-dev"; #define STRINGIZE(x) STRINGIZEx(x)
UTF8PROC_DLLEXPORT const char *utf8proc_version(void) {
return STRINGIZE(UTF8PROC_VERSION_MAJOR) "." STRINGIZE(UTF8PROC_VERSION_MINOR) "." STRINGIZE(UTF8PROC_VERSION_PATCH) "";
} }
DLLEXPORT const char *utf8proc_errmsg(ssize_t errcode) { UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode) {
switch (errcode) { switch (errcode) {
case UTF8PROC_ERROR_NOMEM: case UTF8PROC_ERROR_NOMEM:
return "Memory for processing UTF-8 data could not be allocated."; return "Memory for processing UTF-8 data could not be allocated.";
@@ -105,54 +109,87 @@ DLLEXPORT const char *utf8proc_errmsg(ssize_t errcode) {
} }
} }
DLLEXPORT ssize_t utf8proc_iterate( #define utf_cont(ch) (((ch) & 0xc0) == 0x80)
const uint8_t *str, ssize_t strlen, int32_t *dst UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(
const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst
) { ) {
int length; utf8proc_uint32_t uc;
int i; const utf8proc_uint8_t *end;
int32_t uc = -1;
*dst = -1; *dst = -1;
if (!strlen) return 0; if (!strlen) return 0;
length = utf8proc_utf8class[str[0]]; end = str + ((strlen < 0) ? 4 : strlen);
if (!length) return UTF8PROC_ERROR_INVALIDUTF8; uc = *str++;
if (strlen >= 0 && length > strlen) return UTF8PROC_ERROR_INVALIDUTF8; if (uc < 0x80) {
for (i=1; i<length; i++) { *dst = uc;
if ((str[i] & 0xC0) != 0x80) return UTF8PROC_ERROR_INVALIDUTF8; return 1;
} }
switch (length) { // Must be between 0xc2 and 0xf4 inclusive to be valid
case 1: if ((uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
uc = str[0]; if (uc < 0xe0) { // 2-byte sequence
break; // Must have valid continuation character
case 2: if (!utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F); *dst = ((uc & 0x1f)<<6) | (*str & 0x3f);
if (uc < 0x80) uc = -1; return 2;
break;
case 3:
uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
+ (str[2] & 0x3F);
if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
(uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
break;
case 4:
uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
+ ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
if (uc < 0x10000 || uc >= 0x110000) uc = -1;
break;
} }
if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE)) if (uc < 0xf0) { // 3-byte sequence
if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1]))
return UTF8PROC_ERROR_INVALIDUTF8;
// Check for surrogate chars
if (uc == 0xed && *str > 0x9f)
return UTF8PROC_ERROR_INVALIDUTF8;
uc = ((uc & 0xf)<<12) | ((*str & 0x3f)<<6) | (str[1] & 0x3f);
if (uc < 0x800)
return UTF8PROC_ERROR_INVALIDUTF8; return UTF8PROC_ERROR_INVALIDUTF8;
*dst = uc; *dst = uc;
return length; return 3;
}
// 4-byte sequence
// Must have 3 valid continuation characters
if ((str + 2 >= end) || !utf_cont(*str) || !utf_cont(str[1]) || !utf_cont(str[2]))
return UTF8PROC_ERROR_INVALIDUTF8;
// Make sure in correct range (0x10000 - 0x10ffff)
if (uc == 0xf0) {
if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8;
} else if (uc == 0xf4) {
if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8;
}
*dst = ((uc & 7)<<18) | ((*str & 0x3f)<<12) | ((str[1] & 0x3f)<<6) | (str[2] & 0x3f);
return 4;
} }
DLLEXPORT bool utf8proc_codepoint_valid(int32_t uc) { UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t uc) {
if (uc < 0 || uc >= 0x110000 || return (((utf8proc_uint32_t)uc)-0xd800 > 0x07ff) && ((utf8proc_uint32_t)uc < 0x110000);
((uc & 0xFFFF) >= 0xFFFE) || (uc >= 0xD800 && uc < 0xE000) ||
(uc >= 0xFDD0 && uc < 0xFDF0)) return false;
else return true;
} }
DLLEXPORT ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst) { UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
if (uc < 0x00) {
return 0;
} else if (uc < 0x80) {
dst[0] = uc;
return 1;
} else if (uc < 0x800) {
dst[0] = 0xC0 + (uc >> 6);
dst[1] = 0x80 + (uc & 0x3F);
return 2;
// Note: we allow encoding 0xd800-0xdfff here, so as not to change
// the API, however, these are actually invalid in UTF-8
} else if (uc < 0x10000) {
dst[0] = 0xE0 + (uc >> 12);
dst[1] = 0x80 + ((uc >> 6) & 0x3F);
dst[2] = 0x80 + (uc & 0x3F);
return 3;
} else if (uc < 0x110000) {
dst[0] = 0xF0 + (uc >> 18);
dst[1] = 0x80 + ((uc >> 12) & 0x3F);
dst[2] = 0x80 + ((uc >> 6) & 0x3F);
dst[3] = 0x80 + (uc & 0x3F);
return 4;
} else return 0;
}
/* internal "unsafe" version that does not check whether uc is in range */
static utf8proc_ssize_t unsafe_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
if (uc < 0x00) { if (uc < 0x00) {
return 0; return 0;
} else if (uc < 0x80) { } else if (uc < 0x80) {
@@ -183,7 +220,7 @@ DLLEXPORT ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst) {
} }
/* internal "unsafe" version that does not check whether uc is in range */ /* internal "unsafe" version that does not check whether uc is in range */
static const utf8proc_property_t *get_property(int32_t uc) { static const utf8proc_property_t *unsafe_get_property(utf8proc_int32_t uc) {
/* ASSERT: uc >= 0 && uc < 0x110000 */ /* ASSERT: uc >= 0 && uc < 0x110000 */
return utf8proc_properties + ( return utf8proc_properties + (
utf8proc_stage2table[ utf8proc_stage2table[
@@ -192,12 +229,12 @@ static const utf8proc_property_t *get_property(int32_t uc) {
); );
} }
DLLEXPORT const utf8proc_property_t *utf8proc_get_property(int32_t uc) { UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t uc) {
return uc < 0 || uc >= 0x110000 ? utf8proc_properties : get_property(uc); return uc < 0 || uc >= 0x110000 ? utf8proc_properties : unsafe_get_property(uc);
} }
/* return whether there is a grapheme break between boundclasses lbc and tbc */ /* return whether there is a grapheme break between boundclasses lbc and tbc */
static bool grapheme_break(int lbc, int tbc) { static utf8proc_bool grapheme_break(int lbc, int tbc) {
return return
(lbc == UTF8PROC_BOUNDCLASS_START) ? true : (lbc == UTF8PROC_BOUNDCLASS_START) ? true :
(lbc == UTF8PROC_BOUNDCLASS_CR && (lbc == UTF8PROC_BOUNDCLASS_CR &&
@@ -223,22 +260,34 @@ static bool grapheme_break(int lbc, int tbc) {
} }
/* return whether there is a grapheme break between codepoints c1 and c2 */ /* return whether there is a grapheme break between codepoints c1 and c2 */
DLLEXPORT bool utf8proc_grapheme_break(int32_t c1, int32_t c2) { UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(utf8proc_int32_t c1, utf8proc_int32_t c2) {
return grapheme_break(utf8proc_get_property(c1)->boundclass, return grapheme_break(utf8proc_get_property(c1)->boundclass,
utf8proc_get_property(c2)->boundclass); utf8proc_get_property(c2)->boundclass);
} }
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c)
{
utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_mapping;
return cl >= 0 ? cl : c;
}
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c)
{
utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_mapping;
return cu >= 0 ? cu : c;
}
/* return a character width analogous to wcwidth (except portable and /* return a character width analogous to wcwidth (except portable and
hopefully less buggy than most system wcwidth functions). */ hopefully less buggy than most system wcwidth functions). */
DLLEXPORT int utf8proc_charwidth(int32_t c) { UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t c) {
return utf8proc_get_property(c)->charwidth; return utf8proc_get_property(c)->charwidth;
} }
DLLEXPORT int utf8proc_category(int32_t c) { UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t c) {
return utf8proc_get_property(c)->category; return utf8proc_get_property(c)->category;
} }
DLLEXPORT const char *utf8proc_category_string(int32_t c) { UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) {
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"}; 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"};
return s[utf8proc_category(c)]; return s[utf8proc_category(c)];
} }
@@ -247,17 +296,17 @@ DLLEXPORT const char *utf8proc_category_string(int32_t c) {
return utf8proc_decompose_char((replacement_uc), dst, bufsize, \ return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
options & ~UTF8PROC_LUMP, last_boundclass) options & ~UTF8PROC_LUMP, last_boundclass)
DLLEXPORT ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize, int options, int *last_boundclass) { 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) {
const utf8proc_property_t *property; const utf8proc_property_t *property;
utf8proc_propval_t category; utf8proc_propval_t category;
int32_t hangul_sindex; utf8proc_int32_t hangul_sindex;
if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED; if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED;
property = get_property(uc); property = unsafe_get_property(uc);
category = property->category; category = property->category;
hangul_sindex = uc - UTF8PROC_HANGUL_SBASE; hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) { if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) { if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
int32_t hangul_tindex; utf8proc_int32_t hangul_tindex;
if (bufsize >= 1) { if (bufsize >= 1) {
dst[0] = UTF8PROC_HANGUL_LBASE + dst[0] = UTF8PROC_HANGUL_LBASE +
hangul_sindex / UTF8PROC_HANGUL_NCOUNT; hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
@@ -308,10 +357,10 @@ DLLEXPORT ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufs
category == UTF8PROC_CATEGORY_ME) return 0; category == UTF8PROC_CATEGORY_ME) return 0;
} }
if (options & UTF8PROC_CASEFOLD) { if (options & UTF8PROC_CASEFOLD) {
if (property->casefold_mapping) { if (property->casefold_mapping != UINT16_MAX) {
const int32_t *casefold_entry; const utf8proc_int32_t *casefold_entry;
ssize_t written = 0; utf8proc_ssize_t written = 0;
for (casefold_entry = property->casefold_mapping; for (casefold_entry = &utf8proc_sequences[property->casefold_mapping];
*casefold_entry >= 0; casefold_entry++) { *casefold_entry >= 0; casefold_entry++) {
written += utf8proc_decompose_char(*casefold_entry, dst+written, written += utf8proc_decompose_char(*casefold_entry, dst+written,
(bufsize > written) ? (bufsize - written) : 0, options, (bufsize > written) ? (bufsize - written) : 0, options,
@@ -322,11 +371,11 @@ DLLEXPORT ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufs
} }
} }
if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) { if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
if (property->decomp_mapping && if (property->decomp_mapping != UINT16_MAX &&
(!property->decomp_type || (options & UTF8PROC_COMPAT))) { (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
const int32_t *decomp_entry; const utf8proc_int32_t *decomp_entry;
ssize_t written = 0; utf8proc_ssize_t written = 0;
for (decomp_entry = property->decomp_mapping; for (decomp_entry = &utf8proc_sequences[property->decomp_mapping];
*decomp_entry >= 0; decomp_entry++) { *decomp_entry >= 0; decomp_entry++) {
written += utf8proc_decompose_char(*decomp_entry, dst+written, written += utf8proc_decompose_char(*decomp_entry, dst+written,
(bufsize > written) ? (bufsize - written) : 0, options, (bufsize > written) ? (bufsize - written) : 0, options,
@@ -337,7 +386,7 @@ DLLEXPORT ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufs
} }
} }
if (options & UTF8PROC_CHARBOUND) { if (options & UTF8PROC_CHARBOUND) {
bool boundary; utf8proc_bool boundary;
int tbc = property->boundclass; int tbc = property->boundclass;
boundary = grapheme_break(*last_boundclass, tbc); boundary = grapheme_break(*last_boundclass, tbc);
*last_boundclass = tbc; *last_boundclass = tbc;
@@ -351,21 +400,21 @@ DLLEXPORT ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufs
return 1; return 1;
} }
DLLEXPORT ssize_t utf8proc_decompose( UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
const uint8_t *str, ssize_t strlen, const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
int32_t *buffer, ssize_t bufsize, int options utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
) { ) {
/* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */ /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */
ssize_t wpos = 0; utf8proc_ssize_t wpos = 0;
if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE)) if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
return UTF8PROC_ERROR_INVALIDOPTS; return UTF8PROC_ERROR_INVALIDOPTS;
if ((options & UTF8PROC_STRIPMARK) && if ((options & UTF8PROC_STRIPMARK) &&
!(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE)) !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
return UTF8PROC_ERROR_INVALIDOPTS; return UTF8PROC_ERROR_INVALIDOPTS;
{ {
int32_t uc; utf8proc_int32_t uc;
ssize_t rpos = 0; utf8proc_ssize_t rpos = 0;
ssize_t decomp_result; utf8proc_ssize_t decomp_result;
int boundclass = UTF8PROC_BOUNDCLASS_START; int boundclass = UTF8PROC_BOUNDCLASS_START;
while (1) { while (1) {
if (options & UTF8PROC_NULLTERM) { if (options & UTF8PROC_NULLTERM) {
@@ -387,19 +436,20 @@ DLLEXPORT ssize_t utf8proc_decompose(
if (decomp_result < 0) return decomp_result; if (decomp_result < 0) return decomp_result;
wpos += decomp_result; wpos += decomp_result;
/* prohibiting integer overflows due to too long strings: */ /* prohibiting integer overflows due to too long strings: */
if (wpos < 0 || wpos > SSIZE_MAX/sizeof(int32_t)/2) if (wpos < 0 ||
wpos > (utf8proc_ssize_t)(SSIZE_MAX/sizeof(utf8proc_int32_t)/2))
return UTF8PROC_ERROR_OVERFLOW; return UTF8PROC_ERROR_OVERFLOW;
} }
} }
if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) { if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
ssize_t pos = 0; utf8proc_ssize_t pos = 0;
while (pos < wpos-1) { while (pos < wpos-1) {
int32_t uc1, uc2; utf8proc_int32_t uc1, uc2;
const utf8proc_property_t *property1, *property2; const utf8proc_property_t *property1, *property2;
uc1 = buffer[pos]; uc1 = buffer[pos];
uc2 = buffer[pos+1]; uc2 = buffer[pos+1];
property1 = get_property(uc1); property1 = unsafe_get_property(uc1);
property2 = get_property(uc2); property2 = unsafe_get_property(uc2);
if (property1->combining_class > property2->combining_class && if (property1->combining_class > property2->combining_class &&
property2->combining_class > 0) { property2->combining_class > 0) {
buffer[pos] = uc2; buffer[pos] = uc2;
@@ -413,13 +463,13 @@ DLLEXPORT ssize_t utf8proc_decompose(
return wpos; return wpos;
} }
DLLEXPORT ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options) { UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
/* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
ASSERT: 'buffer' has one spare byte of free space at the end! */ ASSERT: 'buffer' has one spare byte of free space at the end! */
if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) { if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
ssize_t rpos; utf8proc_ssize_t rpos;
ssize_t wpos = 0; utf8proc_ssize_t wpos = 0;
int32_t uc; utf8proc_int32_t uc;
for (rpos = 0; rpos < length; rpos++) { for (rpos = 0; rpos < length; rpos++) {
uc = buffer[rpos]; uc = buffer[rpos];
if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++; if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
@@ -448,23 +498,23 @@ DLLEXPORT ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options
length = wpos; length = wpos;
} }
if (options & UTF8PROC_COMPOSE) { if (options & UTF8PROC_COMPOSE) {
int32_t *starter = NULL; utf8proc_int32_t *starter = NULL;
int32_t current_char; utf8proc_int32_t current_char;
const utf8proc_property_t *starter_property = NULL, *current_property; const utf8proc_property_t *starter_property = NULL, *current_property;
utf8proc_propval_t max_combining_class = -1; utf8proc_propval_t max_combining_class = -1;
ssize_t rpos; utf8proc_ssize_t rpos;
ssize_t wpos = 0; utf8proc_ssize_t wpos = 0;
int32_t composition; utf8proc_int32_t composition;
for (rpos = 0; rpos < length; rpos++) { for (rpos = 0; rpos < length; rpos++) {
current_char = buffer[rpos]; current_char = buffer[rpos];
current_property = get_property(current_char); current_property = unsafe_get_property(current_char);
if (starter && current_property->combining_class > max_combining_class) { if (starter && current_property->combining_class > max_combining_class) {
/* combination perhaps possible */ /* combination perhaps possible */
int32_t hangul_lindex; utf8proc_int32_t hangul_lindex;
int32_t hangul_sindex; utf8proc_int32_t hangul_sindex;
hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE; hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) { if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
int32_t hangul_vindex; utf8proc_int32_t hangul_vindex;
hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE; hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) { if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
*starter = UTF8PROC_HANGUL_SBASE + *starter = UTF8PROC_HANGUL_SBASE +
@@ -477,7 +527,7 @@ DLLEXPORT ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options
hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE; hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT && if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
(hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) { (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
int32_t hangul_tindex; utf8proc_int32_t hangul_tindex;
hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE; hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) { if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
*starter += hangul_tindex; *starter += hangul_tindex;
@@ -486,7 +536,7 @@ DLLEXPORT ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options
} }
} }
if (!starter_property) { if (!starter_property) {
starter_property = get_property(*starter); starter_property = unsafe_get_property(*starter);
} }
if (starter_property->comb1st_index >= 0 && if (starter_property->comb1st_index >= 0 &&
current_property->comb2nd_index >= 0) { current_property->comb2nd_index >= 0) {
@@ -495,7 +545,7 @@ DLLEXPORT ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options
current_property->comb2nd_index current_property->comb2nd_index
]; ];
if (composition >= 0 && (!(options & UTF8PROC_STABLE) || if (composition >= 0 && (!(options & UTF8PROC_STABLE) ||
!(get_property(composition)->comp_exclusion))) { !(unsafe_get_property(composition)->comp_exclusion))) {
*starter = composition; *starter = composition;
starter_property = NULL; starter_property = NULL;
continue; continue;
@@ -517,26 +567,33 @@ DLLEXPORT ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options
length = wpos; length = wpos;
} }
{ {
ssize_t rpos, wpos = 0; utf8proc_ssize_t rpos, wpos = 0;
int32_t uc; utf8proc_int32_t uc;
if (options & UTF8PROC_CHARBOUND) {
for (rpos = 0; rpos < length; rpos++) { for (rpos = 0; rpos < length; rpos++) {
uc = buffer[rpos]; uc = buffer[rpos];
wpos += utf8proc_encode_char(uc, ((uint8_t *)buffer) + wpos); wpos += unsafe_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
} }
((uint8_t *)buffer)[wpos] = 0; } else {
for (rpos = 0; rpos < length; rpos++) {
uc = buffer[rpos];
wpos += utf8proc_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
}
}
((utf8proc_uint8_t *)buffer)[wpos] = 0;
return wpos; return wpos;
} }
} }
DLLEXPORT ssize_t utf8proc_map( UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
const uint8_t *str, ssize_t strlen, uint8_t **dstptr, int options const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
) { ) {
int32_t *buffer; utf8proc_int32_t *buffer;
ssize_t result; utf8proc_ssize_t result;
*dstptr = NULL; *dstptr = NULL;
result = utf8proc_decompose(str, strlen, NULL, 0, options); result = utf8proc_decompose(str, strlen, NULL, 0, options);
if (result < 0) return result; if (result < 0) return result;
buffer = (int32_t *) malloc(result * sizeof(int32_t) + 1); buffer = (utf8proc_int32_t *) malloc(result * sizeof(utf8proc_int32_t) + 1);
if (!buffer) return UTF8PROC_ERROR_NOMEM; if (!buffer) return UTF8PROC_ERROR_NOMEM;
result = utf8proc_decompose(str, strlen, buffer, result, options); result = utf8proc_decompose(str, strlen, buffer, result, options);
if (result < 0) { if (result < 0) {
@@ -549,38 +606,39 @@ DLLEXPORT ssize_t utf8proc_map(
return result; return result;
} }
{ {
int32_t *newptr; utf8proc_int32_t *newptr;
newptr = (int32_t *) realloc(buffer, (size_t)result+1); newptr = (utf8proc_int32_t *) realloc(buffer, (size_t)result+1);
if (newptr) buffer = newptr; if (newptr) buffer = newptr;
} }
*dstptr = (uint8_t *)buffer; *dstptr = (utf8proc_uint8_t *)buffer;
return result; return result;
} }
DLLEXPORT uint8_t *utf8proc_NFD(const uint8_t *str) { UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str) {
uint8_t *retval; utf8proc_uint8_t *retval;
utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE | utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
UTF8PROC_DECOMPOSE); UTF8PROC_DECOMPOSE);
return retval; return retval;
} }
DLLEXPORT uint8_t *utf8proc_NFC(const uint8_t *str) { UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str) {
uint8_t *retval; utf8proc_uint8_t *retval;
utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE | utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
UTF8PROC_COMPOSE); UTF8PROC_COMPOSE);
return retval; return retval;
} }
DLLEXPORT uint8_t *utf8proc_NFKD(const uint8_t *str) { UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str) {
uint8_t *retval; utf8proc_uint8_t *retval;
utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE | utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT); UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
return retval; return retval;
} }
DLLEXPORT uint8_t *utf8proc_NFKC(const uint8_t *str) { UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str) {
uint8_t *retval; utf8proc_uint8_t *retval;
utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE | utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
UTF8PROC_COMPOSE | UTF8PROC_COMPAT); UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
return retval; return retval;
} }

View File

@@ -1,4 +1,5 @@
/* /*
* Copyright (c) 2015 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other contributors.
* Copyright (c) 2009 Public Software Group e. V., Berlin, Germany * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
@@ -21,70 +22,106 @@
*/ */
/* /**
* File name: utf8proc.h * @mainpage
* *
* Description: * utf8proc is a free/open-source (MIT/expat licensed) C library
* Header files for utf8proc, which is a mapping tool for UTF-8 strings * providing Unicode normalization, case-folding, and other operations
* with following features: * for strings in the UTF-8 encoding, supporting Unicode version
* - decomposing and composing of strings * 8.0.0. See the utf8proc home page (http://julialang.org/utf8proc/)
* - replacing compatibility characters with their equivalents * for downloads and other information, or the source code on github
* - stripping of "default ignorable characters" * (https://github.com/JuliaLang/utf8proc).
* like SOFT-HYPHEN or ZERO-WIDTH-SPACE *
* - folding of certain characters for string comparison * For the utf8proc API documentation, see: @ref utf8proc.h
* (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-") *
* (see "LUMP" option) * The features of utf8proc include:
* - optional rejection of strings containing non-assigned code points *
* - stripping of control characters * - Transformation of strings (@ref utf8proc_map) to:
* - stripping of character marks (accents, etc.) * - decompose (@ref UTF8PROC_DECOMPOSE) or compose (@ref UTF8PROC_COMPOSE) Unicode combining characters (http://en.wikipedia.org/wiki/Combining_character)
* - transformation of LF, CRLF, CR and NEL to line-feed (LF) * - canonicalize Unicode compatibility characters (@ref UTF8PROC_COMPAT)
* or to the unicode chararacters for paragraph separation (PS) * - strip "ignorable" (@ref UTF8PROC_IGNORE) characters, control characters (@ref UTF8PROC_STRIPCC), or combining characters such as accents (@ref UTF8PROC_STRIPMARK)
* or line separation (LS). * - case-folding (@ref UTF8PROC_CASEFOLD)
* - unicode case folding (for case insensitive string comparisons) * - Unicode normalization: @ref utf8proc_NFD, @ref utf8proc_NFC, @ref utf8proc_NFKD, @ref utf8proc_NFKC
* - rejection of illegal UTF-8 data * - Detecting grapheme boundaries (@ref utf8proc_grapheme_break and @ref UTF8PROC_CHARBOUND)
* (i.e. UTF-8 encoded UTF-16 surrogates) * - Character-width computation: @ref utf8proc_charwidth
* - support for korean hangul characters * - Classification of characters by Unicode category: @ref utf8proc_category and @ref utf8proc_category_string
* Unicode Version 7.0.0 is supported. * - Encode (@ref utf8proc_encode_char) and decode (@ref utf8proc_iterate) Unicode codepoints to/from UTF-8.
*/ */
/** @file */
#ifndef UTF8PROC_H #ifndef UTF8PROC_H
#define UTF8PROC_H #define UTF8PROC_H
/** @name API version
*
* The utf8proc API version MAJOR.MINOR.PATCH, following
* semantic-versioning rules (http://semver.org) based on API
* compatibility.
*
* This is also returned at runtime by @ref utf8proc_version; however, the
* runtime version may append a string like "-dev" to the version number
* for prerelease versions.
*
* @note The shared-library version number in the Makefile may be different,
* being based on ABI compatibility rather than API compatibility.
*/
/** @{ */
/** The MAJOR version number (increased when backwards API compatibility is broken). */
#define UTF8PROC_VERSION_MAJOR 1
/** The MINOR version number (increased when new functionality is added in a backwards-compatible manner). */
#define UTF8PROC_VERSION_MINOR 3
/** The PATCH version (increased for fixes that do not change the API). */
#define UTF8PROC_VERSION_PATCH 0
/** @} */
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef _MSC_VER #ifdef _MSC_VER
typedef signed char int8_t; typedef signed char utf8proc_int8_t;
typedef unsigned char uint8_t; typedef unsigned char utf8proc_uint8_t;
typedef short int16_t; typedef short utf8proc_int16_t;
typedef unsigned short uint16_t; typedef unsigned short utf8proc_uint16_t;
typedef int int32_t; typedef int utf8proc_int32_t;
typedef unsigned int utf8proc_uint32_t;
# ifdef _WIN64 # ifdef _WIN64
# define ssize_t __int64 typedef __int64 utf8proc_ssize_t;
typedef unsigned __int64 utf8proc_size_t;
# else # else
# define ssize_t int typedef int utf8proc_ssize_t;
typedef unsigned int utf8proc_size_t;
# endif # endif
# ifndef __cplusplus # ifndef __cplusplus
typedef unsigned char bool; typedef unsigned char utf8proc_bool;
enum {false, true}; enum {false, true};
# else
typedef bool utf8proc_bool;
# endif # endif
#else #else
# include <stdbool.h> # include <stdbool.h>
# include <inttypes.h> # include <inttypes.h>
typedef int8_t utf8proc_int8_t;
typedef uint8_t utf8proc_uint8_t;
typedef int16_t utf8proc_int16_t;
typedef uint16_t utf8proc_uint16_t;
typedef int32_t utf8proc_int32_t;
typedef uint32_t utf8proc_uint32_t;
typedef size_t utf8proc_size_t;
typedef ssize_t utf8proc_ssize_t;
typedef bool utf8proc_bool;
#endif #endif
#include <limits.h> #include <limits.h>
#ifdef _WIN32 #ifdef _WIN32
# ifdef UTF8PROC_EXPORTS # ifdef UTF8PROC_EXPORTS
# define DLLEXPORT __declspec(dllexport) # define UTF8PROC_DLLEXPORT __declspec(dllexport)
# else # else
# define DLLEXPORT __declspec(dllimport) # define UTF8PROC_DLLEXPORT __declspec(dllimport)
# endif # endif
#elif __GNUC__ >= 4 #elif __GNUC__ >= 4
# define DLLEXPORT __attribute__ ((visibility("default"))) # define UTF8PROC_DLLEXPORT __attribute__ ((visibility("default")))
#else #else
# define DLLEXPORT # define UTF8PROC_DLLEXPORT
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
@@ -95,341 +132,470 @@ extern "C" {
#define SSIZE_MAX ((size_t)SIZE_MAX/2) #define SSIZE_MAX ((size_t)SIZE_MAX/2)
#endif #endif
#define UTF8PROC_NULLTERM (1<<0) #ifndef UINT16_MAX
#define UTF8PROC_STABLE (1<<1) # define UINT16_MAX ~(utf8proc_uint16_t)0
#define UTF8PROC_COMPAT (1<<2) #endif
#define UTF8PROC_COMPOSE (1<<3)
#define UTF8PROC_DECOMPOSE (1<<4)
#define UTF8PROC_IGNORE (1<<5)
#define UTF8PROC_REJECTNA (1<<6)
#define UTF8PROC_NLF2LS (1<<7)
#define UTF8PROC_NLF2PS (1<<8)
#define UTF8PROC_NLF2LF (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS)
#define UTF8PROC_STRIPCC (1<<9)
#define UTF8PROC_CASEFOLD (1<<10)
#define UTF8PROC_CHARBOUND (1<<11)
#define UTF8PROC_LUMP (1<<12)
#define UTF8PROC_STRIPMARK (1<<13)
/*
* Flags being regarded by several functions in the library:
* NULLTERM: The given UTF-8 input is NULL terminated.
* STABLE: Unicode Versioning Stability has to be respected.
* COMPAT: Compatibility decomposition
* (i.e. formatting information is lost)
* COMPOSE: Return a result with composed characters.
* DECOMPOSE: Return a result with decomposed characters.
* IGNORE: Strip "default ignorable characters"
* REJECTNA: Return an error, if the input contains unassigned
* code points.
* NLF2LS: Indicating that NLF-sequences (LF, CRLF, CR, NEL) are
* representing a line break, and should be converted to the
* unicode character for line separation (LS).
* NLF2PS: Indicating that NLF-sequences are representing a paragraph
* break, and should be converted to the unicode character for
* paragraph separation (PS).
* NLF2LF: Indicating that the meaning of NLF-sequences is unknown.
* STRIPCC: Strips and/or convers control characters.
* NLF-sequences are transformed into space, except if one of
* the NLF2LS/PS/LF options is given.
* HorizontalTab (HT) and FormFeed (FF) are treated as a
* NLF-sequence in this case.
* All other control characters are simply removed.
* CASEFOLD: Performs unicode case folding, to be able to do a
* case-insensitive string comparison.
* CHARBOUND: Inserts 0xFF bytes at the beginning of each sequence which
* is representing a single grapheme cluster (see UAX#29).
* LUMP: Lumps certain characters together
* (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-").
* (See lump.md for details.)
* If NLF2LF is set, this includes a transformation of
* paragraph and line separators to ASCII line-feed (LF).
* STRIPMARK: Strips all character markings
* (non-spacing, spacing and enclosing) (i.e. accents)
* NOTE: this option works only with COMPOSE or DECOMPOSE
*/
/**
* Option flags used by several functions in the library.
*/
typedef enum {
/** The given UTF-8 input is NULL terminated. */
UTF8PROC_NULLTERM = (1<<0),
/** Unicode Versioning Stability has to be respected. */
UTF8PROC_STABLE = (1<<1),
/** Compatibility decomposition (i.e. formatting information is lost). */
UTF8PROC_COMPAT = (1<<2),
/** Return a result with decomposed characters. */
UTF8PROC_COMPOSE = (1<<3),
/** Return a result with decomposed characters. */
UTF8PROC_DECOMPOSE = (1<<4),
/** Strip "default ignorable characters" such as SOFT-HYPHEN or ZERO-WIDTH-SPACE. */
UTF8PROC_IGNORE = (1<<5),
/** Return an error, if the input contains unassigned codepoints. */
UTF8PROC_REJECTNA = (1<<6),
/**
* Indicating that NLF-sequences (LF, CRLF, CR, NEL) are representing a
* line break, and should be converted to the codepoint for line
* separation (LS).
*/
UTF8PROC_NLF2LS = (1<<7),
/**
* Indicating that NLF-sequences are representing a paragraph break, and
* should be converted to the codepoint for paragraph separation
* (PS).
*/
UTF8PROC_NLF2PS = (1<<8),
/** Indicating that the meaning of NLF-sequences is unknown. */
UTF8PROC_NLF2LF = (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS),
/** Strips and/or convers control characters.
*
* NLF-sequences are transformed into space, except if one of the
* NLF2LS/PS/LF options is given. HorizontalTab (HT) and FormFeed (FF)
* are treated as a NLF-sequence in this case. All other control
* characters are simply removed.
*/
UTF8PROC_STRIPCC = (1<<9),
/**
* Performs unicode case folding, to be able to do a case-insensitive
* string comparison.
*/
UTF8PROC_CASEFOLD = (1<<10),
/**
* Inserts 0xFF bytes at the beginning of each sequence which is
* representing a single grapheme cluster (see UAX#29).
*/
UTF8PROC_CHARBOUND = (1<<11),
/** Lumps certain characters together.
*
* E.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-". See lump.md for details.
*
* If NLF2LF is set, this includes a transformation of paragraph and
* line separators to ASCII line-feed (LF).
*/
UTF8PROC_LUMP = (1<<12),
/** Strips all character markings.
*
* This includes non-spacing, spacing and enclosing (i.e. accents).
* @note This option works only with @ref UTF8PROC_COMPOSE or
* @ref UTF8PROC_DECOMPOSE
*/
UTF8PROC_STRIPMARK = (1<<13),
} utf8proc_option_t;
/** @name Error codes
* Error codes being returned by almost all functions.
*/
/** @{ */
/** Memory could not be allocated. */
#define UTF8PROC_ERROR_NOMEM -1 #define UTF8PROC_ERROR_NOMEM -1
/** The given string is too long to be processed. */
#define UTF8PROC_ERROR_OVERFLOW -2 #define UTF8PROC_ERROR_OVERFLOW -2
/** The given string is not a legal UTF-8 string. */
#define UTF8PROC_ERROR_INVALIDUTF8 -3 #define UTF8PROC_ERROR_INVALIDUTF8 -3
/** The @ref UTF8PROC_REJECTNA flag was set and an unassigned codepoint was found. */
#define UTF8PROC_ERROR_NOTASSIGNED -4 #define UTF8PROC_ERROR_NOTASSIGNED -4
/** Invalid options have been used. */
#define UTF8PROC_ERROR_INVALIDOPTS -5 #define UTF8PROC_ERROR_INVALIDOPTS -5
/* /** @} */
* Error codes being returned by almost all functions:
* ERROR_NOMEM: Memory could not be allocated.
* ERROR_OVERFLOW: The given string is too long to be processed.
* ERROR_INVALIDUTF8: The given string is not a legal UTF-8 string.
* ERROR_NOTASSIGNED: The REJECTNA flag was set,
* and an unassigned code point was found.
* ERROR_INVALIDOPTS: Invalid options have been used.
*/
typedef int16_t utf8proc_propval_t; /* @name Types */
/** Holds the value of a property. */
typedef utf8proc_int16_t utf8proc_propval_t;
/** Struct containing information about a codepoint. */
typedef struct utf8proc_property_struct { typedef struct utf8proc_property_struct {
/**
* Unicode category.
* @see utf8proc_category_t.
*/
utf8proc_propval_t category; utf8proc_propval_t category;
utf8proc_propval_t combining_class; utf8proc_propval_t combining_class;
/**
* Bidirectional class.
* @see utf8proc_bidi_class_t.
*/
utf8proc_propval_t bidi_class; utf8proc_propval_t bidi_class;
/**
* @anchor Decomposition type.
* @see utf8proc_decomp_type_t.
*/
utf8proc_propval_t decomp_type; utf8proc_propval_t decomp_type;
const int32_t *decomp_mapping; utf8proc_uint16_t decomp_mapping;
const int32_t *casefold_mapping; utf8proc_uint16_t casefold_mapping;
int32_t uppercase_mapping; utf8proc_int32_t uppercase_mapping;
int32_t lowercase_mapping; utf8proc_int32_t lowercase_mapping;
int32_t titlecase_mapping; utf8proc_int32_t titlecase_mapping;
int32_t comb1st_index; utf8proc_int32_t comb1st_index;
int32_t comb2nd_index; utf8proc_int32_t comb2nd_index;
unsigned bidi_mirrored:1; unsigned bidi_mirrored:1;
unsigned comp_exclusion:1; unsigned comp_exclusion:1;
/**
* Can this codepoint be ignored?
*
* Used by @ref utf8proc_decompose_char when @ref UTF8PROC_IGNORE is
* passed as an option.
*/
unsigned ignorable:1; unsigned ignorable:1;
unsigned control_boundary:1; unsigned control_boundary:1;
/**
* Boundclass.
* @see utf8proc_boundclass_t.
*/
unsigned boundclass:4; unsigned boundclass:4;
/** The width of the codepoint. */
unsigned charwidth:2; unsigned charwidth:2;
} utf8proc_property_t; } utf8proc_property_t;
#define UTF8PROC_CATEGORY_CN 0 /** Unicode categories. */
#define UTF8PROC_CATEGORY_LU 1 typedef enum {
#define UTF8PROC_CATEGORY_LL 2 UTF8PROC_CATEGORY_CN = 0, /**< Other, not assigned */
#define UTF8PROC_CATEGORY_LT 3 UTF8PROC_CATEGORY_LU = 1, /**< Letter, uppercase */
#define UTF8PROC_CATEGORY_LM 4 UTF8PROC_CATEGORY_LL = 2, /**< Letter, lowercase */
#define UTF8PROC_CATEGORY_LO 5 UTF8PROC_CATEGORY_LT = 3, /**< Letter, titlecase */
#define UTF8PROC_CATEGORY_MN 6 UTF8PROC_CATEGORY_LM = 4, /**< Letter, modifier */
#define UTF8PROC_CATEGORY_MC 7 UTF8PROC_CATEGORY_LO = 5, /**< Letter, other */
#define UTF8PROC_CATEGORY_ME 8 UTF8PROC_CATEGORY_MN = 6, /**< Mark, nonspacing */
#define UTF8PROC_CATEGORY_ND 9 UTF8PROC_CATEGORY_MC = 7, /**< Mark, spacing combining */
#define UTF8PROC_CATEGORY_NL 10 UTF8PROC_CATEGORY_ME = 8, /**< Mark, enclosing */
#define UTF8PROC_CATEGORY_NO 11 UTF8PROC_CATEGORY_ND = 9, /**< Number, decimal digit */
#define UTF8PROC_CATEGORY_PC 12 UTF8PROC_CATEGORY_NL = 10, /**< Number, letter */
#define UTF8PROC_CATEGORY_PD 13 UTF8PROC_CATEGORY_NO = 11, /**< Number, other */
#define UTF8PROC_CATEGORY_PS 14 UTF8PROC_CATEGORY_PC = 12, /**< Punctuation, connector */
#define UTF8PROC_CATEGORY_PE 15 UTF8PROC_CATEGORY_PD = 13, /**< Punctuation, dash */
#define UTF8PROC_CATEGORY_PI 16 UTF8PROC_CATEGORY_PS = 14, /**< Punctuation, open */
#define UTF8PROC_CATEGORY_PF 17 UTF8PROC_CATEGORY_PE = 15, /**< Punctuation, close */
#define UTF8PROC_CATEGORY_PO 18 UTF8PROC_CATEGORY_PI = 16, /**< Punctuation, initial quote */
#define UTF8PROC_CATEGORY_SM 19 UTF8PROC_CATEGORY_PF = 17, /**< Punctuation, final quote */
#define UTF8PROC_CATEGORY_SC 20 UTF8PROC_CATEGORY_PO = 18, /**< Punctuation, other */
#define UTF8PROC_CATEGORY_SK 21 UTF8PROC_CATEGORY_SM = 19, /**< Symbol, math */
#define UTF8PROC_CATEGORY_SO 22 UTF8PROC_CATEGORY_SC = 20, /**< Symbol, currency */
#define UTF8PROC_CATEGORY_ZS 23 UTF8PROC_CATEGORY_SK = 21, /**< Symbol, modifier */
#define UTF8PROC_CATEGORY_ZL 24 UTF8PROC_CATEGORY_SO = 22, /**< Symbol, other */
#define UTF8PROC_CATEGORY_ZP 25 UTF8PROC_CATEGORY_ZS = 23, /**< Separator, space */
#define UTF8PROC_CATEGORY_CC 26 UTF8PROC_CATEGORY_ZL = 24, /**< Separator, line */
#define UTF8PROC_CATEGORY_CF 27 UTF8PROC_CATEGORY_ZP = 25, /**< Separator, paragraph */
#define UTF8PROC_CATEGORY_CS 28 UTF8PROC_CATEGORY_CC = 26, /**< Other, control */
#define UTF8PROC_CATEGORY_CO 29 UTF8PROC_CATEGORY_CF = 27, /**< Other, format */
#define UTF8PROC_BIDI_CLASS_L 1 UTF8PROC_CATEGORY_CS = 28, /**< Other, surrogate */
#define UTF8PROC_BIDI_CLASS_LRE 2 UTF8PROC_CATEGORY_CO = 29, /**< Other, private use */
#define UTF8PROC_BIDI_CLASS_LRO 3 } utf8proc_category_t;
#define UTF8PROC_BIDI_CLASS_R 4
#define UTF8PROC_BIDI_CLASS_AL 5
#define UTF8PROC_BIDI_CLASS_RLE 6
#define UTF8PROC_BIDI_CLASS_RLO 7
#define UTF8PROC_BIDI_CLASS_PDF 8
#define UTF8PROC_BIDI_CLASS_EN 9
#define UTF8PROC_BIDI_CLASS_ES 10
#define UTF8PROC_BIDI_CLASS_ET 11
#define UTF8PROC_BIDI_CLASS_AN 12
#define UTF8PROC_BIDI_CLASS_CS 13
#define UTF8PROC_BIDI_CLASS_NSM 14
#define UTF8PROC_BIDI_CLASS_BN 15
#define UTF8PROC_BIDI_CLASS_B 16
#define UTF8PROC_BIDI_CLASS_S 17
#define UTF8PROC_BIDI_CLASS_WS 18
#define UTF8PROC_BIDI_CLASS_ON 19
#define UTF8PROC_BIDI_CLASS_LRI 20 /* new in Unicode 6.3 */
#define UTF8PROC_BIDI_CLASS_RLI 21 /* new in Unicode 6.3 */
#define UTF8PROC_BIDI_CLASS_FSI 22 /* new in Unicode 6.3 */
#define UTF8PROC_BIDI_CLASS_PDI 23 /* new in Unicode 6.3 */
#define UTF8PROC_DECOMP_TYPE_FONT 1
#define UTF8PROC_DECOMP_TYPE_NOBREAK 2
#define UTF8PROC_DECOMP_TYPE_INITIAL 3
#define UTF8PROC_DECOMP_TYPE_MEDIAL 4
#define UTF8PROC_DECOMP_TYPE_FINAL 5
#define UTF8PROC_DECOMP_TYPE_ISOLATED 6
#define UTF8PROC_DECOMP_TYPE_CIRCLE 7
#define UTF8PROC_DECOMP_TYPE_SUPER 8
#define UTF8PROC_DECOMP_TYPE_SUB 9
#define UTF8PROC_DECOMP_TYPE_VERTICAL 10
#define UTF8PROC_DECOMP_TYPE_WIDE 11
#define UTF8PROC_DECOMP_TYPE_NARROW 12
#define UTF8PROC_DECOMP_TYPE_SMALL 13
#define UTF8PROC_DECOMP_TYPE_SQUARE 14
#define UTF8PROC_DECOMP_TYPE_FRACTION 15
#define UTF8PROC_DECOMP_TYPE_COMPAT 16
/* values for boundclass property: */ /** Bidirectional character classes. */
#define UTF8PROC_BOUNDCLASS_START 0 typedef enum {
#define UTF8PROC_BOUNDCLASS_OTHER 1 UTF8PROC_BIDI_CLASS_L = 1, /**< Left-to-Right */
#define UTF8PROC_BOUNDCLASS_CR 2 UTF8PROC_BIDI_CLASS_LRE = 2, /**< Left-to-Right Embedding */
#define UTF8PROC_BOUNDCLASS_LF 3 UTF8PROC_BIDI_CLASS_LRO = 3, /**< Left-to-Right Override */
#define UTF8PROC_BOUNDCLASS_CONTROL 4 UTF8PROC_BIDI_CLASS_R = 4, /**< Right-to-Left */
#define UTF8PROC_BOUNDCLASS_EXTEND 5 UTF8PROC_BIDI_CLASS_AL = 5, /**< Right-to-Left Arabic */
#define UTF8PROC_BOUNDCLASS_L 6 UTF8PROC_BIDI_CLASS_RLE = 6, /**< Right-to-Left Embedding */
#define UTF8PROC_BOUNDCLASS_V 7 UTF8PROC_BIDI_CLASS_RLO = 7, /**< Right-to-Left Override */
#define UTF8PROC_BOUNDCLASS_T 8 UTF8PROC_BIDI_CLASS_PDF = 8, /**< Pop Directional Format */
#define UTF8PROC_BOUNDCLASS_LV 9 UTF8PROC_BIDI_CLASS_EN = 9, /**< European Number */
#define UTF8PROC_BOUNDCLASS_LVT 10 UTF8PROC_BIDI_CLASS_ES = 10, /**< European Separator */
#define UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR 11 UTF8PROC_BIDI_CLASS_ET = 11, /**< European Number Terminator */
#define UTF8PROC_BOUNDCLASS_SPACINGMARK 12 UTF8PROC_BIDI_CLASS_AN = 12, /**< Arabic Number */
UTF8PROC_BIDI_CLASS_CS = 13, /**< Common Number Separator */
UTF8PROC_BIDI_CLASS_NSM = 14, /**< Nonspacing Mark */
UTF8PROC_BIDI_CLASS_BN = 15, /**< Boundary Neutral */
UTF8PROC_BIDI_CLASS_B = 16, /**< Paragraph Separator */
UTF8PROC_BIDI_CLASS_S = 17, /**< Segment Separator */
UTF8PROC_BIDI_CLASS_WS = 18, /**< Whitespace */
UTF8PROC_BIDI_CLASS_ON = 19, /**< Other Neutrals */
UTF8PROC_BIDI_CLASS_LRI = 20, /**< Left-to-Right Isolate */
UTF8PROC_BIDI_CLASS_RLI = 21, /**< Right-to-Left Isolate */
UTF8PROC_BIDI_CLASS_FSI = 22, /**< First Strong Isolate */
UTF8PROC_BIDI_CLASS_PDI = 23, /**< Pop Directional Isolate */
} utf8proc_bidi_class_t;
DLLEXPORT extern const int8_t utf8proc_utf8class[256]; /** Decomposition type. */
typedef enum {
UTF8PROC_DECOMP_TYPE_FONT = 1, /**< Font */
UTF8PROC_DECOMP_TYPE_NOBREAK = 2, /**< Nobreak */
UTF8PROC_DECOMP_TYPE_INITIAL = 3, /**< Initial */
UTF8PROC_DECOMP_TYPE_MEDIAL = 4, /**< Medial */
UTF8PROC_DECOMP_TYPE_FINAL = 5, /**< Final */
UTF8PROC_DECOMP_TYPE_ISOLATED = 6, /**< Isolated */
UTF8PROC_DECOMP_TYPE_CIRCLE = 7, /**< Circle */
UTF8PROC_DECOMP_TYPE_SUPER = 8, /**< Super */
UTF8PROC_DECOMP_TYPE_SUB = 9, /**< Sub */
UTF8PROC_DECOMP_TYPE_VERTICAL = 10, /**< Vertical */
UTF8PROC_DECOMP_TYPE_WIDE = 11, /**< Wide */
UTF8PROC_DECOMP_TYPE_NARROW = 12, /**< Narrow */
UTF8PROC_DECOMP_TYPE_SMALL = 13, /**< Small */
UTF8PROC_DECOMP_TYPE_SQUARE = 14, /**< Square */
UTF8PROC_DECOMP_TYPE_FRACTION = 15, /**< Fraction */
UTF8PROC_DECOMP_TYPE_COMPAT = 16, /**< Compat */
} utf8proc_decomp_type_t;
DLLEXPORT const char *utf8proc_version(void); /** Boundclass property. */
typedef enum {
UTF8PROC_BOUNDCLASS_START = 0, /**< Start */
UTF8PROC_BOUNDCLASS_OTHER = 1, /**< Other */
UTF8PROC_BOUNDCLASS_CR = 2, /**< Cr */
UTF8PROC_BOUNDCLASS_LF = 3, /**< Lf */
UTF8PROC_BOUNDCLASS_CONTROL = 4, /**< Control */
UTF8PROC_BOUNDCLASS_EXTEND = 5, /**< Extend */
UTF8PROC_BOUNDCLASS_L = 6, /**< L */
UTF8PROC_BOUNDCLASS_V = 7, /**< V */
UTF8PROC_BOUNDCLASS_T = 8, /**< T */
UTF8PROC_BOUNDCLASS_LV = 9, /**< Lv */
UTF8PROC_BOUNDCLASS_LVT = 10, /**< Lvt */
UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR = 11, /**< Regional indicator */
UTF8PROC_BOUNDCLASS_SPACINGMARK = 12, /**< Spacingmark */
} utf8proc_boundclass_t;
DLLEXPORT const char *utf8proc_errmsg(ssize_t errcode); /**
/* * Array containing the byte lengths of a UTF-8 encoded codepoint based
* Returns a static error string for the given error code. * on the first byte.
*/ */
UTF8PROC_DLLEXPORT extern const utf8proc_int8_t utf8proc_utf8class[256];
DLLEXPORT ssize_t utf8proc_iterate(const uint8_t *str, ssize_t strlen, int32_t *dst); /**
/* * Returns the utf8proc API version as a string MAJOR.MINOR.PATCH
* Reads a single char from the UTF-8 sequence being pointed to by 'str'. * (http://semver.org format), possibly with a "-dev" suffix for
* The maximum number of bytes read is 'strlen', unless 'strlen' is * development versions.
* negative. */
* If a valid unicode char could be read, it is stored in the variable UTF8PROC_DLLEXPORT const char *utf8proc_version(void);
* being pointed to by 'dst', otherwise that variable will be set to -1.
* In case of success the number of bytes read is returned, otherwise a /**
* Returns an informative error string for the given utf8proc error code
* (e.g. the error codes returned by @ref utf8proc_map).
*/
UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode);
/**
* Reads a single codepoint from the UTF-8 sequence being pointed to by `str`.
* The maximum number of bytes read is `strlen`, unless `strlen` is
* negative (in which case up to 4 bytes are read).
*
* If a valid codepoint could be read, it is stored in the variable
* pointed to by `codepoint_ref`, otherwise that variable will be set to -1.
* In case of success, the number of bytes read is returned; otherwise, a
* negative error code is returned. * negative error code is returned.
*/ */
UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *codepoint_ref);
DLLEXPORT bool utf8proc_codepoint_valid(int32_t uc); /**
/* * Check if a codepoint is valid (regardless of whether it has been
* Returns 1, if the given unicode code-point is valid, otherwise 0. * assigned a value by the current Unicode standard).
*
* @return 1 if the given `codepoint` is valid and otherwise return 0.
*/ */
UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t codepoint);
DLLEXPORT ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst); /**
/* * Encodes the codepoint as an UTF-8 string in the byte array pointed
* Encodes the unicode char with the code point 'uc' as an UTF-8 string in * to by `dst`. This array must be at least 4 bytes long.
* the byte array being pointed to by 'dst'. This array has to be at least *
* 4 bytes long. * In case of success the number of bytes written is returned, and
* In case of success the number of bytes written is returned, * otherwise 0 is returned.
* otherwise 0. *
* This function does not check if 'uc' is a valid unicode code point. * This function does not check whether `codepoint` is valid Unicode.
*/ */
UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t codepoint, utf8proc_uint8_t *dst);
DLLEXPORT const utf8proc_property_t *utf8proc_get_property(int32_t uc); /**
/* * Look up the properties for a given codepoint.
* Returns a pointer to a (constant) struct containing information about *
* the unicode char with the given code point 'uc'. * @param codepoint The Unicode codepoint.
* If the character is not existent a pointer to a special struct is *
* returned, where 'category' is 0 (UTF8PROC_CATEGORY_CN). * @returns
* A pointer to a (constant) struct containing information about
* the codepoint.
* @par
* If the codepoint is unassigned or invalid, a pointer to a special struct is
* returned in which `category` is 0 (@ref UTF8PROC_CATEGORY_CN).
*/ */
UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t codepoint);
DLLEXPORT ssize_t utf8proc_decompose_char( /** Decompose a codepoint into an array of codepoints.
int32_t uc, int32_t *dst, ssize_t bufsize, *
int options, int *last_boundclass * @param codepoint the codepoint.
); * @param dst the destination buffer.
/* * @param bufsize the size of the destination buffer.
* Writes a decomposition of the unicode char 'uc' into the array being * @param options one or more of the following flags:
* pointed to by 'dst'. * - @ref UTF8PROC_REJECTNA - return an error `codepoint` is unassigned
* Following flags in the 'options' field are regarded: * - @ref UTF8PROC_IGNORE - strip "default ignorable" codepoints
* REJECTNA: an unassigned unicode code point leads to an error * - @ref UTF8PROC_CASEFOLD - apply Unicode casefolding
* IGNORE: "default ignorable" chars are stripped * - @ref UTF8PROC_COMPAT - replace certain codepoints with their
* CASEFOLD: unicode casefolding is applied
* COMPAT: replace certain characters with their
* compatibility decomposition * compatibility decomposition
* CHARBOUND: Inserts 0xFF bytes before each grapheme cluster * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
* LUMP: lumps certain different characters together * - @ref UTF8PROC_LUMP - lump certain different codepoints together
* STRIPMARK: removes all character marks * - @ref UTF8PROC_STRIPMARK - remove all character marks
* The pointer 'last_boundclass' has to point to an integer variable which * @param last_boundclass
* is storing the last character boundary class, if the CHARBOUND option * Pointer to an integer variable containing
* is used. * the previous codepoint's boundary class if the @ref UTF8PROC_CHARBOUND
* In case of success the number of chars written is returned, * option is used. Otherwise, this parameter is ignored.
* in case of an error, a negative error code is returned. *
* If the number of written chars would be bigger than 'bufsize', * @return
* the buffer (up to 'bufsize') has inpredictable data, and the needed * In case of success, the number of codepoints written is returned; in case
* buffer size is returned. * of an error, a negative error code is returned (@ref utf8proc_errmsg).
* @par
* If the number of written codepoints would be bigger than `bufsize`, the
* required buffer size is returned, while the buffer will be overwritten with
* undefined data.
*/ */
UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(
DLLEXPORT ssize_t utf8proc_decompose( utf8proc_int32_t codepoint, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize,
const uint8_t *str, ssize_t strlen, utf8proc_option_t options, int *last_boundclass
int32_t *buffer, ssize_t bufsize, int options
); );
/*
* Does the same as 'utf8proc_decompose_char', but acts on a whole UTF-8
* string, and orders the decomposed sequences correctly.
* If the NULLTERM flag in 'options' is set, processing will be stopped,
* when a NULL byte is encounted, otherwise 'strlen' bytes are processed.
* The result in form of unicode code points is written into the buffer
* being pointed to by 'buffer', having the length of 'bufsize' entries.
* In case of success the number of chars written is returned,
* in case of an error, a negative error code is returned.
* If the number of written chars would be bigger than 'bufsize',
* the buffer (up to 'bufsize') has inpredictable data, and the needed
* buffer size is returned.
*/
DLLEXPORT ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options); /**
/* * The same as @ref utf8proc_decompose_char, but acts on a whole UTF-8
* Reencodes the sequence of unicode characters given by the pointer * string and orders the decomposed sequences correctly.
* 'buffer' and 'length' as UTF-8. *
* The result is stored in the same memory area where the data is read. * If the @ref UTF8PROC_NULLTERM flag in `options` is set, processing
* Following flags in the 'options' field are regarded: * will be stopped, when a NULL byte is encounted, otherwise `strlen`
* NLF2LS: converts LF, CRLF, CR and NEL into LS * bytes are processed. The result (in the form of 32-bit unicode
* NLF2PS: converts LF, CRLF, CR and NEL into PS * codepoints) is written into the buffer being pointed to by
* NLF2LF: converts LF, CRLF, CR and NEL into LF * `buffer` (which must contain at least `bufsize` entries). In case of
* STRIPCC: strips or converts all non-affected control characters * success, the number of codepoints written is returned; in case of an
* COMPOSE: tries to combine decomposed characters into composite * error, a negative error code is returned (@ref utf8proc_errmsg).
* characters *
* STABLE: prohibits combining characters which would violate * If the number of written codepoints would be bigger than `bufsize`, the
* required buffer size is returned, while the buffer will be overwritten with
* undefined data.
*/
UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
);
/**
* Reencodes the sequence of `length` codepoints pointed to by `buffer`
* UTF-8 data in-place (i.e., the result is also stored in `buffer`).
*
* @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
* @param length the length (in codepoints) of the buffer.
* @param options a bitwise or (`|`) of one or more of the following flags:
* - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
* - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
* - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
* - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
* - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
* codepoints
* - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
* the unicode versioning stability * the unicode versioning stability
* In case of success the length of the resulting UTF-8 string is *
* returned, otherwise a negative error code is returned. * @return
* WARNING: The amount of free space being pointed to by 'buffer', has to * In case of success, the length (in bytes) of the resulting UTF-8 string is
* returned; otherwise, a negative error code is returned (@ref utf8proc_errmsg).
*
* @warning The amount of free space pointed to by `buffer` must
* exceed the amount of the input data by one byte, and the * exceed the amount of the input data by one byte, and the
* entries of the array pointed to by 'str' have to be in the * entries of the array pointed to by `str` have to be in the
* range of 0x0000 to 0x10FFFF, otherwise the program might * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
* crash!
*/ */
UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
DLLEXPORT bool utf8proc_grapheme_break(int32_t c1, int32_t c2); /**
/* * Given a pair of consecutive codepoints, return whether a grapheme break is
* Given a pair of consecutive codepoints (c1,c2), return whether a grapheme break is
* permitted between them (as defined by the extended grapheme clusters in UAX#29). * permitted between them (as defined by the extended grapheme clusters in UAX#29).
*/ */
UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2);
DLLEXPORT int utf8proc_charwidth(int32_t c);
/* Given a codepoint c, return a character width analogous to wcwidth(c),
except that a width of 0 is returned for non-printable characters
instead of -1 as in wcwidth.
If you want to check for particular types of non-printable characters,
(analogous to isprint or iscntrl), use utf8proc_category(c). */
DLLEXPORT int utf8proc_category(int32_t c); /**
/* Return the Unicode character category for c (one of the * Given a codepoint `c`, return the codepoint of the corresponding
UTF8PROC_CATEGORY_* constants.) */ * lower-case character, if any; otherwise (if there is no lower-case
* variant, or if `c` is not a valid codepoint) return `c`.
*/
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c);
DLLEXPORT const char *utf8proc_category_string(int32_t c); /**
/* Return the two-letter (nul-terminated) Unicode category string for * Given a codepoint `c`, return the codepoint of the corresponding
c (e.g. "Lu" or "Co"). */ * upper-case character, if any; otherwise (if there is no upper-case
* variant, or if `c` is not a valid codepoint) return `c`.
*/
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c);
DLLEXPORT ssize_t utf8proc_map( /**
const uint8_t *str, ssize_t strlen, uint8_t **dstptr, int options * Given a codepoint, return a character width analogous to `wcwidth(codepoint)`,
); * except that a width of 0 is returned for non-printable codepoints
/* * instead of -1 as in `wcwidth`.
* Maps the given UTF-8 string being pointed to by 'str' to a new UTF-8 *
* string, which is allocated dynamically, and afterwards pointed to by * @note
* the pointer being pointed to by 'dstptr'. * If you want to check for particular types of non-printable characters,
* If the NULLTERM flag in the 'options' field is set, the length is * (analogous to `isprint` or `iscntrl`), use @ref utf8proc_category. */
* determined by a NULL terminator, otherwise the parameter 'strlen' is UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t codepoint);
* evaluated to determine the string length, but in any case the result
* will be NULL terminated (though it might contain NULL characters /**
* before). Other flags in the 'options' field are passed to the functions * Return the Unicode category for the codepoint (one of the
* defined above, and regarded as described. * @ref utf8proc_category_t constants.)
*/
UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t codepoint);
/**
* Return the two-letter (nul-terminated) Unicode category string for
* the codepoint (e.g. `"Lu"` or `"Co"`).
*/
UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t codepoint);
/**
* Maps the given UTF-8 string pointed to by `str` to a new UTF-8
* string, allocated dynamically by `malloc` and returned via `dstptr`.
*
* If the @ref UTF8PROC_NULLTERM flag in the `options` field is set,
* the length is determined by a NULL terminator, otherwise the
* parameter `strlen` is evaluated to determine the string length, but
* in any case the result will be NULL terminated (though it might
* contain NULL characters with the string if `str` contained NULL
* characters). Other flags in the `options` field are passed to the
* functions defined above, and regarded as described.
*
* In case of success the length of the new string is returned, * In case of success the length of the new string is returned,
* otherwise a negative error code is returned. * otherwise a negative error code is returned.
* NOTICE: The memory of the new UTF-8 string will have been allocated with *
* 'malloc', and has theirfore to be freed with 'free'. * @note The memory of the new UTF-8 string will have been allocated
* with `malloc`, and should therefore be deallocated with `free`.
*/ */
UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
);
DLLEXPORT uint8_t *utf8proc_NFD(const uint8_t *str); /** @name Unicode normalization
DLLEXPORT uint8_t *utf8proc_NFC(const uint8_t *str); *
DLLEXPORT uint8_t *utf8proc_NFKD(const uint8_t *str);
DLLEXPORT uint8_t *utf8proc_NFKC(const uint8_t *str);
/*
* Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC
* normalized version of the null-terminated string 'str'. * normalized version of the null-terminated string `str`. These
* are shortcuts to calling @ref utf8proc_map with @ref UTF8PROC_NULLTERM
* combined with @ref UTF8PROC_STABLE and flags indicating the normalization.
*/ */
/** @{ */
/** NFD normalization (@ref UTF8PROC_DECOMPOSE). */
UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str);
/** NFC normalization (@ref UTF8PROC_COMPOSE). */
UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str);
/** NFD normalization (@ref UTF8PROC_DECOMPOSE and @ref UTF8PROC_COMPAT). */
UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str);
/** NFD normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT). */
UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str);
/** @} */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

File diff suppressed because it is too large Load Diff