2
* Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice shall be included in
12
* all copies or substantial portions of the Software.
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
* DEALINGS IN THE SOFTWARE.
25
* File name: utf8proc.h
28
* Header files for libutf8proc, which is a mapping tool for UTF-8 strings
29
* with following features:
30
* - decomposing and composing of strings
31
* - replacing compatibility characters with their equivalents
32
* - stripping of "default ignorable characters"
33
* like SOFT-HYPHEN or ZERO-WIDTH-SPACE
34
* - folding of certain characters for string comparison
35
* (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-")
37
* - optional rejection of strings containing non-assigned code points
38
* - stripping of control characters
39
* - stripping of character marks (accents, etc.)
40
* - transformation of LF, CRLF, CR and NEL to line-feed (LF)
41
* or to the unicode chararacters for paragraph separation (PS)
42
* or line separation (LS).
43
* - unicode case folding (for case insensitive string comparisons)
44
* - rejection of illegal UTF-8 data
45
* (i.e. UTF-8 encoded UTF-16 surrogates)
46
* - support for korean hangul characters
47
* Unicode Version 5.0.0 is supported.
56
* Define UTF8PROC_INLINE and include utf8proc.c to embed a static
57
* version of utf8proc in your program or library without exporting
60
#ifdef UTF8PROC_INLINE
61
#define UTF8PROC_API static
62
#undef UTF8PROC_DATA_EXPORT
63
#define UTF8PROC_DATA static
66
#define UTF8PROC_DATA_EXPORT
72
#include <sys/types.h>
78
typedef signed char int8_t;
79
typedef unsigned char uint8_t;
80
typedef short int16_t;
81
typedef unsigned short uint16_t;
83
typedef unsigned char bool;
87
# define ssize_t __int64
91
#elif defined(HAVE_STDBOOL_H) && defined(HAVE_INTTYPES_H)
106
#define SSIZE_MAX ((size_t)SIZE_MAX/2)
109
#define UTF8PROC_NULLTERM (1<<0)
110
#define UTF8PROC_STABLE (1<<1)
111
#define UTF8PROC_COMPAT (1<<2)
112
#define UTF8PROC_COMPOSE (1<<3)
113
#define UTF8PROC_DECOMPOSE (1<<4)
114
#define UTF8PROC_IGNORE (1<<5)
115
#define UTF8PROC_REJECTNA (1<<6)
116
#define UTF8PROC_NLF2LS (1<<7)
117
#define UTF8PROC_NLF2PS (1<<8)
118
#define UTF8PROC_NLF2LF (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS)
119
#define UTF8PROC_STRIPCC (1<<9)
120
#define UTF8PROC_CASEFOLD (1<<10)
121
#define UTF8PROC_CHARBOUND (1<<11)
122
#define UTF8PROC_LUMP (1<<12)
123
#define UTF8PROC_STRIPMARK (1<<13)
125
* Flags being regarded by several functions in the library:
126
* NULLTERM: The given UTF-8 input is NULL terminated.
127
* STABLE: Unicode Versioning Stability has to be respected.
128
* COMPAT: Compatiblity decomposition
129
* (i.e. formatting information is lost)
130
* COMPOSE: Return a result with composed characters.
131
* DECOMPOSE: Return a result with decomposed characters.
132
* IGNORE: Strip "default ignorable characters"
133
* REJECTNA: Return an error, if the input contains unassigned
135
* NLF2LS: Indicating that NLF-sequences (LF, CRLF, CR, NEL) are
136
* representing a line break, and should be converted to the
137
* unicode character for line separation (LS).
138
* NLF2PS: Indicating that NLF-sequences are representing a paragraph
139
* break, and should be converted to the unicode character for
140
* paragraph separation (PS).
141
* NLF2LF: Indicating that the meaning of NLF-sequences is unknown.
142
* STRIPCC: Strips and/or convers control characters.
143
* NLF-sequences are transformed into space, except if one of
144
* the NLF2LS/PS/LF options is given.
145
* HorizontalTab (HT) and FormFeed (FF) are treated as a
146
* NLF-sequence in this case.
147
* All other control characters are simply removed.
148
* CASEFOLD: Performs unicode case folding, to be able to do a
149
* case-insensitive string comparison.
150
* CHARBOUND: Inserts 0xFF bytes at the beginning of each sequence which
151
* is representing a single grapheme cluster (see UAX#29).
152
* LUMP: Lumps certain characters together
153
* (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-").
154
* (See lump.txt for details.)
155
* If NLF2LF is set, this includes a transformation of
156
* paragraph and line separators to ASCII line-feed (LF).
157
* STRIPMARK: Strips all character markings
158
* (non-spacing, spacing and enclosing) (i.e. accents)
159
* NOTE: this option works only with COMPOSE or DECOMPOSE
162
#define UTF8PROC_ERROR_NOMEM -1
163
#define UTF8PROC_ERROR_OVERFLOW -2
164
#define UTF8PROC_ERROR_INVALIDUTF8 -3
165
#define UTF8PROC_ERROR_NOTASSIGNED -4
166
#define UTF8PROC_ERROR_INVALIDOPTS -5
168
* Error codes being returned by almost all functions:
169
* ERROR_NOMEM: Memory could not be allocated.
170
* ERROR_OVERFLOW: The given string is too long to be processed.
171
* ERROR_INVALIDUTF8: The given string is not a legal UTF-8 string.
172
* ERROR_NOTASSIGNED: The REJECTNA flag was set,
173
* and an unassigned code point was found.
174
* ERROR_INVALIDOPTS: Invalid options have been used.
177
typedef int16_t utf8proc_propval_t;
178
typedef struct utf8proc_property_struct {
179
utf8proc_propval_t category;
180
utf8proc_propval_t combining_class;
181
utf8proc_propval_t bidi_class;
182
utf8proc_propval_t decomp_type;
183
const int32_t *decomp_mapping;
184
unsigned bidi_mirrored:1;
185
int32_t uppercase_mapping;
186
int32_t lowercase_mapping;
187
int32_t titlecase_mapping;
188
int32_t comb1st_index;
189
int32_t comb2nd_index;
190
unsigned comp_exclusion:1;
191
unsigned ignorable:1;
192
unsigned control_boundary:1;
194
const int32_t *casefold_mapping;
195
} utf8proc_property_t;
197
#define UTF8PROC_CATEGORY_LU 1
198
#define UTF8PROC_CATEGORY_LL 2
199
#define UTF8PROC_CATEGORY_LT 3
200
#define UTF8PROC_CATEGORY_LM 4
201
#define UTF8PROC_CATEGORY_LO 5
202
#define UTF8PROC_CATEGORY_MN 6
203
#define UTF8PROC_CATEGORY_MC 7
204
#define UTF8PROC_CATEGORY_ME 8
205
#define UTF8PROC_CATEGORY_ND 9
206
#define UTF8PROC_CATEGORY_NL 10
207
#define UTF8PROC_CATEGORY_NO 11
208
#define UTF8PROC_CATEGORY_PC 12
209
#define UTF8PROC_CATEGORY_PD 13
210
#define UTF8PROC_CATEGORY_PS 14
211
#define UTF8PROC_CATEGORY_PE 15
212
#define UTF8PROC_CATEGORY_PI 16
213
#define UTF8PROC_CATEGORY_PF 17
214
#define UTF8PROC_CATEGORY_PO 18
215
#define UTF8PROC_CATEGORY_SM 19
216
#define UTF8PROC_CATEGORY_SC 20
217
#define UTF8PROC_CATEGORY_SK 21
218
#define UTF8PROC_CATEGORY_SO 22
219
#define UTF8PROC_CATEGORY_ZS 23
220
#define UTF8PROC_CATEGORY_ZL 24
221
#define UTF8PROC_CATEGORY_ZP 25
222
#define UTF8PROC_CATEGORY_CC 26
223
#define UTF8PROC_CATEGORY_CF 27
224
#define UTF8PROC_CATEGORY_CS 28
225
#define UTF8PROC_CATEGORY_CO 29
226
#define UTF8PROC_CATEGORY_CN 30
227
#define UTF8PROC_BIDI_CLASS_L 1
228
#define UTF8PROC_BIDI_CLASS_LRE 2
229
#define UTF8PROC_BIDI_CLASS_LRO 3
230
#define UTF8PROC_BIDI_CLASS_R 4
231
#define UTF8PROC_BIDI_CLASS_AL 5
232
#define UTF8PROC_BIDI_CLASS_RLE 6
233
#define UTF8PROC_BIDI_CLASS_RLO 7
234
#define UTF8PROC_BIDI_CLASS_PDF 8
235
#define UTF8PROC_BIDI_CLASS_EN 9
236
#define UTF8PROC_BIDI_CLASS_ES 10
237
#define UTF8PROC_BIDI_CLASS_ET 11
238
#define UTF8PROC_BIDI_CLASS_AN 12
239
#define UTF8PROC_BIDI_CLASS_CS 13
240
#define UTF8PROC_BIDI_CLASS_NSM 14
241
#define UTF8PROC_BIDI_CLASS_BN 15
242
#define UTF8PROC_BIDI_CLASS_B 16
243
#define UTF8PROC_BIDI_CLASS_S 17
244
#define UTF8PROC_BIDI_CLASS_WS 18
245
#define UTF8PROC_BIDI_CLASS_ON 19
246
#define UTF8PROC_DECOMP_TYPE_FONT 1
247
#define UTF8PROC_DECOMP_TYPE_NOBREAK 2
248
#define UTF8PROC_DECOMP_TYPE_INITIAL 3
249
#define UTF8PROC_DECOMP_TYPE_MEDIAL 4
250
#define UTF8PROC_DECOMP_TYPE_FINAL 5
251
#define UTF8PROC_DECOMP_TYPE_ISOLATED 6
252
#define UTF8PROC_DECOMP_TYPE_CIRCLE 7
253
#define UTF8PROC_DECOMP_TYPE_SUPER 8
254
#define UTF8PROC_DECOMP_TYPE_SUB 9
255
#define UTF8PROC_DECOMP_TYPE_VERTICAL 10
256
#define UTF8PROC_DECOMP_TYPE_WIDE 11
257
#define UTF8PROC_DECOMP_TYPE_NARROW 12
258
#define UTF8PROC_DECOMP_TYPE_SMALL 13
259
#define UTF8PROC_DECOMP_TYPE_SQUARE 14
260
#define UTF8PROC_DECOMP_TYPE_FRACTION 15
261
#define UTF8PROC_DECOMP_TYPE_COMPAT 16
263
#ifdef UTF8PROC_DATA_EXPORT
264
extern const int8_t utf8proc_utf8class[256];
268
const char *utf8proc_version(void);
271
const char *utf8proc_errmsg(ssize_t errcode);
273
* Returns a static error string for the given error code.
277
ssize_t utf8proc_iterate(const uint8_t *str, ssize_t strlen, int32_t *dst);
279
* Reads a single char from the UTF-8 sequence being pointed to by 'str'.
280
* The maximum number of bytes read is 'strlen', unless 'strlen' is
282
* If a valid unicode char could be read, it is stored in the variable
283
* being pointed to by 'dst', otherwise that variable will be set to -1.
284
* In case of success the number of bytes read is returned, otherwise a
285
* negative error code is returned.
289
bool utf8proc_codepoint_valid(int32_t uc);
291
* Returns 1, if the given unicode code-point is valid, otherwise 0.
295
ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst);
297
* Encodes the unicode char with the code point 'uc' as an UTF-8 string in
298
* the byte array being pointed to by 'dst'. This array has to be at least
300
* In case of success the number of bytes written is returned,
302
* This function does not check if 'uc' is a valid unicode code point.
306
const utf8proc_property_t *utf8proc_get_property(int32_t uc);
308
* Returns a pointer to a (constant) struct containing information about
309
* the unicode char with the given code point 'uc'.
310
* If the character is not existent a pointer to a special struct is
311
* returned, where 'category' is a NULL pointer.
312
* WARNING: The parameter 'uc' has to be in the range of 0x0000 to
313
* 0x10FFFF, otherwise the program might crash!
317
ssize_t utf8proc_decompose_char(
318
int32_t uc, int32_t *dst, ssize_t bufsize,
319
int options, int *last_boundclass
322
* Writes a decomposition of the unicode char 'uc' into the array being
323
* pointed to by 'dst'.
324
* Following flags in the 'options' field are regarded:
325
* REJECTNA: an unassigned unicode code point leads to an error
326
* IGNORE: "default ignorable" chars are stripped
327
* CASEFOLD: unicode casefolding is applied
328
* COMPAT: replace certain characters with their
329
* compatibility decomposition
330
* CHARBOUND: Inserts 0xFF bytes before each grapheme cluster
331
* LUMP: lumps certain different characters together
332
* STRIPMARK: removes all character marks
333
* The pointer 'last_boundclass' has to point to an integer variable which
334
* is storing the last character boundary class, if the CHARBOUND option
336
* In case of success the number of chars written is returned,
337
* in case of an error, a negative error code is returned.
338
* If the number of written chars would be bigger than 'bufsize',
339
* the buffer (up to 'bufsize') has inpredictable data, and the needed
340
* buffer size is returned.
341
* WARNING: The parameter 'uc' has to be in the range of 0x0000 to
342
* 0x10FFFF, otherwise the program might crash!
346
ssize_t utf8proc_decompose(
347
const uint8_t *str, ssize_t strlen,
348
int32_t *buffer, ssize_t bufsize, int options
351
* Does the same as 'utf8proc_decompose_char', but acts on a whole UTF-8
352
* string, and orders the decomposed sequences correctly.
353
* If the NULLTERM flag in 'options' is set, processing will be stopped,
354
* when a NULL byte is encounted, otherwise 'strlen' bytes are processed.
355
* The result in form of unicode code points is written into the buffer
356
* being pointed to by 'buffer', having the length of 'bufsize' entries.
357
* In case of success the number of chars written is returned,
358
* in case of an error, a negative error code is returned.
359
* If the number of written chars would be bigger than 'bufsize',
360
* the buffer (up to 'bufsize') has inpredictable data, and the needed
361
* buffer size is returned.
365
ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options);
367
* Reencodes the sequence of unicode characters given by the pointer
368
* 'buffer' and 'length' as UTF-8.
369
* The result is stored in the same memory area where the data is read.
370
* Following flags in the 'options' field are regarded:
371
* NLF2LS: converts LF, CRLF, CR and NEL into LS
372
* NLF2PS: converts LF, CRLF, CR and NEL into PS
373
* NLF2LF: converts LF, CRLF, CR and NEL into LF
374
* STRIPCC: strips or converts all non-affected control characters
375
* COMPOSE: tries to combine decomposed characters into composite
377
* STABLE: prohibits combining characters which would violate
378
* the unicode versioning stability
379
* In case of success the length of the resulting UTF-8 string is
380
* returned, otherwise a negative error code is returned.
381
* WARNING: The amount of free space being pointed to by 'buffer', has to
382
* exceed the amount of the input data by one byte, and the
383
* entries of the array pointed to by 'str' have to be in the
384
* range of 0x0000 to 0x10FFFF, otherwise the program might
389
ssize_t utf8proc_map(
390
const uint8_t *str, ssize_t strlen, uint8_t **dstptr, int options
393
* Maps the given UTF-8 string being pointed to by 'str' to a new UTF-8
394
* string, which is allocated dynamically, and afterwards pointed to by
395
* the pointer being pointed to by 'dstptr'.
396
* If the NULLTERM flag in the 'options' field is set, the length is
397
* determined by a NULL terminator, otherwise the parameter 'strlen' is
398
* evaluated to determine the string length, but in any case the result
399
* will be NULL terminated (though it might contain NULL characters
400
* before). Other flags in the 'options' field are passed to the functions
401
* defined above, and regarded as described.
402
* In case of success the length of the new string is returned,
403
* otherwise a negative error code is returned.
404
* NOTICE: The memory of the new UTF-8 string will have been allocated with
405
* 'malloc', and has theirfore to be freed with 'free'.
409
uint8_t *utf8proc_NFD(const uint8_t *str);
411
uint8_t *utf8proc_NFC(const uint8_t *str);
413
uint8_t *utf8proc_NFKD(const uint8_t *str);
415
uint8_t *utf8proc_NFKC(const uint8_t *str);
417
* Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC
418
* normalized version of the null-terminated string 'str'.