ICU-6648 Allow leading zeroes with strict parsing only when zeroes are forced by the pattern

X-SVN-Rev: 25914
This commit is contained in:
Jason Spieth 2009-04-25 19:25:55 +00:00
parent a663ab65c8
commit bfb6fdfefd
2 changed files with 58 additions and 12 deletions

View File

@ -24,6 +24,8 @@ import java.util.Locale;
import java.text.ParsePosition;
import java.text.Format;
import java.text.FieldPosition;
import java.text.ParseException;
import com.ibm.icu.text.DecimalFormat;
public class IntlTestDecimalFormatAPI extends com.ibm.icu.dev.test.TestFmwk
{
@ -365,4 +367,39 @@ public class IntlTestDecimalFormatAPI extends com.ibm.icu.dev.test.TestFmwk
}
}
}
public void testJB6648()
{
DecimalFormat df = new DecimalFormat();
df.setParseStrict(true);
String numstr = new String();
String[] patterns = {
"0",
"00",
"000",
"0,000",
"0.0",
"#000.0"
};
for(int i=0; i < patterns.length; i++) {
df.applyPattern(patterns[i]);
numstr = df.format(5);
try {
Number n = df.parse(numstr);
} catch (ParseException pe) {
errln("ERROR: Failed round trip with strict parsing.");
}
}
df.applyPattern(patterns[1]);
numstr = "005";
try {
Number n = df.parse(numstr);
errln("ERROR: Expected round trip failure not encountered");
} catch (ParseException pe) { }
}
}

View File

@ -2299,6 +2299,12 @@ public class DecimalFormat extends NumberFormat {
boolean leadingZero = false; // did we see a leading zero?
int lastGroup = -1; // where did we last see a grouping separator?
int gs2 = groupingSize2 == 0 ? groupingSize : groupingSize2;
// Strict parsing leading zeroes. If a leading zero would
// be forced by the pattern, then don't fail strict parsing.
boolean strictLeadingZero = false;
int leadingZeroPos = 0;
int leadingZeroCount = 0;
// equivalent grouping and decimal support
@ -2361,11 +2367,11 @@ public class DecimalFormat extends NumberFormat {
if (!sawDecimal) {
if (strictParse && !isExponent) {
// Allow leading zeros in exponents
if (leadingZero) {
strictFail = true;
break;
}
// Count leading zeros for checking later
leadingZero = true;
if (!strictLeadingZero) leadingZeroPos = position + 1;
strictLeadingZero = true;
++leadingZeroCount;
}
// Ignore leading zeros in integer part of number.
continue;
@ -2385,11 +2391,6 @@ public class DecimalFormat extends NumberFormat {
else if (digit > 0 && digit <= 9) // [sic] digit==0 handled above
{
if (strictParse) {
if (leadingZero) {
// a leading zero before a digit is an error with strict parsing
strictFail = true;
break;
}
if (backup != -1) {
if ((lastGroup != -1 && backup - lastGroup - 1 != gs2) ||
(lastGroup == -1 && position - oldStart - 1 > gs2)) {
@ -2521,6 +2522,17 @@ public class DecimalFormat extends NumberFormat {
if (backup != -1) position = backup;
// If there was no decimal point we have an integer
if (!sawDecimal) digits.decimalAt = digitCount; // Not digits.count!
// check for strict parse errors
if (strictParse && strictLeadingZero) {
if ((leadingZeroCount + digits.decimalAt) > this.getMinimumIntegerDigits()) {
parsePosition.setIndex(oldStart);
parsePosition.setErrorIndex(leadingZeroPos);
return false;
}
}
if (strictParse && !sawDecimal) {
if (lastGroup != -1 && position - lastGroup != groupingSize + 1) {
strictFail = true;
@ -2537,9 +2549,6 @@ public class DecimalFormat extends NumberFormat {
return false;
}
// If there was no decimal point we have an integer
if (!sawDecimal) digits.decimalAt = digitCount; // Not digits.count!
// Adjust for exponent, if any
exponent += digits.decimalAt;
if (exponent < -PARSE_MAX_EXPONENT) {