Use the new u*_cmp2 functions.
[gnulib.git] / lib / unicase.h
1 /* Unicode character case mappings.
2    Copyright (C) 2002, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #ifndef _UNICASE_H
18 #define _UNICASE_H
19
20 #include "unitypes.h"
21
22 /* Get bool.  */
23 #include <stdbool.h>
24
25 /* Get size_t.  */
26 #include <stddef.h>
27
28 /* Get uninorm_t.  */
29 #include "uninorm.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /* ========================================================================= */
36
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
41    language aware.  */
42
43 /* Return the uppercase mapping of a Unicode character.  */
44 extern ucs4_t
45        uc_toupper (ucs4_t uc);
46
47 /* Return the lowercase mapping of a Unicode character.  */
48 extern ucs4_t
49        uc_tolower (ucs4_t uc);
50
51 /* Return the titlecase mapping of a Unicode character.  */
52 extern ucs4_t
53        uc_totitle (ucs4_t uc);
54
55 /* ========================================================================= */
56
57 /* String case mappings.  */
58
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.  */
62
63 /* Return the ISO 639 language code of the current locale.
64    Return "" if it is unknown, or in the "C" locale.  */
65 extern const char *
66        uc_locale_language (void);
67
68 /* Conventions:
69
70    All functions prefixed with u8_ operate on UTF-8 encoded strings.
71    Their unit is an uint8_t (1 byte).
72
73    All functions prefixed with u16_ operate on UTF-16 encoded strings.
74    Their unit is an uint16_t (a 2-byte word).
75
76    All functions prefixed with u32_ operate on UCS-4 encoded strings.
77    Their unit is an uint32_t (a 4-byte word).
78
79    All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
80    n units.
81
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.  */
88
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.  */
92 extern uint8_t *
93        u8_toupper (const uint8_t *s, size_t n, const char *iso639_language,
94                    uninorm_t nf,
95                    uint8_t *resultbuf, size_t *lengthp);
96 extern uint16_t *
97        u16_toupper (const uint16_t *s, size_t n, const char *iso639_language,
98                     uninorm_t nf,
99                     uint16_t *resultbuf, size_t *lengthp);
100 extern uint32_t *
101        u32_toupper (const uint32_t *s, size_t n, const char *iso639_language,
102                     uninorm_t nf,
103                     uint32_t *resultbuf, size_t *lengthp);
104
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.  */
108 extern uint8_t *
109        u8_tolower (const uint8_t *s, size_t n, const char *iso639_language,
110                    uninorm_t nf,
111                    uint8_t *resultbuf, size_t *lengthp);
112 extern uint16_t *
113        u16_tolower (const uint16_t *s, size_t n, const char *iso639_language,
114                     uninorm_t nf,
115                     uint16_t *resultbuf, size_t *lengthp);
116 extern uint32_t *
117        u32_tolower (const uint32_t *s, size_t n, const char *iso639_language,
118                     uninorm_t nf,
119                     uint32_t *resultbuf, size_t *lengthp);
120
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.  */
124 extern uint8_t *
125        u8_totitle (const uint8_t *s, size_t n, const char *iso639_language,
126                    uninorm_t nf,
127                    uint8_t *resultbuf, size_t *lengthp);
128 extern uint16_t *
129        u16_totitle (const uint16_t *s, size_t n, const char *iso639_language,
130                     uninorm_t nf,
131                     uint16_t *resultbuf, size_t *lengthp);
132 extern uint32_t *
133        u32_totitle (const uint32_t *s, size_t n, const char *iso639_language,
134                     uninorm_t nf,
135                     uint32_t *resultbuf, size_t *lengthp);
136
137 /* Return the case folded string.
138    The nf argument identifies the normalization form to apply after the
139    case-mapping.  It can also be NULL, for no normalization.  */
140 extern uint8_t *
141        u8_casefold (const uint8_t *s, size_t n, const char *iso639_language,
142                     uninorm_t nf,
143                     uint8_t *resultbuf, size_t *lengthp);
144 extern uint16_t *
145        u16_casefold (const uint16_t *s, size_t n, const char *iso639_language,
146                      uninorm_t nf,
147                      uint16_t *resultbuf, size_t *lengthp);
148 extern uint32_t *
149        u32_casefold (const uint32_t *s, size_t n, const char *iso639_language,
150                      uninorm_t nf,
151                      uint32_t *resultbuf, size_t *lengthp);
152
153 /* Compare S1 and S2, ignoring differences in case and normalization.
154    The nf argument identifies the normalization form to apply after the
155    case-mapping.  It can also be NULL, for no normalization.
156    If successful, set *RESULTP to -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2, and
157    return 0.  Upon failure, return -1 with errno set.  */
158 extern int
159        u8_casecmp (const uint8_t *s1, size_t n1,
160                    const uint8_t *s2, size_t n2,
161                    const char *iso639_language, uninorm_t nf, int *resultp);
162 extern int
163        u16_casecmp (const uint16_t *s1, size_t n1,
164                     const uint16_t *s2, size_t n2,
165                     const char *iso639_language, uninorm_t nf, int *resultp);
166 extern int
167        u32_casecmp (const uint32_t *s1, size_t n1,
168                     const uint32_t *s2, size_t n2,
169                     const char *iso639_language, uninorm_t nf, int *resultp);
170 extern int
171        ulc_casecmp (const char *s1, size_t n1,
172                     const char *s2, size_t n2,
173                     const char *iso639_language, uninorm_t nf, int *resultp);
174
175 /* Converts the string S of length N to a string in locale encoding, in such a
176    way that comparing uN_casexfrm (S1) and uN_casexfrm (S2) with uN_cmp2() is
177    equivalent to comparing S1 and S2 with uN_casecoll().
178    NF must be either UNINORM_NFC, UNINORM_NFKC, or NULL for no normalization.  */
179 extern char *
180        u8_casexfrm (const uint8_t *s, size_t n, const char *iso639_language,
181                     uninorm_t nf, char *resultbuf, size_t *lengthp);
182 extern char *
183        u16_casexfrm (const uint16_t *s, size_t n, const char *iso639_language,
184                      uninorm_t nf, char *resultbuf, size_t *lengthp);
185 extern char *
186        u32_casexfrm (const uint32_t *s, size_t n, const char *iso639_language,
187                      uninorm_t nf, char *resultbuf, size_t *lengthp);
188 extern char *
189        ulc_casexfrm (const char *s, size_t n, const char *iso639_language,
190                      uninorm_t nf, char *resultbuf, size_t *lengthp);
191
192 /* Compare S1 and S2, ignoring differences in case and normalization, using the
193    collation rules of the current locale.
194    The nf argument identifies the normalization form to apply after the
195    case-mapping.  It must be either UNINORM_NFC or UNINORM_NFKC.  It can also
196    be NULL, for no normalization.
197    If successful, set *RESULTP to -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2, and
198    return 0.  Upon failure, return -1 with errno set.  */
199 extern int
200        u8_casecoll (const uint8_t *s1, size_t n1,
201                     const uint8_t *s2, size_t n2,
202                     const char *iso639_language, uninorm_t nf, int *resultp);
203 extern int
204        u16_casecoll (const uint16_t *s1, size_t n1,
205                      const uint16_t *s2, size_t n2,
206                      const char *iso639_language, uninorm_t nf, int *resultp);
207 extern int
208        u32_casecoll (const uint32_t *s1, size_t n1,
209                      const uint32_t *s2, size_t n2,
210                      const char *iso639_language, uninorm_t nf, int *resultp);
211 extern int
212        ulc_casecoll (const char *s1, size_t n1,
213                      const char *s2, size_t n2,
214                      const char *iso639_language, uninorm_t nf, int *resultp);
215
216
217 /* Set *RESULTP to true if mapping NFD(S) to upper case is a no-op, or to false
218    otherwise, and return 0.  Upon failure, return -1 with errno set.  */
219 extern int
220        u8_is_uppercase (const uint8_t *s, size_t n,
221                         const char *iso639_language,
222                         bool *resultp);
223 extern int
224        u16_is_uppercase (const uint16_t *s, size_t n,
225                          const char *iso639_language,
226                          bool *resultp);
227 extern int
228        u32_is_uppercase (const uint32_t *s, size_t n,
229                          const char *iso639_language,
230                          bool *resultp);
231
232 /* Set *RESULTP to true if mapping NFD(S) to lower case is a no-op, or to false
233    otherwise, and return 0.  Upon failure, return -1 with errno set.  */
234 extern int
235        u8_is_lowercase (const uint8_t *s, size_t n,
236                         const char *iso639_language,
237                         bool *resultp);
238 extern int
239        u16_is_lowercase (const uint16_t *s, size_t n,
240                          const char *iso639_language,
241                          bool *resultp);
242 extern int
243        u32_is_lowercase (const uint32_t *s, size_t n,
244                          const char *iso639_language,
245                          bool *resultp);
246
247 /* Set *RESULTP to true if mapping NFD(S) to title case is a no-op, or to false
248    otherwise, and return 0.  Upon failure, return -1 with errno set.  */
249 extern int
250        u8_is_titlecase (const uint8_t *s, size_t n,
251                         const char *iso639_language,
252                         bool *resultp);
253 extern int
254        u16_is_titlecase (const uint16_t *s, size_t n,
255                          const char *iso639_language,
256                          bool *resultp);
257 extern int
258        u32_is_titlecase (const uint32_t *s, size_t n,
259                          const char *iso639_language,
260                          bool *resultp);
261
262 /* Set *RESULTP to true if applying case folding to NFD(S) is a no-op, or to
263    false otherwise, and return 0.  Upon failure, return -1 with errno set.  */
264 extern int
265        u8_is_casefolded (const uint8_t *s, size_t n,
266                          const char *iso639_language,
267                          bool *resultp);
268 extern int
269        u16_is_casefolded (const uint16_t *s, size_t n,
270                           const char *iso639_language,
271                           bool *resultp);
272 extern int
273        u32_is_casefolded (const uint32_t *s, size_t n,
274                           const char *iso639_language,
275                           bool *resultp);
276
277 /* Set *RESULTP to true if case matters for S, that is, if mapping NFD(S) to
278    either upper case or lower case or title case is not a no-op.
279    Set *RESULTP to false if NFD(S) maps to itself under the upper case mapping,
280    under the lower case mapping, and under the title case mapping; in other
281    words, when NFD(S) consists entirely of caseless characters.
282    Upon failure, return -1 with errno set.  */
283 extern int
284        u8_is_cased (const uint8_t *s, size_t n,
285                     const char *iso639_language,
286                     bool *resultp);
287 extern int
288        u16_is_cased (const uint16_t *s, size_t n,
289                      const char *iso639_language,
290                      bool *resultp);
291 extern int
292        u32_is_cased (const uint32_t *s, size_t n,
293                      const char *iso639_language,
294                      bool *resultp);
295
296
297 /* ========================================================================= */
298
299 #ifdef __cplusplus
300 }
301 #endif
302
303 #endif /* _UNICASE_H */