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
10 changes: 6 additions & 4 deletions src/main/java/org/apache/commons/text/ExtendedMessageFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,14 @@ private int readArgumentIndex(final String pattern, final ParsePosition pos) {
* @param pos current position.
*/
private void seekNonWs(final String pattern, final ParsePosition pos) {
int len = 0;
final char[] buffer = pattern.toCharArray();
do {
len = StringMatcherFactory.INSTANCE.splitMatcher().isMatch(buffer, pos.getIndex(), 0, buffer.length);
while (pos.getIndex() < buffer.length) {
final int len = StringMatcherFactory.INSTANCE.splitMatcher().isMatch(buffer, pos.getIndex(), 0, buffer.length);
if (len == 0) {
break;
}
pos.setIndex(pos.getIndex() + len);
} while (len > 0 && pos.getIndex() < pattern.length());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ void testArgumentIndexTrailingWhitespaceAtEnd() {
assertThrowsExactly(IllegalArgumentException.class, () -> new ExtendedMessageFormat("{0 ", new HashMap<>()));
}

@Test
void testTruncatedFormatElementWithRegistry() {
// A format element left unterminated at the end of the pattern used to make the
// registry-based constructor seek one past the buffer and throw
// ArrayIndexOutOfBoundsException; it should report the documented
// IllegalArgumentException, as the plain constructor already does.
assertThrowsExactly(IllegalArgumentException.class, () -> new ExtendedMessageFormat("{", new HashMap<>()));
assertThrowsExactly(IllegalArgumentException.class, () -> new ExtendedMessageFormat("{0,", new HashMap<>()));
assertThrowsExactly(IllegalArgumentException.class, () -> new ExtendedMessageFormat("{0}extra{", new HashMap<>()));
}

/**
* Test the built in choice format.
*/
Expand Down
Loading