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