-
Notifications
You must be signed in to change notification settings - Fork 195
AttributedString Index Validity APIs #1177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jmschonfeld
merged 4 commits into
swiftlang:main
from
jmschonfeld:attrstr/index-validation
Feb 28, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
877b55f
AttributedString index validity APIs
jmschonfeld b71290f
Move canImport(Synchronization) into function body
jmschonfeld 22ca8ae
Remove unnecessary bounds checks from validity expression
jmschonfeld 07ae7a4
Update FOUNDATION_FRAMEWORK code to provide Index versions
jmschonfeld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Sources/FoundationEssentials/AttributedString/AttributedString+IndexValidity.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 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 canImport(Synchronization) | ||
internal import Synchronization | ||
#endif | ||
|
||
extension AttributedString.Guts { | ||
typealias Version = UInt | ||
|
||
#if canImport(Synchronization) | ||
private static let _nextVersion = Atomic<Version>(0) | ||
#else | ||
private static let _nextVersion = LockedState<Version>(initialState: 0) | ||
#endif | ||
|
||
static func createNewVersion() -> Version { | ||
#if canImport(Synchronization) | ||
_nextVersion.wrappingAdd(1, ordering: .relaxed).oldValue | ||
#else | ||
_nextVersion.withLock { value in | ||
defer { | ||
value &+= 1 | ||
} | ||
return value | ||
} | ||
#endif | ||
} | ||
|
||
func incrementVersion() { | ||
self.version = Self.createNewVersion() | ||
} | ||
} | ||
|
||
// MARK: - Public API | ||
|
||
@available(FoundationPreview 6.2, *) | ||
extension AttributedString.Index { | ||
public func isValid(within text: some AttributedStringProtocol) -> Bool { | ||
self._version == text.__guts.version && | ||
self >= text.startIndex && | ||
self < text.endIndex | ||
} | ||
|
||
public func isValid(within text: DiscontiguousAttributedSubstring) -> Bool { | ||
self._version == text._guts.version && | ||
text._indices.contains(self._value) | ||
} | ||
} | ||
|
||
@available(FoundationPreview 6.2, *) | ||
extension Range<AttributedString.Index> { | ||
public func isValid(within text: some AttributedStringProtocol) -> Bool { | ||
// Note: By nature of Range's lowerBound <= upperBound requirement, this is also sufficient to determine that lowerBound <= endIndex && upperBound >= startIndex | ||
self.lowerBound._version == text.__guts.version && | ||
self.lowerBound >= text.startIndex && | ||
self.upperBound._version == text.__guts.version && | ||
self.upperBound <= text.endIndex | ||
} | ||
|
||
public func isValid(within text: DiscontiguousAttributedSubstring) -> Bool { | ||
let endIndex = text._indices.ranges.last?.upperBound | ||
return self.lowerBound._version == text._guts.version && | ||
(self.lowerBound._value == endIndex || text._indices.contains(self.lowerBound._value)) && | ||
self.upperBound._version == text._guts.version && | ||
(self.upperBound._value == endIndex || text._indices.contains(self.upperBound._value)) | ||
} | ||
} | ||
|
||
@available(FoundationPreview 6.2, *) | ||
extension RangeSet<AttributedString.Index> { | ||
public func isValid(within text: some AttributedStringProtocol) -> Bool { | ||
self.ranges.allSatisfy { | ||
$0.isValid(within: text) | ||
} | ||
} | ||
|
||
public func isValid(within text: DiscontiguousAttributedSubstring) -> Bool { | ||
self.ranges.allSatisfy { | ||
$0.isValid(within: text) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other places we have
#if canImport(Synchronization) && FOUNDATION_FRAMEWORK
. Is that wrong?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously we needed the
&& FOUNDATION_FRAMEWORK
because non-FOUNDATION_FRAMEWORK
builds ran on a very old version of macOS in CI where we back deployed back to a version that didn't haveSynchronization
(even though thecanImport(Synchronization)
check passes because it was in the SDK). Now that we've bumped the deployment target of the package, we can safely use theSynchronization
module on the macOS version CI is back deployed to, so no need to guard withFOUNDATION_FRAMEWORK
(but there are still some edge case situations where the module doesn't exist at all that we need to guard for with thecanImport
check)