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