Files
swift-collections/Sources/RopeModule/BString/Views/_BSubstring.UTF8View.swift
T
Karoy Lorentey 85a43d9ac6 _BString: Add a substring types and actual collection views
This is more of a technical workaround, as it allows
running generic algorithms on these things. That said,
we could embrace this in general, and sink the
`utf8Index(after:)`, `characterIndex(before:)` etc
function down into these views, instead of having them
on the top level.
2023-03-20 13:12:11 -07:00

120 lines
3.9 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
#if swift(>=5.8)
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BSubstring {
internal struct UTF8View {
internal var _base: _BString
internal var _bounds: Range<Index>
internal init(_unchecked base: _BString, in bounds: Range<Index>) {
self._base = base
self._bounds = bounds
}
internal init(_ base: _BString, in bounds: Range<Index>) {
self._base = base
let lower = base.utf8Index(roundingDown: bounds.lowerBound)
let upper = base.utf8Index(roundingDown: bounds.upperBound)
self._bounds = Range(uncheckedBounds: (lower, upper))
}
internal init(_substring: _BSubstring) {
self.init(_unchecked: _substring._base, in: _substring._bounds)
}
}
internal var utf8: UTF8View {
UTF8View(_substring: self)
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BSubstring.UTF8View {
internal var base: _BString.UTF8View { _base.utf8 }
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BSubstring.UTF8View: Equatable {
internal static func ==(left: Self, right: Self) -> Bool {
_BString.utf8IsEqual(left._base, in: left._bounds, to: right._base, in: right._bounds)
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BSubstring.UTF8View: Hashable {
internal func hash(into hasher: inout Hasher) {
_base.hashUTF8(into: &hasher, from: _bounds.lowerBound, to: _bounds.upperBound)
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BSubstring.UTF8View: BidirectionalCollection {
typealias Index = _BString.Index
typealias Element = UInt8
typealias SubSequence = Self
@inline(__always)
internal var startIndex: Index { _bounds.lowerBound }
@inline(__always)
internal var endIndex: Index { _bounds.upperBound }
@inline(__always)
internal func index(after i: Index) -> Index {
precondition(i < endIndex, "Can't advance above end index")
return _base.utf8Index(after: i)
}
@inline(__always)
internal func index(before i: Index) -> Index {
precondition(i > startIndex, "Can't advance below start index")
return _base.utf8Index(before: i)
}
@inline(__always)
internal func index(_ i: Index, offsetBy distance: Int) -> Index {
precondition(i >= startIndex && i <= endIndex, "Index out of bounds")
let j = _base.utf8Index(i, offsetBy: distance)
precondition(j >= startIndex && j <= endIndex, "Index out of bounds")
return j
}
internal func index(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
precondition(i >= startIndex && i <= endIndex, "Index out of bounds")
guard let j = _base.utf8Index(i, offsetBy: distance, limitedBy: limit) else { return nil }
precondition(j >= startIndex && j <= endIndex, "Index out of bounds")
return j
}
internal func distance(from start: Index, to end: Index) -> Int {
precondition(start >= startIndex && start <= endIndex, "Index out of bounds")
precondition(end >= startIndex && end <= endIndex, "Index out of bounds")
return _base.utf8Distance(from: start, to: end)
}
subscript(position: Index) -> UInt8 {
precondition(position >= startIndex && position < endIndex, "Index out of bounds")
return _base[utf8: position]
}
subscript(bounds: Range<Index>) -> Self {
precondition(
bounds.lowerBound >= startIndex && bounds.upperBound <= endIndex,
"Range out of bounds")
return Self(_base, in: bounds)
}
}
#endif