diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/CompositeFormat.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/CompositeFormat.java index 333496529..f17021792 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/CompositeFormat.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/CompositeFormat.java @@ -64,8 +64,11 @@ public static NumberFormat getDefaultNumberFormat(final Locale locale) { */ public static void parseAndIgnoreWhitespace(final String source, final ParsePosition pos) { + final int startIndex = pos.getIndex(); parseNextCharacter(source, pos); - pos.setIndex(pos.getIndex() - 1); + if (pos.getIndex() > startIndex) { + pos.setIndex(pos.getIndex() - 1); + } } /** @@ -88,7 +91,7 @@ public static char parseNextCharacter(final String source, } while (Character.isWhitespace(c) && index < n); pos.setIndex(index); - if (index < n) { + if (!Character.isWhitespace(c)) { ret = c; } } @@ -172,8 +175,7 @@ public static boolean parseFixedstring(final String source, final int startIndex = pos.getIndex(); final int endIndex = startIndex + expected.length(); - if (startIndex >= source.length() || - endIndex > source.length() || + if (endIndex > source.length() || source.substring(startIndex, endIndex).compareTo(expected) != 0) { // set index back to start, error index should be the start index pos.setIndex(startIndex); diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealMatrixFormatAbstractTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealMatrixFormatAbstractTest.java index e5a230222..8030cc78c 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealMatrixFormatAbstractTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealMatrixFormatAbstractTest.java @@ -359,4 +359,20 @@ public void testForgottenSuffix() { Assert.assertNull("Should not parse <"+source+">", realMatrixFormat.parse(source, pos)); Assert.assertEquals(9, pos.getErrorIndex()); } + + @Test + public void testForgottenSuffixAtEndOfInput() { + ParsePosition pos = new ParsePosition(0); + final String source = "{{1}"; + Assert.assertNull("Should not parse <"+source+">", realMatrixFormat.parse(source, pos)); + Assert.assertEquals(4, pos.getErrorIndex()); + } + + @Test + public void testParseEmptySuffixAtEndOfInput() { + ParsePosition pos = new ParsePosition(0); + final RealMatrix parsed = new RealMatrixFormat("", "", "", "", "; ", ", ").parse("1, 2; 3, 4", pos); + Assert.assertEquals(MatrixUtils.createRealMatrix(new double[][] {{1, 2}, {3, 4}}), parsed); + Assert.assertEquals(10, pos.getIndex()); + } } diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java index 37cffdfd1..3a05cad35 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java @@ -459,4 +459,18 @@ public void testParseRejectsInvalidGroupingSeparators() { } } } + + @Test + public void testParseRejectsTrailingCharacter() { + final ComplexFormat format = new ComplexFormat(); + final String[] invalid = {"2i", "1x", "1 +"}; + for (final String source : invalid) { + try { + format.parse(source); + Assert.fail("Expected parsing to reject: " + source); + } catch (MathParseException expected) { + // Expected. + } + } + } }