ICU-10026 Make Numberformat.parse() even more lenient by allowing 7 alternates for plus and minus when parsing Not just ascii plus and minus.

X-SVN-Rev: 33462
This commit is contained in:
Travis Keep 2013-03-26 23:51:41 +00:00
parent 46a99dae07
commit d2f57f1ad3
2 changed files with 60 additions and 0 deletions

View File

@ -1764,6 +1764,34 @@ public class DecimalFormat extends NumberFormat {
Currency[] currency = new Currency[1];
return (CurrencyAmount) parse(text.toString(), pos, currency);
}
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.
@ -1778,6 +1806,7 @@ 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());
}
@ -2154,6 +2183,27 @@ public class DecimalFormat extends NumberFormat {
0xFF0C, 0xFF0C,
0xFF0E, 0xFF0E,
0xFF61, 0xFF61).freeze();
private static final UnicodeSet minusSigns =
new UnicodeSet(
0x002D, 0x002D,
0x207B, 0x207B,
0x208B, 0x208B,
0x2212, 0x2212,
0x2796, 0x2796,
0xFE63, 0xFE63,
0xFF0D, 0xFF0D).freeze();
private static final UnicodeSet plusSigns =
new UnicodeSet(
0x002B, 0x002B,
0x207A, 0x207A,
0x208A, 0x208A,
0x2795, 0x2795,
0xFB29, 0xFB29,
0xFE62, 0xFE62,
0xFF0B, 0xFF0B).freeze();
// When parsing a number with big exponential value, it requires to transform the
// value into a string representation to construct BigInteger instance. We want to

View File

@ -53,6 +53,16 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
this.errln("Parsing -0.5 should have succeeded.");
}
}
public void TestParseNegativeEnglishButWithAlternativeMinusSign() {
DecimalFormat parser = (DecimalFormat) NumberFormat.getInstance(new ULocale("en"));
try {
double value = parser.parse("\u208B0.5").doubleValue();
assertEquals("Expect -0.5", -0.5, value);
} catch (ParseException e) {
this.errln("Parsing -0.5 should have succeeded.");
}
}
// Test various patterns
public void TestPatterns() {