intl/icu/source/common/ucase.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ucase.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1321 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 2004-2012, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +*******************************************************************************
    1.11 +*   file name:  ucase.cpp
    1.12 +*   encoding:   US-ASCII
    1.13 +*   tab size:   8 (not used)
    1.14 +*   indentation:4
    1.15 +*
    1.16 +*   created on: 2004aug30
    1.17 +*   created by: Markus W. Scherer
    1.18 +*
    1.19 +*   Low-level Unicode character/string case mapping code.
    1.20 +*   Much code moved here (and modified) from uchar.c.
    1.21 +*/
    1.22 +
    1.23 +#include "unicode/utypes.h"
    1.24 +#include "unicode/unistr.h"
    1.25 +#include "unicode/uset.h"
    1.26 +#include "unicode/udata.h" /* UDataInfo */
    1.27 +#include "unicode/utf16.h"
    1.28 +#include "ucmndata.h" /* DataHeader */
    1.29 +#include "udatamem.h"
    1.30 +#include "umutex.h"
    1.31 +#include "uassert.h"
    1.32 +#include "cmemory.h"
    1.33 +#include "utrie2.h"
    1.34 +#include "ucase.h"
    1.35 +#include "ucln_cmn.h"
    1.36 +
    1.37 +struct UCaseProps {
    1.38 +    UDataMemory *mem;
    1.39 +    const int32_t *indexes;
    1.40 +    const uint16_t *exceptions;
    1.41 +    const uint16_t *unfold;
    1.42 +
    1.43 +    UTrie2 trie;
    1.44 +    uint8_t formatVersion[4];
    1.45 +};
    1.46 +
    1.47 +/* ucase_props_data.h is machine-generated by gencase --csource */
    1.48 +#define INCLUDED_FROM_UCASE_CPP
    1.49 +#include "ucase_props_data.h"
    1.50 +
    1.51 +/* UCaseProps singleton ----------------------------------------------------- */
    1.52 +
    1.53 +U_CAPI const UCaseProps * U_EXPORT2
    1.54 +ucase_getSingleton() {
    1.55 +    return &ucase_props_singleton;
    1.56 +}
    1.57 +
    1.58 +/* set of property starts for UnicodeSet ------------------------------------ */
    1.59 +
    1.60 +static UBool U_CALLCONV
    1.61 +_enumPropertyStartsRange(const void *context, UChar32 start, UChar32 /*end*/, uint32_t /*value*/) {
    1.62 +    /* add the start code point to the USet */
    1.63 +    const USetAdder *sa=(const USetAdder *)context;
    1.64 +    sa->add(sa->set, start);
    1.65 +    return TRUE;
    1.66 +}
    1.67 +
    1.68 +U_CFUNC void U_EXPORT2
    1.69 +ucase_addPropertyStarts(const UCaseProps *csp, const USetAdder *sa, UErrorCode *pErrorCode) {
    1.70 +    if(U_FAILURE(*pErrorCode)) {
    1.71 +        return;
    1.72 +    }
    1.73 +
    1.74 +    /* add the start code point of each same-value range of the trie */
    1.75 +    utrie2_enum(&csp->trie, NULL, _enumPropertyStartsRange, sa);
    1.76 +
    1.77 +    /* add code points with hardcoded properties, plus the ones following them */
    1.78 +
    1.79 +    /* (none right now, see comment below) */
    1.80 +
    1.81 +    /*
    1.82 +     * Omit code points with hardcoded specialcasing properties
    1.83 +     * because we do not build property UnicodeSets for them right now.
    1.84 +     */
    1.85 +}
    1.86 +
    1.87 +/* data access primitives --------------------------------------------------- */
    1.88 +
    1.89 +#define GET_EXCEPTIONS(csp, props) ((csp)->exceptions+((props)>>UCASE_EXC_SHIFT))
    1.90 +
    1.91 +#define PROPS_HAS_EXCEPTION(props) ((props)&UCASE_EXCEPTION)
    1.92 +
    1.93 +/* number of bits in an 8-bit integer value */
    1.94 +static const uint8_t flagsOffset[256]={
    1.95 +    0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
    1.96 +    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
    1.97 +    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
    1.98 +    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
    1.99 +    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
   1.100 +    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
   1.101 +    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
   1.102 +    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
   1.103 +    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
   1.104 +    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
   1.105 +    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
   1.106 +    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
   1.107 +    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
   1.108 +    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
   1.109 +    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
   1.110 +    4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
   1.111 +};
   1.112 +
   1.113 +#define HAS_SLOT(flags, idx) ((flags)&(1<<(idx)))
   1.114 +#define SLOT_OFFSET(flags, idx) flagsOffset[(flags)&((1<<(idx))-1)]
   1.115 +
   1.116 +/*
   1.117 + * Get the value of an optional-value slot where HAS_SLOT(excWord, idx).
   1.118 + *
   1.119 + * @param excWord (in) initial exceptions word
   1.120 + * @param idx (in) desired slot index
   1.121 + * @param pExc16 (in/out) const uint16_t * after excWord=*pExc16++;
   1.122 + *               moved to the last uint16_t of the value, use +1 for beginning of next slot
   1.123 + * @param value (out) int32_t or uint32_t output if hasSlot, otherwise not modified
   1.124 + */
   1.125 +#define GET_SLOT_VALUE(excWord, idx, pExc16, value) \
   1.126 +    if(((excWord)&UCASE_EXC_DOUBLE_SLOTS)==0) { \
   1.127 +        (pExc16)+=SLOT_OFFSET(excWord, idx); \
   1.128 +        (value)=*pExc16; \
   1.129 +    } else { \
   1.130 +        (pExc16)+=2*SLOT_OFFSET(excWord, idx); \
   1.131 +        (value)=*pExc16++; \
   1.132 +        (value)=((value)<<16)|*pExc16; \
   1.133 +    }
   1.134 +
   1.135 +/* simple case mappings ----------------------------------------------------- */
   1.136 +
   1.137 +U_CAPI UChar32 U_EXPORT2
   1.138 +ucase_tolower(const UCaseProps *csp, UChar32 c) {
   1.139 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.140 +    if(!PROPS_HAS_EXCEPTION(props)) {
   1.141 +        if(UCASE_GET_TYPE(props)>=UCASE_UPPER) {
   1.142 +            c+=UCASE_GET_DELTA(props);
   1.143 +        }
   1.144 +    } else {
   1.145 +        const uint16_t *pe=GET_EXCEPTIONS(csp, props);
   1.146 +        uint16_t excWord=*pe++;
   1.147 +        if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
   1.148 +            GET_SLOT_VALUE(excWord, UCASE_EXC_LOWER, pe, c);
   1.149 +        }
   1.150 +    }
   1.151 +    return c;
   1.152 +}
   1.153 +
   1.154 +U_CAPI UChar32 U_EXPORT2
   1.155 +ucase_toupper(const UCaseProps *csp, UChar32 c) {
   1.156 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.157 +    if(!PROPS_HAS_EXCEPTION(props)) {
   1.158 +        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
   1.159 +            c+=UCASE_GET_DELTA(props);
   1.160 +        }
   1.161 +    } else {
   1.162 +        const uint16_t *pe=GET_EXCEPTIONS(csp, props);
   1.163 +        uint16_t excWord=*pe++;
   1.164 +        if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
   1.165 +            GET_SLOT_VALUE(excWord, UCASE_EXC_UPPER, pe, c);
   1.166 +        }
   1.167 +    }
   1.168 +    return c;
   1.169 +}
   1.170 +
   1.171 +U_CAPI UChar32 U_EXPORT2
   1.172 +ucase_totitle(const UCaseProps *csp, UChar32 c) {
   1.173 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.174 +    if(!PROPS_HAS_EXCEPTION(props)) {
   1.175 +        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
   1.176 +            c+=UCASE_GET_DELTA(props);
   1.177 +        }
   1.178 +    } else {
   1.179 +        const uint16_t *pe=GET_EXCEPTIONS(csp, props);
   1.180 +        uint16_t excWord=*pe++;
   1.181 +        int32_t idx;
   1.182 +        if(HAS_SLOT(excWord, UCASE_EXC_TITLE)) {
   1.183 +            idx=UCASE_EXC_TITLE;
   1.184 +        } else if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
   1.185 +            idx=UCASE_EXC_UPPER;
   1.186 +        } else {
   1.187 +            return c;
   1.188 +        }
   1.189 +        GET_SLOT_VALUE(excWord, idx, pe, c);
   1.190 +    }
   1.191 +    return c;
   1.192 +}
   1.193 +
   1.194 +static const UChar iDot[2] = { 0x69, 0x307 };
   1.195 +static const UChar jDot[2] = { 0x6a, 0x307 };
   1.196 +static const UChar iOgonekDot[3] = { 0x12f, 0x307 };
   1.197 +static const UChar iDotGrave[3] = { 0x69, 0x307, 0x300 };
   1.198 +static const UChar iDotAcute[3] = { 0x69, 0x307, 0x301 };
   1.199 +static const UChar iDotTilde[3] = { 0x69, 0x307, 0x303 };
   1.200 +
   1.201 +
   1.202 +U_CFUNC void U_EXPORT2
   1.203 +ucase_addCaseClosure(const UCaseProps *csp, UChar32 c, const USetAdder *sa) {
   1.204 +    uint16_t props;
   1.205 +
   1.206 +    /*
   1.207 +     * Hardcode the case closure of i and its relatives and ignore the
   1.208 +     * data file data for these characters.
   1.209 +     * The Turkic dotless i and dotted I with their case mapping conditions
   1.210 +     * and case folding option make the related characters behave specially.
   1.211 +     * This code matches their closure behavior to their case folding behavior.
   1.212 +     */
   1.213 +
   1.214 +    switch(c) {
   1.215 +    case 0x49:
   1.216 +        /* regular i and I are in one equivalence class */
   1.217 +        sa->add(sa->set, 0x69);
   1.218 +        return;
   1.219 +    case 0x69:
   1.220 +        sa->add(sa->set, 0x49);
   1.221 +        return;
   1.222 +    case 0x130:
   1.223 +        /* dotted I is in a class with <0069 0307> (for canonical equivalence with <0049 0307>) */
   1.224 +        sa->addString(sa->set, iDot, 2);
   1.225 +        return;
   1.226 +    case 0x131:
   1.227 +        /* dotless i is in a class by itself */
   1.228 +        return;
   1.229 +    default:
   1.230 +        /* otherwise use the data file data */
   1.231 +        break;
   1.232 +    }
   1.233 +
   1.234 +    props=UTRIE2_GET16(&csp->trie, c);
   1.235 +    if(!PROPS_HAS_EXCEPTION(props)) {
   1.236 +        if(UCASE_GET_TYPE(props)!=UCASE_NONE) {
   1.237 +            /* add the one simple case mapping, no matter what type it is */
   1.238 +            int32_t delta=UCASE_GET_DELTA(props);
   1.239 +            if(delta!=0) {
   1.240 +                sa->add(sa->set, c+delta);
   1.241 +            }
   1.242 +        }
   1.243 +    } else {
   1.244 +        /*
   1.245 +         * c has exceptions, so there may be multiple simple and/or
   1.246 +         * full case mappings. Add them all.
   1.247 +         */
   1.248 +        const uint16_t *pe0, *pe=GET_EXCEPTIONS(csp, props);
   1.249 +        const UChar *closure;
   1.250 +        uint16_t excWord=*pe++;
   1.251 +        int32_t idx, closureLength, fullLength, length;
   1.252 +
   1.253 +        pe0=pe;
   1.254 +
   1.255 +        /* add all simple case mappings */
   1.256 +        for(idx=UCASE_EXC_LOWER; idx<=UCASE_EXC_TITLE; ++idx) {
   1.257 +            if(HAS_SLOT(excWord, idx)) {
   1.258 +                pe=pe0;
   1.259 +                GET_SLOT_VALUE(excWord, idx, pe, c);
   1.260 +                sa->add(sa->set, c);
   1.261 +            }
   1.262 +        }
   1.263 +
   1.264 +        /* get the closure string pointer & length */
   1.265 +        if(HAS_SLOT(excWord, UCASE_EXC_CLOSURE)) {
   1.266 +            pe=pe0;
   1.267 +            GET_SLOT_VALUE(excWord, UCASE_EXC_CLOSURE, pe, closureLength);
   1.268 +            closureLength&=UCASE_CLOSURE_MAX_LENGTH; /* higher bits are reserved */
   1.269 +            closure=(const UChar *)pe+1; /* behind this slot, unless there are full case mappings */
   1.270 +        } else {
   1.271 +            closureLength=0;
   1.272 +            closure=NULL;
   1.273 +        }
   1.274 +
   1.275 +        /* add the full case folding */
   1.276 +        if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
   1.277 +            pe=pe0;
   1.278 +            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, fullLength);
   1.279 +
   1.280 +            /* start of full case mapping strings */
   1.281 +            ++pe;
   1.282 +
   1.283 +            fullLength&=0xffff; /* bits 16 and higher are reserved */
   1.284 +
   1.285 +            /* skip the lowercase result string */
   1.286 +            pe+=fullLength&UCASE_FULL_LOWER;
   1.287 +            fullLength>>=4;
   1.288 +
   1.289 +            /* add the full case folding string */
   1.290 +            length=fullLength&0xf;
   1.291 +            if(length!=0) {
   1.292 +                sa->addString(sa->set, (const UChar *)pe, length);
   1.293 +                pe+=length;
   1.294 +            }
   1.295 +
   1.296 +            /* skip the uppercase and titlecase strings */
   1.297 +            fullLength>>=4;
   1.298 +            pe+=fullLength&0xf;
   1.299 +            fullLength>>=4;
   1.300 +            pe+=fullLength;
   1.301 +
   1.302 +            closure=(const UChar *)pe; /* behind full case mappings */
   1.303 +        }
   1.304 +
   1.305 +        /* add each code point in the closure string */
   1.306 +        for(idx=0; idx<closureLength;) {
   1.307 +            U16_NEXT_UNSAFE(closure, idx, c);
   1.308 +            sa->add(sa->set, c);
   1.309 +        }
   1.310 +    }
   1.311 +}
   1.312 +
   1.313 +/*
   1.314 + * compare s, which has a length, with t, which has a maximum length or is NUL-terminated
   1.315 + * must be length>0 and max>0 and length<=max
   1.316 + */
   1.317 +static inline int32_t
   1.318 +strcmpMax(const UChar *s, int32_t length, const UChar *t, int32_t max) {
   1.319 +    int32_t c1, c2;
   1.320 +
   1.321 +    max-=length; /* we require length<=max, so no need to decrement max in the loop */
   1.322 +    do {
   1.323 +        c1=*s++;
   1.324 +        c2=*t++;
   1.325 +        if(c2==0) {
   1.326 +            return 1; /* reached the end of t but not of s */
   1.327 +        }
   1.328 +        c1-=c2;
   1.329 +        if(c1!=0) {
   1.330 +            return c1; /* return difference result */
   1.331 +        }
   1.332 +    } while(--length>0);
   1.333 +    /* ends with length==0 */
   1.334 +
   1.335 +    if(max==0 || *t==0) {
   1.336 +        return 0; /* equal to length of both strings */
   1.337 +    } else {
   1.338 +        return -max; /* return lengh difference */
   1.339 +    }
   1.340 +}
   1.341 +
   1.342 +U_CFUNC UBool U_EXPORT2
   1.343 +ucase_addStringCaseClosure(const UCaseProps *csp, const UChar *s, int32_t length, const USetAdder *sa) {
   1.344 +    int32_t i, start, limit, result, unfoldRows, unfoldRowWidth, unfoldStringWidth;
   1.345 +
   1.346 +    if(csp->unfold==NULL || s==NULL) {
   1.347 +        return FALSE; /* no reverse case folding data, or no string */
   1.348 +    }
   1.349 +    if(length<=1) {
   1.350 +        /* the string is too short to find any match */
   1.351 +        /*
   1.352 +         * more precise would be:
   1.353 +         * if(!u_strHasMoreChar32Than(s, length, 1))
   1.354 +         * but this does not make much practical difference because
   1.355 +         * a single supplementary code point would just not be found
   1.356 +         */
   1.357 +        return FALSE;
   1.358 +    }
   1.359 +
   1.360 +    const uint16_t *unfold=csp->unfold;
   1.361 +    unfoldRows=unfold[UCASE_UNFOLD_ROWS];
   1.362 +    unfoldRowWidth=unfold[UCASE_UNFOLD_ROW_WIDTH];
   1.363 +    unfoldStringWidth=unfold[UCASE_UNFOLD_STRING_WIDTH];
   1.364 +    unfold+=unfoldRowWidth;
   1.365 +
   1.366 +    if(length>unfoldStringWidth) {
   1.367 +        /* the string is too long to find any match */
   1.368 +        return FALSE;
   1.369 +    }
   1.370 +
   1.371 +    /* do a binary search for the string */
   1.372 +    start=0;
   1.373 +    limit=unfoldRows;
   1.374 +    while(start<limit) {
   1.375 +        i=(start+limit)/2;
   1.376 +        const UChar *p=reinterpret_cast<const UChar *>(unfold+(i*unfoldRowWidth));
   1.377 +        result=strcmpMax(s, length, p, unfoldStringWidth);
   1.378 +
   1.379 +        if(result==0) {
   1.380 +            /* found the string: add each code point, and its case closure */
   1.381 +            UChar32 c;
   1.382 +
   1.383 +            for(i=unfoldStringWidth; i<unfoldRowWidth && p[i]!=0;) {
   1.384 +                U16_NEXT_UNSAFE(p, i, c);
   1.385 +                sa->add(sa->set, c);
   1.386 +                ucase_addCaseClosure(csp, c, sa);
   1.387 +            }
   1.388 +            return TRUE;
   1.389 +        } else if(result<0) {
   1.390 +            limit=i;
   1.391 +        } else /* result>0 */ {
   1.392 +            start=i+1;
   1.393 +        }
   1.394 +    }
   1.395 +
   1.396 +    return FALSE; /* string not found */
   1.397 +}
   1.398 +
   1.399 +U_NAMESPACE_BEGIN
   1.400 +
   1.401 +FullCaseFoldingIterator::FullCaseFoldingIterator()
   1.402 +        : unfold(reinterpret_cast<const UChar *>(ucase_props_singleton.unfold)),
   1.403 +          unfoldRows(unfold[UCASE_UNFOLD_ROWS]),
   1.404 +          unfoldRowWidth(unfold[UCASE_UNFOLD_ROW_WIDTH]),
   1.405 +          unfoldStringWidth(unfold[UCASE_UNFOLD_STRING_WIDTH]),
   1.406 +          currentRow(0),
   1.407 +          rowCpIndex(unfoldStringWidth) {
   1.408 +    unfold+=unfoldRowWidth;
   1.409 +}
   1.410 +
   1.411 +UChar32
   1.412 +FullCaseFoldingIterator::next(UnicodeString &full) {
   1.413 +    // Advance past the last-delivered code point.
   1.414 +    const UChar *p=unfold+(currentRow*unfoldRowWidth);
   1.415 +    if(rowCpIndex>=unfoldRowWidth || p[rowCpIndex]==0) {
   1.416 +        ++currentRow;
   1.417 +        p+=unfoldRowWidth;
   1.418 +        rowCpIndex=unfoldStringWidth;
   1.419 +    }
   1.420 +    if(currentRow>=unfoldRows) { return U_SENTINEL; }
   1.421 +    // Set "full" to the NUL-terminated string in the first unfold column.
   1.422 +    int32_t length=unfoldStringWidth;
   1.423 +    while(length>0 && p[length-1]==0) { --length; }
   1.424 +    full.setTo(FALSE, p, length);
   1.425 +    // Return the code point.
   1.426 +    UChar32 c;
   1.427 +    U16_NEXT_UNSAFE(p, rowCpIndex, c);
   1.428 +    return c;
   1.429 +}
   1.430 +
   1.431 +U_NAMESPACE_END
   1.432 +
   1.433 +/** @return UCASE_NONE, UCASE_LOWER, UCASE_UPPER, UCASE_TITLE */
   1.434 +U_CAPI int32_t U_EXPORT2
   1.435 +ucase_getType(const UCaseProps *csp, UChar32 c) {
   1.436 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.437 +    return UCASE_GET_TYPE(props);
   1.438 +}
   1.439 +
   1.440 +/** @return same as ucase_getType() and set bit 2 if c is case-ignorable */
   1.441 +U_CAPI int32_t U_EXPORT2
   1.442 +ucase_getTypeOrIgnorable(const UCaseProps *csp, UChar32 c) {
   1.443 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.444 +    return UCASE_GET_TYPE_AND_IGNORABLE(props);
   1.445 +}
   1.446 +
   1.447 +/** @return UCASE_NO_DOT, UCASE_SOFT_DOTTED, UCASE_ABOVE, UCASE_OTHER_ACCENT */
   1.448 +static inline int32_t
   1.449 +getDotType(const UCaseProps *csp, UChar32 c) {
   1.450 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.451 +    if(!PROPS_HAS_EXCEPTION(props)) {
   1.452 +        return props&UCASE_DOT_MASK;
   1.453 +    } else {
   1.454 +        const uint16_t *pe=GET_EXCEPTIONS(csp, props);
   1.455 +        return (*pe>>UCASE_EXC_DOT_SHIFT)&UCASE_DOT_MASK;
   1.456 +    }
   1.457 +}
   1.458 +
   1.459 +U_CAPI UBool U_EXPORT2
   1.460 +ucase_isSoftDotted(const UCaseProps *csp, UChar32 c) {
   1.461 +    return (UBool)(getDotType(csp, c)==UCASE_SOFT_DOTTED);
   1.462 +}
   1.463 +
   1.464 +U_CAPI UBool U_EXPORT2
   1.465 +ucase_isCaseSensitive(const UCaseProps *csp, UChar32 c) {
   1.466 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.467 +    return (UBool)((props&UCASE_SENSITIVE)!=0);
   1.468 +}
   1.469 +
   1.470 +/* string casing ------------------------------------------------------------ */
   1.471 +
   1.472 +/*
   1.473 + * These internal functions form the core of string case mappings.
   1.474 + * They map single code points to result code points or strings and take
   1.475 + * all necessary conditions (context, locale ID, options) into account.
   1.476 + *
   1.477 + * They do not iterate over the source or write to the destination
   1.478 + * so that the same functions are useful for non-standard string storage,
   1.479 + * such as in a Replaceable (for Transliterator) or UTF-8/32 strings etc.
   1.480 + * For the same reason, the "surrounding text" context is passed in as a
   1.481 + * UCaseContextIterator which does not make any assumptions about
   1.482 + * the underlying storage.
   1.483 + *
   1.484 + * This section contains helper functions that check for conditions
   1.485 + * in the input text surrounding the current code point
   1.486 + * according to SpecialCasing.txt.
   1.487 + *
   1.488 + * Each helper function gets the index
   1.489 + * - after the current code point if it looks at following text
   1.490 + * - before the current code point if it looks at preceding text
   1.491 + *
   1.492 + * Unicode 3.2 UAX 21 "Case Mappings" defines the conditions as follows:
   1.493 + *
   1.494 + * Final_Sigma
   1.495 + *   C is preceded by a sequence consisting of
   1.496 + *     a cased letter and a case-ignorable sequence,
   1.497 + *   and C is not followed by a sequence consisting of
   1.498 + *     an ignorable sequence and then a cased letter.
   1.499 + *
   1.500 + * More_Above
   1.501 + *   C is followed by one or more characters of combining class 230 (ABOVE)
   1.502 + *   in the combining character sequence.
   1.503 + *
   1.504 + * After_Soft_Dotted
   1.505 + *   The last preceding character with combining class of zero before C
   1.506 + *   was Soft_Dotted,
   1.507 + *   and there is no intervening combining character class 230 (ABOVE).
   1.508 + *
   1.509 + * Before_Dot
   1.510 + *   C is followed by combining dot above (U+0307).
   1.511 + *   Any sequence of characters with a combining class that is neither 0 nor 230
   1.512 + *   may intervene between the current character and the combining dot above.
   1.513 + *
   1.514 + * The erratum from 2002-10-31 adds the condition
   1.515 + *
   1.516 + * After_I
   1.517 + *   The last preceding base character was an uppercase I, and there is no
   1.518 + *   intervening combining character class 230 (ABOVE).
   1.519 + *
   1.520 + *   (See Jitterbug 2344 and the comments on After_I below.)
   1.521 + *
   1.522 + * Helper definitions in Unicode 3.2 UAX 21:
   1.523 + *
   1.524 + * D1. A character C is defined to be cased
   1.525 + *     if it meets any of the following criteria:
   1.526 + *
   1.527 + *   - The general category of C is Titlecase Letter (Lt)
   1.528 + *   - In [CoreProps], C has one of the properties Uppercase, or Lowercase
   1.529 + *   - Given D = NFD(C), then it is not the case that:
   1.530 + *     D = UCD_lower(D) = UCD_upper(D) = UCD_title(D)
   1.531 + *     (This third criterium does not add any characters to the list
   1.532 + *      for Unicode 3.2. Ignored.)
   1.533 + *
   1.534 + * D2. A character C is defined to be case-ignorable
   1.535 + *     if it meets either of the following criteria:
   1.536 + *
   1.537 + *   - The general category of C is
   1.538 + *     Nonspacing Mark (Mn), or Enclosing Mark (Me), or Format Control (Cf), or
   1.539 + *     Letter Modifier (Lm), or Symbol Modifier (Sk)
   1.540 + *   - C is one of the following characters 
   1.541 + *     U+0027 APOSTROPHE
   1.542 + *     U+00AD SOFT HYPHEN (SHY)
   1.543 + *     U+2019 RIGHT SINGLE QUOTATION MARK
   1.544 + *            (the preferred character for apostrophe)
   1.545 + *
   1.546 + * D3. A case-ignorable sequence is a sequence of
   1.547 + *     zero or more case-ignorable characters.
   1.548 + */
   1.549 +
   1.550 +#define is_a(c) ((c)=='a' || (c)=='A')
   1.551 +#define is_d(c) ((c)=='d' || (c)=='D')
   1.552 +#define is_e(c) ((c)=='e' || (c)=='E')
   1.553 +#define is_i(c) ((c)=='i' || (c)=='I')
   1.554 +#define is_l(c) ((c)=='l' || (c)=='L')
   1.555 +#define is_n(c) ((c)=='n' || (c)=='N')
   1.556 +#define is_r(c) ((c)=='r' || (c)=='R')
   1.557 +#define is_t(c) ((c)=='t' || (c)=='T')
   1.558 +#define is_u(c) ((c)=='u' || (c)=='U')
   1.559 +#define is_z(c) ((c)=='z' || (c)=='Z')
   1.560 +
   1.561 +/* separator? */
   1.562 +#define is_sep(c) ((c)=='_' || (c)=='-' || (c)==0)
   1.563 +
   1.564 +/**
   1.565 + * Requires non-NULL locale ID but otherwise does the equivalent of
   1.566 + * checking for language codes as if uloc_getLanguage() were called:
   1.567 + * Accepts both 2- and 3-letter codes and accepts case variants.
   1.568 + */
   1.569 +U_CFUNC int32_t
   1.570 +ucase_getCaseLocale(const char *locale, int32_t *locCache) {
   1.571 +    int32_t result;
   1.572 +    char c;
   1.573 +
   1.574 +    if(locCache!=NULL && (result=*locCache)!=UCASE_LOC_UNKNOWN) {
   1.575 +        return result;
   1.576 +    }
   1.577 +
   1.578 +    result=UCASE_LOC_ROOT;
   1.579 +
   1.580 +    /*
   1.581 +     * This function used to use uloc_getLanguage(), but the current code
   1.582 +     * removes the dependency of this low-level code on uloc implementation code
   1.583 +     * and is faster because not the whole locale ID has to be
   1.584 +     * examined and copied/transformed.
   1.585 +     *
   1.586 +     * Because this code does not want to depend on uloc, the caller must
   1.587 +     * pass in a non-NULL locale, i.e., may need to call uloc_getDefault().
   1.588 +     */
   1.589 +    c=*locale++;
   1.590 +    if(is_t(c)) {
   1.591 +        /* tr or tur? */
   1.592 +        c=*locale++;
   1.593 +        if(is_u(c)) {
   1.594 +            c=*locale++;
   1.595 +        }
   1.596 +        if(is_r(c)) {
   1.597 +            c=*locale;
   1.598 +            if(is_sep(c)) {
   1.599 +                result=UCASE_LOC_TURKISH;
   1.600 +            }
   1.601 +        }
   1.602 +    } else if(is_a(c)) {
   1.603 +        /* az or aze? */
   1.604 +        c=*locale++;
   1.605 +        if(is_z(c)) {
   1.606 +            c=*locale++;
   1.607 +            if(is_e(c)) {
   1.608 +                c=*locale;
   1.609 +            }
   1.610 +            if(is_sep(c)) {
   1.611 +                result=UCASE_LOC_TURKISH;
   1.612 +            }
   1.613 +        }
   1.614 +    } else if(is_l(c)) {
   1.615 +        /* lt or lit? */
   1.616 +        c=*locale++;
   1.617 +        if(is_i(c)) {
   1.618 +            c=*locale++;
   1.619 +        }
   1.620 +        if(is_t(c)) {
   1.621 +            c=*locale;
   1.622 +            if(is_sep(c)) {
   1.623 +                result=UCASE_LOC_LITHUANIAN;
   1.624 +            }
   1.625 +        }
   1.626 +    } else if(is_n(c)) {
   1.627 +        /* nl or nld? */
   1.628 +        c=*locale++;
   1.629 +        if(is_l(c)) {
   1.630 +            c=*locale++;
   1.631 +            if(is_d(c)) {
   1.632 +                c=*locale;
   1.633 +            }
   1.634 +            if(is_sep(c)) {
   1.635 +                result=UCASE_LOC_DUTCH;
   1.636 +            }
   1.637 +        }
   1.638 +    }
   1.639 +
   1.640 +    if(locCache!=NULL) {
   1.641 +        *locCache=result;
   1.642 +    }
   1.643 +    return result;
   1.644 +}
   1.645 +
   1.646 +/*
   1.647 + * Is followed by
   1.648 + *   {case-ignorable}* cased
   1.649 + * ?
   1.650 + * (dir determines looking forward/backward)
   1.651 + * If a character is case-ignorable, it is skipped regardless of whether
   1.652 + * it is also cased or not.
   1.653 + */
   1.654 +static UBool
   1.655 +isFollowedByCasedLetter(const UCaseProps *csp, UCaseContextIterator *iter, void *context, int8_t dir) {
   1.656 +    UChar32 c;
   1.657 +
   1.658 +    if(iter==NULL) {
   1.659 +        return FALSE;
   1.660 +    }
   1.661 +
   1.662 +    for(/* dir!=0 sets direction */; (c=iter(context, dir))>=0; dir=0) {
   1.663 +        int32_t type=ucase_getTypeOrIgnorable(csp, c);
   1.664 +        if(type&4) {
   1.665 +            /* case-ignorable, continue with the loop */
   1.666 +        } else if(type!=UCASE_NONE) {
   1.667 +            return TRUE; /* followed by cased letter */
   1.668 +        } else {
   1.669 +            return FALSE; /* uncased and not case-ignorable */
   1.670 +        }
   1.671 +    }
   1.672 +
   1.673 +    return FALSE; /* not followed by cased letter */
   1.674 +}
   1.675 +
   1.676 +/* Is preceded by Soft_Dotted character with no intervening cc=230 ? */
   1.677 +static UBool
   1.678 +isPrecededBySoftDotted(const UCaseProps *csp, UCaseContextIterator *iter, void *context) {
   1.679 +    UChar32 c;
   1.680 +    int32_t dotType;
   1.681 +    int8_t dir;
   1.682 +
   1.683 +    if(iter==NULL) {
   1.684 +        return FALSE;
   1.685 +    }
   1.686 +
   1.687 +    for(dir=-1; (c=iter(context, dir))>=0; dir=0) {
   1.688 +        dotType=getDotType(csp, c);
   1.689 +        if(dotType==UCASE_SOFT_DOTTED) {
   1.690 +            return TRUE; /* preceded by TYPE_i */
   1.691 +        } else if(dotType!=UCASE_OTHER_ACCENT) {
   1.692 +            return FALSE; /* preceded by different base character (not TYPE_i), or intervening cc==230 */
   1.693 +        }
   1.694 +    }
   1.695 +
   1.696 +    return FALSE; /* not preceded by TYPE_i */
   1.697 +}
   1.698 +
   1.699 +/*
   1.700 + * See Jitterbug 2344:
   1.701 + * The condition After_I for Turkic-lowercasing of U+0307 combining dot above
   1.702 + * is checked in ICU 2.0, 2.1, 2.6 but was not in 2.2 & 2.4 because
   1.703 + * we made those releases compatible with Unicode 3.2 which had not fixed
   1.704 + * a related bug in SpecialCasing.txt.
   1.705 + *
   1.706 + * From the Jitterbug 2344 text:
   1.707 + * ... this bug is listed as a Unicode erratum
   1.708 + * from 2002-10-31 at http://www.unicode.org/uni2errata/UnicodeErrata.html
   1.709 + * <quote>
   1.710 + * There are two errors in SpecialCasing.txt.
   1.711 + * 1. Missing semicolons on two lines. ... [irrelevant for ICU]
   1.712 + * 2. An incorrect context definition. Correct as follows:
   1.713 + * < 0307; ; 0307; 0307; tr After_Soft_Dotted; # COMBINING DOT ABOVE
   1.714 + * < 0307; ; 0307; 0307; az After_Soft_Dotted; # COMBINING DOT ABOVE
   1.715 + * ---
   1.716 + * > 0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE
   1.717 + * > 0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE
   1.718 + * where the context After_I is defined as:
   1.719 + * The last preceding base character was an uppercase I, and there is no
   1.720 + * intervening combining character class 230 (ABOVE).
   1.721 + * </quote>
   1.722 + *
   1.723 + * Note that SpecialCasing.txt even in Unicode 3.2 described the condition as:
   1.724 + *
   1.725 + * # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i.
   1.726 + * # This matches the behavior of the canonically equivalent I-dot_above
   1.727 + *
   1.728 + * See also the description in this place in older versions of uchar.c (revision 1.100).
   1.729 + *
   1.730 + * Markus W. Scherer 2003-feb-15
   1.731 + */
   1.732 +
   1.733 +/* Is preceded by base character 'I' with no intervening cc=230 ? */
   1.734 +static UBool
   1.735 +isPrecededBy_I(const UCaseProps *csp, UCaseContextIterator *iter, void *context) {
   1.736 +    UChar32 c;
   1.737 +    int32_t dotType;
   1.738 +    int8_t dir;
   1.739 +
   1.740 +    if(iter==NULL) {
   1.741 +        return FALSE;
   1.742 +    }
   1.743 +
   1.744 +    for(dir=-1; (c=iter(context, dir))>=0; dir=0) {
   1.745 +        if(c==0x49) {
   1.746 +            return TRUE; /* preceded by I */
   1.747 +        }
   1.748 +        dotType=getDotType(csp, c);
   1.749 +        if(dotType!=UCASE_OTHER_ACCENT) {
   1.750 +            return FALSE; /* preceded by different base character (not I), or intervening cc==230 */
   1.751 +        }
   1.752 +    }
   1.753 +
   1.754 +    return FALSE; /* not preceded by I */
   1.755 +}
   1.756 +
   1.757 +/* Is followed by one or more cc==230 ? */
   1.758 +static UBool
   1.759 +isFollowedByMoreAbove(const UCaseProps *csp, UCaseContextIterator *iter, void *context) {
   1.760 +    UChar32 c;
   1.761 +    int32_t dotType;
   1.762 +    int8_t dir;
   1.763 +
   1.764 +    if(iter==NULL) {
   1.765 +        return FALSE;
   1.766 +    }
   1.767 +
   1.768 +    for(dir=1; (c=iter(context, dir))>=0; dir=0) {
   1.769 +        dotType=getDotType(csp, c);
   1.770 +        if(dotType==UCASE_ABOVE) {
   1.771 +            return TRUE; /* at least one cc==230 following */
   1.772 +        } else if(dotType!=UCASE_OTHER_ACCENT) {
   1.773 +            return FALSE; /* next base character, no more cc==230 following */
   1.774 +        }
   1.775 +    }
   1.776 +
   1.777 +    return FALSE; /* no more cc==230 following */
   1.778 +}
   1.779 +
   1.780 +/* Is followed by a dot above (without cc==230 in between) ? */
   1.781 +static UBool
   1.782 +isFollowedByDotAbove(const UCaseProps *csp, UCaseContextIterator *iter, void *context) {
   1.783 +    UChar32 c;
   1.784 +    int32_t dotType;
   1.785 +    int8_t dir;
   1.786 +
   1.787 +    if(iter==NULL) {
   1.788 +        return FALSE;
   1.789 +    }
   1.790 +
   1.791 +    for(dir=1; (c=iter(context, dir))>=0; dir=0) {
   1.792 +        if(c==0x307) {
   1.793 +            return TRUE;
   1.794 +        }
   1.795 +        dotType=getDotType(csp, c);
   1.796 +        if(dotType!=UCASE_OTHER_ACCENT) {
   1.797 +            return FALSE; /* next base character or cc==230 in between */
   1.798 +        }
   1.799 +    }
   1.800 +
   1.801 +    return FALSE; /* no dot above following */
   1.802 +}
   1.803 +
   1.804 +U_CAPI int32_t U_EXPORT2
   1.805 +ucase_toFullLower(const UCaseProps *csp, UChar32 c,
   1.806 +                  UCaseContextIterator *iter, void *context,
   1.807 +                  const UChar **pString,
   1.808 +                  const char *locale, int32_t *locCache)
   1.809 +{
   1.810 +    UChar32 result=c;
   1.811 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.812 +    if(!PROPS_HAS_EXCEPTION(props)) {
   1.813 +        if(UCASE_GET_TYPE(props)>=UCASE_UPPER) {
   1.814 +            result=c+UCASE_GET_DELTA(props);
   1.815 +        }
   1.816 +    } else {
   1.817 +        const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2;
   1.818 +        uint16_t excWord=*pe++;
   1.819 +        int32_t full;
   1.820 +
   1.821 +        pe2=pe;
   1.822 +
   1.823 +        if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) {
   1.824 +            /* use hardcoded conditions and mappings */
   1.825 +            int32_t loc=ucase_getCaseLocale(locale, locCache);
   1.826 +
   1.827 +            /*
   1.828 +             * Test for conditional mappings first
   1.829 +             *   (otherwise the unconditional default mappings are always taken),
   1.830 +             * then test for characters that have unconditional mappings in SpecialCasing.txt,
   1.831 +             * then get the UnicodeData.txt mappings.
   1.832 +             */
   1.833 +            if( loc==UCASE_LOC_LITHUANIAN &&
   1.834 +                    /* base characters, find accents above */
   1.835 +                    (((c==0x49 || c==0x4a || c==0x12e) &&
   1.836 +                        isFollowedByMoreAbove(csp, iter, context)) ||
   1.837 +                    /* precomposed with accent above, no need to find one */
   1.838 +                    (c==0xcc || c==0xcd || c==0x128))
   1.839 +            ) {
   1.840 +                /*
   1.841 +                    # Lithuanian
   1.842 +
   1.843 +                    # Lithuanian retains the dot in a lowercase i when followed by accents.
   1.844 +
   1.845 +                    # Introduce an explicit dot above when lowercasing capital I's and J's
   1.846 +                    # whenever there are more accents above.
   1.847 +                    # (of the accents used in Lithuanian: grave, acute, tilde above, and ogonek)
   1.848 +
   1.849 +                    0049; 0069 0307; 0049; 0049; lt More_Above; # LATIN CAPITAL LETTER I
   1.850 +                    004A; 006A 0307; 004A; 004A; lt More_Above; # LATIN CAPITAL LETTER J
   1.851 +                    012E; 012F 0307; 012E; 012E; lt More_Above; # LATIN CAPITAL LETTER I WITH OGONEK
   1.852 +                    00CC; 0069 0307 0300; 00CC; 00CC; lt; # LATIN CAPITAL LETTER I WITH GRAVE
   1.853 +                    00CD; 0069 0307 0301; 00CD; 00CD; lt; # LATIN CAPITAL LETTER I WITH ACUTE
   1.854 +                    0128; 0069 0307 0303; 0128; 0128; lt; # LATIN CAPITAL LETTER I WITH TILDE
   1.855 +                 */
   1.856 +                switch(c) {
   1.857 +                case 0x49:  /* LATIN CAPITAL LETTER I */
   1.858 +                    *pString=iDot;
   1.859 +                    return 2;
   1.860 +                case 0x4a:  /* LATIN CAPITAL LETTER J */
   1.861 +                    *pString=jDot;
   1.862 +                    return 2;
   1.863 +                case 0x12e: /* LATIN CAPITAL LETTER I WITH OGONEK */
   1.864 +                    *pString=iOgonekDot;
   1.865 +                    return 2;
   1.866 +                case 0xcc:  /* LATIN CAPITAL LETTER I WITH GRAVE */
   1.867 +                    *pString=iDotGrave;
   1.868 +                    return 3;
   1.869 +                case 0xcd:  /* LATIN CAPITAL LETTER I WITH ACUTE */
   1.870 +                    *pString=iDotAcute;
   1.871 +                    return 3;
   1.872 +                case 0x128: /* LATIN CAPITAL LETTER I WITH TILDE */
   1.873 +                    *pString=iDotTilde;
   1.874 +                    return 3;
   1.875 +                default:
   1.876 +                    return 0; /* will not occur */
   1.877 +                }
   1.878 +            /* # Turkish and Azeri */
   1.879 +            } else if(loc==UCASE_LOC_TURKISH && c==0x130) {
   1.880 +                /*
   1.881 +                    # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri
   1.882 +                    # The following rules handle those cases.
   1.883 +
   1.884 +                    0130; 0069; 0130; 0130; tr # LATIN CAPITAL LETTER I WITH DOT ABOVE
   1.885 +                    0130; 0069; 0130; 0130; az # LATIN CAPITAL LETTER I WITH DOT ABOVE
   1.886 +                 */
   1.887 +                return 0x69;
   1.888 +            } else if(loc==UCASE_LOC_TURKISH && c==0x307 && isPrecededBy_I(csp, iter, context)) {
   1.889 +                /*
   1.890 +                    # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i.
   1.891 +                    # This matches the behavior of the canonically equivalent I-dot_above
   1.892 +
   1.893 +                    0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE
   1.894 +                    0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE
   1.895 +                 */
   1.896 +                return 0; /* remove the dot (continue without output) */
   1.897 +            } else if(loc==UCASE_LOC_TURKISH && c==0x49 && !isFollowedByDotAbove(csp, iter, context)) {
   1.898 +                /*
   1.899 +                    # When lowercasing, unless an I is before a dot_above, it turns into a dotless i.
   1.900 +
   1.901 +                    0049; 0131; 0049; 0049; tr Not_Before_Dot; # LATIN CAPITAL LETTER I
   1.902 +                    0049; 0131; 0049; 0049; az Not_Before_Dot; # LATIN CAPITAL LETTER I
   1.903 +                 */
   1.904 +                return 0x131;
   1.905 +            } else if(c==0x130) {
   1.906 +                /*
   1.907 +                    # Preserve canonical equivalence for I with dot. Turkic is handled below.
   1.908 +
   1.909 +                    0130; 0069 0307; 0130; 0130; # LATIN CAPITAL LETTER I WITH DOT ABOVE
   1.910 +                 */
   1.911 +                *pString=iDot;
   1.912 +                return 2;
   1.913 +            } else if(  c==0x3a3 &&
   1.914 +                        !isFollowedByCasedLetter(csp, iter, context, 1) &&
   1.915 +                        isFollowedByCasedLetter(csp, iter, context, -1) /* -1=preceded */
   1.916 +            ) {
   1.917 +                /* greek capital sigma maps depending on surrounding cased letters (see SpecialCasing.txt) */
   1.918 +                /*
   1.919 +                    # Special case for final form of sigma
   1.920 +
   1.921 +                    03A3; 03C2; 03A3; 03A3; Final_Sigma; # GREEK CAPITAL LETTER SIGMA
   1.922 +                 */
   1.923 +                return 0x3c2; /* greek small final sigma */
   1.924 +            } else {
   1.925 +                /* no known conditional special case mapping, use a normal mapping */
   1.926 +            }
   1.927 +        } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
   1.928 +            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
   1.929 +            full&=UCASE_FULL_LOWER;
   1.930 +            if(full!=0) {
   1.931 +                /* set the output pointer to the lowercase mapping */
   1.932 +                *pString=reinterpret_cast<const UChar *>(pe+1);
   1.933 +
   1.934 +                /* return the string length */
   1.935 +                return full;
   1.936 +            }
   1.937 +        }
   1.938 +
   1.939 +        if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
   1.940 +            GET_SLOT_VALUE(excWord, UCASE_EXC_LOWER, pe2, result);
   1.941 +        }
   1.942 +    }
   1.943 +
   1.944 +    return (result==c) ? ~result : result;
   1.945 +}
   1.946 +
   1.947 +/* internal */
   1.948 +static int32_t
   1.949 +toUpperOrTitle(const UCaseProps *csp, UChar32 c,
   1.950 +               UCaseContextIterator *iter, void *context,
   1.951 +               const UChar **pString,
   1.952 +               const char *locale, int32_t *locCache,
   1.953 +               UBool upperNotTitle) {
   1.954 +    UChar32 result=c;
   1.955 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
   1.956 +    if(!PROPS_HAS_EXCEPTION(props)) {
   1.957 +        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
   1.958 +            result=c+UCASE_GET_DELTA(props);
   1.959 +        }
   1.960 +    } else {
   1.961 +        const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2;
   1.962 +        uint16_t excWord=*pe++;
   1.963 +        int32_t full, idx;
   1.964 +
   1.965 +        pe2=pe;
   1.966 +
   1.967 +        if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) {
   1.968 +            /* use hardcoded conditions and mappings */
   1.969 +            int32_t loc=ucase_getCaseLocale(locale, locCache);
   1.970 +
   1.971 +            if(loc==UCASE_LOC_TURKISH && c==0x69) {
   1.972 +                /*
   1.973 +                    # Turkish and Azeri
   1.974 +
   1.975 +                    # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri
   1.976 +                    # The following rules handle those cases.
   1.977 +
   1.978 +                    # When uppercasing, i turns into a dotted capital I
   1.979 +
   1.980 +                    0069; 0069; 0130; 0130; tr; # LATIN SMALL LETTER I
   1.981 +                    0069; 0069; 0130; 0130; az; # LATIN SMALL LETTER I
   1.982 +                */
   1.983 +                return 0x130;
   1.984 +            } else if(loc==UCASE_LOC_LITHUANIAN && c==0x307 && isPrecededBySoftDotted(csp, iter, context)) {
   1.985 +                /*
   1.986 +                    # Lithuanian
   1.987 +
   1.988 +                    # Lithuanian retains the dot in a lowercase i when followed by accents.
   1.989 +
   1.990 +                    # Remove DOT ABOVE after "i" with upper or titlecase
   1.991 +
   1.992 +                    0307; 0307; ; ; lt After_Soft_Dotted; # COMBINING DOT ABOVE
   1.993 +                 */
   1.994 +                return 0; /* remove the dot (continue without output) */
   1.995 +            } else {
   1.996 +                /* no known conditional special case mapping, use a normal mapping */
   1.997 +            }
   1.998 +        } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
   1.999 +            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
  1.1000 +
  1.1001 +            /* start of full case mapping strings */
  1.1002 +            ++pe;
  1.1003 +
  1.1004 +            /* skip the lowercase and case-folding result strings */
  1.1005 +            pe+=full&UCASE_FULL_LOWER;
  1.1006 +            full>>=4;
  1.1007 +            pe+=full&0xf;
  1.1008 +            full>>=4;
  1.1009 +
  1.1010 +            if(upperNotTitle) {
  1.1011 +                full&=0xf;
  1.1012 +            } else {
  1.1013 +                /* skip the uppercase result string */
  1.1014 +                pe+=full&0xf;
  1.1015 +                full=(full>>4)&0xf;
  1.1016 +            }
  1.1017 +
  1.1018 +            if(full!=0) {
  1.1019 +                /* set the output pointer to the result string */
  1.1020 +                *pString=reinterpret_cast<const UChar *>(pe);
  1.1021 +
  1.1022 +                /* return the string length */
  1.1023 +                return full;
  1.1024 +            }
  1.1025 +        }
  1.1026 +
  1.1027 +        if(!upperNotTitle && HAS_SLOT(excWord, UCASE_EXC_TITLE)) {
  1.1028 +            idx=UCASE_EXC_TITLE;
  1.1029 +        } else if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
  1.1030 +            /* here, titlecase is same as uppercase */
  1.1031 +            idx=UCASE_EXC_UPPER;
  1.1032 +        } else {
  1.1033 +            return ~c;
  1.1034 +        }
  1.1035 +        GET_SLOT_VALUE(excWord, idx, pe2, result);
  1.1036 +    }
  1.1037 +
  1.1038 +    return (result==c) ? ~result : result;
  1.1039 +}
  1.1040 +
  1.1041 +U_CAPI int32_t U_EXPORT2
  1.1042 +ucase_toFullUpper(const UCaseProps *csp, UChar32 c,
  1.1043 +                  UCaseContextIterator *iter, void *context,
  1.1044 +                  const UChar **pString,
  1.1045 +                  const char *locale, int32_t *locCache) {
  1.1046 +    return toUpperOrTitle(csp, c, iter, context, pString, locale, locCache, TRUE);
  1.1047 +}
  1.1048 +
  1.1049 +U_CAPI int32_t U_EXPORT2
  1.1050 +ucase_toFullTitle(const UCaseProps *csp, UChar32 c,
  1.1051 +                  UCaseContextIterator *iter, void *context,
  1.1052 +                  const UChar **pString,
  1.1053 +                  const char *locale, int32_t *locCache) {
  1.1054 +    return toUpperOrTitle(csp, c, iter, context, pString, locale, locCache, FALSE);
  1.1055 +}
  1.1056 +
  1.1057 +/* case folding ------------------------------------------------------------- */
  1.1058 +
  1.1059 +/*
  1.1060 + * Case folding is similar to lowercasing.
  1.1061 + * The result may be a simple mapping, i.e., a single code point, or
  1.1062 + * a full mapping, i.e., a string.
  1.1063 + * If the case folding for a code point is the same as its simple (1:1) lowercase mapping,
  1.1064 + * then only the lowercase mapping is stored.
  1.1065 + *
  1.1066 + * Some special cases are hardcoded because their conditions cannot be
  1.1067 + * parsed and processed from CaseFolding.txt.
  1.1068 + *
  1.1069 + * Unicode 3.2 CaseFolding.txt specifies for its status field:
  1.1070 +
  1.1071 +# C: common case folding, common mappings shared by both simple and full mappings.
  1.1072 +# F: full case folding, mappings that cause strings to grow in length. Multiple characters are separated by spaces.
  1.1073 +# S: simple case folding, mappings to single characters where different from F.
  1.1074 +# T: special case for uppercase I and dotted uppercase I
  1.1075 +#    - For non-Turkic languages, this mapping is normally not used.
  1.1076 +#    - For Turkic languages (tr, az), this mapping can be used instead of the normal mapping for these characters.
  1.1077 +#
  1.1078 +# Usage:
  1.1079 +#  A. To do a simple case folding, use the mappings with status C + S.
  1.1080 +#  B. To do a full case folding, use the mappings with status C + F.
  1.1081 +#
  1.1082 +#    The mappings with status T can be used or omitted depending on the desired case-folding
  1.1083 +#    behavior. (The default option is to exclude them.)
  1.1084 +
  1.1085 + * Unicode 3.2 has 'T' mappings as follows:
  1.1086 +
  1.1087 +0049; T; 0131; # LATIN CAPITAL LETTER I
  1.1088 +0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE
  1.1089 +
  1.1090 + * while the default mappings for these code points are:
  1.1091 +
  1.1092 +0049; C; 0069; # LATIN CAPITAL LETTER I
  1.1093 +0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
  1.1094 +
  1.1095 + * U+0130 has no simple case folding (simple-case-folds to itself).
  1.1096 + */
  1.1097 +
  1.1098 +/* return the simple case folding mapping for c */
  1.1099 +U_CAPI UChar32 U_EXPORT2
  1.1100 +ucase_fold(const UCaseProps *csp, UChar32 c, uint32_t options) {
  1.1101 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
  1.1102 +    if(!PROPS_HAS_EXCEPTION(props)) {
  1.1103 +        if(UCASE_GET_TYPE(props)>=UCASE_UPPER) {
  1.1104 +            c+=UCASE_GET_DELTA(props);
  1.1105 +        }
  1.1106 +    } else {
  1.1107 +        const uint16_t *pe=GET_EXCEPTIONS(csp, props);
  1.1108 +        uint16_t excWord=*pe++;
  1.1109 +        int32_t idx;
  1.1110 +        if(excWord&UCASE_EXC_CONDITIONAL_FOLD) {
  1.1111 +            /* special case folding mappings, hardcoded */
  1.1112 +            if((options&_FOLD_CASE_OPTIONS_MASK)==U_FOLD_CASE_DEFAULT) {
  1.1113 +                /* default mappings */
  1.1114 +                if(c==0x49) {
  1.1115 +                    /* 0049; C; 0069; # LATIN CAPITAL LETTER I */
  1.1116 +                    return 0x69;
  1.1117 +                } else if(c==0x130) {
  1.1118 +                    /* no simple case folding for U+0130 */
  1.1119 +                    return c;
  1.1120 +                }
  1.1121 +            } else {
  1.1122 +                /* Turkic mappings */
  1.1123 +                if(c==0x49) {
  1.1124 +                    /* 0049; T; 0131; # LATIN CAPITAL LETTER I */
  1.1125 +                    return 0x131;
  1.1126 +                } else if(c==0x130) {
  1.1127 +                    /* 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
  1.1128 +                    return 0x69;
  1.1129 +                }
  1.1130 +            }
  1.1131 +        }
  1.1132 +        if(HAS_SLOT(excWord, UCASE_EXC_FOLD)) {
  1.1133 +            idx=UCASE_EXC_FOLD;
  1.1134 +        } else if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
  1.1135 +            idx=UCASE_EXC_LOWER;
  1.1136 +        } else {
  1.1137 +            return c;
  1.1138 +        }
  1.1139 +        GET_SLOT_VALUE(excWord, idx, pe, c);
  1.1140 +    }
  1.1141 +    return c;
  1.1142 +}
  1.1143 +
  1.1144 +/*
  1.1145 + * Issue for canonical caseless match (UAX #21):
  1.1146 + * Turkic casefolding (using "T" mappings in CaseFolding.txt) does not preserve
  1.1147 + * canonical equivalence, unlike default-option casefolding.
  1.1148 + * For example, I-grave and I + grave fold to strings that are not canonically
  1.1149 + * equivalent.
  1.1150 + * For more details, see the comment in unorm_compare() in unorm.cpp
  1.1151 + * and the intermediate prototype changes for Jitterbug 2021.
  1.1152 + * (For example, revision 1.104 of uchar.c and 1.4 of CaseFolding.txt.)
  1.1153 + *
  1.1154 + * This did not get fixed because it appears that it is not possible to fix
  1.1155 + * it for uppercase and lowercase characters (I-grave vs. i-grave)
  1.1156 + * together in a way that they still fold to common result strings.
  1.1157 + */
  1.1158 +
  1.1159 +U_CAPI int32_t U_EXPORT2
  1.1160 +ucase_toFullFolding(const UCaseProps *csp, UChar32 c,
  1.1161 +                    const UChar **pString,
  1.1162 +                    uint32_t options)
  1.1163 +{
  1.1164 +    UChar32 result=c;
  1.1165 +    uint16_t props=UTRIE2_GET16(&csp->trie, c);
  1.1166 +    if(!PROPS_HAS_EXCEPTION(props)) {
  1.1167 +        if(UCASE_GET_TYPE(props)>=UCASE_UPPER) {
  1.1168 +            result=c+UCASE_GET_DELTA(props);
  1.1169 +        }
  1.1170 +    } else {
  1.1171 +        const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2;
  1.1172 +        uint16_t excWord=*pe++;
  1.1173 +        int32_t full, idx;
  1.1174 +
  1.1175 +        pe2=pe;
  1.1176 +
  1.1177 +        if(excWord&UCASE_EXC_CONDITIONAL_FOLD) {
  1.1178 +            /* use hardcoded conditions and mappings */
  1.1179 +            if((options&_FOLD_CASE_OPTIONS_MASK)==U_FOLD_CASE_DEFAULT) {
  1.1180 +                /* default mappings */
  1.1181 +                if(c==0x49) {
  1.1182 +                    /* 0049; C; 0069; # LATIN CAPITAL LETTER I */
  1.1183 +                    return 0x69;
  1.1184 +                } else if(c==0x130) {
  1.1185 +                    /* 0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
  1.1186 +                    *pString=iDot;
  1.1187 +                    return 2;
  1.1188 +                }
  1.1189 +            } else {
  1.1190 +                /* Turkic mappings */
  1.1191 +                if(c==0x49) {
  1.1192 +                    /* 0049; T; 0131; # LATIN CAPITAL LETTER I */
  1.1193 +                    return 0x131;
  1.1194 +                } else if(c==0x130) {
  1.1195 +                    /* 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
  1.1196 +                    return 0x69;
  1.1197 +                }
  1.1198 +            }
  1.1199 +        } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
  1.1200 +            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
  1.1201 +
  1.1202 +            /* start of full case mapping strings */
  1.1203 +            ++pe;
  1.1204 +
  1.1205 +            /* skip the lowercase result string */
  1.1206 +            pe+=full&UCASE_FULL_LOWER;
  1.1207 +            full=(full>>4)&0xf;
  1.1208 +
  1.1209 +            if(full!=0) {
  1.1210 +                /* set the output pointer to the result string */
  1.1211 +                *pString=reinterpret_cast<const UChar *>(pe);
  1.1212 +
  1.1213 +                /* return the string length */
  1.1214 +                return full;
  1.1215 +            }
  1.1216 +        }
  1.1217 +
  1.1218 +        if(HAS_SLOT(excWord, UCASE_EXC_FOLD)) {
  1.1219 +            idx=UCASE_EXC_FOLD;
  1.1220 +        } else if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
  1.1221 +            idx=UCASE_EXC_LOWER;
  1.1222 +        } else {
  1.1223 +            return ~c;
  1.1224 +        }
  1.1225 +        GET_SLOT_VALUE(excWord, idx, pe2, result);
  1.1226 +    }
  1.1227 +
  1.1228 +    return (result==c) ? ~result : result;
  1.1229 +}
  1.1230 +
  1.1231 +/* case mapping properties API ---------------------------------------------- */
  1.1232 +
  1.1233 +#define GET_CASE_PROPS() &ucase_props_singleton
  1.1234 +
  1.1235 +/* public API (see uchar.h) */
  1.1236 +
  1.1237 +U_CAPI UBool U_EXPORT2
  1.1238 +u_isULowercase(UChar32 c) {
  1.1239 +    return (UBool)(UCASE_LOWER==ucase_getType(GET_CASE_PROPS(), c));
  1.1240 +}
  1.1241 +
  1.1242 +U_CAPI UBool U_EXPORT2
  1.1243 +u_isUUppercase(UChar32 c) {
  1.1244 +    return (UBool)(UCASE_UPPER==ucase_getType(GET_CASE_PROPS(), c));
  1.1245 +}
  1.1246 +
  1.1247 +/* Transforms the Unicode character to its lower case equivalent.*/
  1.1248 +U_CAPI UChar32 U_EXPORT2
  1.1249 +u_tolower(UChar32 c) {
  1.1250 +    return ucase_tolower(GET_CASE_PROPS(), c);
  1.1251 +}
  1.1252 +    
  1.1253 +/* Transforms the Unicode character to its upper case equivalent.*/
  1.1254 +U_CAPI UChar32 U_EXPORT2
  1.1255 +u_toupper(UChar32 c) {
  1.1256 +    return ucase_toupper(GET_CASE_PROPS(), c);
  1.1257 +}
  1.1258 +
  1.1259 +/* Transforms the Unicode character to its title case equivalent.*/
  1.1260 +U_CAPI UChar32 U_EXPORT2
  1.1261 +u_totitle(UChar32 c) {
  1.1262 +    return ucase_totitle(GET_CASE_PROPS(), c);
  1.1263 +}
  1.1264 +
  1.1265 +/* return the simple case folding mapping for c */
  1.1266 +U_CAPI UChar32 U_EXPORT2
  1.1267 +u_foldCase(UChar32 c, uint32_t options) {
  1.1268 +    return ucase_fold(GET_CASE_PROPS(), c, options);
  1.1269 +}
  1.1270 +
  1.1271 +U_CFUNC int32_t U_EXPORT2
  1.1272 +ucase_hasBinaryProperty(UChar32 c, UProperty which) {
  1.1273 +    /* case mapping properties */
  1.1274 +    const UChar *resultString;
  1.1275 +    int32_t locCache;
  1.1276 +    const UCaseProps *csp=GET_CASE_PROPS();
  1.1277 +    if(csp==NULL) {
  1.1278 +        return FALSE;
  1.1279 +    }
  1.1280 +    switch(which) {
  1.1281 +    case UCHAR_LOWERCASE:
  1.1282 +        return (UBool)(UCASE_LOWER==ucase_getType(csp, c));
  1.1283 +    case UCHAR_UPPERCASE:
  1.1284 +        return (UBool)(UCASE_UPPER==ucase_getType(csp, c));
  1.1285 +    case UCHAR_SOFT_DOTTED:
  1.1286 +        return ucase_isSoftDotted(csp, c);
  1.1287 +    case UCHAR_CASE_SENSITIVE:
  1.1288 +        return ucase_isCaseSensitive(csp, c);
  1.1289 +    case UCHAR_CASED:
  1.1290 +        return (UBool)(UCASE_NONE!=ucase_getType(csp, c));
  1.1291 +    case UCHAR_CASE_IGNORABLE:
  1.1292 +        return (UBool)(ucase_getTypeOrIgnorable(csp, c)>>2);
  1.1293 +    /*
  1.1294 +     * Note: The following Changes_When_Xyz are defined as testing whether
  1.1295 +     * the NFD form of the input changes when Xyz-case-mapped.
  1.1296 +     * However, this simpler implementation of these properties,
  1.1297 +     * ignoring NFD, passes the tests.
  1.1298 +     * The implementation needs to be changed if the tests start failing.
  1.1299 +     * When that happens, optimizations should be used to work with the
  1.1300 +     * per-single-code point ucase_toFullXyz() functions unless
  1.1301 +     * the NFD form has more than one code point,
  1.1302 +     * and the property starts set needs to be the union of the
  1.1303 +     * start sets for normalization and case mappings.
  1.1304 +     */
  1.1305 +    case UCHAR_CHANGES_WHEN_LOWERCASED:
  1.1306 +        locCache=UCASE_LOC_ROOT;
  1.1307 +        return (UBool)(ucase_toFullLower(csp, c, NULL, NULL, &resultString, "", &locCache)>=0);
  1.1308 +    case UCHAR_CHANGES_WHEN_UPPERCASED:
  1.1309 +        locCache=UCASE_LOC_ROOT;
  1.1310 +        return (UBool)(ucase_toFullUpper(csp, c, NULL, NULL, &resultString, "", &locCache)>=0);
  1.1311 +    case UCHAR_CHANGES_WHEN_TITLECASED:
  1.1312 +        locCache=UCASE_LOC_ROOT;
  1.1313 +        return (UBool)(ucase_toFullTitle(csp, c, NULL, NULL, &resultString, "", &locCache)>=0);
  1.1314 +    /* case UCHAR_CHANGES_WHEN_CASEFOLDED: -- in uprops.c */
  1.1315 +    case UCHAR_CHANGES_WHEN_CASEMAPPED:
  1.1316 +        locCache=UCASE_LOC_ROOT;
  1.1317 +        return (UBool)(
  1.1318 +            ucase_toFullLower(csp, c, NULL, NULL, &resultString, "", &locCache)>=0 ||
  1.1319 +            ucase_toFullUpper(csp, c, NULL, NULL, &resultString, "", &locCache)>=0 ||
  1.1320 +            ucase_toFullTitle(csp, c, NULL, NULL, &resultString, "", &locCache)>=0);
  1.1321 +    default:
  1.1322 +        return FALSE;
  1.1323 +    }
  1.1324 +}

mercurial