This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/master by this push:
new 8d39db110 Fix end-of-input handling in CompositeFormat cursor helpers
(#338)
8d39db110 is described below
commit 8d39db110990ab153727d1167003fb224367f087
Author: insaf021 <[email protected]>
AuthorDate: Tue Oct 27 03:19:33 2026 +0530
Fix end-of-input handling in CompositeFormat cursor helpers (#338)
Do not silently ignore non-whitespace character at the end of input.
---
.../commons/math4/legacy/util/CompositeFormat.java | 10 ++++++----
.../legacy/linear/RealMatrixFormatAbstractTest.java | 16 ++++++++++++++++
.../math4/legacy/util/ComplexFormatAbstractTest.java | 14 ++++++++++++++
3 files changed, 36 insertions(+), 4 deletions(-)
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 final class CompositeFormat {
*/
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 final class CompositeFormat {
} while (Character.isWhitespace(c) && index < n);
pos.setIndex(index);
- if (index < n) {
+ if (!Character.isWhitespace(c)) {
ret = c;
}
}
@@ -172,8 +175,7 @@ public final class CompositeFormat {
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 abstract class RealMatrixFormatAbstractTest {
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 abstract class ComplexFormatAbstractTest {
}
}
}
+
+ @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.
+ }
+ }
+ }
}