Skip to content

keep chop from splitting a trailing surrogate pair - #1770

Open
alhudz wants to merge 1 commit into
apache:masterfrom
alhudz:chop-trailing-surrogate
Open

keep chop from splitting a trailing surrogate pair#1770
alhudz wants to merge 1 commit into
apache:masterfrom
alhudz:chop-trailing-surrogate

Conversation

@alhudz

@alhudz alhudz commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

StringUtils.chop cuts the last UTF-16 char, so a String ending in a supplementary code point is left holding a lone surrogate.

Repro: chop("x😀"), a String ending in U+1F600.
Expected: "x", dropping the whole trailing code point, the same way chop("a") returns "".
Actual: "x\uD83D", only the low surrogate is removed and a lone high surrogate is left behind (malformed UTF-16).
Cause: the cut is str.substring(0, length - 1) with no surrogate check, unlike the sibling abbreviate/truncate which already guard their cuts with splitsSurrogatePair.
Fix: reuse splitsSurrogatePair(str, length - 1) to drop both halves when the final char is a low surrogate. The \r\n handling and every documented example stay unchanged.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request.
  • Run a successful build using the default Maven goal with mvn.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request has a meaningful subject line and body.

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.

1 participant