Debian Codegraph @

podofo/3rdparty/utf8proc/utf8proc.h

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