]> granicus.if.org Git - icu/commitdiff
ICU-9677 fix affixes-ignored-for-parse - check in handleChanged() instead of relying...
authorSteven R. Loomis <srl@icu-project.org>
Tue, 4 Dec 2012 21:33:15 +0000 (21:33 +0000)
committerSteven R. Loomis <srl@icu-project.org>
Tue, 4 Dec 2012 21:33:15 +0000 (21:33 +0000)
X-SVN-Rev: 32918

icu4c/source/i18n/decimfmt.cpp
icu4c/source/test/intltest/numrgts.cpp
icu4c/source/test/intltest/numrgts.h

index 3f61bdc9ff733a87c8eb434e53aec79829534bc1..d5f4f6a513c19a57fa8d9c44970f50240af099cc 100644 (file)
@@ -1028,6 +1028,15 @@ void DecimalFormat::handleChanged() {
 #endif
   if (fFormatWidth!=0) {
       debug("No Parse fastpath: fFormatWidth");
+  } else if(fPositivePrefix.length()>0) {
+    debug("No Parse fastpath: positive prefix");
+  } else if(fPositiveSuffix.length()>0) {
+    debug("No Parse fastpath: positive suffix");
+  } else if(fNegativePrefix.length()>1 
+            || ((fNegativePrefix.length()==1) && (fNegativePrefix.charAt(0)!=0x002D))) {
+    debug("No Parse fastpath: negative prefix that isn't '-'");
+  } else if(fNegativeSuffix.length()>0) {
+    debug("No Parse fastpath: negative suffix");
   } else {
     data.fFastParseStatus = kFastpathYES;
     debug("parse fastpath: YES");
@@ -2169,7 +2178,7 @@ UBool DecimalFormat::subparse(const UnicodeString& text,
     UChar dbgbuf[300];
     UnicodeString s(dbgbuf,0,300);;
     s.append((UnicodeString)"PARSE \"").append(text.tempSubString(position)).append((UnicodeString)"\" " );
-#define DBGAPPD(x) if(x) { s.append(UnicodeString(#x "="));  if(x->isEmpty()) { s.append(UnicodeString("<empty>")); } else { s.append(*x); } s.append(UnicodeString(" ")); }
+#define DBGAPPD(x) if(x) { s.append(UnicodeString(#x "="));  if(x->isEmpty()) { s.append(UnicodeString("<empty>")); } else { s.append(*x); } s.append(UnicodeString(" ")); } else { s.append(UnicodeString(#x "=NULL ")); }
     DBGAPPD(negPrefix);
     DBGAPPD(negSuffix);
     DBGAPPD(posPrefix);
@@ -2218,9 +2227,8 @@ UBool DecimalFormat::subparse(const UnicodeString& text,
         (intOnly)?'y':'n',
         (strictParse)?'y':'n');
 #endif
-      if(ch=='-') {
-        /* for now- no negs. */
-        j=l+1;//=break
+      if(ch==0x002D) { // '-'
+        j=l+1;//=break - negative number.
         
         /*
           parsedNum.append('-',err); 
index 167e588878744d7ae754a4561ad634e7095e299b..4470c37a49eb344524c3f461ab3f0db59828b0f8 100644 (file)
@@ -171,6 +171,7 @@ NumberFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const cha
         CASE(61,Test8199);
         CASE(62,Test9109);
         CASE(63,Test9780);
+        CASE(64,Test9677);
 
         default: name = ""; break;
     }
@@ -2892,4 +2893,94 @@ void NumberFormatRegressionTest::Test9780(void) {
     }
     delete nf;
 }
+
+
+void NumberFormatRegressionTest::Test9677(void) {
+  static const UChar pattern[] = { 0x23,0x23,0x23,0x23,0x2E,0x23,0x23,0x23,0x23,0 }; // "####.####"
+  static const UChar positivePrefix[] = { 0x40,0 }; // "@"
+  static const UChar negativePrefix[] = { 0x6E,0 }; // "n"
+  static const UChar text[] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0 }; // 123456789
+  static const UChar text2[] = { 0x6E, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0 }; // n123456789
+  
+  UErrorCode status = U_ZERO_ERROR;
+  LocalUNumberFormatPointer f(unum_open(UNUM_DEFAULT, NULL, 0, "en_US", NULL, &status));
+
+  assertSuccess("opening unum_open", status);
+  if (U_SUCCESS(status)) {
+    unum_applyPattern(f.getAlias(), FALSE, pattern, -1, NULL, &status);
+    unum_setTextAttribute(f.getAlias(), UNUM_POSITIVE_PREFIX, positivePrefix, -1, &status);
+    assertSuccess("setting attributes", status);
+  }
+
+  if(U_SUCCESS(status)) {
+    int32_t n = unum_parse(f.getAlias(), text, -1, NULL, &status);
+    logln("unum_parse status %s, result %d\n", u_errorName(status), n);
+
+    if(U_FAILURE(status)) {
+        logln("Got expected parse error %s\n", u_errorName(status));
+        status = U_ZERO_ERROR;
+    } else {
+        errln("FAIL: unum_parse status %s, result %d - expected failure\n", u_errorName(status), n);
+    }
+  }
+
+  if (U_SUCCESS(status)) {
+    unum_setTextAttribute(f.getAlias(), UNUM_POSITIVE_PREFIX, NULL, 0, &status);
+    assertSuccess("setting attributes", status);
+    logln("removed positive prefix");
+  }
+
+  if(U_SUCCESS(status)) {
+    int32_t n = unum_parse(f.getAlias(), text, -1, NULL, &status);
+    logln("unum_parse status %s, result %d\n", u_errorName(status), n);
+
+    if(U_FAILURE(status)) {
+        errln("FAIL: with pos prefix removed, parse error %s\n", u_errorName(status));
+        status = U_ZERO_ERROR;
+    } else {
+        if(n!=123456789) {
+          errln("FAIL: with pos prefix removed , unum_parse status %s, result %d expected 123456789\n", u_errorName(status), n);
+        } else {
+          logln("PASS: with pos prefix removed , unum_parse status %s, result %d expected 123456789\n", u_errorName(status),n);
+        }
+    }
+  }
+
+  if(U_SUCCESS(status)) {
+    int32_t n = unum_parse(f.getAlias(), text2, -1, NULL, &status);
+    logln("unum_parse status %s, result %d\n", u_errorName(status), n);
+
+    if(U_FAILURE(status)) {
+        logln("text2: Got expected parse error %s\n", u_errorName(status));
+        status = U_ZERO_ERROR;
+    } else {
+        errln("FAIL: text2: unum_parse status %s, result %d - expected failure\n", u_errorName(status), n);
+    }
+  }
+
+  if (U_SUCCESS(status)) {
+    unum_setTextAttribute(f.getAlias(), UNUM_NEGATIVE_PREFIX, negativePrefix, -1, &status);
+    assertSuccess("setting attributes", status);
+    logln("Set a different neg prefix prefix");
+  }
+
+  if(U_SUCCESS(status)) {
+    int32_t n = unum_parse(f.getAlias(), text2, -1, NULL, &status);
+    logln("unum_parse status %s, result %d\n", u_errorName(status), n);
+
+    if(U_FAILURE(status)) {
+        errln("FAIL: with different neg prefix , parse error %s\n", u_errorName(status));
+        status = U_ZERO_ERROR;
+    } else {
+;
+        if(n!=-123456789) {
+          errln("FAIL: with different neg prefix , unum_parse status %s, result %d expected -123456789\n", u_errorName(status), n);
+        } else {
+          logln("PASS: with different neg prefix , unum_parse status %s, result %d expected -123456789\n", u_errorName(status), n);
+        }
+    }
+  }
+}
+
+
 #endif /* #if !UCONFIG_NO_FORMATTING */
index 6dad32d37ff1c740625ce0f0d2634a542c39b03a..96dc2995752ee9a61fcc60eda33f88225a213e15 100644 (file)
@@ -93,6 +93,7 @@ public:
     void Test8199(void);
     void Test9109(void);
     void Test9780(void);
+    void Test9677(void);
 protected:
     UBool failure(UErrorCode status, const UnicodeString& msg, UBool possibleDataError=FALSE);
     UBool failure(UErrorCode status, const UnicodeString& msg, const char *l, UBool possibleDataError=FALSE);