utf8proc

A clean C library for processing UTF-8 Unicode data
git clone https://git.sinitax.com/juliastrings/utf8proc
Log | Files | Refs | README | LICENSE | sfeed.txt

utf8proc.h (31394B)


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