//===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project // // Copyright (c) 2023 - 2026 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 // // SPDX-License-Identifier: Apache-2.0 WITH Swift-exception // //===----------------------------------------------------------------------===// #if compiler(>=6.2) && !$Embedded @available(SwiftStdlib 6.2, *) extension BigString._Chunk { mutating func append(_ other: borrowing BigString._Chunk) { _append(other.utf8Span, 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 = scalarIndex(roundingDown: Index(utf8Offset: 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: consuming Substring, _ other: Counts) { _append(other) { _ = $0.initialize(from: str.utf8) } } mutating func _append(_ span: UTF8Span, _ other: Counts) { _append(other) { buffer in span.span.withUnsafeBufferPointer { _ = buffer.initialize(fromContentsOf: $0) } } } mutating func _append( _ newCounts: Counts, _ body: (UnsafeMutableBufferPointer) -> () ) { ensureUnique() let utf8Before = Int(counts.utf8) counts.append(newCounts) storage.withUnsafeMutablePointerToElements { let buffer = UnsafeMutableBufferPointer( start: $0, count: Self.maxUTF8Count ) body(buffer.extracting(utf8Before...)) } invariantCheck() } // Note: This assumes you've already called 'ensureUnique()' mutating func _shift(_ range: Range, to: Index) { let originalRange = range.lowerBound.utf8Offset.. Index? { let count = slice.string.utf8.count precondition(utf8Count + count <= Self.maxUTF8Count) let parts = self.splitCounts(at: index) // Shift the right parts down to make space for the insertion. let from = index.utf8Offset let to = index.utf8Offset + slice.string.utf8.count _shift(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 = index.utf8Offset 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: Index ) -> InsertResult { ensureUnique() let origCount = self.utf8Count let rem = ingester.remainingUTF8 guard rem > 0 else { return .inline(nil) } let sum = origCount + rem let offset = index.utf8Offset 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 desiredIndex = Index(utf8Offset: desired) let cut = scalarIndex(roundingDown: desiredIndex) var spawn = split(at: cut) let i = Index(utf8Offset: offset - 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 i = Index(utf8Offset: desired - rem) let cut = scalarIndex(roundingDown: i) assert(cut >= index) var spawn = split(at: cut) guard var r = self.insertAll(from: &ingester, at: Index(utf8Offset: offset)), nil == spawn.resyncBreaks(startingAt: spawn.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.startIndex, old: &old, new: &new) if stop != nil { return .split(spawn: spawn, endStates: nil) } return .split(spawn: spawn, endStates: (sum - offset, old, new)) } } #endif // compiler(>=6.2) && !$Embedded