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:
parent
a663ab65c8
commit
bfb6fdfefd
@ -24,6 +24,8 @@ import java.util.Locale;
|
|||||||
import java.text.ParsePosition;
|
import java.text.ParsePosition;
|
||||||
import java.text.Format;
|
import java.text.Format;
|
||||||
import java.text.FieldPosition;
|
import java.text.FieldPosition;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import com.ibm.icu.text.DecimalFormat;
|
||||||
|
|
||||||
public class IntlTestDecimalFormatAPI extends com.ibm.icu.dev.test.TestFmwk
|
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) { }
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2299,6 +2299,12 @@ public class DecimalFormat extends NumberFormat {
|
|||||||
boolean leadingZero = false; // did we see a leading zero?
|
boolean leadingZero = false; // did we see a leading zero?
|
||||||
int lastGroup = -1; // where did we last see a grouping separator?
|
int lastGroup = -1; // where did we last see a grouping separator?
|
||||||
int gs2 = groupingSize2 == 0 ? groupingSize : groupingSize2;
|
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
|
// equivalent grouping and decimal support
|
||||||
|
|
||||||
@ -2361,11 +2367,11 @@ public class DecimalFormat extends NumberFormat {
|
|||||||
if (!sawDecimal) {
|
if (!sawDecimal) {
|
||||||
if (strictParse && !isExponent) {
|
if (strictParse && !isExponent) {
|
||||||
// Allow leading zeros in exponents
|
// Allow leading zeros in exponents
|
||||||
if (leadingZero) {
|
// Count leading zeros for checking later
|
||||||
strictFail = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
leadingZero = true;
|
leadingZero = true;
|
||||||
|
if (!strictLeadingZero) leadingZeroPos = position + 1;
|
||||||
|
strictLeadingZero = true;
|
||||||
|
++leadingZeroCount;
|
||||||
}
|
}
|
||||||
// Ignore leading zeros in integer part of number.
|
// Ignore leading zeros in integer part of number.
|
||||||
continue;
|
continue;
|
||||||
@ -2385,11 +2391,6 @@ public class DecimalFormat extends NumberFormat {
|
|||||||
else if (digit > 0 && digit <= 9) // [sic] digit==0 handled above
|
else if (digit > 0 && digit <= 9) // [sic] digit==0 handled above
|
||||||
{
|
{
|
||||||
if (strictParse) {
|
if (strictParse) {
|
||||||
if (leadingZero) {
|
|
||||||
// a leading zero before a digit is an error with strict parsing
|
|
||||||
strictFail = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (backup != -1) {
|
if (backup != -1) {
|
||||||
if ((lastGroup != -1 && backup - lastGroup - 1 != gs2) ||
|
if ((lastGroup != -1 && backup - lastGroup - 1 != gs2) ||
|
||||||
(lastGroup == -1 && position - oldStart - 1 > gs2)) {
|
(lastGroup == -1 && position - oldStart - 1 > gs2)) {
|
||||||
@ -2521,6 +2522,17 @@ public class DecimalFormat extends NumberFormat {
|
|||||||
|
|
||||||
if (backup != -1) position = backup;
|
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 (strictParse && !sawDecimal) {
|
||||||
if (lastGroup != -1 && position - lastGroup != groupingSize + 1) {
|
if (lastGroup != -1 && position - lastGroup != groupingSize + 1) {
|
||||||
strictFail = true;
|
strictFail = true;
|
||||||
@ -2537,9 +2549,6 @@ public class DecimalFormat extends NumberFormat {
|
|||||||
return false;
|
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
|
// Adjust for exponent, if any
|
||||||
exponent += digits.decimalAt;
|
exponent += digits.decimalAt;
|
||||||
if (exponent < -PARSE_MAX_EXPONENT) {
|
if (exponent < -PARSE_MAX_EXPONENT) {
|
||||||
|
Loading…
Reference in New Issue
Block a user