1 /* 2 * Copyright (c) 2015 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other 3 * contributors. 4 * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 * DEALINGS IN THE SOFTWARE. 23 */ 24 25 26 /** 27 * @mainpage 28 * 29 * utf8proc is a free/open-source (MIT/expat licensed) C library 30 * providing Unicode normalization, case-folding, and other operations 31 * for strings in the UTF-8 encoding, supporting Unicode version 32 * 9.0.0. See the utf8proc home page (http://julialang.org/utf8proc/) 33 * for downloads and other information, or the source code on github 34 * (https://github.com/JuliaLang/utf8proc). 35 * 36 * For the utf8proc API documentation, see: @ref utf8proc.h 37 * 38 * The features of utf8proc include: 39 * 40 * - Transformation of strings (@ref utf8proc_map) to: 41 * - decompose (@ref UTF8PROC_DECOMPOSE) or compose (@ref UTF8PROC_COMPOSE) Unicode combining characters 42 *(http://en.wikipedia.org/wiki/Combining_character) 43 * - canonicalize Unicode compatibility characters (@ref UTF8PROC_COMPAT) 44 * - strip "ignorable" (@ref UTF8PROC_IGNORE) characters, control characters (@ref UTF8PROC_STRIPCC), or combining 45 * characters such as accents (@ref UTF8PROC_STRIPMARK) 46 * - case-folding (@ref UTF8PROC_CASEFOLD) 47 * - Unicode normalization: @ref utf8proc_NFD, @ref utf8proc_NFC, @ref utf8proc_NFKD, @ref utf8proc_NFKC 48 * - Detecting grapheme boundaries (@ref utf8proc_grapheme_break and @ref UTF8PROC_CHARBOUND) 49 * - Character-width computation: @ref utf8proc_charwidth 50 * - Classification of characters by Unicode category: @ref utf8proc_category and @ref utf8proc_category_string 51 * - Encode (@ref utf8proc_encode_char) and decode (@ref utf8proc_iterate) Unicode codepoints to/from UTF-8. 52 */ 53 54 /** @file */ 55 56 #ifndef UTF8PROC_H 57 #define UTF8PROC_H 58 59 /** @name API version 60 * 61 * The utf8proc API version MAJOR.MINOR.PATCH, following 62 * semantic-versioning rules (http://semver.org) based on API 63 * compatibility. 64 * 65 * This is also returned at runtime by @ref utf8proc_version; however, the 66 * runtime version may append a string like "-dev" to the version number 67 * for prerelease versions. 68 * 69 * @note The shared-library version number in the Makefile 70 * (and CMakeLists.txt, and MANIFEST) may be different, 71 * being based on ABI compatibility rather than API compatibility. 72 */ 73 /** @{ */ 74 /** The MAJOR version number (increased when backwards API compatibility is broken). */ 75 #define UTF8PROC_VERSION_MAJOR 2 76 /** The MINOR version number (increased when new functionality is added in a backwards-compatible manner). */ 77 #define UTF8PROC_VERSION_MINOR 2 78 /** The PATCH version (increased for fixes that do not change the API). */ 79 #define UTF8PROC_VERSION_PATCH 0 80 /** @} */ 81 82 #include <stdlib.h> 83 84 #if defined(_MSC_VER) && _MSC_VER < 1800 85 // MSVC prior to 2013 lacked stdbool.h and inttypes.h 86 typedef signed char utf8proc_int8_t; 87 typedef unsigned char utf8proc_uint8_t; 88 typedef short utf8proc_int16_t; 89 typedef unsigned short utf8proc_uint16_t; 90 typedef int utf8proc_int32_t; 91 typedef unsigned int utf8proc_uint32_t; 92 # ifdef _WIN64 93 typedef __int64 utf8proc_ssize_t; 94 typedef unsigned __int64 utf8proc_size_t; 95 # else 96 typedef int utf8proc_ssize_t; 97 typedef unsigned int utf8proc_size_t; 98 # endif 99 # ifndef __cplusplus 100 // emulate C99 bool 101 typedef unsigned char utf8proc_bool; 102 # ifndef __bool_true_false_are_defined 103 # define false 0 104 # define true 1 105 # define __bool_true_false_are_defined 1 106 # endif 107 # else 108 typedef bool utf8proc_bool; 109 # endif 110 #else 111 # include <stddef.h> 112 # include <stdbool.h> 113 # include <inttypes.h> 114 typedef int8_t utf8proc_int8_t; 115 typedef uint8_t utf8proc_uint8_t; 116 typedef int16_t utf8proc_int16_t; 117 typedef uint16_t utf8proc_uint16_t; 118 typedef int32_t utf8proc_int32_t; 119 typedef uint32_t utf8proc_uint32_t; 120 typedef size_t utf8proc_size_t; 121 typedef ptrdiff_t utf8proc_ssize_t; 122 typedef bool utf8proc_bool; 123 #endif 124 #include <limits.h> 125 126 #define UTF8PROC_STATIC 127 128 #ifdef UTF8PROC_STATIC 129 # define UTF8PROC_DLLEXPORT 130 #else 131 # ifdef _WIN32 132 # ifdef UTF8PROC_EXPORTS 133 # define UTF8PROC_DLLEXPORT __declspec(dllexport) 134 # else 135 # define UTF8PROC_DLLEXPORT __declspec(dllimport) 136 # endif 137 # elif __GNUC__ >= 4 138 # define UTF8PROC_DLLEXPORT __attribute__((visibility("default"))) 139 # else 140 # define UTF8PROC_DLLEXPORT 141 # endif 142 #endif 143 144 #ifdef __cplusplus 145 extern "C" { 146 #endif 147 148 #ifndef SSIZE_MAX 149 #define SSIZE_MAX ((size_t) SIZE_MAX / 2) 150 #endif 151 152 #ifndef UINT16_MAX 153 # define UINT16_MAX 65535U 154 #endif 155 156 /** 157 * Option flags used by several functions in the library. 158 */ 159 typedef enum { 160 /** The given UTF-8 input is NULL terminated. */ 161 UTF8PROC_NULLTERM = (1 << 0), 162 /** Unicode Versioning Stability has to be respected. */ 163 UTF8PROC_STABLE = (1 << 1), 164 /** Compatibility decomposition (i.e. formatting information is lost). */ 165 UTF8PROC_COMPAT = (1 << 2), 166 /** Return a result with decomposed characters. */ 167 UTF8PROC_COMPOSE = (1 << 3), 168 /** Return a result with decomposed characters. */ 169 UTF8PROC_DECOMPOSE = (1 << 4), 170 /** Strip "default ignorable characters" such as SOFT-HYPHEN or ZERO-WIDTH-SPACE. */ 171 UTF8PROC_IGNORE = (1 << 5), 172 /** Return an error, if the input contains unassigned codepoints. */ 173 UTF8PROC_REJECTNA = (1 << 6), 174 /** 175 * Indicating that NLF-sequences (LF, CRLF, CR, NEL) are representing a 176 * line break, and should be converted to the codepoint for line 177 * separation (LS). 178 */ 179 UTF8PROC_NLF2LS = (1 << 7), 180 /** 181 * Indicating that NLF-sequences are representing a paragraph break, and 182 * should be converted to the codepoint for paragraph separation 183 * (PS). 184 */ 185 UTF8PROC_NLF2PS = (1 << 8), 186 /** Indicating that the meaning of NLF-sequences is unknown. */ 187 UTF8PROC_NLF2LF = (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS), 188 /** Strips and/or convers control characters. 189 * 190 * NLF-sequences are transformed into space, except if one of the 191 * NLF2LS/PS/LF options is given. HorizontalTab (HT) and FormFeed (FF) 192 * are treated as a NLF-sequence in this case. All other control 193 * characters are simply removed. 194 */ 195 UTF8PROC_STRIPCC = (1 << 9), 196 /** 197 * Performs unicode case folding, to be able to do a case-insensitive 198 * string comparison. 199 */ 200 UTF8PROC_CASEFOLD = (1 << 10), 201 /** 202 * Inserts 0xFF bytes at the beginning of each sequence which is 203 * representing a single grapheme cluster (see UAX#29). 204 */ 205 UTF8PROC_CHARBOUND = (1 << 11), 206 /** Lumps certain characters together. 207 * 208 * E.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-". See lump.md for details. 209 * 210 * If NLF2LF is set, this includes a transformation of paragraph and 211 * line separators to ASCII line-feed (LF). 212 */ 213 UTF8PROC_LUMP = (1 << 12), 214 /** Strips all character markings. 215 * 216 * This includes non-spacing, spacing and enclosing (i.e. accents). 217 * @note This option works only with @ref UTF8PROC_COMPOSE or 218 * @ref UTF8PROC_DECOMPOSE 219 */ 220 UTF8PROC_STRIPMARK = (1 << 13), 221 /** 222 * Strip unassigned codepoints. 223 */ 224 UTF8PROC_STRIPNA = (1 << 14), 225 } utf8proc_option_t; 226 227 /** @name Error codes 228 * Error codes being returned by almost all functions. 229 */ 230 /** @{ */ 231 /** Memory could not be allocated. */ 232 #define UTF8PROC_ERROR_NOMEM -1 233 /** The given string is too long to be processed. */ 234 #define UTF8PROC_ERROR_OVERFLOW -2 235 /** The given string is not a legal UTF-8 string. */ 236 #define UTF8PROC_ERROR_INVALIDUTF8 -3 237 /** The @ref UTF8PROC_REJECTNA flag was set and an unassigned codepoint was found. */ 238 #define UTF8PROC_ERROR_NOTASSIGNED -4 239 /** Invalid options have been used. */ 240 #define UTF8PROC_ERROR_INVALIDOPTS -5 241 /** @} */ 242 243 /* @name Types */ 244 245 /** Holds the value of a property. */ 246 typedef utf8proc_int16_t utf8proc_propval_t; 247 248 /** Struct containing information about a codepoint. */ 249 typedef struct utf8proc_property_struct { 250 /** 251 * Unicode category. 252 * @see utf8proc_category_t. 253 */ 254 utf8proc_propval_t category; 255 utf8proc_propval_t combining_class; 256 /** 257 * Bidirectional class. 258 * @see utf8proc_bidi_class_t. 259 */ 260 utf8proc_propval_t bidi_class; 261 /** 262 * @anchor Decomposition type. 263 * @see utf8proc_decomp_type_t. 264 */ 265 utf8proc_propval_t decomp_type; 266 utf8proc_uint16_t decomp_seqindex; 267 utf8proc_uint16_t casefold_seqindex; 268 utf8proc_uint16_t uppercase_seqindex; 269 utf8proc_uint16_t lowercase_seqindex; 270 utf8proc_uint16_t titlecase_seqindex; 271 utf8proc_uint16_t comb_index; 272 unsigned bidi_mirrored : 1; 273 unsigned comp_exclusion : 1; 274 /** 275 * Can this codepoint be ignored? 276 * 277 * Used by @ref utf8proc_decompose_char when @ref UTF8PROC_IGNORE is 278 * passed as an option. 279 */ 280 unsigned ignorable : 1; 281 unsigned control_boundary : 1; 282 /** The width of the codepoint. */ 283 unsigned charwidth : 2; 284 unsigned pad : 2; 285 /** 286 * Boundclass. 287 * @see utf8proc_boundclass_t. 288 */ 289 unsigned boundclass : 8; 290 } utf8proc_property_t; 291 292 /** Unicode categories. */ 293 typedef enum { 294 UTF8PROC_CATEGORY_CN = 0, /**< Other, not assigned */ 295 UTF8PROC_CATEGORY_LU = 1, /**< Letter, uppercase */ 296 UTF8PROC_CATEGORY_LL = 2, /**< Letter, lowercase */ 297 UTF8PROC_CATEGORY_LT = 3, /**< Letter, titlecase */ 298 UTF8PROC_CATEGORY_LM = 4, /**< Letter, modifier */ 299 UTF8PROC_CATEGORY_LO = 5, /**< Letter, other */ 300 UTF8PROC_CATEGORY_MN = 6, /**< Mark, nonspacing */ 301 UTF8PROC_CATEGORY_MC = 7, /**< Mark, spacing combining */ 302 UTF8PROC_CATEGORY_ME = 8, /**< Mark, enclosing */ 303 UTF8PROC_CATEGORY_ND = 9, /**< Number, decimal digit */ 304 UTF8PROC_CATEGORY_NL = 10, /**< Number, letter */ 305 UTF8PROC_CATEGORY_NO = 11, /**< Number, other */ 306 UTF8PROC_CATEGORY_PC = 12, /**< Punctuation, connector */ 307 UTF8PROC_CATEGORY_PD = 13, /**< Punctuation, dash */ 308 UTF8PROC_CATEGORY_PS = 14, /**< Punctuation, open */ 309 UTF8PROC_CATEGORY_PE = 15, /**< Punctuation, close */ 310 UTF8PROC_CATEGORY_PI = 16, /**< Punctuation, initial quote */ 311 UTF8PROC_CATEGORY_PF = 17, /**< Punctuation, final quote */ 312 UTF8PROC_CATEGORY_PO = 18, /**< Punctuation, other */ 313 UTF8PROC_CATEGORY_SM = 19, /**< Symbol, math */ 314 UTF8PROC_CATEGORY_SC = 20, /**< Symbol, currency */ 315 UTF8PROC_CATEGORY_SK = 21, /**< Symbol, modifier */ 316 UTF8PROC_CATEGORY_SO = 22, /**< Symbol, other */ 317 UTF8PROC_CATEGORY_ZS = 23, /**< Separator, space */ 318 UTF8PROC_CATEGORY_ZL = 24, /**< Separator, line */ 319 UTF8PROC_CATEGORY_ZP = 25, /**< Separator, paragraph */ 320 UTF8PROC_CATEGORY_CC = 26, /**< Other, control */ 321 UTF8PROC_CATEGORY_CF = 27, /**< Other, format */ 322 UTF8PROC_CATEGORY_CS = 28, /**< Other, surrogate */ 323 UTF8PROC_CATEGORY_CO = 29, /**< Other, private use */ 324 } utf8proc_category_t; 325 326 /** Bidirectional character classes. */ 327 typedef enum { 328 UTF8PROC_BIDI_CLASS_L = 1, /**< Left-to-Right */ 329 UTF8PROC_BIDI_CLASS_LRE = 2, /**< Left-to-Right Embedding */ 330 UTF8PROC_BIDI_CLASS_LRO = 3, /**< Left-to-Right Override */ 331 UTF8PROC_BIDI_CLASS_R = 4, /**< Right-to-Left */ 332 UTF8PROC_BIDI_CLASS_AL = 5, /**< Right-to-Left Arabic */ 333 UTF8PROC_BIDI_CLASS_RLE = 6, /**< Right-to-Left Embedding */ 334 UTF8PROC_BIDI_CLASS_RLO = 7, /**< Right-to-Left Override */ 335 UTF8PROC_BIDI_CLASS_PDF = 8, /**< Pop Directional Format */ 336 UTF8PROC_BIDI_CLASS_EN = 9, /**< European Number */ 337 UTF8PROC_BIDI_CLASS_ES = 10, /**< European Separator */ 338 UTF8PROC_BIDI_CLASS_ET = 11, /**< European Number Terminator */ 339 UTF8PROC_BIDI_CLASS_AN = 12, /**< Arabic Number */ 340 UTF8PROC_BIDI_CLASS_CS = 13, /**< Common Number Separator */ 341 UTF8PROC_BIDI_CLASS_NSM = 14, /**< Nonspacing Mark */ 342 UTF8PROC_BIDI_CLASS_BN = 15, /**< Boundary Neutral */ 343 UTF8PROC_BIDI_CLASS_B = 16, /**< Paragraph Separator */ 344 UTF8PROC_BIDI_CLASS_S = 17, /**< Segment Separator */ 345 UTF8PROC_BIDI_CLASS_WS = 18, /**< Whitespace */ 346 UTF8PROC_BIDI_CLASS_ON = 19, /**< Other Neutrals */ 347 UTF8PROC_BIDI_CLASS_LRI = 20, /**< Left-to-Right Isolate */ 348 UTF8PROC_BIDI_CLASS_RLI = 21, /**< Right-to-Left Isolate */ 349 UTF8PROC_BIDI_CLASS_FSI = 22, /**< First Strong Isolate */ 350 UTF8PROC_BIDI_CLASS_PDI = 23, /**< Pop Directional Isolate */ 351 } utf8proc_bidi_class_t; 352 353 /** Decomposition type. */ 354 typedef enum { 355 UTF8PROC_DECOMP_TYPE_FONT = 1, /**< Font */ 356 UTF8PROC_DECOMP_TYPE_NOBREAK = 2, /**< Nobreak */ 357 UTF8PROC_DECOMP_TYPE_INITIAL = 3, /**< Initial */ 358 UTF8PROC_DECOMP_TYPE_MEDIAL = 4, /**< Medial */ 359 UTF8PROC_DECOMP_TYPE_FINAL = 5, /**< Final */ 360 UTF8PROC_DECOMP_TYPE_ISOLATED = 6, /**< Isolated */ 361 UTF8PROC_DECOMP_TYPE_CIRCLE = 7, /**< Circle */ 362 UTF8PROC_DECOMP_TYPE_SUPER = 8, /**< Super */ 363 UTF8PROC_DECOMP_TYPE_SUB = 9, /**< Sub */ 364 UTF8PROC_DECOMP_TYPE_VERTICAL = 10, /**< Vertical */ 365 UTF8PROC_DECOMP_TYPE_WIDE = 11, /**< Wide */ 366 UTF8PROC_DECOMP_TYPE_NARROW = 12, /**< Narrow */ 367 UTF8PROC_DECOMP_TYPE_SMALL = 13, /**< Small */ 368 UTF8PROC_DECOMP_TYPE_SQUARE = 14, /**< Square */ 369 UTF8PROC_DECOMP_TYPE_FRACTION = 15, /**< Fraction */ 370 UTF8PROC_DECOMP_TYPE_COMPAT = 16, /**< Compat */ 371 } utf8proc_decomp_type_t; 372 373 /** Boundclass property. (TR29) */ 374 typedef enum { 375 UTF8PROC_BOUNDCLASS_START = 0, /**< Start */ 376 UTF8PROC_BOUNDCLASS_OTHER = 1, /**< Other */ 377 UTF8PROC_BOUNDCLASS_CR = 2, /**< Cr */ 378 UTF8PROC_BOUNDCLASS_LF = 3, /**< Lf */ 379 UTF8PROC_BOUNDCLASS_CONTROL = 4, /**< Control */ 380 UTF8PROC_BOUNDCLASS_EXTEND = 5, /**< Extend */ 381 UTF8PROC_BOUNDCLASS_L = 6, /**< L */ 382 UTF8PROC_BOUNDCLASS_V = 7, /**< V */ 383 UTF8PROC_BOUNDCLASS_T = 8, /**< T */ 384 UTF8PROC_BOUNDCLASS_LV = 9, /**< Lv */ 385 UTF8PROC_BOUNDCLASS_LVT = 10, /**< Lvt */ 386 UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR = 11, /**< Regional indicator */ 387 UTF8PROC_BOUNDCLASS_SPACINGMARK = 12, /**< Spacingmark */ 388 UTF8PROC_BOUNDCLASS_PREPEND = 13, /**< Prepend */ 389 UTF8PROC_BOUNDCLASS_ZWJ = 14, /**< Zero Width Joiner */ 390 UTF8PROC_BOUNDCLASS_E_BASE = 15, /**< Emoji Base */ 391 UTF8PROC_BOUNDCLASS_E_MODIFIER = 16, /**< Emoji Modifier */ 392 UTF8PROC_BOUNDCLASS_GLUE_AFTER_ZWJ = 17, /**< Glue_After_ZWJ */ 393 UTF8PROC_BOUNDCLASS_E_BASE_GAZ = 18, /**< E_BASE + GLUE_AFTER_ZJW */ 394 } utf8proc_boundclass_t; 395 396 /** 397 * Function pointer type passed to @ref utf8proc_map_custom and 398 * @ref utf8proc_decompose_custom, which is used to specify a user-defined 399 * mapping of codepoints to be applied in conjunction with other mappings. 400 */ 401 typedef utf8proc_int32_t (*utf8proc_custom_func)(utf8proc_int32_t codepoint, void *data); 402 403 /** 404 * Array containing the byte lengths of a UTF-8 encoded codepoint based 405 * on the first byte. 406 */ 407 UTF8PROC_DLLEXPORT extern const utf8proc_int8_t utf8proc_utf8class[256]; 408 409 /** 410 * Returns the utf8proc API version as a string MAJOR.MINOR.PATCH 411 * (http://semver.org format), possibly with a "-dev" suffix for 412 * development versions. 413 */ 414 UTF8PROC_DLLEXPORT const char* utf8proc_version(void); 415 416 /** 417 * Returns an informative error string for the given utf8proc error code 418 * (e.g. the error codes returned by @ref utf8proc_map). 419 */ 420 UTF8PROC_DLLEXPORT const char* utf8proc_errmsg(utf8proc_ssize_t errcode); 421 422 /** 423 * Reads a single codepoint from the UTF-8 sequence being pointed to by `str`. 424 * The maximum number of bytes read is `strlen`, unless `strlen` is 425 * negative (in which case up to 4 bytes are read). 426 * 427 * If a valid codepoint could be read, it is stored in the variable 428 * pointed to by `codepoint_ref`, otherwise that variable will be set to -1. 429 * In case of success, the number of bytes read is returned; otherwise, a 430 * negative error code is returned. 431 */ 432 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate( 433 const utf8proc_uint8_t *str, 434 utf8proc_ssize_t strlen, 435 utf8proc_int32_t *codepoint_ref); 436 437 /** 438 * Check if a codepoint is valid (regardless of whether it has been 439 * assigned a value by the current Unicode standard). 440 * 441 * @return 1 if the given `codepoint` is valid and otherwise return 0. 442 */ 443 UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t codepoint); 444 445 /** 446 * Encodes the codepoint as an UTF-8 string in the byte array pointed 447 * to by `dst`. This array must be at least 4 bytes long. 448 * 449 * In case of success the number of bytes written is returned, and 450 * otherwise 0 is returned. 451 * 452 * This function does not check whether `codepoint` is valid Unicode. 453 */ 454 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t codepoint, utf8proc_uint8_t *dst); 455 456 /** 457 * Look up the properties for a given codepoint. 458 * 459 * @param codepoint The Unicode codepoint. 460 * 461 * @returns 462 * A pointer to a (constant) struct containing information about 463 * the codepoint. 464 * @par 465 * If the codepoint is unassigned or invalid, a pointer to a special struct is 466 * returned in which `category` is 0 (@ref UTF8PROC_CATEGORY_CN). 467 */ 468 UTF8PROC_DLLEXPORT const utf8proc_property_t* utf8proc_get_property(utf8proc_int32_t codepoint); 469 470 /** Decompose a codepoint into an array of codepoints. 471 * 472 * @param codepoint the codepoint. 473 * @param dst the destination buffer. 474 * @param bufsize the size of the destination buffer. 475 * @param options one or more of the following flags: 476 * - @ref UTF8PROC_REJECTNA - return an error `codepoint` is unassigned 477 * - @ref UTF8PROC_IGNORE - strip "default ignorable" codepoints 478 * - @ref UTF8PROC_CASEFOLD - apply Unicode casefolding 479 * - @ref UTF8PROC_COMPAT - replace certain codepoints with their 480 * compatibility decomposition 481 * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster 482 * - @ref UTF8PROC_LUMP - lump certain different codepoints together 483 * - @ref UTF8PROC_STRIPMARK - remove all character marks 484 * - @ref UTF8PROC_STRIPNA - remove unassigned codepoints 485 * @param last_boundclass 486 * Pointer to an integer variable containing 487 * the previous codepoint's boundary class if the @ref UTF8PROC_CHARBOUND 488 * option is used. Otherwise, this parameter is ignored. 489 * 490 * @return 491 * In case of success, the number of codepoints written is returned; in case 492 * of an error, a negative error code is returned (@ref utf8proc_errmsg). 493 * @par 494 * If the number of written codepoints would be bigger than `bufsize`, the 495 * required buffer size is returned, while the buffer will be overwritten with 496 * undefined data. 497 */ 498 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char( 499 utf8proc_int32_t codepoint, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, 500 utf8proc_option_t options, int *last_boundclass 501 ); 502 503 /** 504 * The same as @ref utf8proc_decompose_char, but acts on a whole UTF-8 505 * string and orders the decomposed sequences correctly. 506 * 507 * If the @ref UTF8PROC_NULLTERM flag in `options` is set, processing 508 * will be stopped, when a NULL byte is encounted, otherwise `strlen` 509 * bytes are processed. The result (in the form of 32-bit unicode 510 * codepoints) is written into the buffer being pointed to by 511 * `buffer` (which must contain at least `bufsize` entries). In case of 512 * success, the number of codepoints written is returned; in case of an 513 * error, a negative error code is returned (@ref utf8proc_errmsg). 514 * See @ref utf8proc_decompose_custom to supply additional transformations. 515 * 516 * If the number of written codepoints would be bigger than `bufsize`, the 517 * required buffer size is returned, while the buffer will be overwritten with 518 * undefined data. 519 */ 520 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose( 521 const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, 522 utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options 523 ); 524 525 /** 526 * The same as @ref utf8proc_decompose, but also takes a `custom_func` mapping function 527 * that is called on each codepoint in `str` before any other transformations 528 * (along with a `custom_data` pointer that is passed through to `custom_func`). 529 * The `custom_func` argument is ignored if it is `NULL`. See also @ref utf8proc_map_custom. 530 */ 531 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom( 532 const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, 533 utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options, 534 utf8proc_custom_func custom_func, void *custom_data 535 ); 536 537 /** 538 * Normalizes the sequence of `length` codepoints pointed to by `buffer` 539 * in-place (i.e., the result is also stored in `buffer`). 540 * 541 * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode. 542 * @param length the length (in codepoints) of the buffer. 543 * @param options a bitwise or (`|`) of one or more of the following flags: 544 * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS 545 * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS 546 * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF 547 * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters 548 * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite 549 * codepoints 550 * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate 551 * the unicode versioning stability 552 * 553 * @return 554 * In case of success, the length (in codepoints) of the normalized UTF-32 string is 555 * returned; otherwise, a negative error code is returned (@ref utf8proc_errmsg). 556 * 557 * @warning The entries of the array pointed to by `str` have to be in the 558 * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash! 559 */ 560 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32( 561 utf8proc_int32_t *buffer, 562 utf8proc_ssize_t length, 563 utf8proc_option_t options); 564 565 /** 566 * Reencodes the sequence of `length` codepoints pointed to by `buffer` 567 * UTF-8 data in-place (i.e., the result is also stored in `buffer`). 568 * Can optionally normalize the UTF-32 sequence prior to UTF-8 conversion. 569 * 570 * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode. 571 * @param length the length (in codepoints) of the buffer. 572 * @param options a bitwise or (`|`) of one or more of the following flags: 573 * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS 574 * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS 575 * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF 576 * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters 577 * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite 578 * codepoints 579 * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate 580 * the unicode versioning stability 581 * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster 582 * 583 * @return 584 * In case of success, the length (in bytes) of the resulting nul-terminated 585 * UTF-8 string is returned; otherwise, a negative error code is returned 586 * (@ref utf8proc_errmsg). 587 * 588 * @warning The amount of free space pointed to by `buffer` must 589 * exceed the amount of the input data by one byte, and the 590 * entries of the array pointed to by `str` have to be in the 591 * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash! 592 */ 593 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode( 594 utf8proc_int32_t *buffer, 595 utf8proc_ssize_t length, 596 utf8proc_option_t options); 597 598 /** 599 * Given a pair of consecutive codepoints, return whether a grapheme break is 600 * permitted between them (as defined by the extended grapheme clusters in UAX#29). 601 * 602 * @param state Beginning with Version 29 (Unicode 9.0.0), this algorithm requires 603 * state to break graphemes. This state can be passed in as a pointer 604 * in the `state` argument and should initially be set to 0. If the 605 * state is not passed in (i.e. a null pointer is passed), UAX#29 rules 606 * GB10/12/13 which require this state will not be applied, essentially 607 * matching the rules in Unicode 8.0.0. 608 * 609 * @warning If the state parameter is used, `utf8proc_grapheme_break_stateful` must 610 * be called IN ORDER on ALL potential breaks in a string. 611 */ 612 UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful( 613 utf8proc_int32_t codepoint1, 614 utf8proc_int32_t codepoint2, 615 utf8proc_int32_t *state); 616 617 /** 618 * Same as @ref utf8proc_grapheme_break_stateful, except without support for the 619 * Unicode 9 additions to the algorithm. Supported for legacy reasons. 620 */ 621 UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2); 622 623 624 /** 625 * Given a codepoint `c`, return the codepoint of the corresponding 626 * lower-case character, if any; otherwise (if there is no lower-case 627 * variant, or if `c` is not a valid codepoint) return `c`. 628 */ 629 UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c); 630 631 /** 632 * Given a codepoint `c`, return the codepoint of the corresponding 633 * upper-case character, if any; otherwise (if there is no upper-case 634 * variant, or if `c` is not a valid codepoint) return `c`. 635 */ 636 UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c); 637 638 /** 639 * Given a codepoint `c`, return the codepoint of the corresponding 640 * title-case character, if any; otherwise (if there is no title-case 641 * variant, or if `c` is not a valid codepoint) return `c`. 642 */ 643 UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c); 644 645 /** 646 * Given a codepoint, return a character width analogous to `wcwidth(codepoint)`, 647 * except that a width of 0 is returned for non-printable codepoints 648 * instead of -1 as in `wcwidth`. 649 * 650 * @note 651 * If you want to check for particular types of non-printable characters, 652 * (analogous to `isprint` or `iscntrl`), use @ref utf8proc_category. */ 653 UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t codepoint); 654 655 /** 656 * Return the Unicode category for the codepoint (one of the 657 * @ref utf8proc_category_t constants.) 658 */ 659 UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t codepoint); 660 661 /** 662 * Return the two-letter (nul-terminated) Unicode category string for 663 * the codepoint (e.g. `"Lu"` or `"Co"`). 664 */ 665 UTF8PROC_DLLEXPORT const char* utf8proc_category_string(utf8proc_int32_t codepoint); 666 667 /** 668 * Maps the given UTF-8 string pointed to by `str` to a new UTF-8 669 * string, allocated dynamically by `malloc` and returned via `dstptr`. 670 * 671 * If the @ref UTF8PROC_NULLTERM flag in the `options` field is set, 672 * the length is determined by a NULL terminator, otherwise the 673 * parameter `strlen` is evaluated to determine the string length, but 674 * in any case the result will be NULL terminated (though it might 675 * contain NULL characters with the string if `str` contained NULL 676 * characters). Other flags in the `options` field are passed to the 677 * functions defined above, and regarded as described. See also 678 * @ref utfproc_map_custom to supply a custom codepoint transformation. 679 * 680 * In case of success the length of the new string is returned, 681 * otherwise a negative error code is returned. 682 * 683 * @note The memory of the new UTF-8 string will have been allocated 684 * with `malloc`, and should therefore be deallocated with `free`. 685 */ 686 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map( 687 const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options 688 ); 689 690 /** 691 * Like @ref utf8proc_map, but also takes a `custom_func` mapping function 692 * that is called on each codepoint in `str` before any other transformations 693 * (along with a `custom_data` pointer that is passed through to `custom_func`). 694 * The `custom_func` argument is ignored if it is `NULL`. 695 */ 696 UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom( 697 const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options, 698 utf8proc_custom_func custom_func, void *custom_data 699 ); 700 701 /** @name Unicode normalization 702 * 703 * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD, NFKC or 704 * NFKC_Casefold normalized version of the null-terminated string `str`. These 705 * are shortcuts to calling @ref utf8proc_map with @ref UTF8PROC_NULLTERM 706 * combined with @ref UTF8PROC_STABLE and flags indicating the normalization. 707 */ 708 /** @{ */ 709 /** NFD normalization (@ref UTF8PROC_DECOMPOSE). */ 710 UTF8PROC_DLLEXPORT utf8proc_uint8_t* utf8proc_NFD(const utf8proc_uint8_t *str); 711 712 /** NFC normalization (@ref UTF8PROC_COMPOSE). */ 713 UTF8PROC_DLLEXPORT utf8proc_uint8_t* utf8proc_NFC(const utf8proc_uint8_t *str); 714 715 /** NFKD normalization (@ref UTF8PROC_DECOMPOSE and @ref UTF8PROC_COMPAT). */ 716 UTF8PROC_DLLEXPORT utf8proc_uint8_t* utf8proc_NFKD(const utf8proc_uint8_t *str); 717 718 /** NFKC normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT). */ 719 UTF8PROC_DLLEXPORT utf8proc_uint8_t* utf8proc_NFKC(const utf8proc_uint8_t *str); 720 721 /** 722 * NFKC_Casefold normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT 723 * and @ref UTF8PROC_CASEFOLD and @ref UTF8PROC_IGNORE). 724 **/ 725 UTF8PROC_DLLEXPORT utf8proc_uint8_t* utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str); 726 727 /** @} */ 728 729 #ifdef __cplusplus 730 } 731 #endif 732 733 #endif 734