preserve UTF-16 surrogate pairs in MaskXmlInvalidCharacters - fixes #290#291
Merged
FreeAndNil merged 1 commit intomasterfrom Apr 16, 2026
Merged
Conversation
The regex [^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD] operated on individual UTF-16 char units, causing both halves of a valid surrogate pair to be replaced, silently corrupting supplementary characters (U+10000–U+10FFFF) such as emoji in XML log output. Fix by prepending a surrogate-pair alternative to the regex so valid pairs are matched and preserved as a unit; only lone surrogates and other XML-illegal code units are replaced with the mask string. Also optimise CountSubstrings: use a char loop for single-character substrings (all current callers) and StringComparison.Ordinal for the multi-character CDATA token path. Add unit tests covering surrogate pair preservation, lone surrogates, and CountSubstrings edge cases.
fluffynuts
approved these changes
Apr 16, 2026
This was referenced Apr 20, 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.
fixes #290
The regex [^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD] operated on individual UTF-16 char units, causing both halves of a valid surrogate pair to be replaced, silently corrupting supplementary characters (U+10000–U+10FFFF) such as emoji in XML log output.
Fix by prepending a surrogate-pair alternative to the regex so valid pairs are matched and preserved as a unit; only lone surrogates and other XML-illegal code units are replaced with the mask string.
Also optimise CountSubstrings: use a char loop for single-character substrings (all current callers) and StringComparison.Ordinal for the multi-character CDATA token path.
Add unit tests covering surrogate pair preservation, lone surrogates, and CountSubstrings edge cases.