utf8proc

view utf8proc.h @ 6:d04d3a9b486e

Version 1.0.3

- Fixed a bug in the ruby library, which caused an error, when splitting an empty string at grapheme cluster boundaries (method String#utf8chars).
author jbe
date Fri Mar 16 12:00:00 2007 +0100 (2007-03-16)
parents 4ee0d5f54af1
children fcfd8c836c64
line source
1 /*
2 * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany
3 * Author: Jan Behrens <jan.behrens@flexiguided.de>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the FlexiGuided GmbH nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * This library contains derived data from a modified version of the
33 * Unicode data files.
34 *
35 * The original data files are available at
36 * http://www.unicode.org/Public/UNIDATA/
37 *
38 * Please notice the copyright statement in the file "utf8proc_data.c".
39 *
40 */
43 /*
44 * File name: utf8proc.h
45 * Version: 1.0
46 * Last changed: 2006-09-17
47 *
48 * Description:
49 * Header files for libutf8proc, which is a mapping tool for UTF-8 strings
50 * with following features:
51 * - decomposing and composing of strings
52 * - replacing compatibility characters with their equivalents
53 * - stripping of "default ignorable characters"
54 * like SOFT-HYPHEN or ZERO-WIDTH-SPACE
55 * - folding of certain characters for string comparison
56 * (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-")
57 * (see "LUMP" option)
58 * - optional rejection of strings containing non-assigned code points
59 * - stripping of control characters
60 * - stripping of character marks (accents, etc.)
61 * - transformation of LF, CRLF, CR and NEL to line-feed (LF)
62 * or to the unicode chararacters for paragraph separation (PS)
63 * or line separation (LS).
64 * - unicode case folding (for case insensitive string comparisons)
65 * - rejection of illegal UTF-8 data (i.e. UTF-8 encoded UTF-16 surrogates)
66 * - support for korean hangul characters
67 * Unicode Version 5.0.0 is supported.
68 */
71 #ifndef UTF8PROC_H
72 #define UTF8PROC_H
75 #include <stdlib.h>
76 #include <stdbool.h>
77 #include <sys/types.h>
78 #include <inttypes.h>
79 #include <limits.h>
81 #ifndef SSIZE_MAX
82 #define SSIZE_MAX (SIZE_MAX/2)
83 #endif
85 #define UTF8PROC_NULLTERM (1<<0)
86 #define UTF8PROC_STABLE (1<<1)
87 #define UTF8PROC_COMPAT (1<<2)
88 #define UTF8PROC_COMPOSE (1<<3)
89 #define UTF8PROC_DECOMPOSE (1<<4)
90 #define UTF8PROC_IGNORE (1<<5)
91 #define UTF8PROC_REJECTNA (1<<6)
92 #define UTF8PROC_NLF2LS (1<<7)
93 #define UTF8PROC_NLF2PS (1<<8)
94 #define UTF8PROC_NLF2LF (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS)
95 #define UTF8PROC_STRIPCC (1<<9)
96 #define UTF8PROC_CASEFOLD (1<<10)
97 #define UTF8PROC_CHARBOUND (1<<11)
98 #define UTF8PROC_LUMP (1<<12)
99 #define UTF8PROC_STRIPMARK (1<<13)
100 /*
101 * Flags being regarded by several functions in the library:
102 * NULLTERM: The given UTF-8 input is NULL terminated.
103 * STABLE: Unicode Versioning Stability has to be respected.
104 * COMPAT: Compatiblity decomposition
105 * (i.e. formatting information is lost)
106 * COMPOSE: Return a result with composed characters.
107 * DECOMPOSE: Return a result with decomposed characters.
108 * IGNORE: Strip "default ignorable characters"
109 * REJECTNA: Return an error, if the input contains unassigned code points.
110 * NLF2LS: Indicating that NLF-sequences (LF, CRLF, CR, NEL) are
111 * representing a line break, and should be converted to the
112 * unicode character for line separation (LS).
113 * NLF2PS: Indicating that NLF-sequences are representing a paragraph
114 * break, and should be converted to the unicode character for
115 * paragraph separation (PS).
116 * NLF2LF: Indicating that the meaning of NLF-sequences is unknown.
117 * STRIPCC: Strips and/or convers control characters.
118 * NLF-sequences are transformed into space, except if one of the
119 * NLF2LS/PS/LF options is given.
120 * HorizontalTab (HT) and FormFeed (FF) are treated as a
121 * NLF-sequence in this case.
122 * All other control characters are simply removed.
123 * CASEFOLD: Performs unicode case folding, to be able to do a
124 * case-insensitive string comparison.
125 * CHARBOUND: Inserts 0xFF bytes at the beginning of each sequence which is
126 * representing a single grapheme cluster (a single character).
127 * LUMP: Lumps certain characters together
128 * (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-").
129 * (See lump.txt for details.)
130 * If NLF2LF is set, this includes a transformation of paragraph
131 * and line separators to ASCII line-feed (LF).
132 * STRIPMARK: Strips all character markings
133 * (non-spacing, spacing and enclosing) (i.e. accents)
134 * NOTE: this option works only with COMPOSE or DECOMPOSE
135 */
137 #define UTF8PROC_ERROR_NOMEM -1
138 #define UTF8PROC_ERROR_OVERFLOW -2
139 #define UTF8PROC_ERROR_INVALIDUTF8 -3
140 #define UTF8PROC_ERROR_NOTASSIGNED -4
141 #define UTF8PROC_ERROR_INVALIDOPTS -5
142 /*
143 * Error codes being returned by almost all functions:
144 * ERROR_NOMEM: Memory could not be allocated.
145 * ERROR_OVERFLOW: The given string is too long to be processed.
146 * ERROR_INVALIDUTF8: The given string is not a legal UTF-8 string.
147 * ERROR_NOTASSIGNED: The REJECTNA flag was set,
148 * and an unassigned code point was found.
149 * ERROR_INVALIDOPTS: Invalid options have been used.
150 */
152 typedef int16_t utf8proc_propval_t;
153 typedef struct utf8proc_property_struct {
154 utf8proc_propval_t category;
155 utf8proc_propval_t combining_class;
156 utf8proc_propval_t bidi_class;
157 utf8proc_propval_t decomp_type;
158 const int32_t *decomp_mapping;
159 const unsigned bidi_mirrored:1;
160 const int32_t uppercase_mapping;
161 const int32_t lowercase_mapping;
162 const int32_t titlecase_mapping;
163 const int32_t comb1st_index;
164 const int32_t comb2nd_index;
165 const unsigned comp_exclusion:1;
166 const unsigned ignorable:1;
167 const unsigned control_boundary:1;
168 const unsigned extend:1;
169 const int32_t *casefold_mapping;
170 } utf8proc_property_t;
172 #define UTF8PROC_CATEGORY_LU 1
173 #define UTF8PROC_CATEGORY_LL 2
174 #define UTF8PROC_CATEGORY_LT 3
175 #define UTF8PROC_CATEGORY_LM 4
176 #define UTF8PROC_CATEGORY_LO 5
177 #define UTF8PROC_CATEGORY_MN 6
178 #define UTF8PROC_CATEGORY_MC 7
179 #define UTF8PROC_CATEGORY_ME 8
180 #define UTF8PROC_CATEGORY_ND 9
181 #define UTF8PROC_CATEGORY_NL 10
182 #define UTF8PROC_CATEGORY_NO 11
183 #define UTF8PROC_CATEGORY_PC 12
184 #define UTF8PROC_CATEGORY_PD 13
185 #define UTF8PROC_CATEGORY_PS 14
186 #define UTF8PROC_CATEGORY_PE 15
187 #define UTF8PROC_CATEGORY_PI 16
188 #define UTF8PROC_CATEGORY_PF 17
189 #define UTF8PROC_CATEGORY_PO 18
190 #define UTF8PROC_CATEGORY_SM 19
191 #define UTF8PROC_CATEGORY_SC 20
192 #define UTF8PROC_CATEGORY_SK 21
193 #define UTF8PROC_CATEGORY_SO 22
194 #define UTF8PROC_CATEGORY_ZS 23
195 #define UTF8PROC_CATEGORY_ZL 24
196 #define UTF8PROC_CATEGORY_ZP 25
197 #define UTF8PROC_CATEGORY_CC 26
198 #define UTF8PROC_CATEGORY_CF 27
199 #define UTF8PROC_CATEGORY_CS 28
200 #define UTF8PROC_CATEGORY_CO 29
201 #define UTF8PROC_CATEGORY_CN 30
202 #define UTF8PROC_BIDI_CLASS_L 1
203 #define UTF8PROC_BIDI_CLASS_LRE 2
204 #define UTF8PROC_BIDI_CLASS_LRO 3
205 #define UTF8PROC_BIDI_CLASS_R 4
206 #define UTF8PROC_BIDI_CLASS_AL 5
207 #define UTF8PROC_BIDI_CLASS_RLE 6
208 #define UTF8PROC_BIDI_CLASS_RLO 7
209 #define UTF8PROC_BIDI_CLASS_PDF 8
210 #define UTF8PROC_BIDI_CLASS_EN 9
211 #define UTF8PROC_BIDI_CLASS_ES 10
212 #define UTF8PROC_BIDI_CLASS_ET 11
213 #define UTF8PROC_BIDI_CLASS_AN 12
214 #define UTF8PROC_BIDI_CLASS_CS 13
215 #define UTF8PROC_BIDI_CLASS_NSM 14
216 #define UTF8PROC_BIDI_CLASS_BN 15
217 #define UTF8PROC_BIDI_CLASS_B 16
218 #define UTF8PROC_BIDI_CLASS_S 17
219 #define UTF8PROC_BIDI_CLASS_WS 18
220 #define UTF8PROC_BIDI_CLASS_ON 19
221 #define UTF8PROC_DECOMP_TYPE_FONT 1
222 #define UTF8PROC_DECOMP_TYPE_NOBREAK 2
223 #define UTF8PROC_DECOMP_TYPE_INITIAL 3
224 #define UTF8PROC_DECOMP_TYPE_MEDIAL 4
225 #define UTF8PROC_DECOMP_TYPE_FINAL 5
226 #define UTF8PROC_DECOMP_TYPE_ISOLATED 6
227 #define UTF8PROC_DECOMP_TYPE_CIRCLE 7
228 #define UTF8PROC_DECOMP_TYPE_SUPER 8
229 #define UTF8PROC_DECOMP_TYPE_SUB 9
230 #define UTF8PROC_DECOMP_TYPE_VERTICAL 10
231 #define UTF8PROC_DECOMP_TYPE_WIDE 11
232 #define UTF8PROC_DECOMP_TYPE_NARROW 12
233 #define UTF8PROC_DECOMP_TYPE_SMALL 13
234 #define UTF8PROC_DECOMP_TYPE_SQUARE 14
235 #define UTF8PROC_DECOMP_TYPE_FRACTION 15
236 #define UTF8PROC_DECOMP_TYPE_COMPAT 16
238 extern const int8_t utf8proc_utf8class[256];
240 const char *utf8proc_errmsg(ssize_t errcode);
241 /*
242 * Returns a static error string for the given error code.
243 */
245 ssize_t utf8proc_iterate(uint8_t *str, ssize_t strlen, int32_t *dst);
246 /*
247 * Reads a single char from the UTF-8 sequence being pointed to by 'str'.
248 * The maximum number of bytes read is 'strlen', unless 'strlen' is
249 * negative.
250 * If a valid unicode char could be read, it is stored in the variable
251 * being pointed to by 'dst', otherwise that variable will be set to -1.
252 * In case of success the number of bytes read is returned, otherwise a
253 * negative error code is returned.
254 */
256 ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst);
257 /*
258 * Encodes the unicode char with the code point 'uc' as an UTF-8 string in
259 * the byte array being pointed to by 'dst'. This array has to be at least
260 * 4 bytes long.
261 * In case of success the number of bytes written is returned, otherwise 0.
262 * This function does not check if 'uc' is a valid unicode code point.
263 */
265 const utf8proc_property_t *utf8proc_get_property(int32_t uc);
266 /*
267 * Returns a pointer to a (constant) struct containing information about
268 * the unicode char with the given code point 'uc'.
269 * If the character is not existent a pointer to a special struct is
270 * returned, where 'category' is a NULL pointer.
271 * WARNING: The parameter 'uc' has to be in the range of 0x0000 to
272 * 0x10FFFF, otherwise the program might crash!
273 */
275 ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize,
276 int options, int *last_boundclass);
277 /*
278 * Writes a decomposition of the unicode char 'uc' into the array being
279 * pointed to by 'dst'.
280 * Following flags in the 'options' field are regarded:
281 * REJECTNA: an unassigned unicode code point leads to an error
282 * IGNORE: "default ignorable" chars are stripped
283 * CASEFOLD: unicode casefolding is applied
284 * COMPAT: replace certain characters with their
285 * compatibility decomposition
286 * CHARBOUND: Inserts 0xFF bytes before each grapheme cluster
287 * LUMP: lumps certain different characters together
288 * STRIPMARK: removes all character marks
289 * The pointer 'last_boundclass' has to point to an integer variable which is
290 * storing the last character boundary class, if the CHARBOUND option is
291 * used.
292 * In case of success the number of chars written is returned,
293 * in case of an error, a negative error code is returned.
294 * If the number of written chars would be bigger than 'bufsize',
295 * the buffer (up to 'bufsize') has inpredictable data, and the needed
296 * buffer size is returned.
297 * WARNING: The parameter 'uc' has to be in the range of 0x0000 to
298 * 0x10FFFF, otherwise the program might crash!
299 */
301 ssize_t utf8proc_decompose(uint8_t *str, ssize_t strlen,
302 int32_t *buffer, ssize_t bufsize, int options);
303 /*
304 * Does the same as 'utf8proc_decompose_char', but acts on a whole UTF-8
305 * string, and orders the decomposed sequences correctly.
306 * If the NULLTERM flag in 'options' is set, processing will be stopped,
307 * when a NULL byte is encounted, otherwise 'strlen' bytes are processed.
308 * The result in form of unicode code points is written into the buffer
309 * being pointed to by 'buffer', having the length of 'bufsize' entries.
310 * In case of success the number of chars written is returned,
311 * in case of an error, a negative error code is returned.
312 * If the number of written chars would be bigger than 'bufsize',
313 * the buffer (up to 'bufsize') has inpredictable data, and the needed
314 * buffer size is returned.
315 */
317 ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options);
318 /*
319 * Reencodes the sequence of unicode characters given by the pointer
320 * 'buffer' and 'length' as UTF-8.
321 * The result is stored in the same memory area where the data is read.
322 * Following flags in the 'options' field are regarded:
323 * NLF2LS: converts LF, CRLF, CR and NEL into LS
324 * NLF2PS: converts LF, CRLF, CR and NEL into PS
325 * NLF2LF: converts LF, CRLF, CR and NEL into LF
326 * STRIPCC: strips or converts all non-affected control characters
327 * COMPOSE: tries to combine decomposed characters into composite characters
328 * STABLE: prohibits combining characters which would violate
329 * the unicode versioning stability
330 * In case of success the length of the resulting UTF-8 string is returned,
331 * otherwise a negative error code is returned.
332 * WARNING: The amount of free space being pointed to by 'buffer', has to
333 * exceed the amount of the input data by one byte, and the
334 * entries of the array pointed to by 'str' have to be in the
335 * range of 0x0000 to 0x10FFFF, otherwise the program might crash!
336 */
338 ssize_t utf8proc_map(uint8_t *str, ssize_t strlen, uint8_t **dstptr,
339 int options);
340 /*
341 * Maps the given UTF-8 string being pointed to by 'str' to a new UTF-8
342 * string, which is allocated dynamically, and afterwards pointed to by the
343 * pointer being pointed to by 'dstptr'.
344 * If the NULLTERM flag in the 'options' field is set, the length is
345 * determined by a NULL terminator, otherwise the parameter 'strlen' is
346 * evaluated to determine the string length, but in any case the result
347 * will be NULL terminated (though it might contain NULL characters before).
348 * Other flags in the 'options' field are passed to the functions defined
349 * above, and regarded as described.
350 * In case of success the length of the new string is returned,
351 * otherwise a negative error code is returned.
352 * NOTICE: The memory of the new UTF-8 string will have been allocated with
353 * 'malloc', and has theirfore to be freed with 'free'.
354 */
356 uint8_t *utf8proc_NFD(uint8_t *str);
357 uint8_t *utf8proc_NFC(uint8_t *str);
358 uint8_t *utf8proc_NFKD(uint8_t *str);
359 uint8_t *utf8proc_NFKC(uint8_t *str);
360 /*
361 * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC
362 * normalized version of the null-terminated string 'str'.
363 */
366 #endif

Impressum / About Us