]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/usprep.cpp
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / common / usprep.cpp
index 93832e9652150ca7fb83f79a8a36f2dae796ffae..d96e825c24d55b7ecc985dcd9dd4f081c4bee60c 100644 (file)
@@ -1,12 +1,14 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
  *******************************************************************************
  *
- *   Copyright (C) 2003-2012, International Business Machines
+ *   Copyright (C) 2003-2016, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  *
  *******************************************************************************
  *   file name:  usprep.cpp
- *   encoding:   US-ASCII
+ *   encoding:   UTF-8
  *   tab size:   8 (not used)
  *   indentation:4
  *
@@ -20,7 +22,7 @@
 
 #include "unicode/usprep.h"
 
-#include "unicode/unorm.h"
+#include "unicode/normalizer2.h"
 #include "unicode/ustring.h"
 #include "unicode/uchar.h"
 #include "unicode/uversion.h"
@@ -33,6 +35,7 @@
 #include "udataswp.h"
 #include "ucln_cmn.h"
 #include "ubidi_props.h"
+#include "uprops.h"
 
 U_NAMESPACE_USE
 
@@ -42,6 +45,7 @@ U_CDECL_BEGIN
 Static cache for already opened StringPrep profiles
 */
 static UHashtable *SHARED_DATA_HASHTABLE = NULL;
+static icu::UInitOnce gSharedDataInitOnce;
 
 static UMutex usprepMutex = U_MUTEX_INITIALIZER;
 
@@ -134,7 +138,7 @@ static int32_t
 usprep_internal_flushCache(UBool noRefCount){
     UStringPrepProfile *profile = NULL;
     UStringPrepKey  *key  = NULL;
-    int32_t pos = -1;
+    int32_t pos = UHASH_FIRST;
     int32_t deletedNum = 0;
     const UHashElement *e;
 
@@ -195,32 +199,25 @@ static UBool U_CALLCONV usprep_cleanup(void){
             SHARED_DATA_HASHTABLE = NULL;
         }
     }
-
+    gSharedDataInitOnce.reset();
     return (SHARED_DATA_HASHTABLE == NULL);
 }
 U_CDECL_END
 
 
 /** Initializes the cache for resources */
+static void U_CALLCONV
+createCache(UErrorCode &status) {
+    SHARED_DATA_HASHTABLE = uhash_open(hashEntry, compareEntries, NULL, &status);
+    if (U_FAILURE(status)) {
+        SHARED_DATA_HASHTABLE = NULL;
+    }
+    ucln_common_registerCleanup(UCLN_COMMON_USPREP, usprep_cleanup);
+}
+
 static void 
 initCache(UErrorCode *status) {
-    UBool makeCache;
-    UMTX_CHECK(&usprepMutex, (SHARED_DATA_HASHTABLE ==  NULL), makeCache);
-    if(makeCache) {
-        UHashtable *newCache = uhash_open(hashEntry, compareEntries, NULL, status);
-        if (U_SUCCESS(*status)) {
-            umtx_lock(&usprepMutex);
-            if(SHARED_DATA_HASHTABLE == NULL) {
-                SHARED_DATA_HASHTABLE = newCache;
-                ucln_common_registerCleanup(UCLN_COMMON_USPREP, usprep_cleanup);
-                newCache = NULL;
-            }
-            umtx_unlock(&usprepMutex);
-        }
-        if(newCache != NULL) {
-            uhash_close(newCache);
-        }
-    }
+    umtx_initOnce(gSharedDataInitOnce, &createCache, *status);
 }
 
 static UBool U_CALLCONV
@@ -414,7 +411,7 @@ usprep_openByType(UStringPrepProfileType type,
         return NULL;
     }
     int32_t index = (int32_t)type;
