michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2004-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: ucase.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2004aug30 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Low-level Unicode character/string case mapping code. michael@0: * Much code moved here (and modified) from uchar.c. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/uset.h" michael@0: #include "unicode/udata.h" /* UDataInfo */ michael@0: #include "unicode/utf16.h" michael@0: #include "ucmndata.h" /* DataHeader */ michael@0: #include "udatamem.h" michael@0: #include "umutex.h" michael@0: #include "uassert.h" michael@0: #include "cmemory.h" michael@0: #include "utrie2.h" michael@0: #include "ucase.h" michael@0: #include "ucln_cmn.h" michael@0: michael@0: struct UCaseProps { michael@0: UDataMemory *mem; michael@0: const int32_t *indexes; michael@0: const uint16_t *exceptions; michael@0: const uint16_t *unfold; michael@0: michael@0: UTrie2 trie; michael@0: uint8_t formatVersion[4]; michael@0: }; michael@0: michael@0: /* ucase_props_data.h is machine-generated by gencase --csource */ michael@0: #define INCLUDED_FROM_UCASE_CPP michael@0: #include "ucase_props_data.h" michael@0: michael@0: /* UCaseProps singleton ----------------------------------------------------- */ michael@0: michael@0: U_CAPI const UCaseProps * U_EXPORT2 michael@0: ucase_getSingleton() { michael@0: return &ucase_props_singleton; michael@0: } michael@0: michael@0: /* set of property starts for UnicodeSet ------------------------------------ */ michael@0: michael@0: static UBool U_CALLCONV michael@0: _enumPropertyStartsRange(const void *context, UChar32 start, UChar32 /*end*/, uint32_t /*value*/) { michael@0: /* add the start code point to the USet */ michael@0: const USetAdder *sa=(const USetAdder *)context; michael@0: sa->add(sa->set, start); michael@0: return TRUE; michael@0: } michael@0: michael@0: U_CFUNC void U_EXPORT2 michael@0: ucase_addPropertyStarts(const UCaseProps *csp, const USetAdder *sa, UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return; michael@0: } michael@0: michael@0: /* add the start code point of each same-value range of the trie */ michael@0: utrie2_enum(&csp->trie, NULL, _enumPropertyStartsRange, sa); michael@0: michael@0: /* add code points with hardcoded properties, plus the ones following them */ michael@0: michael@0: /* (none right now, see comment below) */ michael@0: michael@0: /* michael@0: * Omit code points with hardcoded specialcasing properties michael@0: * because we do not build property UnicodeSets for them right now. michael@0: */ michael@0: } michael@0: michael@0: /* data access primitives --------------------------------------------------- */ michael@0: michael@0: #define GET_EXCEPTIONS(csp, props) ((csp)->exceptions+((props)>>UCASE_EXC_SHIFT)) michael@0: michael@0: #define PROPS_HAS_EXCEPTION(props) ((props)&UCASE_EXCEPTION) michael@0: michael@0: /* number of bits in an 8-bit integer value */ michael@0: static const uint8_t flagsOffset[256]={ michael@0: 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 michael@0: }; michael@0: michael@0: #define HAS_SLOT(flags, idx) ((flags)&(1<<(idx))) michael@0: #define SLOT_OFFSET(flags, idx) flagsOffset[(flags)&((1<<(idx))-1)] michael@0: michael@0: /* michael@0: * Get the value of an optional-value slot where HAS_SLOT(excWord, idx). michael@0: * michael@0: * @param excWord (in) initial exceptions word michael@0: * @param idx (in) desired slot index michael@0: * @param pExc16 (in/out) const uint16_t * after excWord=*pExc16++; michael@0: * moved to the last uint16_t of the value, use +1 for beginning of next slot michael@0: * @param value (out) int32_t or uint32_t output if hasSlot, otherwise not modified michael@0: */ michael@0: #define GET_SLOT_VALUE(excWord, idx, pExc16, value) \ michael@0: if(((excWord)&UCASE_EXC_DOUBLE_SLOTS)==0) { \ michael@0: (pExc16)+=SLOT_OFFSET(excWord, idx); \ michael@0: (value)=*pExc16; \ michael@0: } else { \ michael@0: (pExc16)+=2*SLOT_OFFSET(excWord, idx); \ michael@0: (value)=*pExc16++; \ michael@0: (value)=((value)<<16)|*pExc16; \ michael@0: } michael@0: michael@0: /* simple case mappings ----------------------------------------------------- */ michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: ucase_tolower(const UCaseProps *csp, UChar32 c) { michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: if(UCASE_GET_TYPE(props)>=UCASE_UPPER) { michael@0: c+=UCASE_GET_DELTA(props); michael@0: } michael@0: } else { michael@0: const uint16_t *pe=GET_EXCEPTIONS(csp, props); michael@0: uint16_t excWord=*pe++; michael@0: if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) { michael@0: GET_SLOT_VALUE(excWord, UCASE_EXC_LOWER, pe, c); michael@0: } michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: ucase_toupper(const UCaseProps *csp, UChar32 c) { michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: if(UCASE_GET_TYPE(props)==UCASE_LOWER) { michael@0: c+=UCASE_GET_DELTA(props); michael@0: } michael@0: } else { michael@0: const uint16_t *pe=GET_EXCEPTIONS(csp, props); michael@0: uint16_t excWord=*pe++; michael@0: if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) { michael@0: GET_SLOT_VALUE(excWord, UCASE_EXC_UPPER, pe, c); michael@0: } michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: ucase_totitle(const UCaseProps *csp, UChar32 c) { michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: if(UCASE_GET_TYPE(props)==UCASE_LOWER) { michael@0: c+=UCASE_GET_DELTA(props); michael@0: } michael@0: } else { michael@0: const uint16_t *pe=GET_EXCEPTIONS(csp, props); michael@0: uint16_t excWord=*pe++; michael@0: int32_t idx; michael@0: if(HAS_SLOT(excWord, UCASE_EXC_TITLE)) { michael@0: idx=UCASE_EXC_TITLE; michael@0: } else if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) { michael@0: idx=UCASE_EXC_UPPER; michael@0: } else { michael@0: return c; michael@0: } michael@0: GET_SLOT_VALUE(excWord, idx, pe, c); michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: static const UChar iDot[2] = { 0x69, 0x307 }; michael@0: static const UChar jDot[2] = { 0x6a, 0x307 }; michael@0: static const UChar iOgonekDot[3] = { 0x12f, 0x307 }; michael@0: static const UChar iDotGrave[3] = { 0x69, 0x307, 0x300 }; michael@0: static const UChar iDotAcute[3] = { 0x69, 0x307, 0x301 }; michael@0: static const UChar iDotTilde[3] = { 0x69, 0x307, 0x303 }; michael@0: michael@0: michael@0: U_CFUNC void U_EXPORT2 michael@0: ucase_addCaseClosure(const UCaseProps *csp, UChar32 c, const USetAdder *sa) { michael@0: uint16_t props; michael@0: michael@0: /* michael@0: * Hardcode the case closure of i and its relatives and ignore the michael@0: * data file data for these characters. michael@0: * The Turkic dotless i and dotted I with their case mapping conditions michael@0: * and case folding option make the related characters behave specially. michael@0: * This code matches their closure behavior to their case folding behavior. michael@0: */ michael@0: michael@0: switch(c) { michael@0: case 0x49: michael@0: /* regular i and I are in one equivalence class */ michael@0: sa->add(sa->set, 0x69); michael@0: return; michael@0: case 0x69: michael@0: sa->add(sa->set, 0x49); michael@0: return; michael@0: case 0x130: michael@0: /* dotted I is in a class with <0069 0307> (for canonical equivalence with <0049 0307>) */ michael@0: sa->addString(sa->set, iDot, 2); michael@0: return; michael@0: case 0x131: michael@0: /* dotless i is in a class by itself */ michael@0: return; michael@0: default: michael@0: /* otherwise use the data file data */ michael@0: break; michael@0: } michael@0: michael@0: props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: if(UCASE_GET_TYPE(props)!=UCASE_NONE) { michael@0: /* add the one simple case mapping, no matter what type it is */ michael@0: int32_t delta=UCASE_GET_DELTA(props); michael@0: if(delta!=0) { michael@0: sa->add(sa->set, c+delta); michael@0: } michael@0: } michael@0: } else { michael@0: /* michael@0: * c has exceptions, so there may be multiple simple and/or michael@0: * full case mappings. Add them all. michael@0: */ michael@0: const uint16_t *pe0, *pe=GET_EXCEPTIONS(csp, props); michael@0: const UChar *closure; michael@0: uint16_t excWord=*pe++; michael@0: int32_t idx, closureLength, fullLength, length; michael@0: michael@0: pe0=pe; michael@0: michael@0: /* add all simple case mappings */ michael@0: for(idx=UCASE_EXC_LOWER; idx<=UCASE_EXC_TITLE; ++idx) { michael@0: if(HAS_SLOT(excWord, idx)) { michael@0: pe=pe0; michael@0: GET_SLOT_VALUE(excWord, idx, pe, c); michael@0: sa->add(sa->set, c); michael@0: } michael@0: } michael@0: michael@0: /* get the closure string pointer & length */ michael@0: if(HAS_SLOT(excWord, UCASE_EXC_CLOSURE)) { michael@0: pe=pe0; michael@0: GET_SLOT_VALUE(excWord, UCASE_EXC_CLOSURE, pe, closureLength); michael@0: closureLength&=UCASE_CLOSURE_MAX_LENGTH; /* higher bits are reserved */ michael@0: closure=(const UChar *)pe+1; /* behind this slot, unless there are full case mappings */ michael@0: } else { michael@0: closureLength=0; michael@0: closure=NULL; michael@0: } michael@0: michael@0: /* add the full case folding */ michael@0: if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) { michael@0: pe=pe0; michael@0: GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, fullLength); michael@0: michael@0: /* start of full case mapping strings */ michael@0: ++pe; michael@0: michael@0: fullLength&=0xffff; /* bits 16 and higher are reserved */ michael@0: michael@0: /* skip the lowercase result string */ michael@0: pe+=fullLength&UCASE_FULL_LOWER; michael@0: fullLength>>=4; michael@0: michael@0: /* add the full case folding string */ michael@0: length=fullLength&0xf; michael@0: if(length!=0) { michael@0: sa->addString(sa->set, (const UChar *)pe, length); michael@0: pe+=length; michael@0: } michael@0: michael@0: /* skip the uppercase and titlecase strings */ michael@0: fullLength>>=4; michael@0: pe+=fullLength&0xf; michael@0: fullLength>>=4; michael@0: pe+=fullLength; michael@0: michael@0: closure=(const UChar *)pe; /* behind full case mappings */ michael@0: } michael@0: michael@0: /* add each code point in the closure string */ michael@0: for(idx=0; idxadd(sa->set, c); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * compare s, which has a length, with t, which has a maximum length or is NUL-terminated michael@0: * must be length>0 and max>0 and length<=max michael@0: */ michael@0: static inline int32_t michael@0: strcmpMax(const UChar *s, int32_t length, const UChar *t, int32_t max) { michael@0: int32_t c1, c2; michael@0: michael@0: max-=length; /* we require length<=max, so no need to decrement max in the loop */ michael@0: do { michael@0: c1=*s++; michael@0: c2=*t++; michael@0: if(c2==0) { michael@0: return 1; /* reached the end of t but not of s */ michael@0: } michael@0: c1-=c2; michael@0: if(c1!=0) { michael@0: return c1; /* return difference result */ michael@0: } michael@0: } while(--length>0); michael@0: /* ends with length==0 */ michael@0: michael@0: if(max==0 || *t==0) { michael@0: return 0; /* equal to length of both strings */ michael@0: } else { michael@0: return -max; /* return lengh difference */ michael@0: } michael@0: } michael@0: michael@0: U_CFUNC UBool U_EXPORT2 michael@0: ucase_addStringCaseClosure(const UCaseProps *csp, const UChar *s, int32_t length, const USetAdder *sa) { michael@0: int32_t i, start, limit, result, unfoldRows, unfoldRowWidth, unfoldStringWidth; michael@0: michael@0: if(csp->unfold==NULL || s==NULL) { michael@0: return FALSE; /* no reverse case folding data, or no string */ michael@0: } michael@0: if(length<=1) { michael@0: /* the string is too short to find any match */ michael@0: /* michael@0: * more precise would be: michael@0: * if(!u_strHasMoreChar32Than(s, length, 1)) michael@0: * but this does not make much practical difference because michael@0: * a single supplementary code point would just not be found michael@0: */ michael@0: return FALSE; michael@0: } michael@0: michael@0: const uint16_t *unfold=csp->unfold; michael@0: unfoldRows=unfold[UCASE_UNFOLD_ROWS]; michael@0: unfoldRowWidth=unfold[UCASE_UNFOLD_ROW_WIDTH]; michael@0: unfoldStringWidth=unfold[UCASE_UNFOLD_STRING_WIDTH]; michael@0: unfold+=unfoldRowWidth; michael@0: michael@0: if(length>unfoldStringWidth) { michael@0: /* the string is too long to find any match */ michael@0: return FALSE; michael@0: } michael@0: michael@0: /* do a binary search for the string */ michael@0: start=0; michael@0: limit=unfoldRows; michael@0: while(start(unfold+(i*unfoldRowWidth)); michael@0: result=strcmpMax(s, length, p, unfoldStringWidth); michael@0: michael@0: if(result==0) { michael@0: /* found the string: add each code point, and its case closure */ michael@0: UChar32 c; michael@0: michael@0: for(i=unfoldStringWidth; iadd(sa->set, c); michael@0: ucase_addCaseClosure(csp, c, sa); michael@0: } michael@0: return TRUE; michael@0: } else if(result<0) { michael@0: limit=i; michael@0: } else /* result>0 */ { michael@0: start=i+1; michael@0: } michael@0: } michael@0: michael@0: return FALSE; /* string not found */ michael@0: } michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: FullCaseFoldingIterator::FullCaseFoldingIterator() michael@0: : unfold(reinterpret_cast(ucase_props_singleton.unfold)), michael@0: unfoldRows(unfold[UCASE_UNFOLD_ROWS]), michael@0: unfoldRowWidth(unfold[UCASE_UNFOLD_ROW_WIDTH]), michael@0: unfoldStringWidth(unfold[UCASE_UNFOLD_STRING_WIDTH]), michael@0: currentRow(0), michael@0: rowCpIndex(unfoldStringWidth) { michael@0: unfold+=unfoldRowWidth; michael@0: } michael@0: michael@0: UChar32 michael@0: FullCaseFoldingIterator::next(UnicodeString &full) { michael@0: // Advance past the last-delivered code point. michael@0: const UChar *p=unfold+(currentRow*unfoldRowWidth); michael@0: if(rowCpIndex>=unfoldRowWidth || p[rowCpIndex]==0) { michael@0: ++currentRow; michael@0: p+=unfoldRowWidth; michael@0: rowCpIndex=unfoldStringWidth; michael@0: } michael@0: if(currentRow>=unfoldRows) { return U_SENTINEL; } michael@0: // Set "full" to the NUL-terminated string in the first unfold column. michael@0: int32_t length=unfoldStringWidth; michael@0: while(length>0 && p[length-1]==0) { --length; } michael@0: full.setTo(FALSE, p, length); michael@0: // Return the code point. michael@0: UChar32 c; michael@0: U16_NEXT_UNSAFE(p, rowCpIndex, c); michael@0: return c; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: /** @return UCASE_NONE, UCASE_LOWER, UCASE_UPPER, UCASE_TITLE */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucase_getType(const UCaseProps *csp, UChar32 c) { michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: return UCASE_GET_TYPE(props); michael@0: } michael@0: michael@0: /** @return same as ucase_getType() and set bit 2 if c is case-ignorable */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucase_getTypeOrIgnorable(const UCaseProps *csp, UChar32 c) { michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: return UCASE_GET_TYPE_AND_IGNORABLE(props); michael@0: } michael@0: michael@0: /** @return UCASE_NO_DOT, UCASE_SOFT_DOTTED, UCASE_ABOVE, UCASE_OTHER_ACCENT */ michael@0: static inline int32_t michael@0: getDotType(const UCaseProps *csp, UChar32 c) { michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: return props&UCASE_DOT_MASK; michael@0: } else { michael@0: const uint16_t *pe=GET_EXCEPTIONS(csp, props); michael@0: return (*pe>>UCASE_EXC_DOT_SHIFT)&UCASE_DOT_MASK; michael@0: } michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucase_isSoftDotted(const UCaseProps *csp, UChar32 c) { michael@0: return (UBool)(getDotType(csp, c)==UCASE_SOFT_DOTTED); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucase_isCaseSensitive(const UCaseProps *csp, UChar32 c) { michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: return (UBool)((props&UCASE_SENSITIVE)!=0); michael@0: } michael@0: michael@0: /* string casing ------------------------------------------------------------ */ michael@0: michael@0: /* michael@0: * These internal functions form the core of string case mappings. michael@0: * They map single code points to result code points or strings and take michael@0: * all necessary conditions (context, locale ID, options) into account. michael@0: * michael@0: * They do not iterate over the source or write to the destination michael@0: * so that the same functions are useful for non-standard string storage, michael@0: * such as in a Replaceable (for Transliterator) or UTF-8/32 strings etc. michael@0: * For the same reason, the "surrounding text" context is passed in as a michael@0: * UCaseContextIterator which does not make any assumptions about michael@0: * the underlying storage. michael@0: * michael@0: * This section contains helper functions that check for conditions michael@0: * in the input text surrounding the current code point michael@0: * according to SpecialCasing.txt. michael@0: * michael@0: * Each helper function gets the index michael@0: * - after the current code point if it looks at following text michael@0: * - before the current code point if it looks at preceding text michael@0: * michael@0: * Unicode 3.2 UAX 21 "Case Mappings" defines the conditions as follows: michael@0: * michael@0: * Final_Sigma michael@0: * C is preceded by a sequence consisting of michael@0: * a cased letter and a case-ignorable sequence, michael@0: * and C is not followed by a sequence consisting of michael@0: * an ignorable sequence and then a cased letter. michael@0: * michael@0: * More_Above michael@0: * C is followed by one or more characters of combining class 230 (ABOVE) michael@0: * in the combining character sequence. michael@0: * michael@0: * After_Soft_Dotted michael@0: * The last preceding character with combining class of zero before C michael@0: * was Soft_Dotted, michael@0: * and there is no intervening combining character class 230 (ABOVE). michael@0: * michael@0: * Before_Dot michael@0: * C is followed by combining dot above (U+0307). michael@0: * Any sequence of characters with a combining class that is neither 0 nor 230 michael@0: * may intervene between the current character and the combining dot above. michael@0: * michael@0: * The erratum from 2002-10-31 adds the condition michael@0: * michael@0: * After_I michael@0: * The last preceding base character was an uppercase I, and there is no michael@0: * intervening combining character class 230 (ABOVE). michael@0: * michael@0: * (See Jitterbug 2344 and the comments on After_I below.) michael@0: * michael@0: * Helper definitions in Unicode 3.2 UAX 21: michael@0: * michael@0: * D1. A character C is defined to be cased michael@0: * if it meets any of the following criteria: michael@0: * michael@0: * - The general category of C is Titlecase Letter (Lt) michael@0: * - In [CoreProps], C has one of the properties Uppercase, or Lowercase michael@0: * - Given D = NFD(C), then it is not the case that: michael@0: * D = UCD_lower(D) = UCD_upper(D) = UCD_title(D) michael@0: * (This third criterium does not add any characters to the list michael@0: * for Unicode 3.2. Ignored.) michael@0: * michael@0: * D2. A character C is defined to be case-ignorable michael@0: * if it meets either of the following criteria: michael@0: * michael@0: * - The general category of C is michael@0: * Nonspacing Mark (Mn), or Enclosing Mark (Me), or Format Control (Cf), or michael@0: * Letter Modifier (Lm), or Symbol Modifier (Sk) michael@0: * - C is one of the following characters michael@0: * U+0027 APOSTROPHE michael@0: * U+00AD SOFT HYPHEN (SHY) michael@0: * U+2019 RIGHT SINGLE QUOTATION MARK michael@0: * (the preferred character for apostrophe) michael@0: * michael@0: * D3. A case-ignorable sequence is a sequence of michael@0: * zero or more case-ignorable characters. michael@0: */ michael@0: michael@0: #define is_a(c) ((c)=='a' || (c)=='A') michael@0: #define is_d(c) ((c)=='d' || (c)=='D') michael@0: #define is_e(c) ((c)=='e' || (c)=='E') michael@0: #define is_i(c) ((c)=='i' || (c)=='I') michael@0: #define is_l(c) ((c)=='l' || (c)=='L') michael@0: #define is_n(c) ((c)=='n' || (c)=='N') michael@0: #define is_r(c) ((c)=='r' || (c)=='R') michael@0: #define is_t(c) ((c)=='t' || (c)=='T') michael@0: #define is_u(c) ((c)=='u' || (c)=='U') michael@0: #define is_z(c) ((c)=='z' || (c)=='Z') michael@0: michael@0: /* separator? */ michael@0: #define is_sep(c) ((c)=='_' || (c)=='-' || (c)==0) michael@0: michael@0: /** michael@0: * Requires non-NULL locale ID but otherwise does the equivalent of michael@0: * checking for language codes as if uloc_getLanguage() were called: michael@0: * Accepts both 2- and 3-letter codes and accepts case variants. michael@0: */ michael@0: U_CFUNC int32_t michael@0: ucase_getCaseLocale(const char *locale, int32_t *locCache) { michael@0: int32_t result; michael@0: char c; michael@0: michael@0: if(locCache!=NULL && (result=*locCache)!=UCASE_LOC_UNKNOWN) { michael@0: return result; michael@0: } michael@0: michael@0: result=UCASE_LOC_ROOT; michael@0: michael@0: /* michael@0: * This function used to use uloc_getLanguage(), but the current code michael@0: * removes the dependency of this low-level code on uloc implementation code michael@0: * and is faster because not the whole locale ID has to be michael@0: * examined and copied/transformed. michael@0: * michael@0: * Because this code does not want to depend on uloc, the caller must michael@0: * pass in a non-NULL locale, i.e., may need to call uloc_getDefault(). michael@0: */ michael@0: c=*locale++; michael@0: if(is_t(c)) { michael@0: /* tr or tur? */ michael@0: c=*locale++; michael@0: if(is_u(c)) { michael@0: c=*locale++; michael@0: } michael@0: if(is_r(c)) { michael@0: c=*locale; michael@0: if(is_sep(c)) { michael@0: result=UCASE_LOC_TURKISH; michael@0: } michael@0: } michael@0: } else if(is_a(c)) { michael@0: /* az or aze? */ michael@0: c=*locale++; michael@0: if(is_z(c)) { michael@0: c=*locale++; michael@0: if(is_e(c)) { michael@0: c=*locale; michael@0: } michael@0: if(is_sep(c)) { michael@0: result=UCASE_LOC_TURKISH; michael@0: } michael@0: } michael@0: } else if(is_l(c)) { michael@0: /* lt or lit? */ michael@0: c=*locale++; michael@0: if(is_i(c)) { michael@0: c=*locale++; michael@0: } michael@0: if(is_t(c)) { michael@0: c=*locale; michael@0: if(is_sep(c)) { michael@0: result=UCASE_LOC_LITHUANIAN; michael@0: } michael@0: } michael@0: } else if(is_n(c)) { michael@0: /* nl or nld? */ michael@0: c=*locale++; michael@0: if(is_l(c)) { michael@0: c=*locale++; michael@0: if(is_d(c)) { michael@0: c=*locale; michael@0: } michael@0: if(is_sep(c)) { michael@0: result=UCASE_LOC_DUTCH; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(locCache!=NULL) { michael@0: *locCache=result; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /* michael@0: * Is followed by michael@0: * {case-ignorable}* cased michael@0: * ? michael@0: * (dir determines looking forward/backward) michael@0: * If a character is case-ignorable, it is skipped regardless of whether michael@0: * it is also cased or not. michael@0: */ michael@0: static UBool michael@0: isFollowedByCasedLetter(const UCaseProps *csp, UCaseContextIterator *iter, void *context, int8_t dir) { michael@0: UChar32 c; michael@0: michael@0: if(iter==NULL) { michael@0: return FALSE; michael@0: } michael@0: michael@0: for(/* dir!=0 sets direction */; (c=iter(context, dir))>=0; dir=0) { michael@0: int32_t type=ucase_getTypeOrIgnorable(csp, c); michael@0: if(type&4) { michael@0: /* case-ignorable, continue with the loop */ michael@0: } else if(type!=UCASE_NONE) { michael@0: return TRUE; /* followed by cased letter */ michael@0: } else { michael@0: return FALSE; /* uncased and not case-ignorable */ michael@0: } michael@0: } michael@0: michael@0: return FALSE; /* not followed by cased letter */ michael@0: } michael@0: michael@0: /* Is preceded by Soft_Dotted character with no intervening cc=230 ? */ michael@0: static UBool michael@0: isPrecededBySoftDotted(const UCaseProps *csp, UCaseContextIterator *iter, void *context) { michael@0: UChar32 c; michael@0: int32_t dotType; michael@0: int8_t dir; michael@0: michael@0: if(iter==NULL) { michael@0: return FALSE; michael@0: } michael@0: michael@0: for(dir=-1; (c=iter(context, dir))>=0; dir=0) { michael@0: dotType=getDotType(csp, c); michael@0: if(dotType==UCASE_SOFT_DOTTED) { michael@0: return TRUE; /* preceded by TYPE_i */ michael@0: } else if(dotType!=UCASE_OTHER_ACCENT) { michael@0: return FALSE; /* preceded by different base character (not TYPE_i), or intervening cc==230 */ michael@0: } michael@0: } michael@0: michael@0: return FALSE; /* not preceded by TYPE_i */ michael@0: } michael@0: michael@0: /* michael@0: * See Jitterbug 2344: michael@0: * The condition After_I for Turkic-lowercasing of U+0307 combining dot above michael@0: * is checked in ICU 2.0, 2.1, 2.6 but was not in 2.2 & 2.4 because michael@0: * we made those releases compatible with Unicode 3.2 which had not fixed michael@0: * a related bug in SpecialCasing.txt. michael@0: * michael@0: * From the Jitterbug 2344 text: michael@0: * ... this bug is listed as a Unicode erratum michael@0: * from 2002-10-31 at http://www.unicode.org/uni2errata/UnicodeErrata.html michael@0: * michael@0: * There are two errors in SpecialCasing.txt. michael@0: * 1. Missing semicolons on two lines. ... [irrelevant for ICU] michael@0: * 2. An incorrect context definition. Correct as follows: michael@0: * < 0307; ; 0307; 0307; tr After_Soft_Dotted; # COMBINING DOT ABOVE michael@0: * < 0307; ; 0307; 0307; az After_Soft_Dotted; # COMBINING DOT ABOVE michael@0: * --- michael@0: * > 0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE michael@0: * > 0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE michael@0: * where the context After_I is defined as: michael@0: * The last preceding base character was an uppercase I, and there is no michael@0: * intervening combining character class 230 (ABOVE). michael@0: * michael@0: * michael@0: * Note that SpecialCasing.txt even in Unicode 3.2 described the condition as: michael@0: * michael@0: * # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i. michael@0: * # This matches the behavior of the canonically equivalent I-dot_above michael@0: * michael@0: * See also the description in this place in older versions of uchar.c (revision 1.100). michael@0: * michael@0: * Markus W. Scherer 2003-feb-15 michael@0: */ michael@0: michael@0: /* Is preceded by base character 'I' with no intervening cc=230 ? */ michael@0: static UBool michael@0: isPrecededBy_I(const UCaseProps *csp, UCaseContextIterator *iter, void *context) { michael@0: UChar32 c; michael@0: int32_t dotType; michael@0: int8_t dir; michael@0: michael@0: if(iter==NULL) { michael@0: return FALSE; michael@0: } michael@0: michael@0: for(dir=-1; (c=iter(context, dir))>=0; dir=0) { michael@0: if(c==0x49) { michael@0: return TRUE; /* preceded by I */ michael@0: } michael@0: dotType=getDotType(csp, c); michael@0: if(dotType!=UCASE_OTHER_ACCENT) { michael@0: return FALSE; /* preceded by different base character (not I), or intervening cc==230 */ michael@0: } michael@0: } michael@0: michael@0: return FALSE; /* not preceded by I */ michael@0: } michael@0: michael@0: /* Is followed by one or more cc==230 ? */ michael@0: static UBool michael@0: isFollowedByMoreAbove(const UCaseProps *csp, UCaseContextIterator *iter, void *context) { michael@0: UChar32 c; michael@0: int32_t dotType; michael@0: int8_t dir; michael@0: michael@0: if(iter==NULL) { michael@0: return FALSE; michael@0: } michael@0: michael@0: for(dir=1; (c=iter(context, dir))>=0; dir=0) { michael@0: dotType=getDotType(csp, c); michael@0: if(dotType==UCASE_ABOVE) { michael@0: return TRUE; /* at least one cc==230 following */ michael@0: } else if(dotType!=UCASE_OTHER_ACCENT) { michael@0: return FALSE; /* next base character, no more cc==230 following */ michael@0: } michael@0: } michael@0: michael@0: return FALSE; /* no more cc==230 following */ michael@0: } michael@0: michael@0: /* Is followed by a dot above (without cc==230 in between) ? */ michael@0: static UBool michael@0: isFollowedByDotAbove(const UCaseProps *csp, UCaseContextIterator *iter, void *context) { michael@0: UChar32 c; michael@0: int32_t dotType; michael@0: int8_t dir; michael@0: michael@0: if(iter==NULL) { michael@0: return FALSE; michael@0: } michael@0: michael@0: for(dir=1; (c=iter(context, dir))>=0; dir=0) { michael@0: if(c==0x307) { michael@0: return TRUE; michael@0: } michael@0: dotType=getDotType(csp, c); michael@0: if(dotType!=UCASE_OTHER_ACCENT) { michael@0: return FALSE; /* next base character or cc==230 in between */ michael@0: } michael@0: } michael@0: michael@0: return FALSE; /* no dot above following */ michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucase_toFullLower(const UCaseProps *csp, UChar32 c, michael@0: UCaseContextIterator *iter, void *context, michael@0: const UChar **pString, michael@0: const char *locale, int32_t *locCache) michael@0: { michael@0: UChar32 result=c; michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: if(UCASE_GET_TYPE(props)>=UCASE_UPPER) { michael@0: result=c+UCASE_GET_DELTA(props); michael@0: } michael@0: } else { michael@0: const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2; michael@0: uint16_t excWord=*pe++; michael@0: int32_t full; michael@0: michael@0: pe2=pe; michael@0: michael@0: if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) { michael@0: /* use hardcoded conditions and mappings */ michael@0: int32_t loc=ucase_getCaseLocale(locale, locCache); michael@0: michael@0: /* michael@0: * Test for conditional mappings first michael@0: * (otherwise the unconditional default mappings are always taken), michael@0: * then test for characters that have unconditional mappings in SpecialCasing.txt, michael@0: * then get the UnicodeData.txt mappings. michael@0: */ michael@0: if( loc==UCASE_LOC_LITHUANIAN && michael@0: /* base characters, find accents above */ michael@0: (((c==0x49 || c==0x4a || c==0x12e) && michael@0: isFollowedByMoreAbove(csp, iter, context)) || michael@0: /* precomposed with accent above, no need to find one */ michael@0: (c==0xcc || c==0xcd || c==0x128)) michael@0: ) { michael@0: /* michael@0: # Lithuanian michael@0: michael@0: # Lithuanian retains the dot in a lowercase i when followed by accents. michael@0: michael@0: # Introduce an explicit dot above when lowercasing capital I's and J's michael@0: # whenever there are more accents above. michael@0: # (of the accents used in Lithuanian: grave, acute, tilde above, and ogonek) michael@0: michael@0: 0049; 0069 0307; 0049; 0049; lt More_Above; # LATIN CAPITAL LETTER I michael@0: 004A; 006A 0307; 004A; 004A; lt More_Above; # LATIN CAPITAL LETTER J michael@0: 012E; 012F 0307; 012E; 012E; lt More_Above; # LATIN CAPITAL LETTER I WITH OGONEK michael@0: 00CC; 0069 0307 0300; 00CC; 00CC; lt; # LATIN CAPITAL LETTER I WITH GRAVE michael@0: 00CD; 0069 0307 0301; 00CD; 00CD; lt; # LATIN CAPITAL LETTER I WITH ACUTE michael@0: 0128; 0069 0307 0303; 0128; 0128; lt; # LATIN CAPITAL LETTER I WITH TILDE michael@0: */ michael@0: switch(c) { michael@0: case 0x49: /* LATIN CAPITAL LETTER I */ michael@0: *pString=iDot; michael@0: return 2; michael@0: case 0x4a: /* LATIN CAPITAL LETTER J */ michael@0: *pString=jDot; michael@0: return 2; michael@0: case 0x12e: /* LATIN CAPITAL LETTER I WITH OGONEK */ michael@0: *pString=iOgonekDot; michael@0: return 2; michael@0: case 0xcc: /* LATIN CAPITAL LETTER I WITH GRAVE */ michael@0: *pString=iDotGrave; michael@0: return 3; michael@0: case 0xcd: /* LATIN CAPITAL LETTER I WITH ACUTE */ michael@0: *pString=iDotAcute; michael@0: return 3; michael@0: case 0x128: /* LATIN CAPITAL LETTER I WITH TILDE */ michael@0: *pString=iDotTilde; michael@0: return 3; michael@0: default: michael@0: return 0; /* will not occur */ michael@0: } michael@0: /* # Turkish and Azeri */ michael@0: } else if(loc==UCASE_LOC_TURKISH && c==0x130) { michael@0: /* michael@0: # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri michael@0: # The following rules handle those cases. michael@0: michael@0: 0130; 0069; 0130; 0130; tr # LATIN CAPITAL LETTER I WITH DOT ABOVE michael@0: 0130; 0069; 0130; 0130; az # LATIN CAPITAL LETTER I WITH DOT ABOVE michael@0: */ michael@0: return 0x69; michael@0: } else if(loc==UCASE_LOC_TURKISH && c==0x307 && isPrecededBy_I(csp, iter, context)) { michael@0: /* michael@0: # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i. michael@0: # This matches the behavior of the canonically equivalent I-dot_above michael@0: michael@0: 0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE michael@0: 0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE michael@0: */ michael@0: return 0; /* remove the dot (continue without output) */ michael@0: } else if(loc==UCASE_LOC_TURKISH && c==0x49 && !isFollowedByDotAbove(csp, iter, context)) { michael@0: /* michael@0: # When lowercasing, unless an I is before a dot_above, it turns into a dotless i. michael@0: michael@0: 0049; 0131; 0049; 0049; tr Not_Before_Dot; # LATIN CAPITAL LETTER I michael@0: 0049; 0131; 0049; 0049; az Not_Before_Dot; # LATIN CAPITAL LETTER I michael@0: */ michael@0: return 0x131; michael@0: } else if(c==0x130) { michael@0: /* michael@0: # Preserve canonical equivalence for I with dot. Turkic is handled below. michael@0: michael@0: 0130; 0069 0307; 0130; 0130; # LATIN CAPITAL LETTER I WITH DOT ABOVE michael@0: */ michael@0: *pString=iDot; michael@0: return 2; michael@0: } else if( c==0x3a3 && michael@0: !isFollowedByCasedLetter(csp, iter, context, 1) && michael@0: isFollowedByCasedLetter(csp, iter, context, -1) /* -1=preceded */ michael@0: ) { michael@0: /* greek capital sigma maps depending on surrounding cased letters (see SpecialCasing.txt) */ michael@0: /* michael@0: # Special case for final form of sigma michael@0: michael@0: 03A3; 03C2; 03A3; 03A3; Final_Sigma; # GREEK CAPITAL LETTER SIGMA michael@0: */ michael@0: return 0x3c2; /* greek small final sigma */ michael@0: } else { michael@0: /* no known conditional special case mapping, use a normal mapping */ michael@0: } michael@0: } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) { michael@0: GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full); michael@0: full&=UCASE_FULL_LOWER; michael@0: if(full!=0) { michael@0: /* set the output pointer to the lowercase mapping */ michael@0: *pString=reinterpret_cast(pe+1); michael@0: michael@0: /* return the string length */ michael@0: return full; michael@0: } michael@0: } michael@0: michael@0: if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) { michael@0: GET_SLOT_VALUE(excWord, UCASE_EXC_LOWER, pe2, result); michael@0: } michael@0: } michael@0: michael@0: return (result==c) ? ~result : result; michael@0: } michael@0: michael@0: /* internal */ michael@0: static int32_t michael@0: toUpperOrTitle(const UCaseProps *csp, UChar32 c, michael@0: UCaseContextIterator *iter, void *context, michael@0: const UChar **pString, michael@0: const char *locale, int32_t *locCache, michael@0: UBool upperNotTitle) { michael@0: UChar32 result=c; michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: if(UCASE_GET_TYPE(props)==UCASE_LOWER) { michael@0: result=c+UCASE_GET_DELTA(props); michael@0: } michael@0: } else { michael@0: const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2; michael@0: uint16_t excWord=*pe++; michael@0: int32_t full, idx; michael@0: michael@0: pe2=pe; michael@0: michael@0: if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) { michael@0: /* use hardcoded conditions and mappings */ michael@0: int32_t loc=ucase_getCaseLocale(locale, locCache); michael@0: michael@0: if(loc==UCASE_LOC_TURKISH && c==0x69) { michael@0: /* michael@0: # Turkish and Azeri michael@0: michael@0: # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri michael@0: # The following rules handle those cases. michael@0: michael@0: # When uppercasing, i turns into a dotted capital I michael@0: michael@0: 0069; 0069; 0130; 0130; tr; # LATIN SMALL LETTER I michael@0: 0069; 0069; 0130; 0130; az; # LATIN SMALL LETTER I michael@0: */ michael@0: return 0x130; michael@0: } else if(loc==UCASE_LOC_LITHUANIAN && c==0x307 && isPrecededBySoftDotted(csp, iter, context)) { michael@0: /* michael@0: # Lithuanian michael@0: michael@0: # Lithuanian retains the dot in a lowercase i when followed by accents. michael@0: michael@0: # Remove DOT ABOVE after "i" with upper or titlecase michael@0: michael@0: 0307; 0307; ; ; lt After_Soft_Dotted; # COMBINING DOT ABOVE michael@0: */ michael@0: return 0; /* remove the dot (continue without output) */ michael@0: } else { michael@0: /* no known conditional special case mapping, use a normal mapping */ michael@0: } michael@0: } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) { michael@0: GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full); michael@0: michael@0: /* start of full case mapping strings */ michael@0: ++pe; michael@0: michael@0: /* skip the lowercase and case-folding result strings */ michael@0: pe+=full&UCASE_FULL_LOWER; michael@0: full>>=4; michael@0: pe+=full&0xf; michael@0: full>>=4; michael@0: michael@0: if(upperNotTitle) { michael@0: full&=0xf; michael@0: } else { michael@0: /* skip the uppercase result string */ michael@0: pe+=full&0xf; michael@0: full=(full>>4)&0xf; michael@0: } michael@0: michael@0: if(full!=0) { michael@0: /* set the output pointer to the result string */ michael@0: *pString=reinterpret_cast(pe); michael@0: michael@0: /* return the string length */ michael@0: return full; michael@0: } michael@0: } michael@0: michael@0: if(!upperNotTitle && HAS_SLOT(excWord, UCASE_EXC_TITLE)) { michael@0: idx=UCASE_EXC_TITLE; michael@0: } else if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) { michael@0: /* here, titlecase is same as uppercase */ michael@0: idx=UCASE_EXC_UPPER; michael@0: } else { michael@0: return ~c; michael@0: } michael@0: GET_SLOT_VALUE(excWord, idx, pe2, result); michael@0: } michael@0: michael@0: return (result==c) ? ~result : result; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucase_toFullUpper(const UCaseProps *csp, UChar32 c, michael@0: UCaseContextIterator *iter, void *context, michael@0: const UChar **pString, michael@0: const char *locale, int32_t *locCache) { michael@0: return toUpperOrTitle(csp, c, iter, context, pString, locale, locCache, TRUE); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucase_toFullTitle(const UCaseProps *csp, UChar32 c, michael@0: UCaseContextIterator *iter, void *context, michael@0: const UChar **pString, michael@0: const char *locale, int32_t *locCache) { michael@0: return toUpperOrTitle(csp, c, iter, context, pString, locale, locCache, FALSE); michael@0: } michael@0: michael@0: /* case folding ------------------------------------------------------------- */ michael@0: michael@0: /* michael@0: * Case folding is similar to lowercasing. michael@0: * The result may be a simple mapping, i.e., a single code point, or michael@0: * a full mapping, i.e., a string. michael@0: * If the case folding for a code point is the same as its simple (1:1) lowercase mapping, michael@0: * then only the lowercase mapping is stored. michael@0: * michael@0: * Some special cases are hardcoded because their conditions cannot be michael@0: * parsed and processed from CaseFolding.txt. michael@0: * michael@0: * Unicode 3.2 CaseFolding.txt specifies for its status field: michael@0: michael@0: # C: common case folding, common mappings shared by both simple and full mappings. michael@0: # F: full case folding, mappings that cause strings to grow in length. Multiple characters are separated by spaces. michael@0: # S: simple case folding, mappings to single characters where different from F. michael@0: # T: special case for uppercase I and dotted uppercase I michael@0: # - For non-Turkic languages, this mapping is normally not used. michael@0: # - For Turkic languages (tr, az), this mapping can be used instead of the normal mapping for these characters. michael@0: # michael@0: # Usage: michael@0: # A. To do a simple case folding, use the mappings with status C + S. michael@0: # B. To do a full case folding, use the mappings with status C + F. michael@0: # michael@0: # The mappings with status T can be used or omitted depending on the desired case-folding michael@0: # behavior. (The default option is to exclude them.) michael@0: michael@0: * Unicode 3.2 has 'T' mappings as follows: michael@0: michael@0: 0049; T; 0131; # LATIN CAPITAL LETTER I michael@0: 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE michael@0: michael@0: * while the default mappings for these code points are: michael@0: michael@0: 0049; C; 0069; # LATIN CAPITAL LETTER I michael@0: 0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE michael@0: michael@0: * U+0130 has no simple case folding (simple-case-folds to itself). michael@0: */ michael@0: michael@0: /* return the simple case folding mapping for c */ michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: ucase_fold(const UCaseProps *csp, UChar32 c, uint32_t options) { michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: if(UCASE_GET_TYPE(props)>=UCASE_UPPER) { michael@0: c+=UCASE_GET_DELTA(props); michael@0: } michael@0: } else { michael@0: const uint16_t *pe=GET_EXCEPTIONS(csp, props); michael@0: uint16_t excWord=*pe++; michael@0: int32_t idx; michael@0: if(excWord&UCASE_EXC_CONDITIONAL_FOLD) { michael@0: /* special case folding mappings, hardcoded */ michael@0: if((options&_FOLD_CASE_OPTIONS_MASK)==U_FOLD_CASE_DEFAULT) { michael@0: /* default mappings */ michael@0: if(c==0x49) { michael@0: /* 0049; C; 0069; # LATIN CAPITAL LETTER I */ michael@0: return 0x69; michael@0: } else if(c==0x130) { michael@0: /* no simple case folding for U+0130 */ michael@0: return c; michael@0: } michael@0: } else { michael@0: /* Turkic mappings */ michael@0: if(c==0x49) { michael@0: /* 0049; T; 0131; # LATIN CAPITAL LETTER I */ michael@0: return 0x131; michael@0: } else if(c==0x130) { michael@0: /* 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE */ michael@0: return 0x69; michael@0: } michael@0: } michael@0: } michael@0: if(HAS_SLOT(excWord, UCASE_EXC_FOLD)) { michael@0: idx=UCASE_EXC_FOLD; michael@0: } else if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) { michael@0: idx=UCASE_EXC_LOWER; michael@0: } else { michael@0: return c; michael@0: } michael@0: GET_SLOT_VALUE(excWord, idx, pe, c); michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: /* michael@0: * Issue for canonical caseless match (UAX #21): michael@0: * Turkic casefolding (using "T" mappings in CaseFolding.txt) does not preserve michael@0: * canonical equivalence, unlike default-option casefolding. michael@0: * For example, I-grave and I + grave fold to strings that are not canonically michael@0: * equivalent. michael@0: * For more details, see the comment in unorm_compare() in unorm.cpp michael@0: * and the intermediate prototype changes for Jitterbug 2021. michael@0: * (For example, revision 1.104 of uchar.c and 1.4 of CaseFolding.txt.) michael@0: * michael@0: * This did not get fixed because it appears that it is not possible to fix michael@0: * it for uppercase and lowercase characters (I-grave vs. i-grave) michael@0: * together in a way that they still fold to common result strings. michael@0: */ michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucase_toFullFolding(const UCaseProps *csp, UChar32 c, michael@0: const UChar **pString, michael@0: uint32_t options) michael@0: { michael@0: UChar32 result=c; michael@0: uint16_t props=UTRIE2_GET16(&csp->trie, c); michael@0: if(!PROPS_HAS_EXCEPTION(props)) { michael@0: if(UCASE_GET_TYPE(props)>=UCASE_UPPER) { michael@0: result=c+UCASE_GET_DELTA(props); michael@0: } michael@0: } else { michael@0: const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2; michael@0: uint16_t excWord=*pe++; michael@0: int32_t full, idx; michael@0: michael@0: pe2=pe; michael@0: michael@0: if(excWord&UCASE_EXC_CONDITIONAL_FOLD) { michael@0: /* use hardcoded conditions and mappings */ michael@0: if((options&_FOLD_CASE_OPTIONS_MASK)==U_FOLD_CASE_DEFAULT) { michael@0: /* default mappings */ michael@0: if(c==0x49) { michael@0: /* 0049; C; 0069; # LATIN CAPITAL LETTER I */ michael@0: return 0x69; michael@0: } else if(c==0x130) { michael@0: /* 0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE */ michael@0: *pString=iDot; michael@0: return 2; michael@0: } michael@0: } else { michael@0: /* Turkic mappings */ michael@0: if(c==0x49) { michael@0: /* 0049; T; 0131; # LATIN CAPITAL LETTER I */ michael@0: return 0x131; michael@0: } else if(c==0x130) { michael@0: /* 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE */ michael@0: return 0x69; michael@0: } michael@0: } michael@0: } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) { michael@0: GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full); michael@0: michael@0: /* start of full case mapping strings */ michael@0: ++pe; michael@0: michael@0: /* skip the lowercase result string */ michael@0: pe+=full&UCASE_FULL_LOWER; michael@0: full=(full>>4)&0xf; michael@0: michael@0: if(full!=0) { michael@0: /* set the output pointer to the result string */ michael@0: *pString=reinterpret_cast(pe); michael@0: michael@0: /* return the string length */ michael@0: return full; michael@0: } michael@0: } michael@0: michael@0: if(HAS_SLOT(excWord, UCASE_EXC_FOLD)) { michael@0: idx=UCASE_EXC_FOLD; michael@0: } else if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) { michael@0: idx=UCASE_EXC_LOWER; michael@0: } else { michael@0: return ~c; michael@0: } michael@0: GET_SLOT_VALUE(excWord, idx, pe2, result); michael@0: } michael@0: michael@0: return (result==c) ? ~result : result; michael@0: } michael@0: michael@0: /* case mapping properties API ---------------------------------------------- */ michael@0: michael@0: #define GET_CASE_PROPS() &ucase_props_singleton michael@0: michael@0: /* public API (see uchar.h) */ michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: u_isULowercase(UChar32 c) { michael@0: return (UBool)(UCASE_LOWER==ucase_getType(GET_CASE_PROPS(), c)); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: u_isUUppercase(UChar32 c) { michael@0: return (UBool)(UCASE_UPPER==ucase_getType(GET_CASE_PROPS(), c)); michael@0: } michael@0: michael@0: /* Transforms the Unicode character to its lower case equivalent.*/ michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: u_tolower(UChar32 c) { michael@0: return ucase_tolower(GET_CASE_PROPS(), c); michael@0: } michael@0: michael@0: /* Transforms the Unicode character to its upper case equivalent.*/ michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: u_toupper(UChar32 c) { michael@0: return ucase_toupper(GET_CASE_PROPS(), c); michael@0: } michael@0: michael@0: /* Transforms the Unicode character to its title case equivalent.*/ michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: u_totitle(UChar32 c) { michael@0: return ucase_totitle(GET_CASE_PROPS(), c); michael@0: } michael@0: michael@0: /* return the simple case folding mapping for c */ michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: u_foldCase(UChar32 c, uint32_t options) { michael@0: return ucase_fold(GET_CASE_PROPS(), c, options); michael@0: } michael@0: michael@0: U_CFUNC int32_t U_EXPORT2 michael@0: ucase_hasBinaryProperty(UChar32 c, UProperty which) { michael@0: /* case mapping properties */ michael@0: const UChar *resultString; michael@0: int32_t locCache; michael@0: const UCaseProps *csp=GET_CASE_PROPS(); michael@0: if(csp==NULL) { michael@0: return FALSE; michael@0: } michael@0: switch(which) { michael@0: case UCHAR_LOWERCASE: michael@0: return (UBool)(UCASE_LOWER==ucase_getType(csp, c)); michael@0: case UCHAR_UPPERCASE: michael@0: return (UBool)(UCASE_UPPER==ucase_getType(csp, c)); michael@0: case UCHAR_SOFT_DOTTED: michael@0: return ucase_isSoftDotted(csp, c); michael@0: case UCHAR_CASE_SENSITIVE: michael@0: return ucase_isCaseSensitive(csp, c); michael@0: case UCHAR_CASED: michael@0: return (UBool)(UCASE_NONE!=ucase_getType(csp, c)); michael@0: case UCHAR_CASE_IGNORABLE: michael@0: return (UBool)(ucase_getTypeOrIgnorable(csp, c)>>2); michael@0: /* michael@0: * Note: The following Changes_When_Xyz are defined as testing whether michael@0: * the NFD form of the input changes when Xyz-case-mapped. michael@0: * However, this simpler implementation of these properties, michael@0: * ignoring NFD, passes the tests. michael@0: * The implementation needs to be changed if the tests start failing. michael@0: * When that happens, optimizations should be used to work with the michael@0: * per-single-code point ucase_toFullXyz() functions unless michael@0: * the NFD form has more than one code point, michael@0: * and the property starts set needs to be the union of the michael@0: * start sets for normalization and case mappings. michael@0: */ michael@0: case UCHAR_CHANGES_WHEN_LOWERCASED: michael@0: locCache=UCASE_LOC_ROOT; michael@0: return (UBool)(ucase_toFullLower(csp, c, NULL, NULL, &resultString, "", &locCache)>=0); michael@0: case UCHAR_CHANGES_WHEN_UPPERCASED: michael@0: locCache=UCASE_LOC_ROOT; michael@0: return (UBool)(ucase_toFullUpper(csp, c, NULL, NULL, &resultString, "", &locCache)>=0); michael@0: case UCHAR_CHANGES_WHEN_TITLECASED: michael@0: locCache=UCASE_LOC_ROOT; michael@0: return (UBool)(ucase_toFullTitle(csp, c, NULL, NULL, &resultString, "", &locCache)>=0); michael@0: /* case UCHAR_CHANGES_WHEN_CASEFOLDED: -- in uprops.c */ michael@0: case UCHAR_CHANGES_WHEN_CASEMAPPED: michael@0: locCache=UCASE_LOC_ROOT; michael@0: return (UBool)( michael@0: ucase_toFullLower(csp, c, NULL, NULL, &resultString, "", &locCache)>=0 || michael@0: ucase_toFullUpper(csp, c, NULL, NULL, &resultString, "", &locCache)>=0 || michael@0: ucase_toFullTitle(csp, c, NULL, NULL, &resultString, "", &locCache)>=0); michael@0: default: michael@0: return FALSE; michael@0: } michael@0: }