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.
* @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();
// (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 {
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.
*/
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();
}
}
- public void TestParseNegativeEnglishButWithAlternativeMinusSign() {
+ public void TestParseNegativeWithAlternativeMinusSign() {
DecimalFormat parser = (DecimalFormat) NumberFormat.getInstance(new ULocale("en"));
try {
double value = parser.parse("\u208B0.5").doubleValue();