-    if (index < 0 || index >= (int32_t)(sizeof(PROFILE_NAMES)/sizeof(PROFILE_NAMES[0]))) {
+    if (index < 0 || index >= UPRV_LENGTHOF(PROFILE_NAMES)) {
         *status = U_ILLEGAL_ARGUMENT_ERROR;
         return NULL;
     }
@@ -508,8 +505,7 @@ getValues(uint16_t trieWord, int16_t& value, UBool& isIndex){
     return type;
 }
 
-
-
+// TODO: change to writing to UnicodeString not UChar *
 static int32_t 
 usprep_map(  const UStringPrepProfile* profile, 
              const UChar* src, int32_t srcLength, 
@@ -604,20 +600,7 @@ usprep_map(  const UStringPrepProfile* profile,
     return u_terminateUChars(dest, destCapacity, destIndex, status);
 }
 
-
-static int32_t 
-usprep_normalize(   const UChar* src, int32_t srcLength, 
-                    UChar* dest, int32_t destCapacity,
-                    UErrorCode* status ){
-    return unorm_normalize(
-        src, srcLength,
-        UNORM_NFKC, UNORM_UNICODE_3_2,
-        dest, destCapacity,
-        status);
-}
-
-
- /*
+/*
    1) Map -- For each character in the input, check if it has a mapping
       and, if so, replace it with its mapping.  
 
@@ -655,10 +638,6 @@ usprep_normalize(   const UChar* src, int32_t srcLength,
           character MUST be the first character of the string, and a
           RandALCat character MUST be the last character of the string.
 */
-
-#define MAX_STACK_BUFFER_SIZE 300
-
-
 U_CAPI int32_t U_EXPORT2
 usprep_prepare(   const UStringPrepProfile* profile,
                   const UChar* src, int32_t srcLength, 
@@ -668,100 +647,91 @@ usprep_prepare(   const UStringPrepProfile* profile,
                   UErrorCode* status ){
 
     // check error status
-    if(status == NULL || U_FAILURE(*status)){
+    if(U_FAILURE(*status)){
         return 0;
     }
-    
+
     //check arguments
-    if(profile==NULL || src==NULL || srcLength<-1 || (dest==NULL && destCapacity!=0)) {
+    if(profile==NULL ||
+            (src==NULL ? srcLength!=0 : srcLength<-1) ||
+            (dest==NULL ? destCapacity!=0 : destCapacity<0)) {
         *status=U_ILLEGAL_ARGUMENT_ERROR;
         return 0;
     }
 
-    UChar b1Stack[MAX_STACK_BUFFER_SIZE], b2Stack[MAX_STACK_BUFFER_SIZE];
-    UChar *b1 = b1Stack, *b2 = b2Stack;
-    int32_t b1Len, b2Len=0,
-            b1Capacity = MAX_STACK_BUFFER_SIZE , 
-            b2Capacity = MAX_STACK_BUFFER_SIZE;
-    uint16_t result;
-    int32_t b2Index = 0;
-    UCharDirection direction=U_CHAR_DIRECTION_COUNT, firstCharDir=U_CHAR_DIRECTION_COUNT;
-    UBool leftToRight=FALSE, rightToLeft=FALSE;
-    int32_t rtlPos =-1, ltrPos =-1;
-
     //get the string length
-    if(srcLength == -1){
+    if(srcLength < 0){
         srcLength = u_strlen(src);
     }
     // map
-    b1Len = usprep_map(profile, src, srcLength, b1, b1Capacity, options, parseError, status);
+    UnicodeString s1;
+    UChar *b1 = s1.getBuffer(srcLength);
+    if(b1==NULL){
+        *status = U_MEMORY_ALLOCATION_ERROR;
+        return 0;
+    }
+    int32_t b1Len = usprep_map(profile, src, srcLength,
+                               b1, s1.getCapacity(), options, parseError, status);
+    s1.releaseBuffer(U_SUCCESS(*status) ? b1Len : 0);
 
     if(*status == U_BUFFER_OVERFLOW_ERROR){
         // redo processing of string
         /* we do not have enough room so grow the buffer*/
-        b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
+        b1 = s1.getBuffer(b1Len);
         if(b1==NULL){
             *status = U_MEMORY_ALLOCATION_ERROR;
-            goto CLEANUP;
+            return 0;
         }
 
         *status = U_ZERO_ERROR; // reset error
-        
-        b1Len = usprep_map(profile, src, srcLength, b1, b1Len, options, parseError, status);
-        
+        b1Len = usprep_map(profile, src, srcLength,
+                           b1, s1.getCapacity(), options, parseError, status);
+        s1.releaseBuffer(U_SUCCESS(*status) ? b1Len : 0);
+    }
+    if(U_FAILURE(*status)){
+        return 0;
     }
 
     // normalize
-    if(profile->doNFKC == TRUE){
-        b2Len = usprep_normalize(b1,b1Len, b2,b2Capacity,status);
-        
-        if(*status == U_BUFFER_OVERFLOW_ERROR){
-            // redo processing of string
-            /* we do not have enough room so grow the buffer*/
-            b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
-            if(b2==NULL){
-                *status = U_MEMORY_ALLOCATION_ERROR;
-                goto CLEANUP;
-            }
-
-            *status = U_ZERO_ERROR; // reset error
-        
-            b2Len = usprep_normalize(b1,b1Len, b2,b2Len,status);
-        
+    UnicodeString s2;
+    if(profile->doNFKC){
+        const Normalizer2 *n2 = Normalizer2::getNFKCInstance(*status);
+        FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*status));
+        if(U_FAILURE(*status)){
+            return 0;
         }
-
+        fn2.normalize(s1, s2, *status);
     }else{
-        b2 = b1;
-        b2Len = b1Len;
+        s2.fastCopyFrom(s1);
     }
-    
-
     if(U_FAILURE(*status)){
-        goto CLEANUP;
+        return 0;
     }
 
-    UChar32 ch;
-    UStringPrepType type;
-    int16_t value;
-    UBool isIndex;
-    
     // Prohibit and checkBiDi in one pass
-    for(b2Index=0; b2Index<b2Len;){
-        
-        ch = 0;
+    const UChar *b2 = s2.getBuffer();
+    int32_t b2Len = s2.length();
+    UCharDirection direction=U_CHAR_DIRECTION_COUNT, firstCharDir=U_CHAR_DIRECTION_COUNT;
+    UBool leftToRight=FALSE, rightToLeft=FALSE;
+    int32_t rtlPos =-1, ltrPos =-1;
 
+    for(int32_t b2Index=0; b2Index<b2Len;){
+        UChar32 ch = 0;
         U16_NEXT(b2, b2Index, b2Len, ch);
 
+        uint16_t result;
         UTRIE_GET16(&profile->sprepTrie,ch,result);
-        
-        type = getValues(result, value, isIndex);
+
+        int16_t value;
+        UBool isIndex;
+        UStringPrepType type = getValues(result, value, isIndex);
 
         if( type == USPREP_PROHIBITED || 
             ((result < _SPREP_TYPE_THRESHOLD) && (result & 0x01) /* first bit says it the code point is prohibited*/)
            ){
             *status = U_STRINGPREP_PROHIBITED_ERROR;
             uprv_syntaxError(b1, b2Index-U16_LENGTH(ch), b2Len, parseError);
-            goto CLEANUP;
+            return 0;
         }
 
         if(profile->checkBiDi) {
@@ -778,13 +748,13 @@ usprep_prepare(   const UStringPrepProfile* profile,
                 rtlPos = b2Index-1;
             }
         }
-    }           
+    }
     if(profile->checkBiDi == TRUE){
         // satisfy 2
         if( leftToRight == TRUE && rightToLeft == TRUE){
             *status = U_STRINGPREP_CHECK_BIDI_ERROR;
             uprv_syntaxError(b2,(rtlPos>ltrPos) ? rtlPos : ltrPos, b2Len, parseError);
-            goto CLEANUP;
+            return 0;
         }
 
         //satisfy 3
@@ -797,21 +767,7 @@ usprep_prepare(   const UStringPrepProfile* profile,
             return FALSE;
         }
     }
-    if(b2Len>0 && b2Len <= destCapacity){
-        uprv_memmove(dest,b2, b2Len*U_SIZEOF_UCHAR);
-    }
-
-CLEANUP:
-    if(b1!=b1Stack){
-        uprv_free(b1);
-        b1=NULL;
-    }
-
-    if(b2!=b1Stack && b2!=b2Stack && b2!=b1 /* b1 should not be freed twice */){
-        uprv_free(b2);
-        b2=NULL;
-    }
-    return u_terminateUChars(dest, destCapacity, b2Len, status);
+    return s2.extract(dest, destCapacity, *status);
 }
 
 
@@ -909,7 +865,7 @@ usprep_swap(const UDataSwapper *ds,
         /* swap the uint16_t mappingTable[] */
         count=indexes[_SPREP_INDEX_MAPPING_DATA_SIZE];
         ds->swapArray16(ds, inBytes+offset, count, outBytes+offset, pErrorCode);
-        offset+=count;
+        //offset+=count;
     }
 
     return headerSize+size;