Skip to content

Commit f7c674e

Browse files
committed
[stdlib] Slice._bounds, Range<String.Index>._encodedOffsetRange: New helpers
1 parent ff58d54 commit f7c674e

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

stdlib/public/core/Range.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,3 +1027,14 @@ extension PartialRangeUpTo: Sendable where Bound: Sendable { }
10271027
extension PartialRangeThrough: Sendable where Bound: Sendable { }
10281028
extension PartialRangeFrom: Sendable where Bound: Sendable { }
10291029
extension PartialRangeFrom.Iterator: Sendable where Bound: Sendable { }
1030+
1031+
extension Range where Bound == String.Index {
1032+
@_alwaysEmitIntoClient // Swift 5.7
1033+
internal var _encodedOffsetRange: Range<Int> {
1034+
_internalInvariant(
1035+
(lowerBound._canBeUTF8 && upperBound._canBeUTF8)
1036+
|| (lowerBound._canBeUTF16 && upperBound._canBeUTF16))
1037+
return Range<Int>(
1038+
_uncheckedBounds: (lowerBound._encodedOffset, upperBound._encodedOffset))
1039+
}
1040+
}

stdlib/public/core/Slice.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public struct Slice<Base: Collection> {
135135
public var base: Base {
136136
return _base
137137
}
138+
139+
@_alwaysEmitIntoClient @inline(__always)
140+
internal var _bounds: Range<Base.Index> {
141+
Range(_uncheckedBounds: (_startIndex, _endIndex))
142+
}
138143
}
139144

140145
extension Slice: Collection {
@@ -157,21 +162,21 @@ extension Slice: Collection {
157162
@inlinable // generic-performance
158163
public subscript(index: Index) -> Base.Element {
159164
get {
160-
_failEarlyRangeCheck(index, bounds: startIndex..<endIndex)
165+
_failEarlyRangeCheck(index, bounds: _bounds)
161166
return _base[index]
162167
}
163168
}
164169

165170
@inlinable // generic-performance
166171
public subscript(bounds: Range<Index>) -> Slice<Base> {
167172
get {
168-
_failEarlyRangeCheck(bounds, bounds: startIndex..<endIndex)
173+
_failEarlyRangeCheck(bounds, bounds: _bounds)
169174
return Slice(base: _base, bounds: bounds)
170175
}
171176
}
172177

173-
public var indices: Indices {
174-
return _base.indices[_startIndex..<_endIndex]
178+
public var indices: Indices {
179+
return _base.indices[_bounds]
175180
}
176181

177182
@inlinable // generic-performance
@@ -264,11 +269,11 @@ extension Slice: MutableCollection where Base: MutableCollection {
264269
@inlinable // generic-performance
265270
public subscript(index: Index) -> Base.Element {
266271
get {
267-
_failEarlyRangeCheck(index, bounds: startIndex..<endIndex)
272+
_failEarlyRangeCheck(index, bounds: _bounds)
268273
return _base[index]
269274
}
270275
set {
271-
_failEarlyRangeCheck(index, bounds: startIndex..<endIndex)
276+
_failEarlyRangeCheck(index, bounds: _bounds)
272277
_base[index] = newValue
273278
// MutableSlice requires that the underlying collection's subscript
274279
// setter does not invalidate indices, so our `startIndex` and `endIndex`
@@ -279,7 +284,7 @@ extension Slice: MutableCollection where Base: MutableCollection {
279284
@inlinable // generic-performance
280285
public subscript(bounds: Range<Index>) -> Slice<Base> {
281286
get {
282-
_failEarlyRangeCheck(bounds, bounds: startIndex..<endIndex)
287+
_failEarlyRangeCheck(bounds, bounds: _bounds)
283288
return Slice(base: _base, bounds: bounds)
284289
}
285290
set {

stdlib/public/core/Substring.swift

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ public struct Substring: Sendable {
113113
@usableFromInline // This used to be @inlinable before 5.7
114114
@available(*, deprecated) // Use `init(_unchecked:)` in new code.
115115
internal init(_ slice: Slice<String>) {
116-
let r = slice.base._guts.validateScalarRange(
117-
slice.startIndex ..< slice.endIndex)
118-
self._slice = Slice(base: slice.base, bounds: r)
116+
let r = slice._base._guts.validateScalarRange(slice._bounds)
117+
self._slice = Slice(base: slice._base, bounds: r)
119118
_invariantCheck()
120119
}
121120

@@ -137,19 +136,13 @@ extension Substring {
137136
public var base: String { return _slice._base }
138137

139138
@inlinable @inline(__always)
140-
internal var _wholeGuts: _StringGuts { return _slice._base._guts }
139+
internal var _wholeGuts: _StringGuts { _slice._base._guts }
141140

142141
@inlinable @inline(__always)
143-
internal var _offsetRange: Range<Int> {
144-
let lower = _slice._startIndex._encodedOffset
145-
let upper = _slice._endIndex._encodedOffset
146-
return Range(_uncheckedBounds: (lower, upper))
147-
}
142+
internal var _offsetRange: Range<Int> { _slice._bounds._encodedOffsetRange }
148143

149144
@inlinable @inline(__always)
150-
internal var _bounds: Range<Index> {
151-
Range(_uncheckedBounds: (startIndex, endIndex))
152-
}
145+
internal var _bounds: Range<Index> { _slice._bounds }
153146
}
154147

155148
extension Substring {
@@ -746,9 +739,7 @@ extension Substring {
746739
internal var _base: String.UTF8View { _slice._base }
747740

748741
@_alwaysEmitIntoClient @inline(__always)
749-
internal var _bounds: Range<Index> {
750-
Range(_uncheckedBounds: (_slice._startIndex, _slice._endIndex))
751-
}
742+
internal var _bounds: Range<Index> { _slice._bounds }
752743
}
753744
}
754745

@@ -912,9 +903,7 @@ extension Substring {
912903
internal var _base: String.UTF16View { _slice._base }
913904

914905
@_alwaysEmitIntoClient @inline(__always)
915-
internal var _bounds: Range<Index> {
916-
Range(_uncheckedBounds: (_slice._startIndex, _slice._endIndex))
917-
}
906+
internal var _bounds: Range<Index> { _slice._bounds }
918907
}
919908
}
920909

@@ -1075,21 +1064,15 @@ extension Substring {
10751064
}
10761065

10771066
extension Substring.UnicodeScalarView {
1078-
@_alwaysEmitIntoClient
1079-
@inline(__always)
1067+
@_alwaysEmitIntoClient @inline(__always)
10801068
internal var _wholeGuts: _StringGuts { _slice._base._guts }
10811069

10821070
@inline(__always)
1083-
internal var _offsetRange: Range<Int> {
1084-
let lower = _slice._startIndex._encodedOffset
1085-
let upper = _slice._endIndex._encodedOffset
1086-
return Range(_uncheckedBounds: (lower, upper))
1087-
}
1071+
internal var _offsetRange: Range<Int> { _slice._bounds._encodedOffsetRange }
10881072

10891073
@_alwaysEmitIntoClient
10901074
@inline(__always)
1091-
internal var _bounds: Range<Index> {
1092-
Range(_uncheckedBounds: (startIndex, endIndex))
1075+
internal var _bounds: Range<Index> { _slice._bounds }
10931076
}
10941077
}
10951078

0 commit comments

Comments
 (0)