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