]> granicus.if.org Git - icu/commitdiff
ICU-10026 Fix DecimalFormat to handle variation of minus signs according to Mark...
authorTravis Keep <keep94@gmail.com>
Tue, 7 May 2013 20:06:58 +0000 (20:06 +0000)
committerTravis Keep <keep94@gmail.com>
Tue, 7 May 2013 20:06:58 +0000 (20:06 +0000)
X-SVN-Rev: 33605

icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index c7ab4aa880fce55e394895d3ef914cee10bbdedd..4e9f0f46e1a0789aaca906f3b19214bc5b999961 100644 (file)
@@ -1764,39 +1764,6 @@ public class DecimalFormat extends NumberFormat {
         Currency[] currency = new Currency[1];
         return (CurrencyAmount) parse(text.toString(), pos, currency);
     }
-    
-    /**
-     * NormalizePlusAndMinus substitutes any rendition of plus or minus sign in
-     * text with the plus or minus sign for the current locale and returns the
-     * new string.
-     */
-    private String normalizePlusAndMinus(String text) {
-        StringBuilder builder = null;
-        int len = text.length();
-        for (int i = 0; i < len; i++) {
-            if (minusSigns.contains(text.charAt(i)) && text.charAt(i) != symbols.getMinusSign()) {
-                builder = append(builder, text, i);
-                builder.append(symbols.getMinusSign());
-            }
-            if (plusSigns.contains(text.charAt(i)) && text.charAt(i) != symbols.getPlusSign()) {
-                builder = append(builder, text, i);
-                builder.append(symbols.getPlusSign());
-            }
-        }
-        if (builder == null) {
-            return text;
-        }
-        return append(builder, text, len).toString();
-        
-    }
-    
-    private StringBuilder append(StringBuilder builder, String text, int upToIndex) {
-        if (builder == null) {
-            builder = new StringBuilder(text.length());
-        }
-        builder.append(text.substring(builder.length(), upToIndex));
-        return builder;
-    }
 
     /**
      * Parses the given text as either a Number or a CurrencyAmount.
@@ -1811,14 +1778,6 @@ public class DecimalFormat extends NumberFormat {
      * @return a Number or CurrencyAmount or null
      */
     private Object parse(String text, ParsePosition parsePosition, Currency[] currency) {
-        text = normalizePlusAndMinus(text);
-        if (symbols.getMinusSign() != '-') {
-            text = text.replace('-', symbols.getMinusSign());
-        }
-        if (symbols.getPlusSign() != '+') {
-            text = text.replace('+', symbols.getPlusSign());
-        }
-       
         int backup;
         int i = backup = parsePosition.getIndex();
 
@@ -2745,7 +2704,7 @@ public class DecimalFormat extends NumberFormat {
                 // (such as U+00A0) that is also in the affix.
                 i = skipUWhiteSpace(affix, i);
             } else {
-                if (pos < input.length() && UTF16.charAt(input, pos) == c) {
+                if (pos < input.length() && equalWithSignCompatibility(UTF16.charAt(input, pos), c)) {
                     i += len;
                     pos += len;
                 } else {
@@ -2756,6 +2715,12 @@ public class DecimalFormat extends NumberFormat {
         return pos - start;
     }
 
+    private static boolean equalWithSignCompatibility(int lhs, int rhs) {
+        return lhs == rhs
+                || (minusSigns.contains(lhs) && minusSigns.contains(rhs))
+                || (plusSigns.contains(lhs) && plusSigns.contains(rhs));
+    }
+
     /**
      * Skips over a run of zero or more Pattern_White_Space characters at pos in text.
      */
index b6ec96cd147469573e667a6384d94b4d1efb73d6..df06f53e6017204197930adc74b1761409b840df 100644 (file)
@@ -44,7 +44,7 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
         new NumberFormatTest().run(args);
     }
     
-    public void TestParseNegativeWithLocaleUsingNonAsciiNegative() {
+    public void TestParseNegativeWithFaLocale() {
         DecimalFormat parser = (DecimalFormat) NumberFormat.getInstance(new ULocale("fa"));
         try {
           double value = parser.parse("-0,5").doubleValue();
@@ -54,7 +54,7 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
         }
     }
     
-    public void TestParseNegativeEnglishButWithAlternativeMinusSign() {
+    public void TestParseNegativeWithAlternativeMinusSign() {
         DecimalFormat parser = (DecimalFormat) NumberFormat.getInstance(new ULocale("en"));
         try {
           double value = parser.parse("\u208B0.5").doubleValue();