@@ -145,6 +145,23 @@ extension Substring {
145
145
return Range ( _uncheckedBounds: ( lower, upper) )
146
146
}
147
147
148
+ @inlinable @inline ( __always)
149
+ internal var _bounds : Range < Index > {
150
+ Range ( _uncheckedBounds: ( startIndex, endIndex) )
151
+ }
152
+ }
153
+
154
+ extension Substring {
155
+ internal var _startIsCharacterAligned : Bool {
156
+ startIndex. _isCharacterAligned
157
+ }
158
+
159
+ internal var _endIsCharacterAligned : Bool {
160
+ endIndex. _isCharacterAligned
161
+ }
162
+ }
163
+
164
+ extension Substring {
148
165
#if !INTERNAL_CHECKS_ENABLED
149
166
@inlinable @inline ( __always) internal func _invariantCheck( ) { }
150
167
#else
@@ -167,28 +184,28 @@ extension Substring {
167
184
extension Substring {
168
185
@inline ( __always)
169
186
internal func _validateScalarIndex( _ i: String . Index ) -> String . Index {
170
- _wholeGuts. validateScalarIndex ( i, from : startIndex , to : endIndex )
187
+ _wholeGuts. validateScalarIndex ( i, in : _bounds )
171
188
}
172
189
173
190
@inline ( __always)
174
191
internal func _validateInclusiveScalarIndex(
175
192
_ i: String . Index
176
193
) -> String . Index {
177
- _wholeGuts. validateInclusiveScalarIndex ( i, from : startIndex , to : endIndex )
194
+ _wholeGuts. validateInclusiveScalarIndex ( i, in : _bounds )
178
195
}
179
196
180
197
@inline ( __always)
181
198
internal func _validateScalarRange(
182
199
_ range: Range < String . Index >
183
200
) -> Range < String . Index > {
184
- _wholeGuts. validateScalarRange ( range, from : startIndex , to : endIndex )
201
+ _wholeGuts. validateScalarRange ( range, in : _bounds )
185
202
}
186
203
187
204
@inline ( __always)
188
205
internal func _roundDownToNearestCharacter(
189
206
_ i: String . Index
190
207
) -> String . Index {
191
- _wholeGuts. roundDownToNearestCharacter ( i, from : startIndex , to : endIndex )
208
+ _wholeGuts. roundDownToNearestCharacter ( i, in : _bounds )
192
209
}
193
210
194
211
/// Return true if and only if `i` is a valid index in this substring,
@@ -647,16 +664,6 @@ extension Substring: StringProtocol {
647
664
}
648
665
}
649
666
650
- extension Substring {
651
- internal var _startIsCharacterAligned : Bool {
652
- startIndex. _isCharacterAligned
653
- }
654
-
655
- internal var _endIsCharacterAligned : Bool {
656
- endIndex. _isCharacterAligned
657
- }
658
- }
659
-
660
667
extension Substring {
661
668
internal func _characterStride( startingAt i: Index ) -> Int {
662
669
_internalInvariant ( i. _isScalarAligned)
@@ -737,6 +744,11 @@ extension Substring {
737
744
738
745
@_alwaysEmitIntoClient @inline ( __always)
739
746
internal var _base : String . UTF8View { _slice. _base }
747
+
748
+ @_alwaysEmitIntoClient @inline ( __always)
749
+ internal var _bounds : Range < Index > {
750
+ Range ( _uncheckedBounds: ( _slice. _startIndex, _slice. _endIndex) )
751
+ }
740
752
}
741
753
}
742
754
@@ -833,7 +845,7 @@ extension Substring.UTF8View: BidirectionalCollection {
833
845
@inlinable
834
846
public subscript( r: Range < Index > ) -> Substring . UTF8View {
835
847
// FIXME(strings): tests.
836
- let r = _wholeGuts. validateSubscalarRange ( r, from : startIndex , to : endIndex )
848
+ let r = _wholeGuts. validateSubscalarRange ( r, in : _bounds )
837
849
return Substring . UTF8View ( _slice. base, _bounds: r)
838
850
}
839
851
}
@@ -894,6 +906,11 @@ extension Substring {
894
906
895
907
@_alwaysEmitIntoClient @inline ( __always)
896
908
internal var _base : String . UTF16View { _slice. _base }
909
+
910
+ @_alwaysEmitIntoClient @inline ( __always)
911
+ internal var _bounds : Range < Index > {
912
+ Range ( _uncheckedBounds: ( _slice. _startIndex, _slice. _endIndex) )
913
+ }
897
914
}
898
915
}
899
916
@@ -981,7 +998,7 @@ extension Substring.UTF16View: BidirectionalCollection {
981
998
982
999
@inlinable
983
1000
public subscript( r: Range < Index > ) -> Substring . UTF16View {
984
- let r = _wholeGuts. validateSubscalarRange ( r, from : startIndex , to : endIndex )
1001
+ let r = _wholeGuts. validateSubscalarRange ( r, in : _bounds )
985
1002
return Substring . UTF16View ( _slice. base, _bounds: r)
986
1003
}
987
1004
}
@@ -1039,16 +1056,31 @@ extension Substring {
1039
1056
1040
1057
/// Creates an instance that slices `base` at `_bounds`.
1041
1058
@usableFromInline // This used to be inlinable before 5.7
1042
- @available ( * , deprecated) // Use `init(_unchecked:)` in new code.
1059
+ @available ( * , deprecated, message : " Use `init(_unchecked:bounds )` in new code " )
1043
1060
internal init ( _ base: String . UnicodeScalarView , _bounds: Range < Index > ) {
1044
1061
let start = base. _guts. scalarAlign ( _bounds. lowerBound)
1045
1062
let end = base. _guts. scalarAlign ( _bounds. upperBound)
1046
1063
_slice = Slice ( base: base, bounds: Range ( _uncheckedBounds: ( start, end) ) )
1047
1064
}
1065
+ }
1066
+ }
1048
1067
1049
- @_alwaysEmitIntoClient
1050
- @inline ( __always)
1051
- internal var _wholeGuts : _StringGuts { _slice. _base. _guts }
1068
+ extension Substring . UnicodeScalarView {
1069
+ @_alwaysEmitIntoClient
1070
+ @inline ( __always)
1071
+ internal var _wholeGuts : _StringGuts { _slice. _base. _guts }
1072
+
1073
+ @inline ( __always)
1074
+ internal var _offsetRange : Range < Int > {
1075
+ let lower = _slice. _startIndex. _encodedOffset
1076
+ let upper = _slice. _endIndex. _encodedOffset
1077
+ return Range ( _uncheckedBounds: ( lower, upper) )
1078
+ }
1079
+
1080
+ @_alwaysEmitIntoClient
1081
+ @inline ( __always)
1082
+ internal var _bounds : Range < Index > {
1083
+ Range ( _uncheckedBounds: ( startIndex, endIndex) )
1052
1084
}
1053
1085
}
1054
1086
@@ -1069,8 +1101,7 @@ extension Substring.UnicodeScalarView: BidirectionalCollection {
1069
1101
1070
1102
@inlinable
1071
1103
public subscript( index: Index ) -> Element {
1072
- let index = _wholeGuts. validateScalarIndex (
1073
- index, from: startIndex, to: endIndex)
1104
+ let index = _wholeGuts. validateScalarIndex ( index, in: _bounds)
1074
1105
return _wholeGuts. errorCorrectedScalar ( startingAt: index. _encodedOffset) . 0
1075
1106
}
1076
1107
@@ -1130,7 +1161,7 @@ extension Substring.UnicodeScalarView: BidirectionalCollection {
1130
1161
1131
1162
public subscript( r: Range < Index > ) -> Substring . UnicodeScalarView {
1132
1163
// Note: This used to be inlinable until Swift 5.7
1133
- let r = _wholeGuts. validateScalarRange ( r, from : startIndex , to : endIndex )
1164
+ let r = _wholeGuts. validateScalarRange ( r, in : _bounds )
1134
1165
return Substring . UnicodeScalarView ( _unchecked: _slice. _base, bounds: r)
1135
1166
}
1136
1167
}
@@ -1175,8 +1206,7 @@ extension Substring.UnicodeScalarView: RangeReplaceableCollection {
1175
1206
_ subrange: Range < Index > , with replacement: C
1176
1207
) where C. Element == Element {
1177
1208
// TODO(lorentey): Review index validation
1178
- let subrange = _slice. _base. _guts. validateScalarRange (
1179
- subrange, from: startIndex, to: endIndex)
1209
+ let subrange = _wholeGuts. validateScalarRange ( subrange, in: _bounds)
1180
1210
_slice. replaceSubrange ( subrange, with: replacement)
1181
1211
}
1182
1212
}
0 commit comments