Files
swift-collections/Sources/RopeModule/BigString/Basics/BigString+Summary.swift

85 lines
2.2 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//===----------------------------------------------------------------------===//
//
// 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 {
struct Summary {
// FIXME: We only need 48 * 3 = 192 bits to represent a nonnegative value; pack these better
// (Unfortunately we also need to represent negative values right now.)
private(set) var characters: Int
private(set) var unicodeScalars: Int
private(set) var utf16: Int
private(set) var utf8: Int
init() {
characters = 0
unicodeScalars = 0
utf16 = 0
utf8 = 0
}
init(_ chunk: BigString._Chunk) {
self.utf8 = chunk.utf8Count
self.utf16 = Int(chunk.counts.utf16)
self.unicodeScalars = Int(chunk.counts.unicodeScalars)
self.characters = Int(chunk.counts.characters)
}
}
}
@available(SwiftStdlib 6.2, *)
extension BigString.Summary: CustomStringConvertible {
var description: String {
"❨\(utf8)⋅\(utf16)⋅\(unicodeScalars)⋅\(characters)❩"
}
}
@available(SwiftStdlib 6.2, *)
extension BigString.Summary: RopeSummary {
@inline(__always)
static var maxNodeSize: Int {
#if DEBUG
return 10
#else
return 15
#endif
}
@inline(__always)
static var nodeSizeBitWidth: Int { 4 }
@inline(__always)
static var zero: Self { Self() }
@inline(__always)
var isZero: Bool { utf8 == 0 }
mutating func add(_ other: BigString.Summary) {
characters += other.characters
unicodeScalars += other.unicodeScalars
utf16 += other.utf16
utf8 += other.utf8
}
mutating func subtract(_ other: BigString.Summary) {
characters -= other.characters
unicodeScalars -= other.unicodeScalars
utf16 -= other.utf16
utf8 -= other.utf8
}
}
#endif // compiler(>=6.2) && !$Embedded