1 /* Unicode character case mappings.
2 Copyright (C) 2002, 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
35 /* ========================================================================= */
37 /* Character case mappings.
38 These mappings are locale and context independent.
39 WARNING! These functions are not sufficient for languages such as German.
40 Better use the functions below that treat an entire string at once and are
43 /* Return the uppercase mapping of a Unicode character. */
45 uc_toupper (ucs4_t uc);
47 /* Return the lowercase mapping of a Unicode character. */
49 uc_tolower (ucs4_t uc);
51 /* Return the titlecase mapping of a Unicode character. */
53 uc_totitle (ucs4_t uc);
55 /* ========================================================================= */
57 /* String case mappings. */
59 /* These functions are locale dependent. The iso639_language argument
60 identifies the language (e.g. "tr" for Turkish). NULL means to use
61 locale independent case mappings. */
63 /* Return the ISO 639 language code of the current locale.
64 Return "" if it is unknown, or in the "C" locale. */
66 uc_locale_language (void);
70 All functions prefixed with u8_ operate on UTF-8 encoded strings.
71 Their unit is an uint8_t (1 byte).
73 All functions prefixed with u16_ operate on UTF-16 encoded strings.
74 Their unit is an uint16_t (a 2-byte word).
76 All functions prefixed with u32_ operate on UCS-4 encoded strings.
77 Their unit is an uint32_t (a 4-byte word).
79 All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
82 Functions returning a string result take a (resultbuf, lengthp) argument
83 pair. If resultbuf is not NULL and the result fits into *lengthp units,
84 it is put in resultbuf, and resultbuf is returned. Otherwise, a freshly
85 allocated string is returned. In both cases, *lengthp is set to the
86 length (number of units) of the returned string. In case of error,
87 NULL is returned and errno is set. */
89 /* Return the uppercase mapping of a string.
90 The nf argument identifies the normalization form to apply after the
91 case-mapping. It can also be NULL, for no normalization. */
93 u8_toupper (const uint8_t *s, size_t n, const char *iso639_language,
95 uint8_t *resultbuf, size_t *lengthp);
97 u16_toupper (const uint16_t *s, size_t n, const char *iso639_language,
99 uint16_t *resultbuf, size_t *lengthp);
101 u32_toupper (const uint32_t *s, size_t n, const char *iso639_language,
103 uint32_t *resultbuf, size_t *lengthp);
105 /* Return the lowercase mapping of a string.
106 The nf argument identifies the normalization form to apply after the
107 case-mapping. It can also be NULL, for no normalization. */
109 u8_tolower (const uint8_t *s, size_t n, const char *iso639_language,
111 uint8_t *resultbuf, size_t *lengthp);
113 u16_tolower (const uint16_t *s, size_t n, const char *iso639_language,
115 uint16_t *resultbuf, size_t *lengthp);
117 u32_tolower (const uint32_t *s, size_t n, const char *iso639_language,
119 uint32_t *resultbuf, size_t *lengthp);
121 /* Return the titlecase mapping of a string.
122 The nf argument identifies the normalization form to apply after the
123 case-mapping. It can also be NULL, for no normalization. */
125 u8_totitle (const uint8_t *s, size_t n, const char *iso639_language,
127 uint8_t *resultbuf, size_t *lengthp);
129 u16_totitle (const uint16_t *s, size_t n, const char *iso639_language,
131 uint16_t *resultbuf, size_t *lengthp);
133 u32_totitle (const uint32_t *s, size_t n, const char *iso639_language,
135 uint32_t *resultbuf, size_t *lengthp);
137 /* The case-mapping context given by a prefix string. */
138 typedef struct casing_prefix_context
140 /* These fields are private, undocumented. */
141 uint32_t last_char_except_ignorable;
142 uint32_t last_char_normal_or_above;
144 casing_prefix_context_t;
145 /* The case-mapping context of the empty prefix string. */
146 extern const casing_prefix_context_t unicase_empty_prefix_context;
147 /* Return the case-mapping context of a given prefix string. */
148 extern casing_prefix_context_t
149 u8_casing_prefix_context (const uint8_t *s, size_t n);
150 extern casing_prefix_context_t
151 u16_casing_prefix_context (const uint16_t *s, size_t n);
152 extern casing_prefix_context_t
153 u32_casing_prefix_context (const uint32_t *s, size_t n);
154 /* Return the case-mapping context of the prefix concat(A, S), given the
155 case-mapping context of the prefix A. */
156 extern casing_prefix_context_t
157 u8_casing_prefixes_context (const uint8_t *s, size_t n,
158 casing_prefix_context_t a_context);
159 extern casing_prefix_context_t
160 u16_casing_prefixes_context (const uint16_t *s, size_t n,
161 casing_prefix_context_t a_context);
162 extern casing_prefix_context_t
163 u32_casing_prefixes_context (const uint32_t *s, size_t n,
164 casing_prefix_context_t a_context);
166 /* The case-mapping context given by a suffix string. */
167 typedef struct casing_suffix_context
169 /* These fields are private, undocumented. */
170 uint32_t first_char_except_ignorable;
173 casing_suffix_context_t;
174 /* The case-mapping context of the empty suffix string. */
175 extern const casing_suffix_context_t unicase_empty_suffix_context;
176 /* Return the case-mapping context of a given suffix string. */
177 extern casing_suffix_context_t
178 u8_casing_suffix_context (const uint8_t *s, size_t n);
179 extern casing_suffix_context_t
180 u16_casing_suffix_context (const uint16_t *s, size_t n);
181 extern casing_suffix_context_t
182 u32_casing_suffix_context (const uint32_t *s, size_t n);
183 /* Return the case-mapping context of the suffix concat(S, A), given the
184 case-mapping context of the suffix A. */
185 extern casing_suffix_context_t
186 u8_casing_suffixes_context (const uint8_t *s, size_t n,
187 casing_suffix_context_t a_context);
188 extern casing_suffix_context_t
189 u16_casing_suffixes_context (const uint16_t *s, size_t n,
190 casing_suffix_context_t a_context);
191 extern casing_suffix_context_t
192 u32_casing_suffixes_context (const uint32_t *s, size_t n,
193 casing_suffix_context_t a_context);
195 /* Return the uppercase mapping of a string that is surrounded by a prefix
198 u8_ct_toupper (const uint8_t *s, size_t n,
199 casing_prefix_context_t prefix_context,
200 casing_suffix_context_t suffix_context,
201 const char *iso639_language,
203 uint8_t *resultbuf, size_t *lengthp);
205 u16_ct_toupper (const uint16_t *s, size_t n,
206 casing_prefix_context_t prefix_context,
207 casing_suffix_context_t suffix_context,
208 const char *iso639_language,
210 uint16_t *resultbuf, size_t *lengthp);
212 u32_ct_toupper (const uint32_t *s, size_t n,
213 casing_prefix_context_t prefix_context,
214 casing_suffix_context_t suffix_context,
215 const char *iso639_language,
217 uint32_t *resultbuf, size_t *lengthp);
219 /* Return the lowercase mapping of a string that is surrounded by a prefix
222 u8_ct_tolower (const uint8_t *s, size_t n,
223 casing_prefix_context_t prefix_context,
224 casing_suffix_context_t suffix_context,
225 const char *iso639_language,
227 uint8_t *resultbuf, size_t *lengthp);
229 u16_ct_tolower (const uint16_t *s, size_t n,
230 casing_prefix_context_t prefix_context,
231 casing_suffix_context_t suffix_context,
232 const char *iso639_language,
234 uint16_t *resultbuf, size_t *lengthp);
236 u32_ct_tolower (const uint32_t *s, size_t n,
237 casing_prefix_context_t prefix_context,
238 casing_suffix_context_t suffix_context,
239 const char *iso639_language,
241 uint32_t *resultbuf, size_t *lengthp);
243 /* Return the titlecase mapping of a string that is surrounded by a prefix
246 u8_ct_totitle (const uint8_t *s, size_t n,
247 casing_prefix_context_t prefix_context,
248 casing_suffix_context_t suffix_context,
249 const char *iso639_language,
251 uint8_t *resultbuf, size_t *lengthp);
253 u16_ct_totitle (const uint16_t *s, size_t n,
254 casing_prefix_context_t prefix_context,
255 casing_suffix_context_t suffix_context,
256 const char *iso639_language,
258 uint16_t *resultbuf, size_t *lengthp);
260 u32_ct_totitle (const uint32_t *s, size_t n,
261 casing_prefix_context_t prefix_context,
262 casing_suffix_context_t suffix_context,
263 const char *iso639_language,
265 uint32_t *resultbuf, size_t *lengthp);
267 /* Return the case folded string.
268 Comparing uN_casefold (S1) and uN_casefold (S2) with uN_cmp2() is equivalent
269 to comparing S1 and S2 with uN_casecmp().
270 The nf argument identifies the normalization form to apply after the
271 case-mapping. It can also be NULL, for no normalization. */
273 u8_casefold (const uint8_t *s, size_t n, const char *iso639_language,
275 uint8_t *resultbuf, size_t *lengthp);
277 u16_casefold (const uint16_t *s, size_t n, const char *iso639_language,
279 uint16_t *resultbuf, size_t *lengthp);
281 u32_casefold (const uint32_t *s, size_t n, const char *iso639_language,
283 uint32_t *resultbuf, size_t *lengthp);
284 /* Likewise, for a string that is surrounded by a prefix and a suffix. */
286 u8_ct_casefold (const uint8_t *s, size_t n,
287 casing_prefix_context_t prefix_context,
288 casing_suffix_context_t suffix_context,
289 const char *iso639_language,
291 uint8_t *resultbuf, size_t *lengthp);
293 u16_ct_casefold (const uint16_t *s, size_t n,
294 casing_prefix_context_t prefix_context,
295 casing_suffix_context_t suffix_context,
296 const char *iso639_language,
298 uint16_t *resultbuf, size_t *lengthp);
300 u32_ct_casefold (const uint32_t *s, size_t n,
301 casing_prefix_context_t prefix_context,
302 casing_suffix_context_t suffix_context,
303 const char *iso639_language,
305 uint32_t *resultbuf, size_t *lengthp);
307 /* Compare S1 and S2, ignoring differences in case and normalization.
308 The nf argument identifies the normalization form to apply after the
309 case-mapping. It can also be NULL, for no normalization.
310 If successful, set *RESULTP to -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2, and
311 return 0. Upon failure, return -1 with errno set. */
313 u8_casecmp (const uint8_t *s1, size_t n1,
314 const uint8_t *s2, size_t n2,
315 const char *iso639_language, uninorm_t nf, int *resultp);
317 u16_casecmp (const uint16_t *s1, size_t n1,
318 const uint16_t *s2, size_t n2,
319 const char *iso639_language, uninorm_t nf, int *resultp);
321 u32_casecmp (const uint32_t *s1, size_t n1,
322 const uint32_t *s2, size_t n2,
323 const char *iso639_language, uninorm_t nf, int *resultp);
325 ulc_casecmp (const char *s1, size_t n1,
326 const char *s2, size_t n2,
327 const char *iso639_language, uninorm_t nf, int *resultp);
329 /* Convert the string S of length N to a NUL-terminated byte sequence, in such
330 a way that comparing uN_casexfrm (S1) and uN_casexfrm (S2) with the gnulib
331 function memcmp2() is equivalent to comparing S1 and S2 with uN_casecoll().
332 NF must be either UNINORM_NFC, UNINORM_NFKC, or NULL for no normalization. */
334 u8_casexfrm (const uint8_t *s, size_t n, const char *iso639_language,
335 uninorm_t nf, char *resultbuf, size_t *lengthp);
337 u16_casexfrm (const uint16_t *s, size_t n, const char *iso639_language,
338 uninorm_t nf, char *resultbuf, size_t *lengthp);
340 u32_casexfrm (const uint32_t *s, size_t n, const char *iso639_language,
341 uninorm_t nf, char *resultbuf, size_t *lengthp);
343 ulc_casexfrm (const char *s, size_t n, const char *iso639_language,
344 uninorm_t nf, char *resultbuf, size_t *lengthp);
346 /* Compare S1 and S2, ignoring differences in case and normalization, using the
347 collation rules of the current locale.
348 The nf argument identifies the normalization form to apply after the
349 case-mapping. It must be either UNINORM_NFC or UNINORM_NFKC. It can also
350 be NULL, for no normalization.
351 If successful, set *RESULTP to -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2, and
352 return 0. Upon failure, return -1 with errno set. */
354 u8_casecoll (const uint8_t *s1, size_t n1,
355 const uint8_t *s2, size_t n2,
356 const char *iso639_language, uninorm_t nf, int *resultp);
358 u16_casecoll (const uint16_t *s1, size_t n1,
359 const uint16_t *s2, size_t n2,
360 const char *iso639_language, uninorm_t nf, int *resultp);
362 u32_casecoll (const uint32_t *s1, size_t n1,
363 const uint32_t *s2, size_t n2,
364 const char *iso639_language, uninorm_t nf, int *resultp);
366 ulc_casecoll (const char *s1, size_t n1,
367 const char *s2, size_t n2,
368 const char *iso639_language, uninorm_t nf, int *resultp);
371 /* Set *RESULTP to true if mapping NFD(S) to upper case is a no-op, or to false
372 otherwise, and return 0. Upon failure, return -1 with errno set. */
374 u8_is_uppercase (const uint8_t *s, size_t n,
375 const char *iso639_language,
378 u16_is_uppercase (const uint16_t *s, size_t n,
379 const char *iso639_language,
382 u32_is_uppercase (const uint32_t *s, size_t n,
383 const char *iso639_language,
386 /* Set *RESULTP to true if mapping NFD(S) to lower case is a no-op, or to false
387 otherwise, and return 0. Upon failure, return -1 with errno set. */
389 u8_is_lowercase (const uint8_t *s, size_t n,
390 const char *iso639_language,
393 u16_is_lowercase (const uint16_t *s, size_t n,
394 const char *iso639_language,
397 u32_is_lowercase (const uint32_t *s, size_t n,
398 const char *iso639_language,
401 /* Set *RESULTP to true if mapping NFD(S) to title case is a no-op, or to false
402 otherwise, and return 0. Upon failure, return -1 with errno set. */
404 u8_is_titlecase (const uint8_t *s, size_t n,
405 const char *iso639_language,
408 u16_is_titlecase (const uint16_t *s, size_t n,
409 const char *iso639_language,
412 u32_is_titlecase (const uint32_t *s, size_t n,
413 const char *iso639_language,
416 /* Set *RESULTP to true if applying case folding to NFD(S) is a no-op, or to
417 false otherwise, and return 0. Upon failure, return -1 with errno set. */
419 u8_is_casefolded (const uint8_t *s, size_t n,
420 const char *iso639_language,
423 u16_is_casefolded (const uint16_t *s, size_t n,
424 const char *iso639_language,
427 u32_is_casefolded (const uint32_t *s, size_t n,
428 const char *iso639_language,
431 /* Set *RESULTP to true if case matters for S, that is, if mapping NFD(S) to
432 either upper case or lower case or title case is not a no-op.
433 Set *RESULTP to false if NFD(S) maps to itself under the upper case mapping,
434 under the lower case mapping, and under the title case mapping; in other
435 words, when NFD(S) consists entirely of caseless characters.
436 Upon failure, return -1 with errno set. */
438 u8_is_cased (const uint8_t *s, size_t n,
439 const char *iso639_language,
442 u16_is_cased (const uint16_t *s, size_t n,
443 const char *iso639_language,
446 u32_is_cased (const uint32_t *s, size_t n,
447 const char *iso639_language,
451 /* ========================================================================= */
457 #endif /* _UNICASE_H */