Imported Upstream version 58.1
[platform/upstream/icu.git] / source / common / ucase.h
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 2004-2012, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  ucase.h
11 *   encoding:   US-ASCII
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2004aug30
16 *   created by: Markus W. Scherer
17 *
18 *   Low-level Unicode character/string case mapping code.
19 */
20
21 #ifndef __UCASE_H__
22 #define __UCASE_H__
23
24 #include "unicode/utypes.h"
25 #include "unicode/uset.h"
26 #include "putilimp.h"
27 #include "uset_imp.h"
28 #include "udataswp.h"
29
30 #ifdef __cplusplus
31 U_NAMESPACE_BEGIN
32
33 class UnicodeString;
34
35 U_NAMESPACE_END
36 #endif
37
38 /* library API -------------------------------------------------------------- */
39
40 U_CDECL_BEGIN
41
42 struct UCaseProps;
43 typedef struct UCaseProps UCaseProps;
44
45 U_CDECL_END
46
47 U_CAPI const UCaseProps * U_EXPORT2
48 ucase_getSingleton(void);
49
50 U_CFUNC void U_EXPORT2
51 ucase_addPropertyStarts(const UCaseProps *csp, const USetAdder *sa, UErrorCode *pErrorCode);
52
53 /**
54  * Requires non-NULL locale ID but otherwise does the equivalent of
55  * checking for language codes as if uloc_getLanguage() were called:
56  * Accepts both 2- and 3-letter codes and accepts case variants.
57  */
58 U_CFUNC int32_t
59 ucase_getCaseLocale(const char *locale, int32_t *locCache);
60
61 /* Casing locale types for ucase_getCaseLocale */
62 enum {
63     UCASE_LOC_UNKNOWN,
64     UCASE_LOC_ROOT,
65     UCASE_LOC_TURKISH,
66     UCASE_LOC_LITHUANIAN,
67     UCASE_LOC_GREEK,
68     UCASE_LOC_DUTCH
69 };
70
71 /**
72  * Bit mask for getting just the options from a string compare options word
73  * that are relevant for case-insensitive string comparison.
74  * See uchar.h. Also include _STRNCMP_STYLE and U_COMPARE_CODE_POINT_ORDER.
75  * @internal
76  */
77 #define _STRCASECMP_OPTIONS_MASK 0xffff
78
79 /**
80  * Bit mask for getting just the options from a string compare options word
81  * that are relevant for case folding (of a single string or code point).
82  * See uchar.h.
83  * @internal
84  */
85 #define _FOLD_CASE_OPTIONS_MASK 0xff
86
87 /* single-code point functions */
88
89 U_CAPI UChar32 U_EXPORT2
90 ucase_tolower(const UCaseProps *csp, UChar32 c);
91
92 U_CAPI UChar32 U_EXPORT2
93 ucase_toupper(const UCaseProps *csp, UChar32 c);
94
95 U_CAPI UChar32 U_EXPORT2
96 ucase_totitle(const UCaseProps *csp, UChar32 c);
97
98 U_CAPI UChar32 U_EXPORT2
99 ucase_fold(const UCaseProps *csp, UChar32 c, uint32_t options);
100
101 /**
102  * Adds all simple case mappings and the full case folding for c to sa,
103  * and also adds special case closure mappings.
104  * c itself is not added.
105  * For example, the mappings
106  * - for s include long s
107  * - for sharp s include ss
108  * - for k include the Kelvin sign
109  */
110 U_CFUNC void U_EXPORT2
111 ucase_addCaseClosure(const UCaseProps *csp, UChar32 c, const USetAdder *sa);
112
113 /**
114  * Maps the string to single code points and adds the associated case closure
115  * mappings.
116  * The string is mapped to code points if it is their full case folding string.
117  * In other words, this performs a reverse full case folding and then
118  * adds the case closure items of the resulting code points.
119  * If the string is found and its closure applied, then
120  * the string itself is added as well as part of its code points' closure.
121  * It must be length>=0.
122  *
123  * @return TRUE if the string was found
124  */
125 U_CFUNC UBool U_EXPORT2
126 ucase_addStringCaseClosure(const UCaseProps *csp, const UChar *s, int32_t length, const USetAdder *sa);
127
128 #ifdef __cplusplus
129 U_NAMESPACE_BEGIN
130
131 /**
132  * Iterator over characters with more than one code point in the full default Case_Folding.
133  */
134 class U_COMMON_API FullCaseFoldingIterator {
135 public:
136     /** Constructor. */
137     FullCaseFoldingIterator();
138     /**
139      * Returns the next (cp, full) pair where "full" is cp's full default Case_Folding.
140      * Returns a negative cp value at the end of the iteration.
141      */
142     UChar32 next(UnicodeString &full);
143 private:
144     FullCaseFoldingIterator(const FullCaseFoldingIterator &);  // no copy
145     FullCaseFoldingIterator &operator=(const FullCaseFoldingIterator &);  // no assignment
146
147     const UChar *unfold;
148     int32_t unfoldRows;
149     int32_t unfoldRowWidth;
150     int32_t unfoldStringWidth;
151     int32_t currentRow;
152     int32_t rowCpIndex;
153 };
154
155 U_NAMESPACE_END
156 #endif
157
158 /** @return UCASE_NONE, UCASE_LOWER, UCASE_UPPER, UCASE_TITLE */
159 U_CAPI int32_t U_EXPORT2
160 ucase_getType(const UCaseProps *csp, UChar32 c);
161
162 /** @return like ucase_getType() but also sets UCASE_IGNORABLE if c is case-ignorable */
163 U_CAPI int32_t U_EXPORT2
164 ucase_getTypeOrIgnorable(const UCaseProps *csp, UChar32 c);
165
166 U_CAPI UBool U_EXPORT2
167 ucase_isSoftDotted(const UCaseProps *csp, UChar32 c);
168
169 U_CAPI UBool U_EXPORT2
170 ucase_isCaseSensitive(const UCaseProps *csp, UChar32 c);
171
172 /* string case mapping functions */
173
174 U_CDECL_BEGIN
175
176 /**
177  * Iterator function for string case mappings, which need to look at the
178  * context (surrounding text) of a given character for conditional mappings.
179  *
180  * The iterator only needs to go backward or forward away from the
181  * character in question. It does not use any indexes on this interface.
182  * It does not support random access or an arbitrary change of
183  * iteration direction.
184  *
185  * The code point being case-mapped itself is never returned by
186  * this iterator.
187  *
188  * @param context A pointer to the iterator's working data.
189  * @param dir If <0 then start iterating backward from the character;
190  *            if >0 then start iterating forward from the character;
191  *            if 0 then continue iterating in the current direction.
192  * @return Next code point, or <0 when the iteration is done.
193  */
194 typedef UChar32 U_CALLCONV
195 UCaseContextIterator(void *context, int8_t dir);
196
197 /**
198  * Sample struct which may be used by some implementations of
199  * UCaseContextIterator.
200  */
201 struct UCaseContext {
202     void *p;
203     int32_t start, index, limit;
204     int32_t cpStart, cpLimit;
205     int8_t dir;
206     int8_t b1, b2, b3;
207 };
208 typedef struct UCaseContext UCaseContext;
209
210 U_CDECL_END
211
212 #define UCASECONTEXT_INITIALIZER { NULL,  0, 0, 0,  0, 0,  0,  0, 0, 0 }
213
214 enum {
215     /**
216      * For string case mappings, a single character (a code point) is mapped
217      * either to itself (in which case in-place mapping functions do nothing),
218      * or to another single code point, or to a string.
219      * Aside from the string contents, these are indicated with a single int32_t
220      * value as follows:
221      *
222      * Mapping to self: Negative values (~self instead of -self to support U+0000)
223      *
224      * Mapping to another code point: Positive values >UCASE_MAX_STRING_LENGTH
225      *
226      * Mapping to a string: The string length (0..UCASE_MAX_STRING_LENGTH) is
227      * returned. Note that the string result may indeed have zero length.
228      */
229     UCASE_MAX_STRING_LENGTH=0x1f
230 };
231
232 /**
233  * Get the full lowercase mapping for c.
234  *
235  * @param csp Case mapping properties.
236  * @param c Character to be mapped.
237  * @param iter Character iterator, used for context-sensitive mappings.
238  *             See UCaseContextIterator for details.
239  *             If iter==NULL then a context-independent result is returned.
240  * @param context Pointer to be passed into iter.
241  * @param pString If the mapping result is a string, then the pointer is
242  *                written to *pString.
243  * @param locale Locale ID for locale-dependent mappings.
244  * @param locCache Initialize to 0; may be used to cache the result of parsing
245  *                 the locale ID for subsequent calls.
246  *                 Can be NULL.
247  * @return Output code point or string length, see UCASE_MAX_STRING_LENGTH.
248  *
249  * @see UCaseContextIterator
250  * @see UCASE_MAX_STRING_LENGTH
251  * @internal
252  */
253 U_CAPI int32_t U_EXPORT2
254 ucase_toFullLower(const UCaseProps *csp, UChar32 c,
255                   UCaseContextIterator *iter, void *context,
256                   const UChar **pString,
257                   const char *locale, int32_t *locCache);
258
259 U_CAPI int32_t U_EXPORT2
260 ucase_toFullUpper(const UCaseProps *csp, UChar32 c,
261                   UCaseContextIterator *iter, void *context,
262                   const UChar **pString,
263                   const char *locale, int32_t *locCache);
264
265 U_CAPI int32_t U_EXPORT2
266 ucase_toFullTitle(const UCaseProps *csp, UChar32 c,
267                   UCaseContextIterator *iter, void *context,
268                   const UChar **pString,
269                   const char *locale, int32_t *locCache);
270
271 U_CAPI int32_t U_EXPORT2
272 ucase_toFullFolding(const UCaseProps *csp, UChar32 c,
273                     const UChar **pString,
274                     uint32_t options);
275
276 U_CFUNC int32_t U_EXPORT2
277 ucase_hasBinaryProperty(UChar32 c, UProperty which);
278
279
280 U_CDECL_BEGIN
281
282 /**
283  * @internal
284  */
285 typedef int32_t U_CALLCONV
286 UCaseMapFull(const UCaseProps *csp, UChar32 c,
287              UCaseContextIterator *iter, void *context,
288              const UChar **pString,
289              const char *locale, int32_t *locCache);
290
291 U_CDECL_END
292
293 /* file definitions --------------------------------------------------------- */
294
295 #define UCASE_DATA_NAME "ucase"
296 #define UCASE_DATA_TYPE "icu"
297
298 /* format "cAsE" */
299 #define UCASE_FMT_0 0x63
300 #define UCASE_FMT_1 0x41
301 #define UCASE_FMT_2 0x53
302 #define UCASE_FMT_3 0x45
303
304 /* indexes into indexes[] */
305 enum {
306     UCASE_IX_INDEX_TOP,
307     UCASE_IX_LENGTH,
308     UCASE_IX_TRIE_SIZE,
309     UCASE_IX_EXC_LENGTH,
310     UCASE_IX_UNFOLD_LENGTH,
311
312     UCASE_IX_MAX_FULL_LENGTH=15,
313     UCASE_IX_TOP=16
314 };
315
316 /* definitions for 16-bit case properties word ------------------------------ */
317
318 /* 2-bit constants for types of cased characters */
319 #define UCASE_TYPE_MASK     3
320 enum {
321     UCASE_NONE,
322     UCASE_LOWER,
323     UCASE_UPPER,
324     UCASE_TITLE
325 };
326
327 #define UCASE_GET_TYPE(props) ((props)&UCASE_TYPE_MASK)
328 #define UCASE_GET_TYPE_AND_IGNORABLE(props) ((props)&7)
329
330 #define UCASE_IGNORABLE         4
331 #define UCASE_SENSITIVE         8
332 #define UCASE_EXCEPTION         0x10
333
334 #define UCASE_DOT_MASK      0x60
335 enum {
336     UCASE_NO_DOT=0,         /* normal characters with cc=0 */
337     UCASE_SOFT_DOTTED=0x20, /* soft-dotted characters with cc=0 */
338     UCASE_ABOVE=0x40,       /* "above" accents with cc=230 */
339     UCASE_OTHER_ACCENT=0x60 /* other accent character (0<cc!=230) */
340 };
341
342 /* no exception: bits 15..7 are a 9-bit signed case mapping delta */
343 #define UCASE_DELTA_SHIFT   7
344 #define UCASE_DELTA_MASK    0xff80
345 #define UCASE_MAX_DELTA     0xff
346 #define UCASE_MIN_DELTA     (-UCASE_MAX_DELTA-1)
347
348 #if U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
349 #   define UCASE_GET_DELTA(props) ((int16_t)(props)>>UCASE_DELTA_SHIFT)
350 #else
351 #   define UCASE_GET_DELTA(props) (int16_t)(((props)&0x8000) ? (((props)>>UCASE_DELTA_SHIFT)|0xfe00) : ((uint16_t)(props)>>UCASE_DELTA_SHIFT))
352 #endif
353
354 /* exception: bits 15..5 are an unsigned 11-bit index into the exceptions array */
355 #define UCASE_EXC_SHIFT     5
356 #define UCASE_EXC_MASK      0xffe0
357 #define UCASE_MAX_EXCEPTIONS ((UCASE_EXC_MASK>>UCASE_EXC_SHIFT)+1)
358
359 /* definitions for 16-bit main exceptions word ------------------------------ */
360
361 /* first 8 bits indicate values in optional slots */
362 enum {
363     UCASE_EXC_LOWER,
364     UCASE_EXC_FOLD,
365     UCASE_EXC_UPPER,
366     UCASE_EXC_TITLE,
367     UCASE_EXC_4,            /* reserved */
368     UCASE_EXC_5,            /* reserved */
369     UCASE_EXC_CLOSURE,
370     UCASE_EXC_FULL_MAPPINGS,
371     UCASE_EXC_ALL_SLOTS     /* one past the last slot */
372 };
373
374 /* each slot is 2 uint16_t instead of 1 */
375 #define UCASE_EXC_DOUBLE_SLOTS      0x100
376
377 /* reserved: exception bits 11..9 */
378
379 /* UCASE_EXC_DOT_MASK=UCASE_DOT_MASK<<UCASE_EXC_DOT_SHIFT */
380 #define UCASE_EXC_DOT_SHIFT     7
381
382 /* normally stored in the main word, but pushed out for larger exception indexes */
383 #define UCASE_EXC_DOT_MASK      0x3000
384 enum {
385     UCASE_EXC_NO_DOT=0,
386     UCASE_EXC_SOFT_DOTTED=0x1000,
387     UCASE_EXC_ABOVE=0x2000,         /* "above" accents with cc=230 */
388     UCASE_EXC_OTHER_ACCENT=0x3000   /* other character (0<cc!=230) */
389 };
390
391 /* complex/conditional mappings */
392 #define UCASE_EXC_CONDITIONAL_SPECIAL   0x4000
393 #define UCASE_EXC_CONDITIONAL_FOLD      0x8000
394
395 /* definitions for lengths word for full case mappings */
396 #define UCASE_FULL_LOWER    0xf
397 #define UCASE_FULL_FOLDING  0xf0
398 #define UCASE_FULL_UPPER    0xf00
399 #define UCASE_FULL_TITLE    0xf000
400
401 /* maximum lengths */
402 #define UCASE_FULL_MAPPINGS_MAX_LENGTH (4*0xf)
403 #define UCASE_CLOSURE_MAX_LENGTH 0xf
404
405 /* constants for reverse case folding ("unfold") data */
406 enum {
407     UCASE_UNFOLD_ROWS,
408     UCASE_UNFOLD_ROW_WIDTH,
409     UCASE_UNFOLD_STRING_WIDTH
410 };
411
412 #endif