Skip to content

NSString addingPercentEncoding methods should return nil for invalid UTF-16#1891

Merged
jrflat merged 1 commit intoswiftlang:mainfrom
jrflat:addingPercentEncoding-invalid-utf16
Apr 10, 2026
Merged

NSString addingPercentEncoding methods should return nil for invalid UTF-16#1891
jrflat merged 1 commit intoswiftlang:mainfrom
jrflat:addingPercentEncoding-invalid-utf16

Conversation

@jrflat
Copy link
Copy Markdown
Contributor

@jrflat jrflat commented Apr 10, 2026

The new Swift implementation for -[NSString addingPercentEncoding...] methods should return nil for NSStrings with invalid (unpaired) UTF-16 surrogates instead of using the U+FFFD replacement character.

Motivation:

Restore previous behavior of returning nil when an NSString contains an unpaired UTF-16 surrogate.

Modifications:

The extensions now validate UTF-16 backed NSStrings via String(validating: ..., as: UTF16.self) instead of just bridging to String (which replaces invalid UTF-16 with U+FFFD when accessing the bytes via .withUTF8 { ... }).

Result:

No behavior change for swift-foundation. On Darwin, these NSString methods maintain their old behavior when the Swift implementation is enabled.

Testing:

NSString testing of these methods for strings containing unpaired UTF-16 surrogates.

@jrflat jrflat requested a review from a team as a code owner April 10, 2026 19:53
@jrflat
Copy link
Copy Markdown
Contributor Author

jrflat commented Apr 10, 2026

@swift-ci please test

@jrflat jrflat requested review from chloe-yeo and jmschonfeld April 10, 2026 19:54
@jrflat jrflat merged commit 3100cd9 into swiftlang:main Apr 10, 2026
40 of 41 checks 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