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