utf8proc

view utf8proc.h @ 15:15450ff3d454

Contribution from libmojibake fork
author Jiahao Chen, Steven G. Johnson, Anthony David Kelman
date Fri Nov 21 08:27:44 2014 -0500 (2014-11-21)
parents 82d33620bb8a
children
line source
1 /*
2 * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 */
24 /*
25 * File name: utf8proc.h
26 *
27 * Description:
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 "-")
36 * (see "LUMP" option)
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 7.0.0 is supported.
48 */
51 #ifndef UTF8PROC_H
52 #define UTF8PROC_H
55 #include <stdlib.h>
56 #include <sys/types.h>
57 #ifdef _MSC_VER
58 typedef signed char int8_t;
59 typedef unsigned char uint8_t;
60 typedef short int16_t;
61 typedef unsigned short uint16_t;
62 typedef int int32_t;
63 # ifdef _WIN64
64 # define ssize_t __int64
65 # else
66 # define ssize_t int
67 # endif
68 # ifndef __cplusplus
69 typedef unsigned char bool;
70 enum {false, true};
71 # endif
72 #else
73 # include <stdbool.h>
74 # include <inttypes.h>
75 #endif
76 #include <limits.h>
78 #ifdef _WIN32
79 # ifdef UTF8PROC_EXPORTS
80 # define DLLEXPORT __declspec(dllexport)
81 # else
82 # define DLLEXPORT __declspec(dllimport)
83 # endif
84 #elif __GNUC__ >= 4
85 # define DLLEXPORT __attribute__ ((visibility("default")))
86 #else
87 # define DLLEXPORT
88 #endif
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
94 #ifndef SSIZE_MAX
95 #define SSIZE_MAX ((size_t)SIZE_MAX/2)
96 #endif
98 #define UTF8PROC_NULLTERM (1<<0)
99 #define UTF8PROC_STABLE (1<<1)
100 #define UTF8PROC_COMPAT (1<<2)
101 #define UTF8PROC_COMPOSE (1<<3)
102 #define UTF8PROC_DECOMPOSE (1<<4)
103 #define UTF8PROC_IGNORE (1<<5)
104 #define UTF8PROC_REJECTNA (1<<6)
105 #define UTF8PROC_NLF2LS (1<<7)
106 #define UTF8PROC_NLF2PS (1<<8)
107 #define UTF8PROC_NLF2LF (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS)
108 #define UTF8PROC_STRIPCC (1<<9)
109 #define UTF8PROC_CASEFOLD (1<<10)
110 #define UTF8PROC_CHARBOUND (1<<11)
111 #define UTF8PROC_LUMP (1<<12)
112 #define UTF8PROC_STRIPMARK (1<<13)
113 /*
114 * Flags being regarded by several functions in the library:
115 * NULLTERM: The given UTF-8 input is NULL terminated.
116 * STABLE: Unicode Versioning Stability has to be respected.
117 * COMPAT: Compatibility decomposition
118 * (i.e. formatting information is lost)
119 * COMPOSE: Return a result with composed characters.
120 * DECOMPOSE: Return a result with decomposed characters.
121 * IGNORE: Strip "default ignorable characters"
122 * REJECTNA: Return an error, if the input contains unassigned
123 * code points.
124 * NLF2LS: Indicating that NLF-sequences (LF, CRLF, CR, NEL) are
125 * representing a line break, and should be converted to the
126 * unicode character for line separation (LS).
127 * NLF2PS: Indicating that NLF-sequences are representing a paragraph
128 * break, and should be converted to the unicode character for
129 * paragraph separation (PS).
130 * NLF2LF: Indicating that the meaning of NLF-sequences is unknown.
131 * STRIPCC: Strips and/or convers control characters.
132 * NLF-sequences are transformed into space, except if one of
133 * the NLF2LS/PS/LF options is given.
134 * HorizontalTab (HT) and FormFeed (FF) are treated as a
135 * NLF-sequence in this case.
136 * All other control characters are simply removed.
137 * CASEFOLD: Performs unicode case folding, to be able to do a
138 * case-insensitive string comparison.
139 * CHARBOUND: Inserts 0xFF bytes at the beginning of each sequence which
140 * is representing a single grapheme cluster (see UAX#29).
141 * LUMP: Lumps certain characters together
142 * (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-").
143 * (See lump.txt for details.)
144 * If NLF2LF is set, this includes a transformation of
145 * paragraph and line separators to ASCII line-feed (LF).
146 * STRIPMARK: Strips all character markings
147 * (non-spacing, spacing and enclosing) (i.e. accents)
148 * NOTE: this option works only with COMPOSE or DECOMPOSE
149 */
151 #define UTF8PROC_ERROR_NOMEM -1
152 #define UTF8PROC_ERROR_OVERFLOW -2
153 #define UTF8PROC_ERROR_INVALIDUTF8 -3
154 #define UTF8PROC_ERROR_NOTASSIGNED -4
155 #define UTF8PROC_ERROR_INVALIDOPTS -5
156 /*
157 * Error codes being returned by almost all functions:
158 * ERROR_NOMEM: Memory could not be allocated.
159 * ERROR_OVERFLOW: The given string is too long to be processed.
160 * ERROR_INVALIDUTF8: The given string is not a legal UTF-8 string.
161 * ERROR_NOTASSIGNED: The REJECTNA flag was set,
162 * and an unassigned code point was found.
163 * ERROR_INVALIDOPTS: Invalid options have been used.
164 */
166 typedef int16_t utf8proc_propval_t;
167 typedef struct utf8proc_property_struct {
168 utf8proc_propval_t category;
169 utf8proc_propval_t combining_class;
170 utf8proc_propval_t bidi_class;
171 utf8proc_propval_t decomp_type;
172 const int32_t *decomp_mapping;
173 unsigned bidi_mirrored:1;
174 int32_t uppercase_mapping;
175 int32_t lowercase_mapping;
176 int32_t titlecase_mapping;
177 int32_t comb1st_index;
178 int32_t comb2nd_index;
179 unsigned comp_exclusion:1;
180 unsigned ignorable:1;
181 unsigned control_boundary:1;
182 unsigned extend:1;
183 const int32_t *casefold_mapping;
184 } utf8proc_property_t;
186 #define UTF8PROC_CATEGORY_LU 1
187 #define UTF8PROC_CATEGORY_LL 2
188 #define UTF8PROC_CATEGORY_LT 3
189 #define UTF8PROC_CATEGORY_LM 4
190 #define UTF8PROC_CATEGORY_LO 5
191 #define UTF8PROC_CATEGORY_MN 6
192 #define UTF8PROC_CATEGORY_MC 7
193 #define UTF8PROC_CATEGORY_ME 8
194 #define UTF8PROC_CATEGORY_ND 9
195 #define UTF8PROC_CATEGORY_NL 10
196 #define UTF8PROC_CATEGORY_NO 11
197 #define UTF8PROC_CATEGORY_PC 12
198 #define UTF8PROC_CATEGORY_PD 13
199 #define UTF8PROC_CATEGORY_PS 14
200 #define UTF8PROC_CATEGORY_PE 15
201 #define UTF8PROC_CATEGORY_PI 16
202 #define UTF8PROC_CATEGORY_PF 17
203 #define UTF8PROC_CATEGORY_PO 18
204 #define UTF8PROC_CATEGORY_SM 19
205 #define UTF8PROC_CATEGORY_SC 20
206 #define UTF8PROC_CATEGORY_SK 21
207 #define UTF8PROC_CATEGORY_SO 22
208 #define UTF8PROC_CATEGORY_ZS 23
209 #define UTF8PROC_CATEGORY_ZL 24
210 #define UTF8PROC_CATEGORY_ZP 25
211 #define UTF8PROC_CATEGORY_CC 26
212 #define UTF8PROC_CATEGORY_CF 27
213 #define UTF8PROC_CATEGORY_CS 28
214 #define UTF8PROC_CATEGORY_CO 29
215 #define UTF8PROC_CATEGORY_CN 30
216 #define UTF8PROC_BIDI_CLASS_L 1
217 #define UTF8PROC_BIDI_CLASS_LRE 2
218 #define UTF8PROC_BIDI_CLASS_LRO 3
219 #define UTF8PROC_BIDI_CLASS_R 4
220 #define UTF8PROC_BIDI_CLASS_AL 5
221 #define UTF8PROC_BIDI_CLASS_RLE 6
222 #define UTF8PROC_BIDI_CLASS_RLO 7
223 #define UTF8PROC_BIDI_CLASS_PDF 8
224 #define UTF8PROC_BIDI_CLASS_EN 9
225 #define UTF8PROC_BIDI_CLASS_ES 10
226 #define UTF8PROC_BIDI_CLASS_ET 11
227 #define UTF8PROC_BIDI_CLASS_AN 12
228 #define UTF8PROC_BIDI_CLASS_CS 13
229 #define UTF8PROC_BIDI_CLASS_NSM 14
230 #define UTF8PROC_BIDI_CLASS_BN 15
231 #define UTF8PROC_BIDI_CLASS_B 16
232 #define UTF8PROC_BIDI_CLASS_S 17
233 #define UTF8PROC_BIDI_CLASS_WS 18
234 #define UTF8PROC_BIDI_CLASS_ON 19
235 #define UTF8PROC_BIDI_CLASS_LRI 20 /* new in Unicode 6.3 */
236 #define UTF8PROC_BIDI_CLASS_RLI 21 /* new in Unicode 6.3 */
237 #define UTF8PROC_BIDI_CLASS_FSI 22 /* new in Unicode 6.3 */
238 #define UTF8PROC_BIDI_CLASS_PDI 23 /* new in Unicode 6.3 */
239 #define UTF8PROC_DECOMP_TYPE_FONT 1
240 #define UTF8PROC_DECOMP_TYPE_NOBREAK 2
241 #define UTF8PROC_DECOMP_TYPE_INITIAL 3
242 #define UTF8PROC_DECOMP_TYPE_MEDIAL 4
243 #define UTF8PROC_DECOMP_TYPE_FINAL 5
244 #define UTF8PROC_DECOMP_TYPE_ISOLATED 6
245 #define UTF8PROC_DECOMP_TYPE_CIRCLE 7
246 #define UTF8PROC_DECOMP_TYPE_SUPER 8
247 #define UTF8PROC_DECOMP_TYPE_SUB 9
248 #define UTF8PROC_DECOMP_TYPE_VERTICAL 10
249 #define UTF8PROC_DECOMP_TYPE_WIDE 11
250 #define UTF8PROC_DECOMP_TYPE_NARROW 12
251 #define UTF8PROC_DECOMP_TYPE_SMALL 13
252 #define UTF8PROC_DECOMP_TYPE_SQUARE 14
253 #define UTF8PROC_DECOMP_TYPE_FRACTION 15
254 #define UTF8PROC_DECOMP_TYPE_COMPAT 16
256 DLLEXPORT extern const int8_t utf8proc_utf8class[256];
258 DLLEXPORT const char *utf8proc_version(void);
260 DLLEXPORT const char *utf8proc_errmsg(ssize_t errcode);
261 /*
262 * Returns a static error string for the given error code.
263 */
265 DLLEXPORT ssize_t utf8proc_iterate(const uint8_t *str, ssize_t strlen, int32_t *dst);
266 /*
267 * Reads a single char from the UTF-8 sequence being pointed to by 'str'.
268 * The maximum number of bytes read is 'strlen', unless 'strlen' is
269 * negative.
270 * If a valid unicode char could be read, it is stored in the variable
271 * being pointed to by 'dst', otherwise that variable will be set to -1.
272 * In case of success the number of bytes read is returned, otherwise a
273 * negative error code is returned.
274 */
276 DLLEXPORT bool utf8proc_codepoint_valid(int32_t uc);
277 /*
278 * Returns 1, if the given unicode code-point is valid, otherwise 0.
279 */
281 DLLEXPORT ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst);
282 /*
283 * Encodes the unicode char with the code point 'uc' as an UTF-8 string in
284 * the byte array being pointed to by 'dst'. This array has to be at least
285 * 4 bytes long.
286 * In case of success the number of bytes written is returned,
287 * otherwise 0.
288 * This function does not check if 'uc' is a valid unicode code point.
289 */
291 DLLEXPORT const utf8proc_property_t *utf8proc_get_property(int32_t uc);
292 /*
293 * Returns a pointer to a (constant) struct containing information about
294 * the unicode char with the given code point 'uc'.
295 * If the character is not existent a pointer to a special struct is
296 * returned, where 'category' is a NULL pointer.
297 * WARNING: The parameter 'uc' has to be in the range of 0x0000 to
298 * 0x10FFFF, otherwise the program might crash!
299 */
301 DLLEXPORT ssize_t utf8proc_decompose_char(
302 int32_t uc, int32_t *dst, ssize_t bufsize,
303 int options, int *last_boundclass
304 );
305 /*
306 * Writes a decomposition of the unicode char 'uc' into the array being
307 * pointed to by 'dst'.
308 * Following flags in the 'options' field are regarded:
309 * REJECTNA: an unassigned unicode code point leads to an error
310 * IGNORE: "default ignorable" chars are stripped
311 * CASEFOLD: unicode casefolding is applied
312 * COMPAT: replace certain characters with their
313 * compatibility decomposition
314 * CHARBOUND: Inserts 0xFF bytes before each grapheme cluster
315 * LUMP: lumps certain different characters together
316 * STRIPMARK: removes all character marks
317 * The pointer 'last_boundclass' has to point to an integer variable which
318 * is storing the last character boundary class, if the CHARBOUND option
319 * is used.
320 * In case of success the number of chars written is returned,
321 * in case of an error, a negative error code is returned.
322 * If the number of written chars would be bigger than 'bufsize',
323 * the buffer (up to 'bufsize') has inpredictable data, and the needed
324 * buffer size is returned.
325 * WARNING: The parameter 'uc' has to be in the range of 0x0000 to
326 * 0x10FFFF, otherwise the program might crash!
327 */
329 DLLEXPORT ssize_t utf8proc_decompose(
330 const uint8_t *str, ssize_t strlen,
331 int32_t *buffer, ssize_t bufsize, int options
332 );
333 /*
334 * Does the same as 'utf8proc_decompose_char', but acts on a whole UTF-8
335 * string, and orders the decomposed sequences correctly.
336 * If the NULLTERM flag in 'options' is set, processing will be stopped,
337 * when a NULL byte is encounted, otherwise 'strlen' bytes are processed.
338 * The result in form of unicode code points is written into the buffer
339 * being pointed to by 'buffer', having the length of 'bufsize' entries.
340 * In case of success the number of chars written is returned,
341 * in case of an error, a negative error code is returned.
342 * If the number of written chars would be bigger than 'bufsize',
343 * the buffer (up to 'bufsize') has inpredictable data, and the needed
344 * buffer size is returned.
345 */
347 DLLEXPORT ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options);
348 /*
349 * Reencodes the sequence of unicode characters given by the pointer
350 * 'buffer' and 'length' as UTF-8.
351 * The result is stored in the same memory area where the data is read.
352 * Following flags in the 'options' field are regarded:
353 * NLF2LS: converts LF, CRLF, CR and NEL into LS
354 * NLF2PS: converts LF, CRLF, CR and NEL into PS
355 * NLF2LF: converts LF, CRLF, CR and NEL into LF
356 * STRIPCC: strips or converts all non-affected control characters
357 * COMPOSE: tries to combine decomposed characters into composite
358 * characters
359 * STABLE: prohibits combining characters which would violate
360 * the unicode versioning stability
361 * In case of success the length of the resulting UTF-8 string is
362 * returned, otherwise a negative error code is returned.
363 * WARNING: The amount of free space being pointed to by 'buffer', has to
364 * exceed the amount of the input data by one byte, and the
365 * entries of the array pointed to by 'str' have to be in the
366 * range of 0x0000 to 0x10FFFF, otherwise the program might
367 * crash!
368 */
370 DLLEXPORT ssize_t utf8proc_map(
371 const uint8_t *str, ssize_t strlen, uint8_t **dstptr, int options
372 );
373 /*
374 * Maps the given UTF-8 string being pointed to by 'str' to a new UTF-8
375 * string, which is allocated dynamically, and afterwards pointed to by
376 * the pointer being pointed to by 'dstptr'.
377 * If the NULLTERM flag in the 'options' field is set, the length is
378 * determined by a NULL terminator, otherwise the parameter 'strlen' is
379 * evaluated to determine the string length, but in any case the result
380 * will be NULL terminated (though it might contain NULL characters
381 * before). Other flags in the 'options' field are passed to the functions
382 * defined above, and regarded as described.
383 * In case of success the length of the new string is returned,
384 * otherwise a negative error code is returned.
385 * NOTICE: The memory of the new UTF-8 string will have been allocated with
386 * 'malloc', and has theirfore to be freed with 'free'.
387 */
389 DLLEXPORT uint8_t *utf8proc_NFD(const uint8_t *str);
390 DLLEXPORT uint8_t *utf8proc_NFC(const uint8_t *str);
391 DLLEXPORT uint8_t *utf8proc_NFKD(const uint8_t *str);
392 DLLEXPORT uint8_t *utf8proc_NFKC(const uint8_t *str);
393 /*
394 * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC
395 * normalized version of the null-terminated string 'str'.
396 */
398 #ifdef __cplusplus
399 }
400 #endif
402 #endif

Impressum / About Us