From 6238bc8da2abe7a1f0cdd98c0af01e9ba1869ec3 Mon Sep 17 00:00:00 2001 From: Justin Lu Date: Thu, 6 Jun 2024 20:34:46 +0000 Subject: [PATCH] 8333456: CompactNumberFormat integer parsing fails when string has no suffix Reviewed-by: naoto --- .../java/text/CompactNumberFormat.java | 2 +- .../Format/NumberFormat/LenientParseTest.java | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/java.base/share/classes/java/text/CompactNumberFormat.java b/src/java.base/share/classes/java/text/CompactNumberFormat.java index 115a21ee662..cb1a9546b12 100644 --- a/src/java.base/share/classes/java/text/CompactNumberFormat.java +++ b/src/java.base/share/classes/java/text/CompactNumberFormat.java @@ -1736,7 +1736,7 @@ public final class CompactNumberFormat extends NumberFormat { // If parse integer only is true and the parsing is broken at // decimal point, then pass/ignore all digits and move pointer // at the start of suffix, to process the suffix part - if (isParseIntegerOnly() + if (isParseIntegerOnly() && position < text.length() && text.charAt(position) == symbols.getDecimalSeparator()) { position++; // Pass decimal character for (; position < text.length(); ++position) { diff --git a/test/jdk/java/text/Format/NumberFormat/LenientParseTest.java b/test/jdk/java/text/Format/NumberFormat/LenientParseTest.java index 2f07521ff23..41f8961ff32 100644 --- a/test/jdk/java/text/Format/NumberFormat/LenientParseTest.java +++ b/test/jdk/java/text/Format/NumberFormat/LenientParseTest.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8327640 8331485 + * @bug 8327640 8331485 8333456 * @summary Test suite for NumberFormat parsing when lenient. * @run junit/othervm -Duser.language=en -Duser.country=US LenientParseTest * @run junit/othervm -Duser.language=ja -Duser.country=JP LenientParseTest @@ -209,6 +209,18 @@ public class LenientParseTest { assertEquals(expectedValue, successParse(cmpctFmt, toParse, toParse.length())); } + // 8333456: Parse values with no compact suffix -> which allows parsing to iterate + // position to the same value as string length which throws + // StringIndexOutOfBoundsException upon charAt invocation + @ParameterizedTest + @MethodSource("compactValidNoSuffixParseStrings") + @EnabledIfSystemProperty(named = "user.language", matches = "en") + public void compactFmtSuccessParseIntOnlyTest(String toParse, double expectedValue) { + cmpctFmt.setParseIntegerOnly(true); + assertEquals(expectedValue, successParse(cmpctFmt, toParse, toParse.length())); + cmpctFmt.setParseIntegerOnly(false); + } + // ---- Helper test methods ---- // Method is used when a String should parse successfully. This does not indicate @@ -407,6 +419,18 @@ public class LenientParseTest { ); } + // No compact suffixes + private static Stream compactValidNoSuffixParseStrings() { + return Stream.of( + Arguments.of("5", 5), + Arguments.of("50", 50), + Arguments.of("50.", 50), + Arguments.of("5,000", 5000), + Arguments.of("5,000.", 5000), + Arguments.of("5,000.00", 5000) + ); + } + // Replace the grouping and decimal separators with localized variants // Used during localization of data private static String localizeText(String text) {