1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2004-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
12 * tab size: 8 (not used)
15 * created on: 2004aug30
16 * created by: Markus W. Scherer
18 * Low-level Unicode character/string case mapping code.
24 #include "unicode/utypes.h"
25 #include "unicode/uset.h"
38 /* library API -------------------------------------------------------------- */
40 U_CFUNC
void U_EXPORT2
41 ucase_addPropertyStarts(const USetAdder
*sa
, UErrorCode
*pErrorCode
);
44 * Requires non-NULL locale ID but otherwise does the equivalent of
45 * checking for language codes as if uloc_getLanguage() were called:
46 * Accepts both 2- and 3-letter codes and accepts case variants.
49 ucase_getCaseLocale(const char *locale
);
51 /* Casing locale types for ucase_getCaseLocale */
62 * Bit mask for getting just the options from a string compare options word
63 * that are relevant for case-insensitive string comparison.
64 * See uchar.h. Also include _STRNCMP_STYLE and U_COMPARE_CODE_POINT_ORDER.
67 #define _STRCASECMP_OPTIONS_MASK 0xffff
70 * Bit mask for getting just the options from a string compare options word
71 * that are relevant for case folding (of a single string or code point).
75 #define _FOLD_CASE_OPTIONS_MASK 0xff
77 /* single-code point functions */
79 U_CAPI UChar32 U_EXPORT2
80 ucase_tolower(UChar32 c
);
82 U_CAPI UChar32 U_EXPORT2
83 ucase_toupper(UChar32 c
);
85 U_CAPI UChar32 U_EXPORT2
86 ucase_totitle(UChar32 c
);
88 U_CAPI UChar32 U_EXPORT2
89 ucase_fold(UChar32 c
, uint32_t options
);
92 * Adds all simple case mappings and the full case folding for c to sa,
93 * and also adds special case closure mappings.
94 * c itself is not added.
95 * For example, the mappings
96 * - for s include long s
97 * - for sharp s include ss
98 * - for k include the Kelvin sign
100 U_CFUNC
void U_EXPORT2
101 ucase_addCaseClosure(UChar32 c
, const USetAdder
*sa
);
104 * Maps the string to single code points and adds the associated case closure
106 * The string is mapped to code points if it is their full case folding string.
107 * In other words, this performs a reverse full case folding and then
108 * adds the case closure items of the resulting code points.
109 * If the string is found and its closure applied, then
110 * the string itself is added as well as part of its code points' closure.
111 * It must be length>=0.
113 * @return TRUE if the string was found
115 U_CFUNC UBool U_EXPORT2
116 ucase_addStringCaseClosure(const UChar
*s
, int32_t length
, const USetAdder
*sa
);
122 * Iterator over characters with more than one code point in the full default Case_Folding.
124 class U_COMMON_API FullCaseFoldingIterator
{
127 FullCaseFoldingIterator();
129 * Returns the next (cp, full) pair where "full" is cp's full default Case_Folding.
130 * Returns a negative cp value at the end of the iteration.
132 UChar32
next(UnicodeString
&full
);
134 FullCaseFoldingIterator(const FullCaseFoldingIterator
&); // no copy
135 FullCaseFoldingIterator
&operator=(const FullCaseFoldingIterator
&); // no assignment
139 int32_t unfoldRowWidth
;
140 int32_t unfoldStringWidth
;
148 /** @return UCASE_NONE, UCASE_LOWER, UCASE_UPPER, UCASE_TITLE */
149 U_CAPI
int32_t U_EXPORT2
150 ucase_getType(UChar32 c
);
152 /** @return like ucase_getType() but also sets UCASE_IGNORABLE if c is case-ignorable */
153 U_CAPI
int32_t U_EXPORT2
154 ucase_getTypeOrIgnorable(UChar32 c
);
156 U_CAPI UBool U_EXPORT2
157 ucase_isSoftDotted(UChar32 c
);
159 U_CAPI UBool U_EXPORT2
160 ucase_isCaseSensitive(UChar32 c
);
162 /* string case mapping functions */
167 * Iterator function for string case mappings, which need to look at the
168 * context (surrounding text) of a given character for conditional mappings.
170 * The iterator only needs to go backward or forward away from the
171 * character in question. It does not use any indexes on this interface.
172 * It does not support random access or an arbitrary change of
173 * iteration direction.
175 * The code point being case-mapped itself is never returned by
178 * @param context A pointer to the iterator's working data.
179 * @param dir If <0 then start iterating backward from the character;
180 * if >0 then start iterating forward from the character;
181 * if 0 then continue iterating in the current direction.
182 * @return Next code point, or <0 when the iteration is done.
184 typedef UChar32 U_CALLCONV
185 UCaseContextIterator(void *context
, int8_t dir
);
188 * Sample struct which may be used by some implementations of
189 * UCaseContextIterator.
191 struct UCaseContext
{
193 int32_t start
, index
, limit
;
194 int32_t cpStart
, cpLimit
;
198 typedef struct UCaseContext UCaseContext
;
202 #define UCASECONTEXT_INITIALIZER { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
206 * For string case mappings, a single character (a code point) is mapped
207 * either to itself (in which case in-place mapping functions do nothing),
208 * or to another single code point, or to a string.
209 * Aside from the string contents, these are indicated with a single int32_t
212 * Mapping to self: Negative values (~self instead of -self to support U+0000)
214 * Mapping to another code point: Positive values >UCASE_MAX_STRING_LENGTH
216 * Mapping to a string: The string length (0..UCASE_MAX_STRING_LENGTH) is
217 * returned. Note that the string result may indeed have zero length.
219 UCASE_MAX_STRING_LENGTH
=0x1f
223 * Get the full lowercase mapping for c.
225 * @param csp Case mapping properties.
226 * @param c Character to be mapped.
227 * @param iter Character iterator, used for context-sensitive mappings.
228 * See UCaseContextIterator for details.
229 * If iter==NULL then a context-independent result is returned.
230 * @param context Pointer to be passed into iter.
231 * @param pString If the mapping result is a string, then the pointer is
232 * written to *pString.
233 * @param caseLocale Case locale value from ucase_getCaseLocale().
234 * @return Output code point or string length, see UCASE_MAX_STRING_LENGTH.
236 * @see UCaseContextIterator
237 * @see UCASE_MAX_STRING_LENGTH
240 U_CAPI
int32_t U_EXPORT2
241 ucase_toFullLower(UChar32 c
,
242 UCaseContextIterator
*iter
, void *context
,
243 const UChar
**pString
,
246 U_CAPI
int32_t U_EXPORT2
247 ucase_toFullUpper(UChar32 c
,
248 UCaseContextIterator
*iter
, void *context
,
249 const UChar
**pString
,
252 U_CAPI
int32_t U_EXPORT2
253 ucase_toFullTitle(UChar32 c
,
254 UCaseContextIterator
*iter
, void *context
,
255 const UChar
**pString
,
258 U_CAPI
int32_t U_EXPORT2
259 ucase_toFullFolding(UChar32 c
,
260 const UChar
**pString
,
263 U_CFUNC
int32_t U_EXPORT2
264 ucase_hasBinaryProperty(UChar32 c
, UProperty which
);
272 typedef int32_t U_CALLCONV
273 UCaseMapFull(UChar32 c
,
274 UCaseContextIterator
*iter
, void *context
,
275 const UChar
**pString
,
280 /* file definitions --------------------------------------------------------- */
282 #define UCASE_DATA_NAME "ucase"
283 #define UCASE_DATA_TYPE "icu"
286 #define UCASE_FMT_0 0x63
287 #define UCASE_FMT_1 0x41
288 #define UCASE_FMT_2 0x53
289 #define UCASE_FMT_3 0x45
291 /* indexes into indexes[] */
297 UCASE_IX_UNFOLD_LENGTH
,
299 UCASE_IX_MAX_FULL_LENGTH
=15,
303 /* definitions for 16-bit case properties word ------------------------------ */
305 /* 2-bit constants for types of cased characters */
306 #define UCASE_TYPE_MASK 3
314 #define UCASE_GET_TYPE(props) ((props)&UCASE_TYPE_MASK)
315 #define UCASE_GET_TYPE_AND_IGNORABLE(props) ((props)&7)
317 #define UCASE_IGNORABLE 4
318 #define UCASE_SENSITIVE 8
319 #define UCASE_EXCEPTION 0x10
321 #define UCASE_DOT_MASK 0x60
323 UCASE_NO_DOT
=0, /* normal characters with cc=0 */
324 UCASE_SOFT_DOTTED
=0x20, /* soft-dotted characters with cc=0 */
325 UCASE_ABOVE
=0x40, /* "above" accents with cc=230 */
326 UCASE_OTHER_ACCENT
=0x60 /* other accent character (0<cc!=230) */
329 /* no exception: bits 15..7 are a 9-bit signed case mapping delta */
330 #define UCASE_DELTA_SHIFT 7
331 #define UCASE_DELTA_MASK 0xff80
332 #define UCASE_MAX_DELTA 0xff
333 #define UCASE_MIN_DELTA (-UCASE_MAX_DELTA-1)
335 #if U_SIGNED_RIGHT_SHIFT_IS_ARITHMETIC
336 # define UCASE_GET_DELTA(props) ((int16_t)(props)>>UCASE_DELTA_SHIFT)
338 # define UCASE_GET_DELTA(props) (int16_t)(((props)&0x8000) ? (((props)>>UCASE_DELTA_SHIFT)|0xfe00) : ((uint16_t)(props)>>UCASE_DELTA_SHIFT))
341 /* exception: bits 15..5 are an unsigned 11-bit index into the exceptions array */
342 #define UCASE_EXC_SHIFT 5
343 #define UCASE_EXC_MASK 0xffe0
344 #define UCASE_MAX_EXCEPTIONS ((UCASE_EXC_MASK>>UCASE_EXC_SHIFT)+1)
346 /* definitions for 16-bit main exceptions word ------------------------------ */
348 /* first 8 bits indicate values in optional slots */
354 UCASE_EXC_4
, /* reserved */
355 UCASE_EXC_5
, /* reserved */
357 UCASE_EXC_FULL_MAPPINGS
,
358 UCASE_EXC_ALL_SLOTS
/* one past the last slot */
361 /* each slot is 2 uint16_t instead of 1 */
362 #define UCASE_EXC_DOUBLE_SLOTS 0x100
364 /* reserved: exception bits 11..9 */
366 /* UCASE_EXC_DOT_MASK=UCASE_DOT_MASK<<UCASE_EXC_DOT_SHIFT */
367 #define UCASE_EXC_DOT_SHIFT 7
369 /* normally stored in the main word, but pushed out for larger exception indexes */
370 #define UCASE_EXC_DOT_MASK 0x3000
373 UCASE_EXC_SOFT_DOTTED
=0x1000,
374 UCASE_EXC_ABOVE
=0x2000, /* "above" accents with cc=230 */
375 UCASE_EXC_OTHER_ACCENT
=0x3000 /* other character (0<cc!=230) */
378 /* complex/conditional mappings */
379 #define UCASE_EXC_CONDITIONAL_SPECIAL 0x4000
380 #define UCASE_EXC_CONDITIONAL_FOLD 0x8000
382 /* definitions for lengths word for full case mappings */
383 #define UCASE_FULL_LOWER 0xf
384 #define UCASE_FULL_FOLDING 0xf0
385 #define UCASE_FULL_UPPER 0xf00
386 #define UCASE_FULL_TITLE 0xf000
388 /* maximum lengths */
389 #define UCASE_FULL_MAPPINGS_MAX_LENGTH (4*0xf)
390 #define UCASE_CLOSURE_MAX_LENGTH 0xf
392 /* constants for reverse case folding ("unfold") data */
395 UCASE_UNFOLD_ROW_WIDTH
,
396 UCASE_UNFOLD_STRING_WIDTH