1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2023 - 2024 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 BigString._Chunk {
mutating func append(_ other: __owned Self) {
self._append(other.string[...], other.counts)
}
mutating func append(from ingester: inout BigString._Ingester) -> Self? {
let desired = BigString._Ingester.desiredNextChunkSize(
remaining: self.utf8Count + ingester.remainingUTF8)
if desired == self.utf8Count {
return nil
}
if desired > self.utf8Count {
if let slice = ingester.nextSlice(maxUTF8Count: desired - self.utf8Count) {
self._append(slice)
}
return nil
}
// Split current chunk.
let cut = string.unicodeScalars._index(roundingDown: string._utf8Index(at: desired))
var new = self.split(at: cut)
precondition(!self.isUndersized)
let slice = ingester.nextSlice()!
new._append(slice)
precondition(ingester.isAtEnd)
precondition(!new.isUndersized)
return new
}
mutating func _append(_ other: Slice) {
let c = Counts(other)
_append(other.string, c)
}
mutating func _append(_ str: __owned Substring, _ other: Counts) {
self.counts.append(other)
self.string += str
invariantCheck()
}
mutating func _prepend(_ other: Slice) {
let c = Counts(other)
_prepend(other.string, c)
}
mutating func _prepend(_ str: __owned Substring, _ other: Counts) {
let c = self.counts
self.counts = other
self.counts.append(c)
self.string = str + self.string
invariantCheck()
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString._Chunk {
mutating func _insert(
_ slice: Slice,
at index: String.Index,
old: inout _CharacterRecognizer,
new: inout _CharacterRecognizer
) -> String.Index? {
let offset = string._utf8Offset(of: index)
let count = slice.string.utf8.count
precondition(utf8Count + count <= Self.maxUTF8Count)
let parts = self.splitCounts(at: index)
self.counts = parts.left
self.counts.append(Counts(slice))
self.counts.append(parts.right)
string.insert(contentsOf: slice.string, at: index)
let end = string._utf8Index(at: offset + count)
return resyncBreaks(startingAt: end, old: &old, new: &new)
}
typealias States = (increment: Int, old: _CharacterRecognizer, new: _CharacterRecognizer)
mutating func insertAll(
from ingester: inout BigString._Ingester,
at index: String.Index
) -> States? {
let remaining = ingester.remainingUTF8
precondition(self.utf8Count + remaining <= Self.maxUTF8Count)
var startState = ingester.state
guard let slice = ingester.nextSlice(maxUTF8Count: remaining) else { return nil }
var endState = ingester.state
assert(ingester.isAtEnd)
let offset = string._utf8Offset(of: index)
if let _ = self._insert(slice, at: index, old: &startState, new: &endState) {
return nil
}
return (self.utf8Count - offset, startState, endState)
}
enum InsertResult {
case inline(States?)
case split(spawn: BigString._Chunk, endStates: States?)
case large
}
mutating func insert(
from ingester: inout BigString._Ingester,
at index: String.Index
) -> InsertResult {
let origCount = self.utf8Count
let rem = ingester.remainingUTF8
guard rem > 0 else { return .inline(nil) }
let sum = origCount + rem
let offset = string._utf8Offset(of: index)
if sum <= Self.maxUTF8Count {
let r = insertAll(from: &ingester, at: index)
return .inline(r)
}
let desired = BigString._Ingester.desiredNextChunkSize(remaining: sum)
guard sum - desired + Self.maxSlicingError <= Self.maxUTF8Count else { return .large }
if desired <= offset {
// Inserted text lies entirely within `spawn`.
let cut = string.unicodeScalars._index(roundingDown: string._utf8Index(at: desired))
var spawn = split(at: cut)
let i = spawn.string._utf8Index(at: offset - self.utf8Count)
let r = spawn.insertAll(from: &ingester, at: i)
assert(r == nil || r?.increment == sum - offset)
return .split(spawn: spawn, endStates: r)
}
if desired >= offset + rem {
// Inserted text lies entirely within `self`.
let cut = string.unicodeScalars._index(roundingDown: string._utf8Index(at: desired - rem))
assert(cut >= index)
var spawn = split(at: cut)
guard
var r = self.insertAll(from: &ingester, at: string._utf8Index(at: offset)),
nil == spawn.resyncBreaks(startingAt: spawn.string.startIndex, old: &r.old, new: &r.new)
else {
return .split(spawn: spawn, endStates: nil)
}
return .split(spawn: spawn, endStates: (sum - offset, r.old, r.new))
}
// Inserted text is split across `self` and `spawn`.
var spawn = split(at: index)
var old = ingester.state
if let slice = ingester.nextSlice(maxUTF8Count: desired - offset) {
self._append(slice)
}
let slice = ingester.nextSlice()!
assert(ingester.isAtEnd)
var new = ingester.state
let stop = spawn._insert(slice, at: spawn.string.startIndex, old: &old, new: &new)
if stop != nil {
return .split(spawn: spawn, endStates: nil)
}
return .split(spawn: spawn, endStates: (sum - offset, old, new))
}
}
#endif
|