Skip to content

Commit d18b5f5

Browse files
committed
[stdlib] Branchless _StringGuts.hasMatchingEncoding
1 parent eadef7a commit d18b5f5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ extension _StringGuts {
340340
/// correct value.
341341
@_alwaysEmitIntoClient @inline(__always)
342342
internal func hasMatchingEncoding(_ i: String.Index) -> Bool {
343-
isUTF8 ? i._canBeUTF8 : i._canBeUTF16
343+
i._hasMatchingEncoding(isUTF8: isUTF8)
344344
}
345345

346346
/// Return an index whose encoding can be assumed to match that of `self`.

stdlib/public/core/StringIndex.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,20 @@ extension String.Index {
441441
_encodingBits != Self.__utf8Bit
442442
}
443443

444+
/// Returns true if the encoding of this index isn't known to be in conflict
445+
/// with the specified encoding.
446+
///
447+
/// If the index was created by code that was built on a stdlib below 5.7,
448+
/// then this check may incorrectly return true on a mismatching index, but it
449+
/// is guaranteed to never incorrectly return false. If all loaded binaries
450+
/// were built in 5.7+, then this method is guaranteed to always return the
451+
/// correct value.
452+
@_alwaysEmitIntoClient // Swift 5.7
453+
@inline(__always)
454+
internal func _hasMatchingEncoding(isUTF8 utf8: Bool) -> Bool {
455+
_encodingBits != Self.__encodingBit(utf16: utf8)
456+
}
457+
444458
/// Returns the same index with the UTF-8 bit set.
445459
@_alwaysEmitIntoClient // Swift 5.7
446460
@inline(__always)

0 commit comments

Comments
 (0)