Skip to content

Commit

Permalink
AttributedString Index Tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jmschonfeld committed Jan 2, 2025
1 parent 2162fc9 commit f068f07
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ extension AttributedString {

var string: BigString
var runs: _InternalRuns
var trackedRanges: [Range<BigString.Index>]

// Note: the caller is responsible for performing attribute fix-ups if needed based on the source of the runs
init(string: BigString, runs: _InternalRuns) {
precondition(string.isEmpty == runs.isEmpty, "An empty attributed string should not contain any runs")
self.string = string
self.runs = runs
self.trackedRanges = []
}

// Note: the caller is responsible for performing attribute fix-ups if needed based on the source of the runs
Expand Down Expand Up @@ -418,18 +420,20 @@ extension AttributedString.Guts {

func _prepareStringMutation(
in range: Range<BigString.Index>
) -> (oldUTF8Count: Int, invalidationRange: Range<Int>) {
) -> (mutationStartUTF8Offset: Int, isInsertion: Bool, oldUTF8Count: Int, invalidationRange: Range<Int>) {
let utf8TargetRange = range._utf8OffsetRange
let invalidationRange = self.enforceAttributeConstraintsBeforeMutation(to: utf8TargetRange)
self._prepareTrackedIndicesUpdate(mutationRange: range)
assert(invalidationRange.lowerBound <= utf8TargetRange.lowerBound)
assert(invalidationRange.upperBound >= utf8TargetRange.upperBound)
return (self.string.utf8.count, invalidationRange)
return (utf8TargetRange.lowerBound, utf8TargetRange.isEmpty, self.string.utf8.count, invalidationRange)
}

func _finalizeStringMutation(
_ state: (oldUTF8Count: Int, invalidationRange: Range<Int>)
_ state: (mutationStartUTF8Offset: Int, isInsertion: Bool, oldUTF8Count: Int, invalidationRange: Range<Int>)
) {
let utf8Delta = self.string.utf8.count - state.oldUTF8Count
self._finalizeTrackedIndicesUpdate(mutationStartOffset: state.mutationStartUTF8Offset, isInsertion: state.isInsertion, utf8LengthDelta: utf8Delta)
let lower = state.invalidationRange.lowerBound
let upper = state.invalidationRange.upperBound + utf8Delta
self.enforceAttributeConstraintsAfterMutation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#if FOUNDATION_FRAMEWORK
@_spi(Unstable) internal import CollectionsInternal
#elseif canImport(_RopeModule)
internal import _RopeModule
#elseif canImport(_FoundationCollections)
internal import _FoundationCollections
#endif

// MARK: - Internal Index Updating

extension AttributedString.Guts {
func _prepareTrackedIndicesUpdate(mutationRange: Range<BigString.Index>) {
// 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
for idx in 0 ..< trackedRanges.count {
let lowerBoundWithinMutation = trackedRanges[idx].lowerBound > mutationRange.lowerBound && trackedRanges[idx].lowerBound < mutationRange.upperBound
let upperBoundWithinMutation = trackedRanges[idx].upperBound > mutationRange.lowerBound && trackedRanges[idx].upperBound < mutationRange.upperBound
switch (lowerBoundWithinMutation, upperBoundWithinMutation) {
case (true, true):
// Range is fully within mutation, collapse it to the start of the mutation
trackedRanges[idx] = Range(uncheckedBounds: (mutationRange.lowerBound, mutationRange.lowerBound))
case (true, false):
// Range starts within mutation but extends beyond mutation - remove portion within mutation
trackedRanges[idx] = Range(uncheckedBounds: (mutationRange.upperBound, trackedRanges[idx].upperBound))
case (false, true):
// Range starts before mutation but extends into mutation - remove portion within mutation
trackedRanges[idx] = Range(uncheckedBounds: (trackedRanges[idx].lowerBound, mutationRange.lowerBound))
case (false, false):
// Neither endpoint of range is within mutation, leave as-is
break
}
}
}

func _finalizeTrackedIndicesUpdate(mutationStartOffset: Int, isInsertion: Bool, utf8LengthDelta: Int) {
// Update indices to point to the correct offsets based on the mutation deltas
for idx in 0 ..< trackedRanges.count {
var lowerBound = trackedRanges[idx].lowerBound
var upperBound = trackedRanges[idx].upperBound

// Shift the lower bound if either:
// A) The lower bound is greater than the start of the mutation (meaning it must be after the mutation due to the prepare step)
// 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)
if lowerBound.utf8Offset > mutationStartOffset || (lowerBound.utf8Offset == mutationStartOffset && isInsertion), utf8LengthDelta != 0 {
lowerBound = string.utf8.index(string.startIndex, offsetBy: lowerBound.utf8Offset + utf8LengthDelta)
} else {
// Form new indices even if the offsets don't change to ensure the indices are valid in the newly-mutated rope
string.formIndex(&lowerBound, offsetBy: 0)
}
// Shift the upper bound if either:
// - The upper bound is greater than the start of the mutation (meaning it must be after the mutation due to the prepare step)
// - 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
if upperBound.utf8Offset > mutationStartOffset || lowerBound != trackedRanges[idx].lowerBound, utf8LengthDelta != 0 {
upperBound = string.utf8.index(string.startIndex, offsetBy: upperBound.utf8Offset + utf8LengthDelta)
} else {
// Form new indices even if the offsets don't change to ensure the indices are valid in the newly-mutated rope
string.formIndex(&lowerBound, offsetBy: 0)
}

trackedRanges[idx] = Range(uncheckedBounds: (lowerBound, upperBound))
}
}
}

// MARK: - Public API

@available(FoundationPreview 6.2, *)
extension AttributedString {
/// 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
/// - Parameters:
/// - range: a range to track throughout the `mutation` block
/// - mutation: a mutating operation, or set of operations, to perform on this `AttributedString`
/// - 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)
public mutating func transform<E>(updating range: Range<Index>, mutation: (inout AttributedString) throws(E) -> Void) throws(E) -> Range<Index>? {
try self.transform(updating: [range], mutation: mutation)?.first
}

/// 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
/// - Parameters:
/// - index: an index to track throughout the `mutation` block
/// - mutation: a mutating operation, or set of operations, to perform on this `AttributedString`
/// - 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.
public mutating func transform<E>(updating ranges: [Range<Index>], mutation: (inout AttributedString) throws(E) -> Void) throws(E) -> [Range<Index>]? {
precondition(!ranges.isEmpty, "Cannot update an empty array of ranges")

// Ensure we are uniquely referenced and mutate the tracked ranges to include the new ranges
ensureUniqueReference()
let originalCount = _guts.trackedRanges.count
for range in ranges {
precondition(range.lowerBound >= self.startIndex && range.lowerBound <= self.endIndex && range.upperBound >= self.startIndex && range.upperBound <= self.endIndex, "AttributedString index is out of bounds")
_guts.trackedRanges.append(range._bstringRange)
}

// Perform the user-supplied mutation on `self`
try mutation(&self)

// Ensure we are still uniquely referenced (it's possible we may have been uniquely referenced before, but the mutation closure created a new reference and we are no longer unique)
ensureUniqueReference()

// If the `trackedRanges` state is inconsistent, tracking has been lost - simply return nil to indicate ranges are no longer available
guard _guts.trackedRanges.count == originalCount + ranges.count else {
return nil
}

// Collect and remove updated ranges
let resultingRanges = _guts.trackedRanges[originalCount...].map(\._attrStrRange)
_guts.trackedRanges.removeSubrange(originalCount...)
return resultingRanges
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,8 @@ extension Range where Bound == BigString.Index {
internal var _utf8OffsetRange: Range<Int> {
Range<Int>(uncheckedBounds: (lowerBound.utf8Offset, upperBound.utf8Offset))
}

internal var _attrStrRange: Range<AttributedString.Index> {
Range<AttributedString.Index>(uncheckedBounds: (.init(lowerBound), .init(upperBound)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_sources(FoundationEssentials PRIVATE
AttributedString+AttributeTransformation.swift
AttributedString+CharacterView.swift
AttributedString+Guts.swift
AttributedString+IndexTracking.swift
AttributedString+Runs+AttributeSlices.swift
AttributedString+Runs+Run.swift
AttributedString+Runs.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ final class TestAttributedStringCOW: XCTestCase {
XCTAssertNotEqual(str, copy, "Mutation operation did not copy when multiple references exist", file: file, line: line)
}

func assertCOWCopyManual(file: StaticString = #filePath, line: UInt = #line, _ operation: (inout AttributedString) -> Void) {
var str = createAttributedString()
let gutsPtr = Unmanaged.passUnretained(str._guts)
operation(&str)
let newGutsPtr = Unmanaged.passUnretained(str._guts)
XCTAssertNotEqual(gutsPtr.toOpaque(), newGutsPtr.toOpaque(), "Mutation operation with manual copy did not perform copy", file: file, line: line)
}

func assertCOWNoCopy(file: StaticString = #filePath, line: UInt = #line, _ operation: (inout AttributedString) -> Void) {
var str = createAttributedString()
let gutsPtr = Unmanaged.passUnretained(str._guts)
Expand Down Expand Up @@ -169,4 +177,38 @@ final class TestAttributedStringCOW: XCTestCase {
$0[makeSubrange($0)].genericSetAttribute()
}
}

func testIndexTracking() {
assertCOWBehavior {
_ = $0.transform(updating: $0.startIndex ..< $0.endIndex) {
$0.testInt = 2
}
}
assertCOWBehavior {
_ = $0.transform(updating: $0.startIndex ..< $0.endIndex) {
$0.insert(AttributedString("_"), at: $0.startIndex)
}
}
assertCOWBehavior {
_ = $0.transform(updating: [$0.startIndex ..< $0.endIndex]) {
$0.testInt = 2
}
}
assertCOWBehavior {
_ = $0.transform(updating: [$0.startIndex ..< $0.endIndex]) {
$0.insert(AttributedString("_"), at: $0.startIndex)
}
}

// Ensure that creating a reference in the transformation closure still causes a copy to happen during post-mutation index updates
var storage = AttributedString()
assertCOWCopyManual {
_ = $0.transform(updating: $0.startIndex ..< $0.endIndex) {
$0.insert(AttributedString("_"), at: $0.startIndex)
// Store a reference after performing the mutation so the mutation doesn't cause an inherent copy
storage = $0
}
}
XCTAssertNotEqual(storage, "")
}
}
Loading

0 comments on commit f068f07

Please sign in to comment.