From eabd9417991fee880a566951c223a0adcaf0832b Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 1 Feb 2023 00:09:44 -0800 Subject: [PATCH] Add some operations taking subsequences --- .../BString/Basics/_BString+Builder.swift | 7 +- .../BString/Operations/_BString+Append.swift | 34 ++++- .../Operations/_BString+Initializers.swift | 31 +++- .../BString/Operations/_BString+Insert.swift | 19 +++ .../Operations/_BString+ReplaceSubrange.swift | 22 +++ .../BigString/BigString+CharacterView.swift | 24 +++- .../BigString+UnicodeScalarView.swift | 18 +++ .../Rope/Basics/_Rope+Builder.swift | 134 +++++++++--------- .../Rope/Operations/_Rope+Extract.swift | 84 +++++++++++ .../Operations/_Rope+RemoveSubrange.swift | 4 +- .../Utilities/Range Utilities.swift | 22 +++ Tests/RopeModuleTests/TestBigString.swift | 25 ++++ 12 files changed, 344 insertions(+), 80 deletions(-) create mode 100644 Sources/RopeModule/Rope/Operations/_Rope+Extract.swift create mode 100644 Sources/RopeModule/Utilities/Range Utilities.swift diff --git a/Sources/RopeModule/BString/Basics/_BString+Builder.swift b/Sources/RopeModule/BString/Basics/_BString+Builder.swift index 5a4fb631..a2a77e90 100644 --- a/Sources/RopeModule/BString/Basics/_BString+Builder.swift +++ b/Sources/RopeModule/BString/Basics/_BString+Builder.swift @@ -94,7 +94,12 @@ extension _BString.Builder { var state = _CharacterRecognizer() append(other.rope, state: &state) } - + + mutating func append(_ other: __owned _BString, in range: Range<_BString.Index>) { + let extract = _BString(other, in: range, state: &self.prefixEndState) + self.base.append(extract.rope) + } + mutating func append(_ other: __owned Rope, state: inout _CharacterRecognizer) { guard !other.isEmpty else { return } var other = _BString(rope: other) diff --git a/Sources/RopeModule/BString/Operations/_BString+Append.swift b/Sources/RopeModule/BString/Operations/_BString+Append.swift index 0806bb60..e22fcd56 100644 --- a/Sources/RopeModule/BString/Operations/_BString+Append.swift +++ b/Sources/RopeModule/BString/Operations/_BString+Append.swift @@ -66,21 +66,49 @@ extension _BString { _ = other.resyncBreaks(old: &old, new: &new) _append(other) } - + + mutating func append(contentsOf other: __owned _BString, in range: Range) { + guard !range._isEmptyUTF8 else { return } + guard !self.isEmpty else { + self = Self(other, in: range) + return + } + + var other = _BString(other, in: range) + let hint = other._firstUnicodeScalar + var old = _CharacterRecognizer() + var new = self._breakState(upTo: endIndex, nextScalarHint: hint).state + _ = other.rope.resyncBreaks(old: &old, new: &new) + _append(other.rope) + } + mutating func prepend(contentsOf other: __owned _BString) { guard !other.isEmpty else { return } guard !self.isEmpty else { self = other return } - let hint = self._firstUnicodeScalar var old = _CharacterRecognizer() var new = other._breakState(upTo: other.endIndex, nextScalarHint: hint).state _ = self.rope.resyncBreaks(old: &old, new: &new) - + _prepend(other.rope) } + + mutating func prepend(contentsOf other: __owned _BString, in range: Range) { + guard !range._isEmptyUTF8 else { return } + let extract = Self(other, in: range) + guard !self.isEmpty else { + self = extract + return + } + let hint = self._firstUnicodeScalar + var old = _CharacterRecognizer() + var new = extract._breakState(upTo: extract.endIndex, nextScalarHint: hint).state + _ = self.rope.resyncBreaks(old: &old, new: &new) + _prepend(extract.rope) + } } extension _BString { diff --git a/Sources/RopeModule/BString/Operations/_BString+Initializers.swift b/Sources/RopeModule/BString/Operations/_BString+Initializers.swift index 4c0060af..11453502 100644 --- a/Sources/RopeModule/BString/Operations/_BString+Initializers.swift +++ b/Sources/RopeModule/BString/Operations/_BString+Initializers.swift @@ -46,6 +46,33 @@ extension _BString { builder.append(piece) self = builder.finalize() } + + internal init( + _ other: Self, + in range: Range + ) { + self.rope = other.rope.extract( + from: range.lowerBound._utf8Offset, + to: range.upperBound._utf8Offset, + in: UTF8Metric()) + var old = other._breakState(upTo: range.lowerBound).state + var new = _CharacterRecognizer() + _ = self.rope.resyncBreaks(old: &old, new: &new) + } + + internal init( + _ other: Self, + in range: Range, + state: inout _CharacterRecognizer + ) { + self.rope = other.rope.extract( + from: range.lowerBound._utf8Offset, + to: range.upperBound._utf8Offset, + in: UTF8Metric()) + var old = other._breakState(upTo: range.lowerBound).state + var new = state + self.rope.resyncBreaksToEnd(old: &old, new: &new) + } } extension String { @@ -59,7 +86,7 @@ extension String { internal init(_from big: _BString, in range: Range<_BString.Index>) { self.init() - guard range.lowerBound._utf8Offset < range.upperBound._utf8Offset else { + guard range._isEmptyUTF8 else { // We can safely ignore UTF-16 offsets, as we're rounding down to scalar boundaries // anyway. return @@ -71,7 +98,7 @@ extension String { self += start.chunk.string[start.path.chunk ..< end.path.chunk] return } - + self += start.chunk.string[start.path.chunk...] var it = big.rope.makeIterator(from: start.path.rope) diff --git a/Sources/RopeModule/BString/Operations/_BString+Insert.swift b/Sources/RopeModule/BString/Operations/_BString+Insert.swift index e73045bb..d0fdffce 100644 --- a/Sources/RopeModule/BString/Operations/_BString+Insert.swift +++ b/Sources/RopeModule/BString/Operations/_BString+Insert.swift @@ -76,3 +76,22 @@ extension _BString { self = builder.finalize() } } + +extension _BString { + mutating func insert(contentsOf other: __owned Self, in range: Range, at index: Index) { + guard index < endIndex else { + precondition(index == endIndex, "Index out of bounds") + self.append(contentsOf: other, in: range) + return + } + guard index > startIndex else { + self.prepend(contentsOf: other, in: range) + return + } + guard !range._isEmptyUTF8 else { return } + // FIXME: Fast path when `other` is tiny. + var builder = self.split(at: index) + builder.append(other, in: range) + self = builder.finalize() + } +} diff --git a/Sources/RopeModule/BString/Operations/_BString+ReplaceSubrange.swift b/Sources/RopeModule/BString/Operations/_BString+ReplaceSubrange.swift index bafec9ee..f3af7655 100644 --- a/Sources/RopeModule/BString/Operations/_BString+ReplaceSubrange.swift +++ b/Sources/RopeModule/BString/Operations/_BString+ReplaceSubrange.swift @@ -33,6 +33,28 @@ extension _BString { self = builder.finalize() } + mutating func replaceSubrange( + _ targetRange: Range, + with newElements: _BString, + in sourceRange: Range + ) { + if sourceRange._isEmptyUTF8 { + removeSubrange(targetRange) + return + } + if targetRange._isEmptyUTF8 { + insert( + contentsOf: newElements, + in: sourceRange, + at: targetRange.lowerBound) + return + } + precondition(targetRange.upperBound <= endIndex, "Index out of bounds") + var builder = _split(removing: targetRange) + builder.append(newElements, in: sourceRange) + self = builder.finalize() + } + mutating func _split(removing range: Range) -> Builder { let lower = range.lowerBound._utf8Offset let upper = range.upperBound._utf8Offset diff --git a/Sources/RopeModule/BigString/BigString+CharacterView.swift b/Sources/RopeModule/BigString/BigString+CharacterView.swift index 48ab9e4f..50b5f61b 100644 --- a/Sources/RopeModule/BigString/BigString+CharacterView.swift +++ b/Sources/RopeModule/BigString/BigString+CharacterView.swift @@ -117,12 +117,6 @@ extension BigString: BidirectionalCollection { } } -extension Range where Bound == BigString.Index { - var _base: Range<_BString.Index> { - Range<_BString.Index>(uncheckedBounds: (lowerBound._value, upperBound._value)) - } -} - extension BigString: RangeReplaceableCollection { public mutating func replaceSubrange( _ subrange: Range, @@ -186,6 +180,12 @@ extension BigString: RangeReplaceableCollection { self._guts = elements._guts } + public init(_ elements: Self.SubSequence) { + let lower = elements.startIndex._value + let upper = elements.endIndex._value + self._guts = _BString(elements.base._guts, in: lower ..< upper) + } + public init(repeating repeatedValue: Character, count: Int) { self._guts = _BString(repeating: _BString(String(repeatedValue)), count: count) } @@ -226,6 +226,12 @@ extension BigString: RangeReplaceableCollection { _guts.append(contentsOf: newElements._guts) } + public mutating func append(contentsOf newElements: __owned Self.SubSequence) { + let lower = newElements.startIndex._value + let upper = newElements.endIndex._value + _guts.append(contentsOf: newElements.base._guts, in: lower ..< upper) + } + public mutating func insert(_ newElement: __owned Character, at i: Index) { _guts.insert(contentsOf: String(newElement), at: i._value) } @@ -254,6 +260,12 @@ extension BigString: RangeReplaceableCollection { _guts.insert(contentsOf: newElements._guts, at: i._value) } + public mutating func insert(contentsOf newElements: __owned Self.SubSequence, at i: Index) { + let lower = newElements.startIndex._value + let upper = newElements.endIndex._value + _guts.insert(contentsOf: newElements.base._guts, in: lower ..< upper, at: i._value) + } + @discardableResult public mutating func remove(at i: Index) -> Character { _guts.removeCharacter(at: i._value) diff --git a/Sources/RopeModule/BigString/BigString+UnicodeScalarView.swift b/Sources/RopeModule/BigString/BigString+UnicodeScalarView.swift index 65a3bdcb..dbce1e65 100644 --- a/Sources/RopeModule/BigString/BigString+UnicodeScalarView.swift +++ b/Sources/RopeModule/BigString/BigString+UnicodeScalarView.swift @@ -196,6 +196,12 @@ extension BigString.UnicodeScalarView: RangeReplaceableCollection { self._guts = elements._guts } + public init(_ elements: Self.SubSequence) { + let lower = elements.startIndex._value + let upper = elements.endIndex._value + self._guts = _BString(elements.base._guts, in: lower ..< upper) + } + public init(repeating scalar: Unicode.Scalar, count: Int) { self._guts = _BString(repeating: _BString(String(scalar)), count: count) } @@ -228,6 +234,12 @@ extension BigString.UnicodeScalarView: RangeReplaceableCollection { _guts.append(contentsOf: newElements._guts) } + public mutating func append(contentsOf newElements: __owned Self.SubSequence) { + let lower = newElements.startIndex._value + let upper = newElements.endIndex._value + _guts.append(contentsOf: newElements.base._guts, in: lower ..< upper) + } + public mutating func insert(_ newElement: __owned Unicode.Scalar, at i: Index) { _guts.insert(contentsOf: String(newElement), at: i._value) } @@ -256,6 +268,12 @@ extension BigString.UnicodeScalarView: RangeReplaceableCollection { _guts.insert(contentsOf: newElements._guts, at: i._value) } + public mutating func insert(contentsOf newElements: __owned Self.SubSequence, at i: Index) { + let lower = newElements.startIndex._value + let upper = newElements.endIndex._value + _guts.insert(contentsOf: newElements.base._guts, in: lower ..< upper, at: i._value) + } + @discardableResult public mutating func remove(at i: Index) -> Unicode.Scalar { _guts.removeUnicodeScalar(at: i._value) diff --git a/Sources/RopeModule/Rope/Basics/_Rope+Builder.swift b/Sources/RopeModule/Rope/Basics/_Rope+Builder.swift index c376a503..bf5f2047 100644 --- a/Sources/RopeModule/Rope/Basics/_Rope+Builder.swift +++ b/Sources/RopeModule/Rope/Basics/_Rope+Builder.swift @@ -44,7 +44,7 @@ extension _Rope { var isPrefixEmpty: Bool { if prefix != nil { return false } - if let seedling = self.prefixLeaf, !seedling.isEmpty { return false } + if let leaf = self.prefixLeaf, !leaf.isEmpty { return false } return prefixTrees.isEmpty } @@ -58,8 +58,8 @@ extension _Rope { for sapling in prefixTrees { sum.add(sapling.summary) } - if let seedling = prefixLeaf { sum.add(seedling.summary) } - if let seed = prefix { sum.add(seed.summary) } + if let leaf = prefixLeaf { sum.add(leaf.summary) } + if let item = prefix { sum.add(item.summary) } return sum } @@ -75,8 +75,8 @@ extension _Rope { var lastPrefixItem: _Rope.Item { get { assert(!isPrefixEmpty) - if let prefix { return prefix } - if let prefixLeaf { return prefixLeaf.lastItem } + if let item = self.prefix { return item } + if let leaf = self.prefixLeaf { return leaf.lastItem } return prefixTrees.last!.root.lastItem } _modify { @@ -127,29 +127,29 @@ extension _Rope { guard prefixTrees[i].forEachWhile({ body($0, nil) }) else { return false } i += 1 } - if let seedling = self.prefixLeaf { - guard seedling.forEachWhile({ body($0, nil) }) else { return false } + if let leaf = self.prefixLeaf { + guard leaf.forEachWhile({ body($0, nil) }) else { return false } } - if let seed = self.prefix { - guard body(seed.value, nil) else { return false } + if let item = self.prefix { + guard body(item.value, nil) else { return false } } return true } - if let seedling = self.prefixLeaf { - let size = metric.size(of: seedling.summary) + if let leaf = self.prefixLeaf { + let size = metric.size(of: leaf.summary) if position < size { - guard seedling.forEachWhile(from: position, in: metric, body) else { return false } - if let seed = self.prefix { - guard body(seed.value, nil) else { return false } + guard leaf.forEachWhile(from: position, in: metric, body) else { return false } + if let item = self.prefix { + guard body(item.value, nil) else { return false } } return true } position -= size } - if let seed = self.prefix { - let i = metric.index(at: position, in: seed.value) - guard body(seed.value, i) else { return false} + if let item = self.prefix { + let i = metric.index(at: position, in: item.value) + guard body(item.value, i) else { return false} } return true } @@ -175,28 +175,28 @@ extension _Rope { mutating func append(_ item: __owned _Rope.Item) { guard !item.isEmpty else { return } - guard var seed = self.prefix._take() else { + guard var prefix = self.prefix._take() else { self.prefix = item return } var item = item - if seed.rebalance(nextNeighbor: &item) { - self.prefix = seed + if prefix.rebalance(nextNeighbor: &item) { + self.prefix = prefix return } - _append(seed) + _append(prefix) self.prefix = item } mutating func _append(_ item: __owned _Rope.Item) { assert(self.prefix == nil) assert(!item.isUndersized) - var seedling = self.prefixLeaf._take() ?? .createLeaf() - seedling._appendItem(item) - if seedling.isFull { - self._append(seedling) + var leaf = self.prefixLeaf._take() ?? .createLeaf() + leaf._appendItem(item) + if leaf.isFull { + self._append(leaf) } else { - self.prefixLeaf = seedling + self.prefixLeaf = leaf } invariantCheck() } @@ -214,19 +214,19 @@ extension _Rope { append(node.firstItem) return } - if let seed = self.prefix._take() { - if let spawn = node.prepend(seed) { + if let item = self.prefix._take() { + if let spawn = node.prepend(item) { append(node) append(spawn) return } } - if var seedling = self.prefixLeaf._take() { - if seedling.rebalance(nextNeighbor: &node), !seedling.isFull { - self.prefixLeaf = seedling + if var leaf = self.prefixLeaf._take() { + if leaf.rebalance(nextNeighbor: &node), !leaf.isFull { + self.prefixLeaf = leaf return } - self._append(seedling) + self._append(leaf) } if node.isFull { @@ -237,13 +237,13 @@ extension _Rope { return } - if var seed = self.prefix._take() { - if !seed.isUndersized || !node.firstItem.rebalance(prevNeighbor: &seed) { - self._append(seed) + if var prefix = self.prefix._take() { + if !prefix.isUndersized || !node.firstItem.rebalance(prevNeighbor: &prefix) { + self._append(prefix) } } - if let seedling = self.prefixLeaf._take() { - _append(seedling) + if let leaf = self.prefixLeaf._take() { + _append(leaf) } _append(node) } @@ -353,33 +353,33 @@ extension _Rope { if let suffixItem = self.suffix._take() { append(suffixItem) } - if var seed = self.prefix._take() { - if !seed.isUndersized { - _append(seed) + if var prefix = self.prefix._take() { + if !prefix.isUndersized { + _append(prefix) } else if !self.isPrefixEmpty { - if !self.lastPrefixItem.rebalance(nextNeighbor: &seed) { - _append(seed) + if !self.lastPrefixItem.rebalance(nextNeighbor: &prefix) { + _append(prefix) } } else if !self.isSuffixEmpty { - if !self.firstSuffixItem.rebalance(prevNeighbor: &seed) { - _append(seed) + if !self.firstSuffixItem.rebalance(prevNeighbor: &prefix) { + _append(prefix) } } else { // We only have the seed; allow undersized chunk - return Rope(root: .createLeaf(seed)) + return Rope(root: .createLeaf(prefix)) } } assert(self.prefix == nil && self.suffix == nil) - while let sapling = suffixTrees.popLast() { - append(sapling) + while let tree = suffixTrees.popLast() { + append(tree) } // Merge all saplings, the seedling and the seed into a single rope. - if let seed = self.prefix._take() { - _append(seed) + if let item = self.prefix._take() { + _append(item) } var rope = Rope(root: prefixLeaf._take()) - while let sapling = prefixTrees.popLast() { - rope.prepend(sapling) + while let tree = prefixTrees.popLast() { + rope.prepend(tree) } assert(prefixLeaf == nil && prefixTrees.isEmpty && suffixTrees.isEmpty) rope.invariantCheck() @@ -394,16 +394,16 @@ extension _Rope { sapling.invariantCheck() h = sapling.height } - if let seedling = self.prefixLeaf { - precondition(seedling.height == 0) - precondition(!seedling.isFull) - seedling.invariantCheck(depth: 0, height: 0) + if let leaf = self.prefixLeaf { + precondition(leaf.height == 0) + precondition(!leaf.isFull) + leaf.invariantCheck(depth: 0, height: 0) } h = 0 - for sapling in suffixTrees.reversed() { - precondition(sapling.height >= h) - sapling.invariantCheck() - h = sapling.height + for tree in suffixTrees.reversed() { + precondition(tree.height >= h) + tree.invariantCheck() + h = tree.height } #endif @@ -414,25 +414,25 @@ extension _Rope { print("Sapling \(i):") self.prefixTrees[i].dump(heightLimit: heightLimit, firstPrefix: " ", restPrefix: " ") } - if let seedling = self.prefixLeaf { + if let leaf = self.prefixLeaf { print("Seedling:") - seedling.dump(heightLimit: heightLimit, firstPrefix: " ", restPrefix: " ") + leaf.dump(heightLimit: heightLimit, firstPrefix: " ", restPrefix: " ") } - if let seed = self.prefix { + if let item = self.prefix { print("Seed:") - print(" \(seed)") + print(" \(item)") } print("---") var i = 0 - if let suffix = self.suffix { + if let item = self.suffix { print("Suffix \(i):") i += 1 - print(" \(suffix)") + print(" \(item)") } - for suffix in self.suffixTrees.reversed() { + for tree in self.suffixTrees.reversed() { print("Suffix \(i)") i += 1 - suffix.dump(heightLimit: heightLimit, firstPrefix: " ", restPrefix: " ") + tree.dump(heightLimit: heightLimit, firstPrefix: " ", restPrefix: " ") } } } diff --git a/Sources/RopeModule/Rope/Operations/_Rope+Extract.swift b/Sources/RopeModule/Rope/Operations/_Rope+Extract.swift new file mode 100644 index 00000000..495d8748 --- /dev/null +++ b/Sources/RopeModule/Rope/Operations/_Rope+Extract.swift @@ -0,0 +1,84 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +extension _Rope { + func extract(from start: Int, to end: Int, in metric: some _RopeMetric) -> Self { + if _root == nil { + precondition(start == 0 && end == 0, "Invalid range") + return Self() + } + var builder = Builder() + root.extract(from: start, to: end, in: metric, into: &builder) + return builder.finalize() + } +} + +extension _Rope.Node { + func extract( + from start: Int, + to end: Int, + in metric: some _RopeMetric, + into builder: inout _Rope.Builder + ) { + let size = metric.size(of: summary) + precondition(start >= 0 && start <= end && end <= size, "Range out of bounds") + + guard start != end else { return } + + if self.isLeaf { + self.readLeaf { + let l = $0.findSlot(at: start, in: metric, preferEnd: false) + let u = $0.findSlot(from: l, offsetBy: end - start, in: metric, preferEnd: true) + let c = $0.children + if l.slot == u.slot { + var item = c[l.slot] + let i = metric.index(at: l.remaining, in: item.value) + var item2 = item.split(at: i) + let j = metric.index( + at: u.remaining - metric.nonnegativeSize(of: item.summary), + in: item2.value) + _ = item2.split(at: j) + builder.append(item2) + return + } + assert(l.slot < u.slot) + var left = c[l.slot] + left = left.split(at: metric.index(at: l.remaining, in: left.value)) + builder.append(left) + for i in l.slot + 1 ..< u.slot { + builder.append(c[i]) + } + var right = c[u.slot] + _ = right.split(at: metric.index(at: u.remaining, in: right.value)) + builder.append(right) + } + return + } + + self.readInner { + let l = $0.findSlot(at: start, in: metric, preferEnd: false) + let u = $0.findSlot(from: l, offsetBy: end - start, in: metric, preferEnd: true) + let c = $0.children + if l.slot == u.slot { + c[l.slot].extract( + from: l.remaining, to: u.remaining, in: metric, into: &builder) + return + } + assert(l.slot < u.slot) + let lsize = metric.nonnegativeSize(of: c[l.slot].summary) + c[l.slot].extract(from: l.remaining, to: lsize, in: metric, into: &builder) + for i in l.slot + 1 ..< u.slot { + builder.append(c[i]) + } + c[u.slot].extract(from: 0, to: u.remaining, in: metric, into: &builder) + } + } +} diff --git a/Sources/RopeModule/Rope/Operations/_Rope+RemoveSubrange.swift b/Sources/RopeModule/Rope/Operations/_Rope+RemoveSubrange.swift index 47aabed5..32eccf76 100644 --- a/Sources/RopeModule/Rope/Operations/_Rope+RemoveSubrange.swift +++ b/Sources/RopeModule/Rope/Operations/_Rope+RemoveSubrange.swift @@ -43,7 +43,9 @@ extension _Rope { in metric: some _RopeMetric ) -> Builder { let size = metric.size(of: summary) - precondition(bounds.lowerBound >= 0 && bounds.upperBound <= size, "Position out of bounds") + precondition( + bounds.lowerBound >= 0 && bounds.upperBound <= size, + "Range out of bounds") guard !bounds.isEmpty else { return builder(splittingAt: bounds.lowerBound, in: metric) diff --git a/Sources/RopeModule/Utilities/Range Utilities.swift b/Sources/RopeModule/Utilities/Range Utilities.swift new file mode 100644 index 00000000..0088e25d --- /dev/null +++ b/Sources/RopeModule/Utilities/Range Utilities.swift @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +extension Range { + internal var _base: Range<_BString.Index> { + Range<_BString.Index>(uncheckedBounds: (lowerBound._value, upperBound._value)) + } +} + +extension Range<_BString.Index> { + internal var _isEmptyUTF8: Bool { + lowerBound._utf8Offset == upperBound._utf8Offset + } +} diff --git a/Tests/RopeModuleTests/TestBigString.swift b/Tests/RopeModuleTests/TestBigString.swift index 443f2d5d..d5502406 100644 --- a/Tests/RopeModuleTests/TestBigString.swift +++ b/Tests/RopeModuleTests/TestBigString.swift @@ -512,4 +512,29 @@ class TestBigString: XCTestCase { checkCharacterIndices(input, big) } } + + func test_init_from_substring() { + let flat = sampleString + let big = BigString(flat) + + let (indices1, indices2) = checkScalarIndices(flat, big) + + for i in randomStride(from: 0, to: indices1.count, by: 1000, seed: 0) { + let a1 = indices1[i] + let a2 = indices2[i] + for j in randomStride(from: i, to: indices1.count, by: 1000, seed: i) { + let b1 = indices1[j] + let b2 = indices2[j] + + let expected = String(flat[a1 ..< b1]) + let actual = BigString(big[a2 ..< b2]) + actual._invariantCheck() + XCTAssertEqual(String(big), flat) + checkUTF8Indices(expected, actual) + checkUTF16Indices(expected, actual) + checkScalarIndices(expected, actual) + checkCharacterIndices(expected, actual) + } + } + } }