Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
}
}
Loading