fix: support UTF-16 surrogate pairs in unicode escape sequences#59
Merged
dsherret merged 1 commit intodprint:mainfrom Nov 12, 2025
Merged
fix: support UTF-16 surrogate pairs in unicode escape sequences#59dsherret merged 1 commit intodprint:mainfrom
dsherret merged 1 commit intodprint:mainfrom
Conversation
Implements RFC 8259 § 7 compliant handling of UTF-16 surrogate pairs. Non-BMP characters (U+10000 to U+10FFFF) encoded as surrogate pairs like \uD834\uDD1E are now correctly decoded to their Unicode code points. The parser now: - Detects high surrogates (0xD800-0xDBFF) and looks ahead for low surrogates - Combines pairs using the formula: ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000 - Validates all edge cases with descriptive error messages - Maintains backward compatibility for normal \uXXXX escapes Added comprehensive tests covering valid pairs, multiple pairs, mixed content, and all error cases for unpaired surrogates.
There was a problem hiding this comment.
Pull Request Overview
This PR implements RFC 8259 § 7 compliant UTF-16 surrogate pair handling, enabling the parser to correctly decode non-BMP Unicode characters (U+10000 to U+10FFFF) that are encoded as surrogate pairs in JSON escape sequences.
- Adds detection and combination logic for UTF-16 surrogate pairs (high: 0xD800-0xDBFF, low: 0xDC00-0xDFFF)
- Implements validation for all edge cases with descriptive error messages for unpaired surrogates
- Maintains backward compatibility for standard \uXXXX Unicode escapes
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/string.rs | Core implementation of surrogate pair detection, validation, and decoding using the RFC 8259 formula |
| src/scanner.rs | Updated error message for unpaired low surrogate to reflect new validation |
| src/parse_to_value.rs | Comprehensive test suite covering valid pairs, multiple pairs, mixed content, and error cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This was referenced Nov 12, 2025
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.
Implements RFC 8259 § 7 compliant handling of UTF-16 surrogate pairs. Non-BMP characters (U+10000 to U+10FFFF) encoded as surrogate pairs like \uD834\uDD1E are now correctly decoded to their Unicode code points.
The parser now:
Added comprehensive tests covering valid pairs, multiple pairs, mixed content, and all error cases for unpaired surrogates.
fixes #31