Skip to content

Commit

Permalink
Make _BString and _Rope public
Browse files Browse the repository at this point in the history
  • Loading branch information
lorentey committed Mar 21, 2023
1 parent 73de433 commit b022b7f
Show file tree
Hide file tree
Showing 62 changed files with 874 additions and 938 deletions.
16 changes: 8 additions & 8 deletions Sources/RopeModule/BString/Basics/_BString+Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension _BString {
/// The estimated maximum number of UTF-8 code units that `_BString` is guaranteed to be able
/// to hold without encountering an overflow in its operations. This corresponds to the capacity
/// of the deepest tree where every node is the minimum possible size.
static var minimumCapacity: Int {
public static var minimumCapacity: Int {
let c = Rope.minimumCapacity
let (r, overflow) = Chunk.minUTF8Count.multipliedReportingOverflow(by: c)
guard !overflow else { return Int.max }
Expand All @@ -25,7 +25,7 @@ extension _BString {

/// The maximum number of UTF-8 code units that `_BString` may be able to store in the best
/// possible case, when every node in the underlying tree is fully filled with data.
static var maximumCapacity: Int {
public static var maximumCapacity: Int {
let c = Rope.maximumCapacity
let (r, overflow) = Chunk.maxUTF8Count.multipliedReportingOverflow(by: c)
guard !overflow else { return Int.max }
Expand Down Expand Up @@ -85,7 +85,7 @@ extension _BString {
}

func utf8Distance(from start: Index, to end: Index) -> Int {
end._utf8Offset - start._utf8Offset
end.utf8Offset - start.utf8Offset
}
}

Expand Down Expand Up @@ -157,7 +157,7 @@ extension _BString {
if r.forward {
assert(distance >= 0)
assert(ci == chunk.string.endIndex)
d += metric.nonnegativeSize(of: chunk.summary)
d += metric._nonnegativeSize(of: chunk.summary)
let start = ri
rope.formIndex(&ri, offsetBy: &d, in: metric, preferEnd: false)
if ri == rope.endIndex {
Expand Down Expand Up @@ -269,7 +269,7 @@ extension _BString {
rope: rope.index(after: ri),
chunk: String.Index(_utf8Offset: 0))
}
return Index(_utf8Offset: i._utf8Offset + 1, rope: ri, chunkOffset: ci._utf8Offset)
return Index(_utf8Offset: i.utf8Offset + 1, rope: ri, chunkOffset: ci._utf8Offset)
}
}

Expand All @@ -294,7 +294,7 @@ extension _BString {
let ci = i._chunkIndex
if ci._utf8Offset > 0 {
return Index(
_utf8Offset: i._utf8Offset &- 1,
_utf8Offset: i.utf8Offset &- 1,
rope: ri,
chunkOffset: ci._utf8Offset &- 1)
}
Expand All @@ -310,7 +310,7 @@ extension _BString {
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString {
func characterIndex(roundingDown i: Index) -> Index {
let offset = i._utf8Offset
let offset = i.utf8Offset
precondition(offset >= 0 && offset <= utf8Count, "Index out of bounds")
guard offset > 0 else { return resolve(i, preferEnd: false)._knownCharacterAligned() }
guard offset < utf8Count else { return resolve(i, preferEnd: true)._knownCharacterAligned() }
Expand Down Expand Up @@ -416,7 +416,7 @@ extension _BString {
extension _BString {
func _character(at start: Index) -> (character: Character, end: Index) {
let start = characterIndex(roundingDown: start)
precondition(start._utf8Offset < utf8Count, "Index out of bounds")
precondition(start.utf8Offset < utf8Count, "Index out of bounds")

var ri = start._rope!
var ci = start._chunkIndex
Expand Down
2 changes: 1 addition & 1 deletion Sources/RopeModule/BString/Basics/_BString+Debugging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString {
func dump(heightLimit: Int = .max) {
public func dump(heightLimit: Int = .max) {
rope.dump(heightLimit: heightLimit)
}
}
Expand Down
31 changes: 18 additions & 13 deletions Sources/RopeModule/BString/Basics/_BString+Index.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString {
internal struct Index: Sendable {
public struct Index: Sendable {
typealias Rope = _BString.Rope

// ┌───────────────────────────────┬───┬───────────┬────────────────────┐
Expand Down Expand Up @@ -61,7 +61,12 @@ extension _BString.Index {
@inline(__always)
internal static var _scalarAlignmentBit: UInt64 { 0x100 }

@available(*, deprecated, renamed: "utf8Offset")
internal var _utf8Offset: Int {
utf8Offset
}

public var utf8Offset: Int {
Int(truncatingIfNeeded: _rawBits &>> 11)
}

Expand All @@ -78,7 +83,7 @@ extension _BString.Index {

/// The base offset of the addressed chunk. Only valid if `_rope` is not nil.
internal var _utf8BaseOffset: Int {
_utf8Offset - _utf8ChunkOffset
utf8Offset - _utf8ChunkOffset
}

@inline(__always)
Expand Down Expand Up @@ -180,30 +185,30 @@ extension _BString.Index {

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString.Index: Equatable {
static func ==(left: Self, right: Self) -> Bool {
public static func ==(left: Self, right: Self) -> Bool {
left._orderingValue == right._orderingValue
}
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString.Index: Comparable {
static func <(left: Self, right: Self) -> Bool {
public static func <(left: Self, right: Self) -> Bool {
left._orderingValue < right._orderingValue
}
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString.Index: Hashable {
func hash(into hasher: inout Hasher) {
public func hash(into hasher: inout Hasher) {
hasher.combine(_orderingValue)
}
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString.Index: CustomStringConvertible {
internal var description: String {
public var description: String {
let utf16Offset = _isUTF16TrailingSurrogate ? "+1" : ""
return "\(_utf8Offset)[utf8]\(utf16Offset)"
return "\(utf8Offset)[utf8]\(utf16Offset)"
}
}

Expand All @@ -212,29 +217,29 @@ extension _BString {
func resolve(_ i: Index, preferEnd: Bool) -> Index {
if var ri = i._rope, rope.isValid(ri) {
if preferEnd {
guard i._utf8Offset > 0, i._utf8ChunkOffset == 0 else { return i }
guard i.utf8Offset > 0, i._utf8ChunkOffset == 0 else { return i }
rope.formIndex(before: &ri)
let length = rope[ri].utf8Count
let ci = String.Index(_utf8Offset: length)
var j = Index(baseUTF8Offset: i._utf8Offset - length, rope: ri, chunk: ci)
var j = Index(baseUTF8Offset: i.utf8Offset - length, rope: ri, chunk: ci)
j._flags = i._flags
return j
}
guard i._utf8Offset < utf8Count, i._utf8ChunkOffset == rope[ri].utf8Count else {
guard i.utf8Offset < utf8Count, i._utf8ChunkOffset == rope[ri].utf8Count else {
return i
}
rope.formIndex(after: &ri)
let ci = String.Index(_utf8Offset: 0)
var j = Index(baseUTF8Offset: i._utf8Offset, rope: ri, chunk: ci)
var j = Index(baseUTF8Offset: i.utf8Offset, rope: ri, chunk: ci)
j._flags = i._flags
return j
}

let (ri, chunkOffset) = rope.find(
at: i._utf8Offset, in: UTF8Metric(), preferEnd: preferEnd)
at: i.utf8Offset, in: UTF8Metric(), preferEnd: preferEnd)
let ci = String.Index(
_utf8Offset: chunkOffset, utf16TrailingSurrogate: i._isUTF16TrailingSurrogate)
return Index(baseUTF8Offset: i._utf8Offset - ci._utf8Offset, rope: ri, chunk: ci)
return Index(baseUTF8Offset: i.utf8Offset - ci._utf8Offset, rope: ri, chunk: ci)
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString {
func invariantCheck() {
public func invariantCheck() {
#if DEBUG
rope.invariantCheck()
let allowUndersize = rope.isSingleton
Expand Down

0 comments on commit b022b7f

Please sign in to comment.