Skip to content

Commit f2d96b4

Browse files
committed
[stdlib] Don’t reject trailing surrogates in UTF16View overload of String.Index(_:within:)
Fix a long-standing issue where the UTF16View overload of `String.Index.init(_:within:)` used to return nil for valid indices that happened to point to a trailing surrogate in a UTF-8-encoded string. rdar://91935537 (cherry picked from commit 5f9828f)
1 parent 14f8ba7 commit f2d96b4

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

stdlib/public/core/StringUTF16View.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,15 @@ extension String.UTF16View.Index {
415415

416416
if _slowPath(target._guts.isForeign) {
417417
guard idx._foreignIsWithin(target) else { return nil }
418-
} else {
419-
guard target._guts.isOnUnicodeScalarBoundary(idx) else { return nil }
418+
} else { // fast UTF-8
419+
guard (
420+
// If the transcoded offset is non-zero, then `idx` addresses a trailing
421+
// surrogate, so its encoding offset is on a scalar boundary, and it's a
422+
// valid UTF-16 index.
423+
idx.transcodedOffset != 0
424+
/// Otherwise we need to reject indices that aren't scalar aligned.
425+
|| target._guts.isOnUnicodeScalarBoundary(idx)
426+
) else { return nil }
420427
}
421428

422429
self = idx

0 commit comments

Comments
 (0)