New module 'unicase/ulc-casexfrm'.
[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 extern char *
188        ulc_casexfrm (const char *s, size_t n, const char *iso639_language,
189                      uninorm_t nf, char *resultbuf, size_t *lengthp);
190
191 /* Compare S1 and S2, ignoring differences in case and normalization, using the
192    collation rules of the current locale.
193    The nf argument identifies the normalization form to apply after the
194    case-mapping.  It must be either UNINORM_NFC or UNINORM_NFKC.  It can also
195    be NULL, for no normalization.
196    If successful, set *RESULTP to -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2, and
197    return 0.  Upon failure, return -1 with errno set.  */
198 extern int
199        u8_casecoll (const uint8_t *s1, size_t n1,
200                     const uint8_t *s2, size_t n2,
201                     const char *iso639_language, uninorm_t nf, int *resultp);
202 extern int
203        u16_casecoll (const uint16_t *s1, size_t n1,
204                      const uint16_t *s2, size_t n2,
205                      const char *iso639_language, uninorm_t nf, int *resultp);
206 extern int
207        u32_casecoll (const uint32_t *s1, size_t n1,
208                      const uint32_t *s2, size_t n2,
209                      const char *iso639_language, uninorm_t nf, int *resultp);
210
211
212 /* Set *RESULTP to true if mapping NFD(S) to upper case is a no-op, or to false
213    otherwise, and return 0.  Upon failure, return -1 with errno set.  */
214 extern int
215        u8_is_uppercase (const uint8_t *s, size_t n,
216                         const char *iso639_language,
217                         bool *resultp);
218 extern int
219        u16_is_uppercase (const uint16_t *s, size_t n,
220                          const char *iso639_language,
221                          bool *resultp);
222 extern int
223        u32_is_uppercase (const uint32_t *s, size_t n,
224                          const char *iso639_language,
225                          bool *resultp);
226
227 /* Set *RESULTP to true if mapping NFD(S) to lower case is a no-op, or to false
228    otherwise, and return 0.  Upon failure, return -1 with errno set.  */
229 extern int
230        u8_is_lowercase (const uint8_t *s, size_t n,
231                         const char *iso639_language,
232                         bool *resultp);
233 extern int
234        u16_is_lowercase (const uint16_t *s, size_t n,
235                          const char *iso639_language,
236                          bool *resultp);
237 extern int
238        u32_is_lowercase (const uint32_t *s, size_t n,
239                          const char *iso639_language,
240                          bool *resultp);
241
242 /* Set *RESULTP to true if mapping NFD(S) to title case is a no-op, or to false
243    otherwise, and return 0.  Upon failure, return -1 with errno set.  */
244 extern int
245        u8_is_titlecase (const uint8_t *s, size_t n,
246                         const char *iso639_language,
247                         bool *resultp);
248 extern int
249        u16_is_titlecase (const uint16_t *s, size_t n,
250                          const char *iso639_language,
251                          bool *resultp);
252 extern int
253        u32_is_titlecase (const uint32_t *s, size_t n,
254                          const char *iso639_language,
255                          bool *resultp);
256
257 /* Set *RESULTP to true if applying case folding to NFD(S) is a no-op, or to
258    false otherwise, and return 0.  Upon failure, return -1 with errno set.  */
259 extern int
260        u8_is_casefolded (const uint8_t *s, size_t n,
261                          const char *iso639_language,
262                          bool *resultp);
263 extern int
264        u16_is_casefolded (const uint16_t *s, size_t n,
265                           const char *iso639_language,
266                           bool *resultp);
267 extern int
268        u32_is_casefolded (const uint32_t *s, size_t n,
269                           const char *iso639_language,
270                           bool *resultp);
271
272 /* Set *RESULTP to true if case matters for S, that is, if mapping NFD(S) to
273    either upper case or lower case or title case is not a no-op.
274    Set *RESULTP to false if NFD(S) maps to itself under the upper case mapping,
275    under the lower case mapping, and under the title case mapping; in other
276    words, when NFD(S) consists entirely of caseless characters.
277    Upon failure, return -1 with errno set.  */
278 extern int
279        u8_is_cased (const uint8_t *s, size_t n,
280                     const char *iso639_language,
281                     bool *resultp);
282 extern int
283        u16_is_cased (const uint16_t *s, size_t n,
284                      const char *iso639_language,
285                      bool *resultp);
286 extern int
287        u32_is_cased (const uint32_t *s, size_t n,
288                      const char *iso639_language,
289                      bool *resultp);
290
291
292 /* ========================================================================= */
293
294 #ifdef __cplusplus
295 }
296 #endif
297
298 #endif /* _UNICASE_H */