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