Skip to content

Commit 2e9fd9e

Browse files
committed
[stdlib] Substring.UnicodeScalarView: Add _invariantCheck
1 parent f7c674e commit 2e9fd9e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ extension _StringGuts {
357357
/// index, but it is guaranteed to never incorrectly return false. If all
358358
/// loaded binaries were built in 5.7+, then this method is guaranteed to
359359
/// always return the correct value.
360+
@_alwaysEmitIntoClient
360361
internal func hasMatchingEncoding(_ i: String.Index) -> Bool {
361362
(isForeign && i._canBeUTF16) || (!isForeign && i._canBeUTF8)
362363
}

stdlib/public/core/Substring.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ extension Substring {
10501050
_unchecked base: String.UnicodeScalarView, bounds: Range<Index>
10511051
) {
10521052
_slice = Slice(base: base, bounds: bounds)
1053+
_invariantCheck()
10531054
}
10541055

10551056
/// Creates an instance that slices `base` at `_bounds`.
@@ -1073,7 +1074,25 @@ extension Substring.UnicodeScalarView {
10731074
@_alwaysEmitIntoClient
10741075
@inline(__always)
10751076
internal var _bounds: Range<Index> { _slice._bounds }
1077+
}
1078+
1079+
extension Substring.UnicodeScalarView {
1080+
#if !INTERNAL_CHECKS_ENABLED
1081+
@_alwaysEmitIntoClient @inline(__always)
1082+
internal func _invariantCheck() {}
1083+
#else
1084+
@_alwaysEmitIntoClient
1085+
@inline(never) @_effects(releasenone)
1086+
internal func _invariantCheck() {
1087+
_internalInvariant(endIndex <= _wholeGuts.endIndex)
1088+
_internalInvariant(
1089+
_wholeGuts.hasMatchingEncoding(startIndex) &&
1090+
_wholeGuts.hasMatchingEncoding(endIndex))
1091+
_internalInvariant(
1092+
startIndex._isScalarAligned && endIndex._isScalarAligned)
1093+
_slice._base._invariantCheck()
10761094
}
1095+
#endif // INTERNAL_CHECKS_ENABLED
10771096
}
10781097

10791098
extension Substring.UnicodeScalarView: BidirectionalCollection {

0 commit comments

Comments
 (0)