Skip to content

Keep ExtendedMessageFormat from rejecting a quoted format style#1744

Merged
garydgregory merged 1 commit into
apache:masterfrom
alhudz:extendedmessageformat-quoted-style
Jul 7, 2026
Merged

Keep ExtendedMessageFormat from rejecting a quoted format style#1744
garydgregory merged 1 commit into
apache:masterfrom
alhudz:extendedmessageformat-quoted-style

Conversation

@alhudz

@alhudz alhudz commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

ExtendedMessageFormat.parseFormatDescription walks a format element's style with a for loop whose update clause always calls next(pos), but the QUOTE branch hands off to getQuotedString, which already leaves pos one past the closing quote. The loop then advances a second time and skips the character right after that quote, so when a style ends with a quoted literal the terminating } is never seen: depth never returns to 0, the loop runs off the end, and the constructor throws IllegalArgumentException: Unterminated format element. This only happens on the registry path, so new ExtendedMessageFormat("{0,number,0'%'}", registry) throws even though java.text.MessageFormat and ExtendedMessageFormat without a registry both parse that pattern to 5%.

The fix switches the loop to the while form the two sibling parsers in this class already use (applyPattern, insertFormats): advance one position per non-quote branch and let the QUOTE branch's getQuotedString do its own advance. Leaving the position arithmetic to the single loop that consumes the quoted run is what stops the double count. The added test fails before the change with the Unterminated format element exception and passes after.

@garydgregory garydgregory merged commit 8425918 into apache:master Jul 7, 2026
20 of 21 checks passed
@garydgregory garydgregory changed the title keep ExtendedMessageFormat from rejecting a quoted format style Keep ExtendedMessageFormat from rejecting a quoted format style Jul 7, 2026
@garydgregory

Copy link
Copy Markdown
Member

@alhudz Thank you, please port to Apache Commons Text if needed.

@alhudz

alhudz commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Checked the Commons Text version: its parseFormatDescription already uses the while form, with getQuotedString doing the advance in the QUOTE branch, so the double advance isn't there. Verified against commons-text 1.14.0: new ExtendedMessageFormat("{0,number,0'%'}", registry).format(new Object[]{5}) -> 5%. Nothing to port. Thanks for merging.

@garydgregory

Copy link
Copy Markdown
Member

@alhudz
You'll want to check against git master, not a release tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants