Skip to content

Commit c9278a1

Browse files
committed
[stdlib] Remove _StringCore from UTF8View.Index
1 parent cd59716 commit c9278a1

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

stdlib/public/core/StringUTF16.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ extension String.UTF16View.Index {
407407
"Invalid String.UTF8Index for this UTF-16 view")
408408

409409
// Detect positions that have no corresponding index.
410-
if !utf8Index._isOnUnicodeScalarBoundary {
410+
if !utf8Index._isOnUnicodeScalarBoundary(in: core) {
411411
return nil
412412
}
413413
_offset = utf8Index._coreIndex

stdlib/public/core/StringUTF8.swift

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ extension String {
174174

175175
init(_ _core: _StringCore) {
176176
self._core = _core
177-
self._endIndex = Index(_core, _core.endIndex, Index._emptyBuffer)
177+
self._endIndex = Index(_coreIndex: _core.endIndex, Index._emptyBuffer)
178178
if _fastPath(_core.count != 0) {
179179
let (_, buffer) = _core._encodeSomeUTF8(from: 0)
180-
self._startIndex = Index(_core, 0, buffer)
180+
self._startIndex = Index(_coreIndex: 0, buffer)
181181
} else {
182182
self._startIndex = self._endIndex
183183
}
@@ -208,27 +208,23 @@ extension String {
208208
public struct Index : Comparable {
209209
internal typealias Buffer = _StringCore._UTF8Chunk
210210

211-
init(_ _core: _StringCore, _ _coreIndex: Int,
212-
_ _buffer: Buffer) {
213-
self._core = _core
211+
init(_coreIndex: Int, _ _buffer: Buffer) {
214212
self._coreIndex = _coreIndex
215213
self._buffer = _buffer
216-
_sanityCheck(_coreIndex >= 0)
217-
_sanityCheck(_coreIndex <= _core.count)
218214
}
219215

220216
/// True iff the index is at the end of its view or if the next
221217
/// byte begins a new UnicodeScalar.
222-
internal var _isOnUnicodeScalarBoundary: Bool {
218+
internal func _isOnUnicodeScalarBoundary(in core: _StringCore) -> Bool {
223219
let buffer = UInt32(truncatingBitPattern: _buffer)
224220
let (codePoint, _) = UTF8._decodeOne(buffer)
225-
return codePoint != nil || _isAtEnd
221+
return codePoint != nil || _isEndIndex(of: core)
226222
}
227223

228224
/// True iff the index is at the end of its view
229-
internal var _isAtEnd: Bool {
225+
internal func _isEndIndex(of core: _StringCore) -> Bool {
230226
return _buffer == Index._emptyBuffer
231-
&& _coreIndex == _core.endIndex
227+
&& _coreIndex == core.endIndex
232228
}
233229

234230
/// The value of the buffer when it is empty
@@ -247,13 +243,12 @@ extension String {
247243
return (thisBuffer >> 8) | _bufferHiByte
248244
}
249245

250-
/// The underlying buffer we're presenting as UTF-8
251-
internal let _core: _StringCore
252246
/// The position of `self`, rounded up to the nearest unicode
253247
/// scalar boundary, in the underlying UTF-16.
254248
internal let _coreIndex: Int
255-
/// If `self` is at the end of its `_core`, has the value `_endBuffer`.
256-
/// Otherwise, the low byte contains the value of
249+
/// If `self` is at the end of its `_core`, has the value `_emptyBuffer`.
250+
/// Otherwise, the low byte contains the value of the UTF-8 code unit
251+
/// at this position.
257252
internal let _buffer: Buffer
258253
}
259254

@@ -292,21 +287,21 @@ extension String {
292287
let nextCoreIndex = i._coreIndex &+ increment
293288
let nextBuffer = Index._nextBuffer(after: i._buffer)
294289

295-
// if the nextBuffer is nonempty, we have all we need
290+
// If the nextBuffer is nonempty, we have all we need
296291
if _fastPath(nextBuffer != Index._emptyBuffer) {
297-
return Index(i._core, nextCoreIndex, nextBuffer)
292+
return Index(_coreIndex: nextCoreIndex, nextBuffer)
298293
}
299294
// If the underlying UTF16 isn't exhausted, fill a new buffer
300-
else if _fastPath(nextCoreIndex < i._core.endIndex) {
301-
let (_, freshBuffer) = i._core._encodeSomeUTF8(from: nextCoreIndex)
302-
return Index(_core, nextCoreIndex, freshBuffer)
295+
else if _fastPath(nextCoreIndex < _core.endIndex) {
296+
let (_, freshBuffer) = _core._encodeSomeUTF8(from: nextCoreIndex)
297+
return Index(_coreIndex: nextCoreIndex, freshBuffer)
303298
}
304299
else {
305300
// Produce the endIndex
306301
_precondition(
307-
nextCoreIndex == i._core.endIndex,
302+
nextCoreIndex == _core.endIndex,
308303
"Can't increment past endIndex of String.UTF8View")
309-
return Index(_core, nextCoreIndex, nextBuffer)
304+
return Index(_coreIndex: nextCoreIndex, nextBuffer)
310305
}
311306
}
312307

@@ -478,7 +473,7 @@ public func < (
478473
extension String.UTF8View.Index {
479474
internal init(_ core: _StringCore, _utf16Offset: Int) {
480475
let (_, buffer) = core._encodeSomeUTF8(from: _utf16Offset)
481-
self.init(core, _utf16Offset, buffer)
476+
self.init(_coreIndex: _utf16Offset, buffer)
482477
}
483478

484479
/// Creates an index in the given UTF-8 view that corresponds exactly to the

stdlib/public/core/StringUnicodeScalarView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ extension String.UnicodeScalarIndex {
471471
"Invalid String.UTF8Index for this UnicodeScalar view")
472472

473473
// Detect positions that have no corresponding index.
474-
if !utf8Index._isOnUnicodeScalarBoundary {
474+
if !utf8Index._isOnUnicodeScalarBoundary(in: core) {
475475
return nil
476476
}
477477
self.init(_position: utf8Index._coreIndex)

0 commit comments

Comments
 (0)