]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/ucase.c
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / common / ucase.c
index 76b26bb93ea870b5295b14b90ece902d6d865811..c21045a06a43b1fe8210cfb7b5c3db2ffbc28212 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2004, International Business Machines
+*   Copyright (C) 2004-2006, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
@@ -33,6 +33,7 @@ struct UCaseProps {
     UDataMemory *mem;
     const int32_t *indexes;
     const uint16_t *exceptions;
+    const UChar *unfold;
 
     UTrie trie;
     uint8_t formatVersion[4];
@@ -40,6 +41,15 @@ struct UCaseProps {
 
 /* data loading etc. -------------------------------------------------------- */
 
+#define UCASE_HARDCODE_DATA 1
+
+#if UCASE_HARDCODE_DATA
+
+/* ucase_props_data.c is machine-generated by gencase --csource */
+#include "ucase_props_data.c"
+
+#else
+
 static UBool U_CALLCONV
 isAcceptable(void *context,
              const char *type, const char *name,
@@ -68,38 +78,50 @@ static UCaseProps *
 ucase_openData(UCaseProps *cspProto,
                const uint8_t *bin, int32_t length, UErrorCode *pErrorCode) {
     UCaseProps *csp;
-    int32_t size, trieSize;
+    int32_t size;
 
     cspProto->indexes=(const int32_t *)bin;
-    if( cspProto->indexes[UCASE_IX_INDEX_TOP]<16 ||
-        (length>=0 && length<cspProto->indexes[UCASE_IX_LENGTH])
+    if( (length>=0 && length<16*4) ||
+        cspProto->indexes[UCASE_IX_INDEX_TOP]<16
     ) {
+        /* length or indexes[] too short for minimum indexes[] length of 16 */
         *pErrorCode=U_INVALID_FORMAT_ERROR;
         return NULL;
     }
-
-    /* get the trie address, after indexes[] */
     size=cspProto->indexes[UCASE_IX_INDEX_TOP]*4;
-    bin+=size;
-    if(length>=0 && (length-=size)<16) {
-        *pErrorCode=U_INVALID_FORMAT_ERROR;
-        return NULL;
+    if(length>=0) {
+        if(length>=size && length>=cspProto->indexes[UCASE_IX_LENGTH]) {
+            length-=size;
+        } else {
+            /* length too short for indexes[] or for the whole data length */
+            *pErrorCode=U_INVALID_FORMAT_ERROR;
+            return NULL;
+        }
     }
+    bin+=size;
+    /* from here on, assume that the sizes of the items fit into the total length */
 
-    /* unserialize the trie */
-    trieSize=cspProto->indexes[UCASE_IX_TRIE_SIZE];
-    trieSize=utrie_unserialize(&cspProto->trie, bin, length>=0 ? length : trieSize, pErrorCode);
+    /* unserialize the trie, after indexes[] */
+    size=cspProto->indexes[UCASE_IX_TRIE_SIZE];
+    utrie_unserialize(&cspProto->trie, bin, size, pErrorCode);
     if(U_FAILURE(*pErrorCode)) {
         return NULL;
     }
+    bin+=size;
 
     /* get exceptions[] */
-    bin+=trieSize;
-    if(length>=0 && (length-=trieSize)<2*cspProto->indexes[UCASE_IX_EXC_LENGTH]) {
-        *pErrorCode=U_INVALID_FORMAT_ERROR;
-        return NULL;
-    }
+    size=2*cspProto->indexes[UCASE_IX_EXC_LENGTH];
     cspProto->exceptions=(const uint16_t *)bin;
+    bin+=size;
+
+    /* get unfold[] */
+    size=2*cspProto->indexes[UCASE_IX_UNFOLD_LENGTH];
+    if(size!=0) {
+        cspProto->unfold=(const UChar *)bin;
+        bin+=size;
+    } else {
+        cspProto->unfold=NULL;
+    }
 
     /* allocate, copy, and return the new UCaseProps */
     csp=(UCaseProps *)uprv_malloc(sizeof(UCaseProps));
@@ -169,30 +191,46 @@ ucase_openBinary(const uint8_t *bin, int32_t length, UErrorCode *pErrorCode) {
     return ucase_openData(&cspProto, bin, length, pErrorCode);
 }
 
+#endif
+
 U_CAPI void U_EXPORT2
 ucase_close(UCaseProps *csp) {
     if(csp!=NULL) {
+#if !UCASE_HARDCODE_DATA
         udata_close(csp->mem);
+#endif
         uprv_free(csp);
     }
 }
 
 /* UCaseProps singleton ----------------------------------------------------- */
 
-static UCaseProps *gCsp=NULL;
+static UCaseProps *gCsp=NULL, *gCspDummy=NULL;
+#if !UCASE_HARDCODE_DATA
 static UErrorCode gErrorCode=U_ZERO_ERROR;
 static int8_t gHaveData=0;
+#endif
 
 static UBool U_CALLCONV ucase_cleanup(void) {
     ucase_close(gCsp);
     gCsp=NULL;
+    ucase_close(gCspDummy);
+    gCspDummy=NULL;
+#if !UCASE_HARDCODE_DATA
     gErrorCode=U_ZERO_ERROR;
     gHaveData=0;
+#endif
     return TRUE;
 }
 
-U_CAPI UCaseProps * U_EXPORT2
+U_CAPI const UCaseProps * U_EXPORT2
 ucase_getSingleton(UErrorCode *pErrorCode) {
+#if UCASE_HARDCODE_DATA
+    if(U_FAILURE(*pErrorCode)) {
+        return NULL;
+    }
+    return &ucase_props_singleton;
+#else
     int8_t haveData;
 
     if(U_FAILURE(*pErrorCode)) {
@@ -230,107 +268,59 @@ ucase_getSingleton(UErrorCode *pErrorCode) {
         ucase_close(csp);
         return gCsp;
     }
+#endif
 }
 
-/* Unicode case mapping data swapping --------------------------------------- */
-
-U_CAPI int32_t U_EXPORT2
-ucase_swap(const UDataSwapper *ds,
-           const void *inData, int32_t length, void *outData,
-           UErrorCode *pErrorCode) {
-    const UDataInfo *pInfo;
-    int32_t headerSize;
-
-    const uint8_t *inBytes;
-    uint8_t *outBytes;
-
-    const int32_t *inIndexes;
-    int32_t indexes[16];
-
-    int32_t i, offset, count, size;
-
-    /* udata_swapDataHeader checks the arguments */
-    headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
-    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
-        return 0;
-    }
+U_CAPI const UCaseProps * U_EXPORT2
+ucase_getDummy(UErrorCode *pErrorCode) {
+    UCaseProps *csp;
 
-    /* check data format and format version */
-    pInfo=(const UDataInfo *)((const char *)inData+4);
-    if(!(
-        pInfo->dataFormat[0]==UCASE_FMT_0 &&    /* dataFormat="cAsE" */
-        pInfo->dataFormat[1]==UCASE_FMT_1 &&
-        pInfo->dataFormat[2]==UCASE_FMT_2 &&
-        pInfo->dataFormat[3]==UCASE_FMT_3 &&
-        pInfo->formatVersion[0]==1 &&
-        pInfo->formatVersion[2]==UTRIE_SHIFT &&
-        pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT
-    )) {
-        udata_printError(ds, "ucase_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as case mapping data\n",
-                         pInfo->dataFormat[0], pInfo->dataFormat[1],
-                         pInfo->dataFormat[2], pInfo->dataFormat[3],
-                         pInfo->formatVersion[0]);
-        *pErrorCode=U_UNSUPPORTED_ERROR;
-        return 0;
+    if(U_FAILURE(*pErrorCode)) {
+        return NULL;
     }
 
-    inBytes=(const uint8_t *)inData+headerSize;
-    outBytes=(uint8_t *)outData+headerSize;
+    UMTX_CHECK(NULL, gCspDummy, csp);
 
-    inIndexes=(const int32_t *)inBytes;
-
-    if(length>=0) {
-        length-=headerSize;
-        if(length<16*4) {
-            udata_printError(ds, "ucase_swap(): too few bytes (%d after header) for case mapping data\n",
-                             length);
-            *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
-            return 0;
+    if(csp!=NULL) {
+        /* the dummy object was already created */
+        return csp;
+    } else /* csp==NULL */ {
+        /* create the dummy object */
+        int32_t *indexes;
+        
+        csp=(UCaseProps *)uprv_malloc(sizeof(UCaseProps)+UCASE_IX_TOP*4+UTRIE_DUMMY_SIZE);
+        if(csp==NULL) {
+            *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
+            return NULL;
         }
-    }
-
-    /* read the first 16 indexes (ICU 3.2/format version 1: UCASE_IX_TOP==16, might grow) */
-    for(i=0; i<16; ++i) {
-        indexes[i]=udata_readInt32(ds, inIndexes[i]);
-    }
-
-    /* get the total length of the data */
-    size=indexes[UCASE_IX_LENGTH];
+        uprv_memset(csp, 0, sizeof(UCaseProps)+UCASE_IX_TOP*4);
 
-    if(length>=0) {
-        if(length<size) {
-            udata_printError(ds, "ucase_swap(): too few bytes (%d after header) for all of case mapping data\n",
-                             length);
-            *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
-            return 0;
-        }
+        csp->indexes=indexes=(int32_t *)(csp+1);
+        indexes[UCASE_IX_INDEX_TOP]=UCASE_IX_TOP;
 
-        /* copy the data for inaccessible bytes */
-        if(inBytes!=outBytes) {
-            uprv_memcpy(outBytes, inBytes, size);
+        indexes[UCASE_IX_TRIE_SIZE]=
+            utrie_unserializeDummy(&csp->trie, indexes+UCASE_IX_TOP, UTRIE_DUMMY_SIZE, 0, 0, TRUE, pErrorCode);
+        if(U_FAILURE(*pErrorCode)) {
+            uprv_free(csp);
+            return NULL;
         }
 
-        offset=0;
-
-        /* swap the int32_t indexes[] */
-        count=indexes[UCASE_IX_INDEX_TOP]*4;
-        ds->swapArray32(ds, inBytes, count, outBytes, pErrorCode);
-        offset+=count;
-
-        /* swap the UTrie */
-        count=indexes[UCASE_IX_TRIE_SIZE];
-        utrie_swap(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
-        offset+=count;
+        csp->formatVersion[0]=1;
+        csp->formatVersion[2]=UTRIE_SHIFT;
+        csp->formatVersion[3]=UTRIE_INDEX_SHIFT;
 
-        /* swap the uint16_t exceptions[] */
-        count=indexes[UCASE_IX_EXC_LENGTH]*2;
-        ds->swapArray16(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
-        offset+=count;
+        /* set the static variables */
+        umtx_lock(NULL);
+        if(gCspDummy==NULL) {
+            gCspDummy=csp;
+            csp=NULL;
+            ucln_common_registerCleanup(UCLN_COMMON_UCASE, ucase_cleanup);
+        }
+        umtx_unlock(NULL);
 
-        U_ASSERT(offset==size);
+        uprv_free(csp);
+        return gCspDummy;
     }
-
-    return headerSize+size;
 }
 
 /* set of property starts for UnicodeSet ------------------------------------ */
@@ -338,13 +328,13 @@ ucase_swap(const UDataSwapper *ds,
 static UBool U_CALLCONV
 _enumPropertyStartsRange(const void *context, UChar32 start, UChar32 limit, uint32_t value) {
     /* add the start code point to the USet */
-    USetAdder *sa=(USetAdder *)context;
+    const USetAdder *sa=(const USetAdder *)context;
     sa->add(sa->set, start);
     return TRUE;
 }
 
 U_CAPI void U_EXPORT2
-ucase_addPropertyStarts(const UCaseProps *csp, USetAdder *sa, UErrorCode *pErrorCode) {
+ucase_addPropertyStarts(const UCaseProps *csp, const USetAdder *sa, UErrorCode *pErrorCode) {
     if(U_FAILURE(*pErrorCode)) {
         return;
     }
@@ -368,8 +358,6 @@ ucase_addPropertyStarts(const UCaseProps *csp, USetAdder *sa, UErrorCode *pError
 #define GET_PROPS(csp, c, result) \
     UTRIE_GET16(&(csp)->trie, c, result);
 
-#define GET_CASE_TYPE(props) ((props)&UCASE_TYPE_MASK)
-#define GET_SIGNED_DELTA(props) ((int16_t)(props)>>UCASE_DELTA_SHIFT)
 #define GET_EXCEPTIONS(csp, props) ((csp)->exceptions+((props)>>UCASE_EXC_SHIFT))
 
 #define PROPS_HAS_EXCEPTION(props) ((props)&UCASE_EXCEPTION)
@@ -423,8 +411,8 @@ ucase_tolower(const UCaseProps *csp, UChar32 c) {
     uint16_t props;
     GET_PROPS(csp, c, props);
     if(!PROPS_HAS_EXCEPTION(props)) {
-        if(GET_CASE_TYPE(props)>=UCASE_UPPER) {
-            c+=GET_SIGNED_DELTA(props);
+        if(UCASE_GET_TYPE(props)>=UCASE_UPPER) {
+            c+=UCASE_GET_DELTA(props);
         }
     } else {
         const uint16_t *pe=GET_EXCEPTIONS(csp, props);
@@ -441,8 +429,8 @@ ucase_toupper(const UCaseProps *csp, UChar32 c) {
     uint16_t props;
     GET_PROPS(csp, c, props);
     if(!PROPS_HAS_EXCEPTION(props)) {
-        if(GET_CASE_TYPE(props)==UCASE_LOWER) {
-            c+=GET_SIGNED_DELTA(props);
+        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
+            c+=UCASE_GET_DELTA(props);
         }
     } else {
         const uint16_t *pe=GET_EXCEPTIONS(csp, props);
@@ -459,8 +447,8 @@ ucase_totitle(const UCaseProps *csp, UChar32 c) {
     uint16_t props;
     GET_PROPS(csp, c, props);
     if(!PROPS_HAS_EXCEPTION(props)) {
-        if(GET_CASE_TYPE(props)==UCASE_LOWER) {
-            c+=GET_SIGNED_DELTA(props);
+        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
+            c+=UCASE_GET_DELTA(props);
         }
     } else {
         const uint16_t *pe=GET_EXCEPTIONS(csp, props);
@@ -478,12 +466,212 @@ ucase_totitle(const UCaseProps *csp, UChar32 c) {
     return c;
 }
 
+U_CAPI void U_EXPORT2
+ucase_addCaseClosure(const UCaseProps *csp, UChar32 c, const USetAdder *sa) {
+    uint16_t props;
+
+    /*
+     * Hardcode the case closure of i and its relatives and ignore the
+     * data file data for these characters.
+     * The Turkic dotless i and dotted I with their case mapping conditions
+     * and case folding option make the related characters behave specially.
+     * This code matches their closure behavior to their case folding behavior.
+     */
+    static const UChar
+        iDot[2]=        { 0x69, 0x307 };
+
+    switch(c) {
+    case 0x49:
+        /* regular i and I are in one equivalence class */
+        sa->add(sa->set, 0x69);
+        return;
+    case 0x69:
+        sa->add(sa->set, 0x49);
+        return;
+    case 0x130:
+        /* dotted I is in a class with <0069 0307> (for canonical equivalence with <0049 0307>) */
+        sa->addString(sa->set, iDot, 2);
+        return;
+    case 0x131:
+        /* dotless i is in a class by itself */
+        return;
+    default:
+        /* otherwise use the data file data */
+        break;
+    }
+
+    GET_PROPS(csp, c, props);
+    if(!PROPS_HAS_EXCEPTION(props)) {
+        if(UCASE_GET_TYPE(props)!=UCASE_NONE) {
+            /* add the one simple case mapping, no matter what type it is */
+            int32_t delta=UCASE_GET_DELTA(props);
+            if(delta!=0) {
+                sa->add(sa->set, c+delta);
+            }
+        }
+    } else {
+        /*
+         * c has exceptions, so there may be multiple simple and/or
+         * full case mappings. Add them all.
+         */
+        const uint16_t *pe0, *pe=GET_EXCEPTIONS(csp, props);
+        const UChar *closure;
+        uint16_t excWord=*pe++;
+        int32_t index, closureLength, fullLength, length;
+
+        pe0=pe;
+
+        /* add all simple case mappings */
+        for(index=UCASE_EXC_LOWER; index<=UCASE_EXC_TITLE; ++index) {
+            if(HAS_SLOT(excWord, index)) {
+                pe=pe0;
+                GET_SLOT_VALUE(excWord, index, pe, c);
+                sa->add(sa->set, c);
+            }
+        }
+
+        /* get the closure string pointer & length */
+        if(HAS_SLOT(excWord, UCASE_EXC_CLOSURE)) {
+            pe=pe0;
+            GET_SLOT_VALUE(excWord, UCASE_EXC_CLOSURE, pe, closureLength);
+            closureLength&=UCASE_CLOSURE_MAX_LENGTH; /* higher bits are reserved */
+            closure=(const UChar *)pe+1; /* behind this slot, unless there are full case mappings */
+        } else {
+            closureLength=0;
+            closure=NULL;
+        }
+
+        /* add the full case folding */
+        if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
+            pe=pe0;
+            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, fullLength);
+
+            /* start of full case mapping strings */
+            ++pe;
+
+            fullLength&=0xffff; /* bits 16 and higher are reserved */
+
+            /* skip the lowercase result string */
+            pe+=fullLength&UCASE_FULL_LOWER;
+            fullLength>>=4;
+
+            /* add the full case folding string */
+            length=fullLength&0xf;
+            if(length!=0) {
+                sa->addString(sa->set, (const UChar *)pe, length);
+                pe+=length;
+            }
+
+            /* skip the uppercase and titlecase strings */
+            fullLength>>=4;
+            pe+=fullLength&0xf;
+            fullLength>>=4;
+            pe+=fullLength;
+
+            closure=(const UChar *)pe; /* behind full case mappings */
+        }
+
+        /* add each code point in the closure string */
+        for(index=0; index<closureLength;) {
+            U16_NEXT_UNSAFE(closure, index, c);
+            sa->add(sa->set, c);
+        }
+    }
+}
+
+/*
+ * compare s, which has a length, with t, which has a maximum length or is NUL-terminated
+ * must be length>0 and max>0 and length<=max
+ */
+static U_INLINE int32_t
+strcmpMax(const UChar *s, int32_t length, const UChar *t, int32_t max) {
+    int32_t c1, c2;
+
+    max-=length; /* we require length<=max, so no need to decrement max in the loop */
+    do {
+        c1=*s++;
+        c2=*t++;
+        if(c2==0) {
+            return 1; /* reached the end of t but not of s */
+        }
+        c1-=c2;
+        if(c1!=0) {
+            return c1; /* return difference result */
+        }
+    } while(--length>0);
+    /* ends with length==0 */
+
+    if(max==0 || *t==0) {
+        return 0; /* equal to length of both strings */
+    } else {
+        return -max; /* return lengh difference */
+    }
+}
+
+U_CAPI UBool U_EXPORT2
+ucase_addStringCaseClosure(const UCaseProps *csp, const UChar *s, int32_t length, const USetAdder *sa) {
+    const UChar *unfold, *p;
+    int32_t i, start, limit, result, unfoldRows, unfoldRowWidth, unfoldStringWidth;
+
+    if(csp->unfold==NULL || s==NULL) {
+        return FALSE; /* no reverse case folding data, or no string */
+    }
+    if(length<=1) {
+        /* the string is too short to find any match */
+        /*
+         * more precise would be:
+         * if(!u_strHasMoreChar32Than(s, length, 1))
+         * but this does not make much practical difference because
+         * a single supplementary code point would just not be found
+         */
+        return FALSE;
+    }
+
+    unfold=csp->unfold;
+    unfoldRows=unfold[UCASE_UNFOLD_ROWS];
+    unfoldRowWidth=unfold[UCASE_UNFOLD_ROW_WIDTH];
+    unfoldStringWidth=unfold[UCASE_UNFOLD_STRING_WIDTH];
+    unfold+=unfoldRowWidth;
+
+    if(length>unfoldStringWidth) {
+        /* the string is too long to find any match */
+        return FALSE;
+    }
+
+    /* do a binary search for the string */
+    start=0;
+    limit=unfoldRows;
+    while(start<limit) {
+        i=(start+limit)/2;
+        p=unfold+(i*unfoldRowWidth);
+        result=strcmpMax(s, length, p, unfoldStringWidth);
+
+        if(result==0) {
+            /* found the string: add each code point, and its case closure */
+            UChar32 c;
+
+            for(i=unfoldStringWidth; i<unfoldRowWidth && p[i]!=0;) {
+                U16_NEXT_UNSAFE(p, i, c);
+                sa->add(sa->set, c);
+                ucase_addCaseClosure(csp, c, sa);
+            }
+            return TRUE;
+        } else if(result<0) {
+            limit=i;
+        } else /* result>0 */ {
+            start=i+1;
+        }
+    }
+
+    return FALSE; /* string not found */
+}
+
 /** @return UCASE_NONE, UCASE_LOWER, UCASE_UPPER, UCASE_TITLE */
 U_CAPI int32_t U_EXPORT2
 ucase_getType(const UCaseProps *csp, UChar32 c) {
     uint16_t props;
     GET_PROPS(csp, c, props);
-    return GET_CASE_TYPE(props);
+    return UCASE_GET_TYPE(props);
 }
 
 /** @return same as ucase_getType(), or <0 if c is case-ignorable */
@@ -492,7 +680,7 @@ ucase_getTypeOrIgnorable(const UCaseProps *csp, UChar32 c) {
     int32_t type;
     uint16_t props;
     GET_PROPS(csp, c, props);
-    type=GET_CASE_TYPE(props);
+    type=UCASE_GET_TYPE(props);
     if(type!=UCASE_NONE) {
         return type;
     } else if(
@@ -530,70 +718,6 @@ ucase_isCaseSensitive(const UCaseProps *csp, UChar32 c) {
     return (UBool)((props&UCASE_SENSITIVE)!=0);
 }
 
-/* public API (see uchar.h) ------------------------------------------------- */
-
-U_CAPI UBool U_EXPORT2
-u_isULowercase(UChar32 c) {
-    UErrorCode errorCode=U_ZERO_ERROR;
-    UCaseProps *csp=ucase_getSingleton(&errorCode);
-    return (UBool)(csp!=NULL && UCASE_LOWER==ucase_getType(csp, c));
-}
-
-U_CAPI UBool U_EXPORT2
-u_isUUppercase(UChar32 c) {
-    UErrorCode errorCode=U_ZERO_ERROR;
-    UCaseProps *csp=ucase_getSingleton(&errorCode);
-    return (UBool)(csp!=NULL && UCASE_UPPER==ucase_getType(csp, c));
-}
-
-/* Transforms the Unicode character to its lower case equivalent.*/
-U_CAPI UChar32 U_EXPORT2
-u_tolower(UChar32 c) {
-    UErrorCode errorCode=U_ZERO_ERROR;
-    UCaseProps *csp=ucase_getSingleton(&errorCode);
-    if(csp!=NULL) {
-        return ucase_tolower(csp, c);
-    } else {
-        return c;
-    }
-}
-    
-/* Transforms the Unicode character to its upper case equivalent.*/
-U_CAPI UChar32 U_EXPORT2
-u_toupper(UChar32 c) {
-    UErrorCode errorCode=U_ZERO_ERROR;
-    UCaseProps *csp=ucase_getSingleton(&errorCode);
-    if(csp!=NULL) {
-        return ucase_toupper(csp, c);
-    } else {
-        return c;
-    }
-}
-
-/* Transforms the Unicode character to its title case equivalent.*/
-U_CAPI UChar32 U_EXPORT2
-u_totitle(UChar32 c) {
-    UErrorCode errorCode=U_ZERO_ERROR;
-    UCaseProps *csp=ucase_getSingleton(&errorCode);
-    if(csp!=NULL) {
-        return ucase_totitle(csp, c);
-    } else {
-        return c;
-    }
-}
-
-/* return the simple case folding mapping for c */
-U_CAPI UChar32 U_EXPORT2
-u_foldCase(UChar32 c, uint32_t options) {
-    UErrorCode errorCode=U_ZERO_ERROR;
-    UCaseProps *csp=ucase_getSingleton(&errorCode);
-    if(csp!=NULL) {
-        return ucase_fold(csp, c, options);
-    } else {
-        return c;
-    }
-}
-
 /* string casing ------------------------------------------------------------ */
 
 /*
@@ -693,13 +817,13 @@ enum {
 /* separator? */
 #define is_sep(c) ((c)=='_' || (c)=='-' || (c)==0)
 
-/*
+/**
  * Requires non-NULL locale ID but otherwise does the equivalent of
  * checking for language codes as if uloc_getLanguage() were called:
  * Accepts both 2- and 3-letter codes and accepts case variants.
  */
-static int32_t
-getCaseLocale(const char *locale, int32_t *locCache) {
+U_CFUNC int32_t
+ucase_getCaseLocale(const char *locale, int32_t *locCache) {
     int32_t result;
     char c;
 
@@ -775,7 +899,7 @@ isFollowedByCasedLetter(const UCaseProps *csp, UCaseContextIterator *iter, void
 
     for(/* dir!=0 sets direction */; (c=iter(context, dir))>=0; dir=0) {
         GET_PROPS(csp, c, props);
-        if(GET_CASE_TYPE(props)!=UCASE_NONE) {
+        if(UCASE_GET_TYPE(props)!=UCASE_NONE) {
             return TRUE; /* followed by cased letter */
         } else if(c==0x307 || (props&(UCASE_EXCEPTION|UCASE_CASE_IGNORABLE))==UCASE_CASE_IGNORABLE) {
             /* case-ignorable, continue with the loop */
@@ -934,8 +1058,8 @@ ucase_toFullLower(const UCaseProps *csp, UChar32 c,
     result=c;
     GET_PROPS(csp, c, props);
     if(!PROPS_HAS_EXCEPTION(props)) {
-        if(GET_CASE_TYPE(props)>=UCASE_UPPER) {
-            result=c+GET_SIGNED_DELTA(props);
+        if(UCASE_GET_TYPE(props)>=UCASE_UPPER) {
+            result=c+UCASE_GET_DELTA(props);
         }
     } else {
         const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2;
@@ -946,7 +1070,7 @@ ucase_toFullLower(const UCaseProps *csp, UChar32 c,
 
         if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) {
             /* use hardcoded conditions and mappings */
-            int32_t loc=getCaseLocale(locale, locCache);
+            int32_t loc=ucase_getCaseLocale(locale, locCache);
 
             /*
              * Test for conditional mappings first
@@ -1081,8 +1205,8 @@ toUpperOrTitle(const UCaseProps *csp, UChar32 c,
     result=c;
     GET_PROPS(csp, c, props);
     if(!PROPS_HAS_EXCEPTION(props)) {
-        if(GET_CASE_TYPE(props)==UCASE_LOWER) {
-            result=c+GET_SIGNED_DELTA(props);
+        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
+            result=c+UCASE_GET_DELTA(props);
         }
     } else {
         const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2;
@@ -1093,7 +1217,7 @@ toUpperOrTitle(const UCaseProps *csp, UChar32 c,
 
         if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) {
             /* use hardcoded conditions and mappings */
-            int32_t loc=getCaseLocale(locale, locCache);
+            int32_t loc=ucase_getCaseLocale(locale, locCache);
 
             if(loc==LOC_TURKISH && c==0x69) {
                 /*
@@ -1219,25 +1343,17 @@ ucase_toFullTitle(const UCaseProps *csp, UChar32 c,
 0049; C; 0069; # LATIN CAPITAL LETTER I
 0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
 
- * U+0130 is otherwise lowercased to U+0069 (UnicodeData.txt).
- *
- * In case this code is used with CaseFolding.txt from an older version of Unicode
- * where CaseFolding.txt contains mappings with a status of 'I' that
- * have the opposite polarity ('I' mappings are included by default but excluded for Turkic),
- * we must also hardcode the Unicode 3.2 mappings for the code points 
- * with 'I' mappings.
- * Unicode 3.1.1 has 'I' mappings for U+0130 and U+0131.
- * Unicode 3.2 has a 'T' mapping for U+0130, and lowercases U+0131 to itself (see UnicodeData.txt).
+ * U+0130 has no simple case folding (simple-case-folds to itself).
  */
 
 /* return the simple case folding mapping for c */
 U_CAPI UChar32 U_EXPORT2
-ucase_fold(UCaseProps *csp, UChar32 c, uint32_t options) {
+ucase_fold(const UCaseProps *csp, UChar32 c, uint32_t options) {
     uint16_t props;
     GET_PROPS(csp, c, props);
     if(!PROPS_HAS_EXCEPTION(props)) {
-        if(GET_CASE_TYPE(props)>=UCASE_UPPER) {
-            c+=GET_SIGNED_DELTA(props);
+        if(UCASE_GET_TYPE(props)>=UCASE_UPPER) {
+            c+=UCASE_GET_DELTA(props);
         }
     } else {
         const uint16_t *pe=GET_EXCEPTIONS(csp, props);
@@ -1251,8 +1367,8 @@ ucase_fold(UCaseProps *csp, UChar32 c, uint32_t options) {
                     /* 0049; C; 0069; # LATIN CAPITAL LETTER I */
                     return 0x69;
                 } else if(c==0x130) {
-                    /* no simple default mapping for U+0130, use UnicodeData.txt */
-                    return 0x69;
+                    /* no simple case folding for U+0130 */
+                    return c;
                 }
             } else {
                 /* Turkic mappings */
@@ -1305,8 +1421,8 @@ ucase_toFullFolding(const UCaseProps *csp, UChar32 c,
     result=c;
     GET_PROPS(csp, c, props);
     if(!PROPS_HAS_EXCEPTION(props)) {
-        if(GET_CASE_TYPE(props)>=UCASE_UPPER) {
-            result=c+GET_SIGNED_DELTA(props);
+        if(UCASE_GET_TYPE(props)>=UCASE_UPPER) {
+            result=c+UCASE_GET_DELTA(props);
         }
     } else {
         const uint16_t *pe=GET_EXCEPTIONS(csp, props), *pe2;
@@ -1368,3 +1484,116 @@ ucase_toFullFolding(const UCaseProps *csp, UChar32 c,
 
     return (result==c) ? ~result : result;
 }
+
+/* case mapping properties API ---------------------------------------------- */
+
+/* get the UCaseProps singleton, or else its dummy, once and for all */
+static const UCaseProps *
+getCaseProps() {
+    /*
+     * This lazy intialization with double-checked locking (without mutex protection for
+     * the initial check) is transiently unsafe under certain circumstances.
+     * Check the readme and use u_init() if necessary.
+     */
+
+    /* the initial check is performed by the GET_CASE_PROPS() macro */
+    const UCaseProps *csp;
+    UErrorCode errorCode=U_ZERO_ERROR;
+
+    csp=ucase_getSingleton(&errorCode);
+    if(U_FAILURE(errorCode)) {
+        errorCode=U_ZERO_ERROR;
+        csp=ucase_getDummy(&errorCode);
+        if(U_FAILURE(errorCode)) {
+            return NULL;
+        }
+    }
+
+    return csp;
+}
+
+/*
+ * In ICU 3.0, most Unicode properties were loaded from uprops.icu.
+ * ICU 3.2 adds ucase.icu for case mapping properties.
+ * ICU 3.4 adds ubidi.icu for bidi/shaping properties and
+ * removes case/bidi/shaping properties from uprops.icu.
+ *
+ * Loading of uprops.icu was never mutex-protected and required u_init()
+ * for thread safety.
+ * In order to maintain performance for all such properties,
+ * ucase.icu and ubidi.icu are loaded lazily, without mutexing.
+ * u_init() will try to load them for thread safety,
+ * but u_init() will not fail if they are missing.
+ *
+ * uchar.c maintains a tri-state flag for (not loaded/loaded/failed to load)
+ * and an error code for load failure.
+ * Instead, here we try to load at most once.
+ * If it works, we use the resulting singleton object.
+ * If it fails, then we get a dummy object, which always works unless
+ * we are seriously out of memory.
+ * After the first try, we have a never-changing pointer to either the
+ * real singleton or the dummy.
+ *
+ * This method is used in Unicode properties APIs (uchar.h) that
+ * do not have a service object and also do not have an error code parameter.
+ * Other API implementations get the singleton themselves
+ * (with mutexing), store it in the service object, and report errors.
+ */
+#define GET_CASE_PROPS() (gCsp!=NULL ? gCsp : getCaseProps())
+
+/* public API (see uchar.h) */
+
+U_CAPI UBool U_EXPORT2
+u_isULowercase(UChar32 c) {
+    return (UBool)(UCASE_LOWER==ucase_getType(GET_CASE_PROPS(), c));
+}
+
+U_CAPI UBool U_EXPORT2
+u_isUUppercase(UChar32 c) {
+    return (UBool)(UCASE_UPPER==ucase_getType(GET_CASE_PROPS(), c));
+}
+
+/* Transforms the Unicode character to its lower case equivalent.*/
+U_CAPI UChar32 U_EXPORT2
+u_tolower(UChar32 c) {
+    return ucase_tolower(GET_CASE_PROPS(), c);
+}
+    
+/* Transforms the Unicode character to its upper case equivalent.*/
+U_CAPI UChar32 U_EXPORT2
+u_toupper(UChar32 c) {
+    return ucase_toupper(GET_CASE_PROPS(), c);
+}
+
+/* Transforms the Unicode character to its title case equivalent.*/
+U_CAPI UChar32 U_EXPORT2
+u_totitle(UChar32 c) {
+    return ucase_totitle(GET_CASE_PROPS(), c);
+}
+
+/* return the simple case folding mapping for c */
+U_CAPI UChar32 U_EXPORT2
+u_foldCase(UChar32 c, uint32_t options) {
+    return ucase_fold(GET_CASE_PROPS(), c, options);
+}
+
+U_CFUNC int32_t U_EXPORT2
+ucase_hasBinaryProperty(UChar32 c, UProperty which) {
+    /* case mapping properties */
+    const UCaseProps *csp=GET_CASE_PROPS();
+    if(csp==NULL) {
+        return FALSE;
+    }
+    switch(which) {
+    case UCHAR_LOWERCASE:
+        return (UBool)(UCASE_LOWER==ucase_getType(csp, c));
+    case UCHAR_UPPERCASE:
+        return (UBool)(UCASE_UPPER==ucase_getType(csp, c));
+    case UCHAR_SOFT_DOTTED:
+        return ucase_isSoftDotted(csp, c);
+    case UCHAR_CASE_SENSITIVE:
+        return ucase_isCaseSensitive(csp, c);
+    default:
+        return FALSE;
+    }
+}