]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/usprep.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / common / usprep.cpp
index 9e5d70c2668ac722c7fb50e6df8f9390a3b5f555..8351a773706c7ad7b08e7dc5756e52d770e834f3 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-2013, 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,10 +45,9 @@ 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;
+static icu::UInitOnce gSharedDataInitOnce = U_INITONCE_INITIALIZER;
 
+static UMutex usprepMutex;
 /* format version of spp file */
 //static uint8_t formatVersion[4]={ 0, 0, 0, 0 };
 
@@ -109,7 +111,9 @@ hashEntry(const UHashTok parm) {
     UHashTok namekey, pathkey;
     namekey.pointer = b->name;
     pathkey.pointer = b->path;
-    return uhash_hashChars(namekey)+37*uhash_hashChars(pathkey);
+    uint32_t unsignedHash = static_cast<uint32_t>(uhash_hashChars(namekey)) +
+            37u * static_cast<uint32_t>(uhash_hashChars(pathkey));
+    return static_cast<int32_t>(unsignedHash);
 }
 
 /* compares two entries */
@@ -135,7 +139,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;
 
@@ -344,17 +348,13 @@ usprep_getProfile(const char* path,
         newProfile->doNFKC = (UBool)((newProfile->indexes[_SPREP_OPTIONS] & _SPREP_NORMALIZATION_ON) > 0);
         newProfile->checkBiDi = (UBool)((newProfile->indexes[_SPREP_OPTIONS] & _SPREP_CHECK_BIDI_ON) > 0);
 
-        if(newProfile->checkBiDi) {
-            newProfile->bdp = ubidi_getSingleton();
-        }
-
         LocalMemory<UStringPrepKey> key;
         LocalMemory<char> keyName;
         LocalMemory<char> keyPath;
         if( key.allocateInsteadAndReset() == NULL ||
-            keyName.allocateInsteadAndCopy(uprv_strlen(name)+1) == NULL ||
+            keyName.allocateInsteadAndCopy(static_cast<int32_t>(uprv_strlen(name)+1)) == NULL ||
             (path != NULL &&
-             keyPath.allocateInsteadAndCopy(uprv_strlen(path)+1) == NULL)
+             keyPath.allocateInsteadAndCopy(static_cast<int32_t>(uprv_strlen(path)+1)) == NULL)
          ) {
             *status = U_MEMORY_ALLOCATION_ERROR;
             usprep_unload(newProfile.getAlias());
@@ -408,7 +408,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;
     }
@@ -502,8 +502,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, 
@@ -598,20 +597,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.  
 
@@ -649,10 +635,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, 
@@ -662,104 +644,95 @@ 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;
+            uprv_syntaxError(b2, b2Index-U16_LENGTH(ch), b2Len, parseError);
+            return 0;
         }
 
         if(profile->checkBiDi) {
-            direction = ubidi_getClass(profile->bdp, ch);
+            direction = ubidi_getClass(ch);
             if(firstCharDir == U_CHAR_DIRECTION_COUNT){
                 firstCharDir = direction;
             }
@@ -772,13 +745,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
@@ -791,21 +764,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);
 }
 
 
@@ -903,7 +862,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;