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: 7 additions & 3 deletions src/main/java/org/apache/commons/text/WordUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,14 @@ public static String wrap(final String str,
if (matcherSize == 0) {
offset--;
}
// wrap really long word one line at a time
wrappedLine.append(str, offset, wrapLength + offset);
// wrap really long word one line at a time, but keep a surrogate pair whole
int wrapAt = wrapLength + offset;
if (Character.isHighSurrogate(str.charAt(wrapAt - 1)) && Character.isLowSurrogate(str.charAt(wrapAt))) {
wrapAt++;
}
wrappedLine.append(str, offset, wrapAt);
wrappedLine.append(newLineStr);
offset += wrapLength;
offset = wrapAt;
matcherSize = -1;
} else {
// do not wrap really long word, just extend beyond limit
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/apache/commons/text/WordUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,14 @@ void testWrap_StringIntStringBoolean() {
assertEquals(expected, WordUtils.wrap(input, 20, "\n", false));
expected = "Click here,\nhttps://commons.apac\nhe.org, to jump to\nthe commons website";
assertEquals(expected, WordUtils.wrap(input, 20, "\n", true));

// a hard break for a long word must not split a surrogate pair across the new line
input = "a\uD83D\uDE00\uD83D\uDE00\uD83D\uDE00\uD83D\uDE00";
expected = "a\uD83D\uDE00\uD83D\uDE00\n\uD83D\uDE00\uD83D\uDE00";
assertEquals(expected, WordUtils.wrap(input, 4, "\n", true));
input = "\uD83D\uDE00\uD83D\uDE00\uD83D\uDE00";
expected = "\uD83D\uDE00\uD83D\uDE00\n\uD83D\uDE00";
assertEquals(expected, WordUtils.wrap(input, 3, "\n", true));
}

@Test
Expand Down
Loading