vile / thirdparty / utf8proc / utf8proc.h
794 lines · 742 sloc · 33.0 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
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 11
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 composed 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 /**
259 * Character combining table.
260 *
261 * The character combining table is formally indexed by two
262 * characters, the first and second character that might form a
263 * combining pair. The table entry then contains the combined
264 * character. Most character pairs cannot be combined. There are
265 * about 1,000 characters that can be the first character in a
266 * combining pair, and for most, there are only a handful for
267 * possible second characters.
268 *
269 * The combining table is stored as sparse matrix in the CSR
270 * (compressed sparse row) format. That is, it is stored as two
271 * arrays, `utf8proc_uint32_t utf8proc_combinations_second[]` and
272 * `utf8proc_uint32_t utf8proc_combinations_combined[]`. These
273 * contain the second combining characters and the combined
274 * character of every combining pair.
275 *
276 * - `comb_index`: Index into the combining table if this character
277 * is the first character in a combining pair, else 0x3ff
278 *
279 * - `comb_length`: Number of table entries for this first character
280 *
281 * - `comb_is_second`: As optimization we also record whether this
282 * character is the second combining character in any pair. If
283 * not, we can skip the table lookup.
284 *
285 * A table lookup starts from a given character pair. It first
286 * checks whether the first character is stored in the table
287 * (checking whether the index is 0x3ff) and whether the second
288 * index is stored in the table (looking at `comb_is_second`). If
289 * so, the `comb_length` table entries will be checked sequentially
290 * for a match.
291 */
292 utf8proc_uint16_t comb_index:10;
293 utf8proc_uint16_t comb_length:5;
294 utf8proc_uint16_t comb_issecond:1;
295 unsigned bidi_mirrored:1;
296 unsigned comp_exclusion:1;
297 /**
298 * Can this codepoint be ignored?
299 *
300 * Used by utf8proc_decompose_char() when @ref UTF8PROC_IGNORE is
301 * passed as an option.
302 */
303 unsigned ignorable:1;
304 unsigned control_boundary:1;
305 /** The width of the codepoint. */
306 unsigned charwidth:2;
307 /** East Asian width class A */
308 unsigned ambiguous_width:1;
309 unsigned pad:1;
310 /**
311 * Boundclass.
312 * @see utf8proc_boundclass_t.
313 */
314 unsigned boundclass:6;
315 unsigned indic_conjunct_break:2;
316} utf8proc_property_t;
317
318/** Unicode categories. */
319typedef enum {
320 UTF8PROC_CATEGORY_CN = 0, /**< Other, not assigned */
321 UTF8PROC_CATEGORY_LU = 1, /**< Letter, uppercase */
322 UTF8PROC_CATEGORY_LL = 2, /**< Letter, lowercase */
323 UTF8PROC_CATEGORY_LT = 3, /**< Letter, titlecase */
324 UTF8PROC_CATEGORY_LM = 4, /**< Letter, modifier */
325 UTF8PROC_CATEGORY_LO = 5, /**< Letter, other */
326 UTF8PROC_CATEGORY_MN = 6, /**< Mark, nonspacing */
327 UTF8PROC_CATEGORY_MC = 7, /**< Mark, spacing combining */
328 UTF8PROC_CATEGORY_ME = 8, /**< Mark, enclosing */
329 UTF8PROC_CATEGORY_ND = 9, /**< Number, decimal digit */
330 UTF8PROC_CATEGORY_NL = 10, /**< Number, letter */
331 UTF8PROC_CATEGORY_NO = 11, /**< Number, other */
332 UTF8PROC_CATEGORY_PC = 12, /**< Punctuation, connector */
333 UTF8PROC_CATEGORY_PD = 13, /**< Punctuation, dash */
334 UTF8PROC_CATEGORY_PS = 14, /**< Punctuation, open */
335 UTF8PROC_CATEGORY_PE = 15, /**< Punctuation, close */
336 UTF8PROC_CATEGORY_PI = 16, /**< Punctuation, initial quote */
337 UTF8PROC_CATEGORY_PF = 17, /**< Punctuation, final quote */
338 UTF8PROC_CATEGORY_PO = 18, /**< Punctuation, other */
339 UTF8PROC_CATEGORY_SM = 19, /**< Symbol, math */
340 UTF8PROC_CATEGORY_SC = 20, /**< Symbol, currency */
341 UTF8PROC_CATEGORY_SK = 21, /**< Symbol, modifier */
342 UTF8PROC_CATEGORY_SO = 22, /**< Symbol, other */
343 UTF8PROC_CATEGORY_ZS = 23, /**< Separator, space */
344 UTF8PROC_CATEGORY_ZL = 24, /**< Separator, line */
345 UTF8PROC_CATEGORY_ZP = 25, /**< Separator, paragraph */
346 UTF8PROC_CATEGORY_CC = 26, /**< Other, control */
347 UTF8PROC_CATEGORY_CF = 27, /**< Other, format */
348 UTF8PROC_CATEGORY_CS = 28, /**< Other, surrogate */
349 UTF8PROC_CATEGORY_CO = 29, /**< Other, private use */
350} utf8proc_category_t;
351
352/** Bidirectional character classes. */
353typedef enum {
354 UTF8PROC_BIDI_CLASS_L = 1, /**< Left-to-Right */
355 UTF8PROC_BIDI_CLASS_LRE = 2, /**< Left-to-Right Embedding */
356 UTF8PROC_BIDI_CLASS_LRO = 3, /**< Left-to-Right Override */
357 UTF8PROC_BIDI_CLASS_R = 4, /**< Right-to-Left */
358 UTF8PROC_BIDI_CLASS_AL = 5, /**< Right-to-Left Arabic */
359 UTF8PROC_BIDI_CLASS_RLE = 6, /**< Right-to-Left Embedding */
360 UTF8PROC_BIDI_CLASS_RLO = 7, /**< Right-to-Left Override */
361 UTF8PROC_BIDI_CLASS_PDF = 8, /**< Pop Directional Format */
362 UTF8PROC_BIDI_CLASS_EN = 9, /**< European Number */
363 UTF8PROC_BIDI_CLASS_ES = 10, /**< European Separator */
364 UTF8PROC_BIDI_CLASS_ET = 11, /**< European Number Terminator */
365 UTF8PROC_BIDI_CLASS_AN = 12, /**< Arabic Number */
366 UTF8PROC_BIDI_CLASS_CS = 13, /**< Common Number Separator */
367 UTF8PROC_BIDI_CLASS_NSM = 14, /**< Nonspacing Mark */
368 UTF8PROC_BIDI_CLASS_BN = 15, /**< Boundary Neutral */
369 UTF8PROC_BIDI_CLASS_B = 16, /**< Paragraph Separator */
370 UTF8PROC_BIDI_CLASS_S = 17, /**< Segment Separator */
371 UTF8PROC_BIDI_CLASS_WS = 18, /**< Whitespace */
372 UTF8PROC_BIDI_CLASS_ON = 19, /**< Other Neutrals */
373 UTF8PROC_BIDI_CLASS_LRI = 20, /**< Left-to-Right Isolate */
374 UTF8PROC_BIDI_CLASS_RLI = 21, /**< Right-to-Left Isolate */
375 UTF8PROC_BIDI_CLASS_FSI = 22, /**< First Strong Isolate */
376 UTF8PROC_BIDI_CLASS_PDI = 23, /**< Pop Directional Isolate */
377} utf8proc_bidi_class_t;
378
379/** Decomposition type. */
380typedef enum {
381 UTF8PROC_DECOMP_TYPE_FONT = 1, /**< Font */
382 UTF8PROC_DECOMP_TYPE_NOBREAK = 2, /**< Nobreak */
383 UTF8PROC_DECOMP_TYPE_INITIAL = 3, /**< Initial */
384 UTF8PROC_DECOMP_TYPE_MEDIAL = 4, /**< Medial */
385 UTF8PROC_DECOMP_TYPE_FINAL = 5, /**< Final */
386 UTF8PROC_DECOMP_TYPE_ISOLATED = 6, /**< Isolated */
387 UTF8PROC_DECOMP_TYPE_CIRCLE = 7, /**< Circle */
388 UTF8PROC_DECOMP_TYPE_SUPER = 8, /**< Super */
389 UTF8PROC_DECOMP_TYPE_SUB = 9, /**< Sub */
390 UTF8PROC_DECOMP_TYPE_VERTICAL = 10, /**< Vertical */
391 UTF8PROC_DECOMP_TYPE_WIDE = 11, /**< Wide */
392 UTF8PROC_DECOMP_TYPE_NARROW = 12, /**< Narrow */
393 UTF8PROC_DECOMP_TYPE_SMALL = 13, /**< Small */
394 UTF8PROC_DECOMP_TYPE_SQUARE = 14, /**< Square */
395 UTF8PROC_DECOMP_TYPE_FRACTION = 15, /**< Fraction */
396 UTF8PROC_DECOMP_TYPE_COMPAT = 16, /**< Compat */
397} utf8proc_decomp_type_t;
398
399/** Boundclass property. (TR29) */
400typedef enum {
401 UTF8PROC_BOUNDCLASS_START = 0, /**< Start */
402 UTF8PROC_BOUNDCLASS_OTHER = 1, /**< Other */
403 UTF8PROC_BOUNDCLASS_CR = 2, /**< Cr */
404 UTF8PROC_BOUNDCLASS_LF = 3, /**< Lf */
405 UTF8PROC_BOUNDCLASS_CONTROL = 4, /**< Control */
406 UTF8PROC_BOUNDCLASS_EXTEND = 5, /**< Extend */
407 UTF8PROC_BOUNDCLASS_L = 6, /**< L */
408 UTF8PROC_BOUNDCLASS_V = 7, /**< V */
409 UTF8PROC_BOUNDCLASS_T = 8, /**< T */
410 UTF8PROC_BOUNDCLASS_LV = 9, /**< Lv */
411 UTF8PROC_BOUNDCLASS_LVT = 10, /**< Lvt */
412 UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR = 11, /**< Regional indicator */
413 UTF8PROC_BOUNDCLASS_SPACINGMARK = 12, /**< Spacingmark */
414 UTF8PROC_BOUNDCLASS_PREPEND = 13, /**< Prepend */
415 UTF8PROC_BOUNDCLASS_ZWJ = 14, /**< Zero Width Joiner */
416
417 /* the following are no longer used in Unicode 11, but we keep
418 the constants here for backward compatibility */
419 UTF8PROC_BOUNDCLASS_E_BASE = 15, /**< Emoji Base */
420 UTF8PROC_BOUNDCLASS_E_MODIFIER = 16, /**< Emoji Modifier */
421 UTF8PROC_BOUNDCLASS_GLUE_AFTER_ZWJ = 17, /**< Glue_After_ZWJ */
422 UTF8PROC_BOUNDCLASS_E_BASE_GAZ = 18, /**< E_BASE + GLUE_AFTER_ZJW */
423
424 /* the Extended_Pictographic property is used in the Unicode 11
425 grapheme-boundary rules, so we store it in the boundclass field */
426 UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC = 19,
427 UTF8PROC_BOUNDCLASS_E_ZWG = 20, /* UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC + ZWJ */
428} utf8proc_boundclass_t;
429
430/** Indic_Conjunct_Break property. (TR44) */
431typedef enum {
432 UTF8PROC_INDIC_CONJUNCT_BREAK_NONE = 0,
433 UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER = 1,
434 UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT = 2,
435 UTF8PROC_INDIC_CONJUNCT_BREAK_EXTEND = 3,
436} utf8proc_indic_conjunct_break_t;
437
438/**
439 * Function pointer type passed to utf8proc_map_custom() and
440 * utf8proc_decompose_custom(), which is used to specify a user-defined
441 * mapping of codepoints to be applied in conjunction with other mappings.
442 */
443typedef utf8proc_int32_t (*utf8proc_custom_func)(utf8proc_int32_t codepoint, void *data);
444
445/**
446 * Array containing the byte lengths of a UTF-8 encoded codepoint based
447 * on the first byte.
448 */
449UTF8PROC_DLLEXPORT extern const utf8proc_int8_t utf8proc_utf8class[256];
450
451/**
452 * Returns the utf8proc API version as a string MAJOR.MINOR.PATCH
453 * (http://semver.org format), possibly with a "-dev" suffix for
454 * development versions.
455 */
456UTF8PROC_DLLEXPORT const char *utf8proc_version(void);
457
458/**
459 * Returns the utf8proc supported Unicode version as a string MAJOR.MINOR.PATCH.
460 */
461UTF8PROC_DLLEXPORT const char *utf8proc_unicode_version(void);
462
463/**
464 * Returns an informative error string for the given utf8proc error code
465 * (e.g. the error codes returned by utf8proc_map()).
466 */
467UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode);
468
469/**
470 * Reads a single codepoint from the UTF-8 sequence being pointed to by `str`.
471 * The maximum number of bytes read is `strlen`, unless `strlen` is
472 * negative (in which case up to 4 bytes are read).
473 *
474 * If a valid codepoint could be read, it is stored in the variable
475 * pointed to by `codepoint_ref`, otherwise that variable will be set to -1.
476 * In case of success, the number of bytes read is returned; otherwise, a
477 * negative error code is returned.
478 */
479UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *codepoint_ref);
480
481/**
482 * Check if a codepoint is valid (regardless of whether it has been
483 * assigned a value by the current Unicode standard).
484 *
485 * @return 1 if the given `codepoint` is valid and otherwise return 0.
486 */
487UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t codepoint);
488
489/**
490 * Encodes the codepoint as an UTF-8 string in the byte array pointed
491 * to by `dst`. This array must be at least 4 bytes long.
492 *
493 * In case of success the number of bytes written is returned, and
494 * otherwise 0 is returned.
495 *
496 * This function does not check whether `codepoint` is valid Unicode.
497 */
498UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t codepoint, utf8proc_uint8_t *dst);
499
500/**
501 * Look up the properties for a given codepoint.
502 *
503 * @param codepoint The Unicode codepoint.
504 *
505 * @returns
506 * A pointer to a (constant) struct containing information about
507 * the codepoint.
508 * @par
509 * If the codepoint is unassigned or invalid, a pointer to a special struct is
510 * returned in which `category` is 0 (@ref UTF8PROC_CATEGORY_CN).
511 */
512UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t codepoint);
513
514/** Decompose a codepoint into an array of codepoints.
515 *
516 * @param codepoint the codepoint.
517 * @param dst the destination buffer.
518 * @param bufsize the size of the destination buffer.
519 * @param options one or more of the following flags:
520 * - @ref UTF8PROC_REJECTNA - return an error if `codepoint` is unassigned
521 * - @ref UTF8PROC_IGNORE - strip "default ignorable" codepoints
522 * - @ref UTF8PROC_CASEFOLD - apply Unicode casefolding
523 * - @ref UTF8PROC_COMPAT - replace certain codepoints with their
524 * compatibility decomposition
525 * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
526 * - @ref UTF8PROC_LUMP - lump certain different codepoints together
527 * - @ref UTF8PROC_STRIPMARK - remove all character marks
528 * - @ref UTF8PROC_STRIPNA - remove unassigned codepoints
529 * @param last_boundclass
530 * Pointer to an integer variable containing
531 * the previous codepoint's (boundclass + indic_conjunct_break << 1) if the @ref UTF8PROC_CHARBOUND
532 * option is used. If the string is being processed in order, this can be initialized to 0 for
533 * the beginning of the string, and is thereafter updated automatically. Otherwise, this parameter is ignored.
534 *
535 * In the current version of utf8proc, the maximum destination buffer with the @ref UTF8PROC_DECOMPOSE
536 * option is 4 elements (or double that with @ref UTF8PROC_CHARBOUND), so this is a good default size.
537 * However, this may increase in future Unicode versions, so you should always check the return value
538 * as described below.
539 *
540 * @return
541 * In case of success, the number of codepoints written is returned; in case
542 * of an error, a negative error code is returned (utf8proc_errmsg()).
543 * @par
544 * If the number of written codepoints would be bigger than `bufsize`, the
545 * required buffer size is returned, while the buffer will be overwritten with
546 * undefined data.
547 */
548UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(
549 utf8proc_int32_t codepoint, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize,
550 utf8proc_option_t options, int *last_boundclass
551);
552
553/**
554 * The same as utf8proc_decompose_char(), but acts on a whole UTF-8
555 * string and orders the decomposed sequences correctly.
556 *
557 * If the @ref UTF8PROC_NULLTERM flag in `options` is set, processing
558 * will be stopped, when a NULL byte is encountered, otherwise `strlen`
559 * bytes are processed. The result (in the form of 32-bit unicode
560 * codepoints) is written into the buffer being pointed to by
561 * `buffer` (which must contain at least `bufsize` entries). In case of
562 * success, the number of codepoints written is returned; in case of an
563 * error, a negative error code is returned (utf8proc_errmsg()).
564 * See utf8proc_decompose_custom() to supply additional transformations.
565 *
566 * If the number of written codepoints would be bigger than `bufsize`, the
567 * required buffer size is returned, while the buffer will be overwritten with
568 * undefined data.
569 */
570UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
571 const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
572 utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
573);
574
575/**
576 * The same as utf8proc_decompose(), but also takes a `custom_func` mapping function
577 * that is called on each codepoint in `str` before any other transformations
578 * (along with a `custom_data` pointer that is passed through to `custom_func`).
579 * The `custom_func` argument is ignored if it is `NULL`. See also utf8proc_map_custom().
580 */
581UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
582 const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
583 utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
584 utf8proc_custom_func custom_func, void *custom_data
585);
586
587/**
588 * Normalizes the sequence of `length` codepoints pointed to by `buffer`
589 * in-place (i.e., the result is also stored in `buffer`).
590 *
591 * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
592 * @param length the length (in codepoints) of the buffer.
593 * @param options a bitwise or (`|`) of one or more of the following flags:
594 * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
595 * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
596 * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
597 * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
598 * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
599 * codepoints
600 * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
601 * the unicode versioning stability
602 *
603 * @return
604 * In case of success, the length (in codepoints) of the normalized UTF-32 string is
605 * returned; otherwise, a negative error code is returned (utf8proc_errmsg()).
606 *
607 * @warning The entries of the array pointed to by `str` have to be in the
608 * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
609 */
610UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
611
612/**
613 * Reencodes the sequence of `length` codepoints pointed to by `buffer`
614 * UTF-8 data in-place (i.e., the result is also stored in `buffer`).
615 * Can optionally normalize the UTF-32 sequence prior to UTF-8 conversion.
616 *
617 * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
618 * @param length the length (in codepoints) of the buffer.
619 * @param options a bitwise or (`|`) of one or more of the following flags:
620 * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
621 * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
622 * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
623 * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
624 * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
625 * codepoints
626 * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
627 * the unicode versioning stability
628 * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
629 *
630 * @return
631 * In case of success, the length (in bytes) of the resulting nul-terminated
632 * UTF-8 string is returned; otherwise, a negative error code is returned
633 * (utf8proc_errmsg()).
634 *
635 * @warning The amount of free space pointed to by `buffer` must
636 * exceed the amount of the input data by one byte, and the
637 * entries of the array pointed to by `str` have to be in the
638 * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
639 */
640UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
641
642/**
643 * Given a pair of consecutive codepoints, return whether a grapheme break is
644 * permitted between them (as defined by the extended grapheme clusters in UAX#29).
645 *
646 * @param codepoint1 The first codepoint.
647 * @param codepoint2 The second codepoint, occurring consecutively after `codepoint1`.
648 * @param state Beginning with Version 29 (Unicode 9.0.0), this algorithm requires
649 * state to break graphemes. This state can be passed in as a pointer
650 * in the `state` argument and should initially be set to 0. If the
651 * state is not passed in (i.e. a null pointer is passed), UAX#29 rules
652 * GB10/12/13 which require this state will not be applied, essentially
653 * matching the rules in Unicode 8.0.0.
654 *
655 * @warning If the state parameter is used, `utf8proc_grapheme_break_stateful` must
656 * be called IN ORDER on ALL potential breaks in a string. However, it
657 * is safe to reset the state to zero after a grapheme break.
658 */
659UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
660 utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2, utf8proc_int32_t *state);
661
662/**
663 * Same as utf8proc_grapheme_break_stateful(), except without support for the
664 * Unicode 9 additions to the algorithm. Supported for legacy reasons.
665 */
666UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
667 utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2);
668
669
670/**
671 * Given a codepoint `c`, return the codepoint of the corresponding
672 * lower-case character, if any; otherwise (if there is no lower-case
673 * variant, or if `c` is not a valid codepoint) return `c`.
674 */
675UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c);
676
677/**
678 * Given a codepoint `c`, return the codepoint of the corresponding
679 * upper-case character, if any; otherwise (if there is no upper-case
680 * variant, or if `c` is not a valid codepoint) return `c`.
681 */
682UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c);
683
684/**
685 * Given a codepoint `c`, return the codepoint of the corresponding
686 * title-case character, if any; otherwise (if there is no title-case
687 * variant, or if `c` is not a valid codepoint) return `c`.
688 */
689UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c);
690
691/**
692 * Given a codepoint `c`, return `1` if the codepoint corresponds to a lower-case character
693 * and `0` otherwise.
694 */
695UTF8PROC_DLLEXPORT int utf8proc_islower(utf8proc_int32_t c);
696
697/**
698 * Given a codepoint `c`, return `1` if the codepoint corresponds to an upper-case character
699 * and `0` otherwise.
700 */
701UTF8PROC_DLLEXPORT int utf8proc_isupper(utf8proc_int32_t c);
702
703/**
704 * Given a codepoint, return a character width analogous to `wcwidth(codepoint)`,
705 * except that a width of 0 is returned for non-printable codepoints
706 * instead of -1 as in `wcwidth`.
707 *
708 * @note
709 * If you want to check for particular types of non-printable characters,
710 * (analogous to `isprint` or `iscntrl`), use utf8proc_category(). */
711UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t codepoint);
712
713/**
714 * Given a codepoint, return whether it has East Asian width class A (Ambiguous)
715 *
716 * Codepoints with this property are considered to have charwidth 1 (if they are printable)
717 * but some East Asian fonts render them as double width.
718 */
719UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_charwidth_ambiguous(utf8proc_int32_t codepoint);
720
721/**
722 * Return the Unicode category for the codepoint (one of the
723 * @ref utf8proc_category_t constants.)
724 */
725UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t codepoint);
726
727/**
728 * Return the two-letter (nul-terminated) Unicode category string for
729 * the codepoint (e.g. `"Lu"` or `"Co"`).
730 */
731UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t codepoint);
732
733/**
734 * Maps the given UTF-8 string pointed to by `str` to a new UTF-8
735 * string, allocated dynamically by `malloc` and returned via `dstptr`.
736 *
737 * If the @ref UTF8PROC_NULLTERM flag in the `options` field is set,
738 * the length is determined by a NULL terminator, otherwise the
739 * parameter `strlen` is evaluated to determine the string length, but
740 * in any case the result will be NULL terminated (though it might
741 * contain NULL characters with the string if `str` contained NULL
742 * characters). Other flags in the `options` field are passed to the
743 * functions defined above, and regarded as described. See also
744 * utf8proc_map_custom() to supply a custom codepoint transformation.
745 *
746 * In case of success the length of the new string is returned,
747 * otherwise a negative error code is returned.
748 *
749 * @note The memory of the new UTF-8 string will have been allocated
750 * with `malloc`, and should therefore be deallocated with `free`.
751 */
752UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
753 const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
754);
755
756/**
757 * Like utf8proc_map(), but also takes a `custom_func` mapping function
758 * that is called on each codepoint in `str` before any other transformations
759 * (along with a `custom_data` pointer that is passed through to `custom_func`).
760 * The `custom_func` argument is ignored if it is `NULL`.
761 */
762UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
763 const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
764 utf8proc_custom_func custom_func, void *custom_data
765);
766
767/** @name Unicode normalization
768 *
769 * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD, NFKC or
770 * NFKC_Casefold normalized version of the null-terminated string `str`. These
771 * are shortcuts to calling utf8proc_map() with @ref UTF8PROC_NULLTERM
772 * combined with @ref UTF8PROC_STABLE and flags indicating the normalization.
773 */
774/** @{ */
775/** NFD normalization (@ref UTF8PROC_DECOMPOSE). */
776UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str);
777/** NFC normalization (@ref UTF8PROC_COMPOSE). */
778UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str);
779/** NFKD normalization (@ref UTF8PROC_DECOMPOSE and @ref UTF8PROC_COMPAT). */
780UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str);
781/** NFKC normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT). */
782UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str);
783/**
784 * NFKC_Casefold normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT
785 * and @ref UTF8PROC_CASEFOLD and @ref UTF8PROC_IGNORE).
786 **/
787UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str);
788/** @} */
789
790#ifdef __cplusplus
791}
792#endif
793
794#endif
795