Skip to content

Keep surrogate pairs intact when wrapping help text - #195

Merged
puzrin merged 1 commit into
nodeca:masterfrom
ice-pring:fix/keep-surrogate-pairs-when-wrapping
Oct 24, 2026
Merged

puzrin merged 1 commit into
nodeca:masterfrom
ice-pring:fix/keep-surrogate-pairs-when-wrapping

Conversation

@ice-pring

Copy link
Copy Markdown
Contributor

Wrapping a long help description can split a valid UTF-16 surrogate pair across lines. For example, a formatter with width: 23 and a description of twenty repeated emoji produces a dangling high surrogate before the newline and a dangling low surrogate after it. Encoding that help output replaces the two halves with replacement characters.

Keep the long-word split boundary outside a surrogate pair. If an otherwise empty line has room for only one code unit, consume the whole pair so wrapping still advances. Remove an exhausted chunk and leave a full current line alone when the adjusted split has no room. Width accounting otherwise follows the existing code-unit approach.

Add eight port-specific cases covering odd boundaries, prefixed text, width one, a fractional width, unaffected ASCII/aligned cases, and the public format_help() output. Add a changelog entry.

Validation on Windows / Node.js 24.15.0 against 40086ea:

  • Before the fix: 6 new cases fail and 2 controls pass. All eight pass afterward.
  • npm test: ESLint and all 1,910 tests pass.
  • An additional bounded check of 4,375 combinations of BMP/astral characters, spaces, and widths produces well-formed lines and preserves every non-whitespace character.
  • git diff --check passes.

Other Node versions and operating systems were not run locally.

@puzrin
puzrin merged commit dda157b into nodeca:master Oct 24, 2026
1 check passed
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