]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/nptrans.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / nptrans.cpp
index c796521b35ba7aba9b0b7e5943bf8ef0d923e154..6c1c7be3361f3063040b90694cced3acfb35947d 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, International Business Machines
+ *   Copyright (C) 2003-2014, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  *
  *******************************************************************************
  *   file name:  nptrans.h
- *   encoding:   US-ASCII
+ *   encoding:   UTF-8
  *   tab size:   8 (not used)
  *   indentation:4
  *
@@ -27,7 +29,7 @@
 #include "ustr_imp.h"
 #include "intltest.h"
 
-#ifdef DEBUG 
+#ifdef NPTRANS_DEBUG 
 #include <stdio.h>
 #endif
 
@@ -63,8 +65,11 @@ NamePrepTransform::NamePrepTransform(UParseError& parseError, UErrorCode& status
         // create the mapping transliterator
         int32_t ruleLen = 0;
         const UChar* ruleUChar = ures_getStringByKey(bundle, "MapNFKC",&ruleLen, &status);
-        UnicodeString rule(ruleUChar, ruleLen);
-        
+        int32_t mapRuleLen = 0;
+        const UChar *mapRuleUChar = ures_getStringByKey(bundle, "MapNoNormalization", &mapRuleLen, &status);
+        UnicodeString rule(mapRuleUChar, mapRuleLen);
+        rule.append(ruleUChar, ruleLen);
+
         mapping = Transliterator::createFromRules(UnicodeString("NamePrepTransform", ""), rule,
                                                    UTRANS_FORWARD, parseError,status);
         if(U_FAILURE(status)) {
@@ -81,14 +86,14 @@ NamePrepTransform::NamePrepTransform(UParseError& parseError, UErrorCode& status
         pattern =  ures_getStringByKey(bundle,"ProhibitedSet",&patternLen, &status);
         UnicodeString test(pattern,patternLen);
         prohibited.applyPattern(test,status);
-#ifdef DEBUG
+#ifdef NPTRANS_DEBUG
         if(U_FAILURE(status)){
             printf("Construction of Unicode set failed\n");
         }
 
         if(U_SUCCESS(status)){
             if(prohibited.contains((UChar) 0x644)){
-                printf("The string contains 0x644 ... damn !!\n");
+                printf("The string contains 0x644 ... !!\n");
             }
             UnicodeString temp;
             prohibited.toPattern(temp,TRUE);
@@ -162,15 +167,14 @@ int32_t NamePrepTransform::map(const UChar* src, int32_t srcLength,
         for(;bufIndex<bufLen;){
             U16_NEXT(buffer, bufIndex, bufLen, ch);
             if(unassigned.contains(ch)){
-                status = U_IDNA_UNASSIGNED_CODEPOINT_FOUND_ERROR;
-                rsource.releaseBuffer();
+                status = U_IDNA_UNASSIGNED_ERROR;
                 return 0;
             }
         }
     }
     // check if there is enough room in the output
     if(bufLen < destCapacity){
-        uprv_memcpy(dest,buffer,bufLen*U_SIZEOF_UCHAR);
+        u_memcpy(dest, buffer, bufLen);
     }
 
     return u_terminateUChars(dest, destCapacity, bufLen, &status);
@@ -195,31 +199,28 @@ int32_t NamePrepTransform::process( const UChar* src, int32_t srcLength,
         return 0;
     }
 
-    UChar b1Stack[MAX_BUFFER_SIZE];
-    UChar *b1 = b1Stack;
-    int32_t b1Len,b1Capacity = MAX_BUFFER_SIZE;
+    UnicodeString b1String;
+    UChar *b1 = b1String.getBuffer(MAX_BUFFER_SIZE);
+    int32_t b1Len;
 
     int32_t b1Index = 0;
     UCharDirection direction=U_CHAR_DIRECTION_COUNT, firstCharDir=U_CHAR_DIRECTION_COUNT;
     UBool leftToRight=FALSE, rightToLeft=FALSE;
 
-    b1Len = map(src,srcLength, b1, b1Capacity,allowUnassigned,parseError, status);
+    b1Len = map(src, srcLength, b1, b1String.getCapacity(), allowUnassigned, parseError, status);
+    b1String.releaseBuffer(b1Len);
 
     if(status == U_BUFFER_OVERFLOW_ERROR){
         // redo processing of string
         /* we do not have enough room so grow the buffer*/
-        if(!u_growBufferFromStatic(b1Stack,&b1,&b1Capacity,b1Len,0)){
-            status = U_MEMORY_ALLOCATION_ERROR;
-            goto CLEANUP;
-        }
-
+        b1 = b1String.getBuffer(b1Len);
         status = U_ZERO_ERROR; // reset error
-        
-        b1Len = map(src,srcLength, b1, b1Len,allowUnassigned, parseError, status);
-        
+        b1Len = map(src, srcLength, b1, b1String.getCapacity(), allowUnassigned, parseError, status);
+        b1String.releaseBuffer(b1Len);
     }
 
     if(U_FAILURE(status)){
+        b1Len = 0;
         goto CLEANUP;
     }
 
@@ -231,7 +232,8 @@ int32_t NamePrepTransform::process( const UChar* src, int32_t srcLength,
         U16_NEXT(b1, b1Index, b1Len, ch);
 
         if(prohibited.contains(ch) && ch!=0x0020){
-            status = U_IDNA_PROHIBITED_CODEPOINT_FOUND_ERROR;
+            status = U_IDNA_PROHIBITED_ERROR;
+            b1Len = 0;
             goto CLEANUP;
         }
 
@@ -250,6 +252,7 @@ int32_t NamePrepTransform::process( const UChar* src, int32_t srcLength,
     // satisfy 2
     if( leftToRight == TRUE && rightToLeft == TRUE){
         status = U_IDNA_CHECK_BIDI_ERROR;
+        b1Len = 0;
         goto CLEANUP;
     }
 
@@ -263,14 +266,10 @@ int32_t NamePrepTransform::process( const UChar* src, int32_t srcLength,
     }
 
     if(b1Len <= destCapacity){
-        uprv_memmove(dest,b1, b1Len*U_SIZEOF_UCHAR);
+        u_memmove(dest, b1, b1Len);
     }
 
 CLEANUP:
-    if(b1!=b1Stack){
-        uprv_free(b1);
-    }
-
     return u_terminateUChars(dest, destCapacity, b1Len, &status);
 }