Skip to content

Commit 6238bc8

Browse files
author
Justin Lu
committedJun 6, 2024
8333456: CompactNumberFormat integer parsing fails when string has no suffix
Reviewed-by: naoto
1 parent 2a37764 commit 6238bc8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed
 

‎src/java.base/share/classes/java/text/CompactNumberFormat.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ public Number parse(String text, ParsePosition pos) {
17361736
// If parse integer only is true and the parsing is broken at
17371737
// decimal point, then pass/ignore all digits and move pointer
17381738
// at the start of suffix, to process the suffix part
1739-
if (isParseIntegerOnly()
1739+
if (isParseIntegerOnly() && position < text.length()
17401740
&& text.charAt(position) == symbols.getDecimalSeparator()) {
17411741
position++; // Pass decimal character
17421742
for (; position < text.length(); ++position) {

‎test/jdk/java/text/Format/NumberFormat/LenientParseTest.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8327640 8331485
26+
* @bug 8327640 8331485 8333456
2727
* @summary Test suite for NumberFormat parsing when lenient.
2828
* @run junit/othervm -Duser.language=en -Duser.country=US LenientParseTest
2929
* @run junit/othervm -Duser.language=ja -Duser.country=JP LenientParseTest
@@ -209,6 +209,18 @@ public void compactFmtSuccessParseTest(String toParse, double expectedValue) {
209209
assertEquals(expectedValue, successParse(cmpctFmt, toParse, toParse.length()));
210210
}
211211

212+
// 8333456: Parse values with no compact suffix -> which allows parsing to iterate
213+
// position to the same value as string length which throws
214+
// StringIndexOutOfBoundsException upon charAt invocation
215+
@ParameterizedTest
216+
@MethodSource("compactValidNoSuffixParseStrings")
217+
@EnabledIfSystemProperty(named = "user.language", matches = "en")
218+
public void compactFmtSuccessParseIntOnlyTest(String toParse, double expectedValue) {
219+
cmpctFmt.setParseIntegerOnly(true);
220+
assertEquals(expectedValue, successParse(cmpctFmt, toParse, toParse.length()));
221+
cmpctFmt.setParseIntegerOnly(false);
222+
}
223+
212224
// ---- Helper test methods ----
213225

214226
// Method is used when a String should parse successfully. This does not indicate
@@ -407,6 +419,18 @@ private static Stream<Arguments> compactValidFullParseStrings() {
407419
);
408420
}
409421

422+
// No compact suffixes
423+
private static Stream<Arguments> compactValidNoSuffixParseStrings() {
424+
return Stream.of(
425+
Arguments.of("5", 5),
426+
Arguments.of("50", 50),
427+
Arguments.of("50.", 50),
428+
Arguments.of("5,000", 5000),
429+
Arguments.of("5,000.", 5000),
430+
Arguments.of("5,000.00", 5000)
431+
);
432+
}
433+
410434
// Replace the grouping and decimal separators with localized variants
411435
// Used during localization of data
412436
private static String localizeText(String text) {

0 commit comments

Comments
 (0)