Skip to content

Commit bfcba67

Browse files
authored
AttributedString Index Tracking (#1109)
This implements tracking of AttributedString indices across mutations
1 parent de6af53 commit bfcba67

File tree

5 files changed

+441
-3
lines changed

5 files changed

+441
-3
lines changed

Sources/FoundationEssentials/AttributedString/AttributedString+Guts.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ extension AttributedString {
3232
var version: Version
3333
var string: BigString
3434
var runs: _InternalRuns
35+
var trackedRanges: [Range<BigString.Index>]
3536

3637
// Note: the caller is responsible for performing attribute fix-ups if needed based on the source of the runs
3738
init(string: BigString, runs: _InternalRuns) {
3839
precondition(string.isEmpty == runs.isEmpty, "An empty attributed string should not contain any runs")
3940
self.version = Self.createNewVersion()
4041
self.string = string
4142
self.runs = runs
43+
self.trackedRanges = []
4244
}
4345

4446
// Note: the caller is responsible for performing attribute fix-ups if needed based on the source of the runs
@@ -424,18 +426,20 @@ extension AttributedString.Guts {
424426

425427
func _prepareStringMutation(
426428
in range: Range<BigString.Index>
427-
) -> (oldUTF8Count: Int, invalidationRange: Range<Int>) {
429+
) -> (mutationStartUTF8Offset: Int, isInsertion: Bool, oldUTF8Count: Int, invalidationRange: Range<Int>) {
428430
let utf8TargetRange = range._utf8OffsetRange
429431
let invalidationRange = self.enforceAttributeConstraintsBeforeMutation(to: utf8TargetRange)
432+
self._prepareTrackedIndicesUpdate(mutationRange: range)
430433
assert(invalidationRange.lowerBound <= utf8TargetRange.lowerBound)
431434
assert(invalidationRange.upperBound >= utf8TargetRange.upperBound)
432-
return (self.string.utf8.count, invalidationRange)
435+
return (utf8TargetRange.lowerBound, utf8TargetRange.isEmpty, self.string.utf8.count, invalidationRange)
433436
}
434437

435438
func _finalizeStringMutation(
436-
_ state: (oldUTF8Count: Int, invalidationRange: Range<Int>)
439+
_ state: (mutationStartUTF8Offset: Int, isInsertion: Bool, oldUTF8Count: Int, invalidationRange: Range<Int>)
437440
) {
438441
let utf8Delta = self.string.utf8.count - state.oldUTF8Count
442+
self._finalizeTrackedIndicesUpdate(mutationStartOffset: state.mutationStartUTF8Offset, isInsertion: state.isInsertion, utf8LengthDelta: utf8Delta)
439443
let lower = state.invalidationRange.lowerBound
440444
let upper = state.invalidationRange.upperBound + utf8Delta
441445
self.enforceAttributeConstraintsAfterMutation(
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#if FOUNDATION_FRAMEWORK
14+
@_spi(Unstable) internal import CollectionsInternal
15+
#elseif canImport(_RopeModule)
16+
internal import _RopeModule
17+
#elseif canImport(_FoundationCollections)
18+
internal import _FoundationCollections
19+
#endif
20+
21+
// MARK: - Internal Index Updating
22+
23+
extension AttributedString.Guts {
24+
func _prepareTrackedIndicesUpdate(mutationRange: Range<BigString.Index>) {
25+
// Move any range endpoints inside of the mutation range to outside of the mutation range since a range should never end up splitting a mutation
26+
for idx in 0 ..< trackedRanges.count {
27+
let lowerBoundWithinMutation = trackedRanges[idx].lowerBound > mutationRange.lowerBound && trackedRanges[idx].lowerBound < mutationRange.upperBound
28+
let upperBoundWithinMutation = trackedRanges[idx].upperBound > mutationRange.lowerBound && trackedRanges[idx].upperBound < mutationRange.upperBound
29+
switch (lowerBoundWithinMutation, upperBoundWithinMutation) {
30+
case (true, true):
31+
// Range is fully within mutation, collapse it to the start of the mutation
32+
trackedRanges[idx] = Range(uncheckedBounds: (mutationRange.lowerBound, mutationRange.lowerBound))
33+
case (true, false):
34+
// Range starts within mutation but extends beyond mutation - remove portion within mutation
35+
trackedRanges[idx] = Range(uncheckedBounds: (mutationRange.upperBound, trackedRanges[idx].upperBound))
36+
case (false, true):
37+
// Range starts before mutation but extends into mutation - remove portion within mutation
38+
trackedRanges[idx] = Range(uncheckedBounds: (trackedRanges[idx].lowerBound, mutationRange.lowerBound))
39+
case (false, false):
40+
// Neither endpoint of range is within mutation, leave as-is
41+
break
42+
}
43+
}
44+
}
45+
46+
func _finalizeTrackedIndicesUpdate(mutationStartOffset: Int, isInsertion: Bool, utf8LengthDelta: Int) {
47+
// Update indices to point to the correct offsets based on the mutation deltas
48+
for idx in 0 ..< trackedRanges.count {
49+
var lowerBound = trackedRanges[idx].lowerBound
50+
var upperBound = trackedRanges[idx].upperBound
51+
52+
// Shift the lower bound if the mutation changed the length of the string and either of the following are true:
53+
// A) The lower bound is greater than the start of the mutation (meaning it must be after the mutation due to the prepare step)
54+
// B) The lower bound is equal to the start of the mutation, but the mutation is an insertion (meaning the text is inserted before the start offset)
55+
if lowerBound.utf8Offset > mutationStartOffset || (lowerBound.utf8Offset == mutationStartOffset && isInsertion), utf8LengthDelta != 0 {
56+
lowerBound = string.utf8.index(string.startIndex, offsetBy: lowerBound.utf8Offset + utf8LengthDelta)
57+
} else {
58+
// Form new indices even if the offsets don't change to ensure the indices are valid in the newly-mutated rope
59+
string.utf8.formIndex(&lowerBound, offsetBy: 0)
60+
}
61+
// Shift the upper bound if the mutation changed the length of the string and either of the following are true:
62+
// A) The upper bound is greater than the start of the mutation (meaning it must be after the mutation due to the prepare step)
63+
// B) The lower bound is shifted in any way (which therefore requires the upper bound to be shifted). This is the case when the tracked range is empty and is at the location of an insertion mutation
64+
if upperBound.utf8Offset > mutationStartOffset || lowerBound != trackedRanges[idx].lowerBound, utf8LengthDelta != 0 {
65+
upperBound = string.utf8.index(string.startIndex, offsetBy: upperBound.utf8Offset + utf8LengthDelta)
66+
} else {
67+
// Form new indices even if the offsets don't change to ensure the indices are valid in the newly-mutated rope
68+
string.utf8.formIndex(&lowerBound, offsetBy: 0)
69+
}
70+
71+
trackedRanges[idx] = Range(uncheckedBounds: (lowerBound, upperBound))
72+
}
73+
}
74+
}
75+
76+
// MARK: - Public API
77+
78+
@available(FoundationPreview 6.2, *)
79+
extension AttributedString {
80+
// MARK: inout API
81+
82+
/// Tracks the location of the provided range throughout the mutation closure, updating the provided range to one that represents the same effective locations after the mutation. If updating the provided range is not possible (tracking failed) then this function will fatal error. Use the Optional-returning variants to provide custom fallback behavior.
83+
/// - Parameters:
84+
/// - range: a range to track throughout the `body` closure
85+
/// - body: a mutating operation, or set of operations, to perform on the value of `self`. The value of `self` is provided to the closure as an `inout AttributedString` that the closure should mutate directly. Do not capture the value of `self` in the provided closure - the closure should mutate the provided `inout` copy.
86+
public mutating func transform<E>(updating range: inout Range<Index>, body: (inout AttributedString) throws(E) -> Void) throws(E) -> Void {
87+
guard let result = try self.transform(updating: range, body: body) else {
88+
fatalError("The provided mutation body did not allow for maintaining index tracking. Ensure that your mutation body mutates the provided AttributedString instead of replacing it with a different AttributedString or use the non-inout version of transform(updating:body:) which returns an Optional value to provide fallback behavior.")
89+
}
90+
range = result
91+
}
92+
93+
/// Tracks the location of the provided ranges throughout the mutation closure, updating them to new ranges that represent the same effective locations after the mutation. If updating the provided ranges is not possible (tracking failed) then this function will fatal error. Use the Optional-returning variants to provide custom fallback behavior.
94+
/// - Parameters:
95+
/// - ranges: a list of ranges to track throughout the `body` closure. The updated array (after the function is called) is guaranteed to be the same size as the provided array. Updated ranges are located at the same indices as their respective original ranges in the input `ranges` array.
96+
/// - body: a mutating operation, or set of operations, to perform on the value of `self`. The value of `self` is provided to the closure as an `inout AttributedString` that the closure should mutate directly. Do not capture the value of `self` in the provided closure - the closure should mutate the provided `inout` copy.
97+
public mutating func transform<E>(updating ranges: inout [Range<Index>], body: (inout AttributedString) throws(E) -> Void) throws(E) -> Void {
98+
guard let result = try self.transform(updating: ranges, body: body) else {
99+
fatalError("The provided mutation body did not allow for maintaining index tracking. Ensure that your mutation body mutates the provided AttributedString instead of replacing it with a different AttributedString or use the non-inout version of transform(updating:body:) which returns an Optional value to provide fallback behavior.")
100+
}
101+
ranges = result
102+
}
103+
104+
// MARK: Optional-returning API
105+
106+
/// Tracks the location of the provided range throughout the mutation closure, returning a new, updated range that represents the same effective locations after the mutation
107+
/// - Parameters:
108+
/// - range: a range to track throughout the `mutation` block
109+
/// - mutation: a mutating operation, or set of operations, to perform on this `AttributedString`
110+
/// - Returns: the updated `Range` that is valid after the mutation has been performed, or `nil` if the mutation performed does not allow for tracking to succeed (such as replacing the provided inout variable with an entirely different AttributedString)
111+
public mutating func transform<E>(updating range: Range<Index>, body: (inout AttributedString) throws(E) -> Void) throws(E) -> Range<Index>? {
112+
try self.transform(updating: [range], body: body)?.first
113+
}
114+
115+
/// Tracks the location of the provided ranges throughout the mutation closure, returning a new, updated range that represents the same effective locations after the mutation
116+
/// - Parameters:
117+
/// - index: an index to track throughout the `mutation` block
118+
/// - mutation: a mutating operation, or set of operations, to perform on this `AttributedString`
119+
/// - Returns: the updated `Range`s that is valid after the mutation has been performed, or `nil` if the mutation performed does not allow for tracking to succeed (such as replacing the provided inout variable with an entirely different AttributedString). When the return value is non-nil, the returned array is guaranteed to be the same size as the provided array with updated ranges at the same Array indices as their respective original ranges in the input array.
120+
public mutating func transform<E>(updating ranges: [Range<Index>], body: (inout AttributedString) throws(E) -> Void) throws(E) -> [Range<Index>]? {
121+
precondition(!ranges.isEmpty, "Cannot update an empty array of ranges")
122+
123+
// Ensure we are uniquely referenced and mutate the tracked ranges to include the new ranges
124+
ensureUniqueReference()
125+
let originalCount = _guts.trackedRanges.count
126+
for range in ranges {
127+
precondition(range.lowerBound >= self.startIndex && range.upperBound <= self.endIndex, "AttributedString index is out of bounds")
128+
_guts.trackedRanges.append(range._bstringRange)
129+
}
130+
131+
// Catch and store any error thrown during mutation here so that we can do any appropriate cleanup afterwards
132+
// We don't use a defer block here because the return value (returned indices) is dependent upon the effects of the cleanup (the ensureUniqueReference() call may change the version that should be stored within the indices)
133+
var thrownError: E?
134+
do {
135+
try body(&self)
136+
} catch {
137+
thrownError = error
138+
}
139+
140+
// Ensure we are still uniquely referenced (it's possible we may have been uniquely referenced before, but the mutation closure created a new reference - even if it threw an error - and we are no longer unique)
141+
// We also must ensure that any indices returned from this function are created after this call so that they are initialized with the updated version number
142+
ensureUniqueReference()
143+
144+
// If the `trackedRanges` state is inconsistent, tracking has been lost. The best we can do to validate consistent state is to ensure we have the correct number of ranges
145+
guard _guts.trackedRanges.count == originalCount + ranges.count else {
146+
// Clear the ranges to prevent any future lingering tracking issues with this AttributedString
147+
_guts.trackedRanges = []
148+
if let thrownError {
149+
throw thrownError
150+
}
151+
return nil
152+
}
153+
154+
defer {
155+
// Tracking state is consistent, so make sure we remove the ranges we added earlier (whether we throw or whether we use these ranges in the return value
156+
// Only remove those ranges added above in order to support recursive tracking
157+
_guts.trackedRanges.removeSubrange(originalCount...)
158+
}
159+
160+
if let thrownError {
161+
throw thrownError
162+
}
163+
164+
return _guts.trackedRanges.suffix(from: originalCount).map {
165+
$0._attributedStringRange(version: _guts.version)
166+
}
167+
}
168+
}

Sources/FoundationEssentials/AttributedString/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ target_sources(FoundationEssentials PRIVATE
1717
AttributedString+AttributeTransformation.swift
1818
AttributedString+CharacterView.swift
1919
AttributedString+Guts.swift
20+
AttributedString+IndexTracking.swift
2021
AttributedString+IndexValidity.swift
2122
AttributedString+Runs+AttributeSlices.swift
2223
AttributedString+Runs+Run.swift

Tests/FoundationEssentialsTests/AttributedString/AttributedStringCOWTests.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ final class TestAttributedStringCOW: XCTestCase {
4545
XCTAssertNotEqual(str, copy, "Mutation operation did not copy when multiple references exist", file: file, line: line)
4646
}
4747

48+
func assertCOWCopyManual(file: StaticString = #filePath, line: UInt = #line, _ operation: (inout AttributedString) -> Void) {
49+
var str = createAttributedString()
50+
let gutsPtr = Unmanaged.passUnretained(str._guts)
51+
operation(&str)
52+
let newGutsPtr = Unmanaged.passUnretained(str._guts)
53+
XCTAssertNotEqual(gutsPtr.toOpaque(), newGutsPtr.toOpaque(), "Mutation operation with manual copy did not perform copy", file: file, line: line)
54+
}
55+
4856
func assertCOWNoCopy(file: StaticString = #filePath, line: UInt = #line, _ operation: (inout AttributedString) -> Void) {
4957
var str = createAttributedString()
5058
let gutsPtr = Unmanaged.passUnretained(str._guts)
@@ -203,4 +211,50 @@ final class TestAttributedStringCOW: XCTestCase {
203211
$0[makeSubrange($0)].genericSetAttribute()
204212
}
205213
}
214+
215+
func testIndexTracking() {
216+
assertCOWBehavior {
217+
_ = $0.transform(updating: $0.startIndex ..< $0.endIndex) {
218+
$0.testInt = 2
219+
}
220+
}
221+
assertCOWBehavior {
222+
_ = $0.transform(updating: $0.startIndex ..< $0.endIndex) {
223+
$0.insert(AttributedString("_"), at: $0.startIndex)
224+
}
225+
}
226+
assertCOWBehavior {
227+
_ = $0.transform(updating: [$0.startIndex ..< $0.endIndex]) {
228+
$0.testInt = 2
229+
}
230+
}
231+
assertCOWBehavior {
232+
_ = $0.transform(updating: [$0.startIndex ..< $0.endIndex]) {
233+
$0.insert(AttributedString("_"), at: $0.startIndex)
234+
}
235+
}
236+
237+
// Ensure that creating a reference in the transformation closure still causes a copy to happen during post-mutation index updates
238+
var storage = AttributedString()
239+
assertCOWCopyManual {
240+
_ = $0.transform(updating: $0.startIndex ..< $0.endIndex) {
241+
$0.insert(AttributedString("_"), at: $0.startIndex)
242+
// Store a reference after performing the mutation so the mutation doesn't cause an inherent copy
243+
storage = $0
244+
}
245+
}
246+
XCTAssertNotEqual(storage, "")
247+
248+
// Ensure the same semantics hold even when the closure throws
249+
storage = AttributedString()
250+
assertCOWCopyManual {
251+
_ = try? $0.transform(updating: $0.startIndex ..< $0.endIndex) {
252+
$0.insert(AttributedString("_"), at: $0.startIndex)
253+
// Store a reference after performing the mutation so the mutation doesn't cause an inherent copy
254+
storage = $0
255+
throw CocoaError(.fileReadUnknown)
256+
}
257+
}
258+
XCTAssertNotEqual(storage, "")
259+
}
206260
}

0 commit comments

Comments
 (0)