Keep WordUtils.wrap from splitting a surrogate pair#755
Merged
Conversation
Member
|
Thank you @alhudz , merged 🚀 |
garydgregory
added a commit
that referenced
this pull request
Jun 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Port of apache/commons-lang#1731 to the Commons Text copy of
WordUtils, as requested by @garydgregory.WordUtils.wrap(str, wrapLength, newLineStr, true)hard-breaks a too-long word at the fixed char offsetwrapLength + offsetand inserts the new line there. When that offset lands between the high and low surrogate of a supplementary code point the pair is split, so a lossless wrap emits a lone high surrogate at the end of one line and a lone low surrogate at the start of the next.Repro:
WordUtils.wrap("a😀😀😀😀", 4, "\n", true)(athen fourU+1F600).Before:
a😀\uD83D\n\uDE00😀\uD83D\n\uDE00, i.e. the 2nd and 4th emoji are split around the\n.After:
a😀😀\n😀😀, no lone surrogates.Fix: nudge the break one char forward when it would land inside a pair, so the whole code point stays on the current line. BMP input and the delimiter-based wrap paths are unaffected, and the other
wrapoverloads delegate to this method.Added assertions to
WordUtilsTest#testWrap_StringIntStringBooleanthat fail on the current tree and pass with the fix.mvn; that'smvnon the command line by itself.