Skip to content
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

Implement AttributedString UTF8 and UTF16 views #1066

Merged
merged 3 commits into from
Dec 17, 2024

Conversation

jmschonfeld
Copy link
Contributor

This adds a new UTF-8 and UTF-16 view to AttributedString, vending APIs similar to the underlying BigString's UTF-8 and UTF-16 view

@jmschonfeld jmschonfeld added enhancement New feature or request API Change Any changes to Foundation's public API surface labels Dec 3, 2024
@jmschonfeld jmschonfeld marked this pull request as ready for review December 10, 2024 22:22
@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

_range = range
}

public init() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentionally public? It wasn't included in the proposal. I don't think you can init a String.UTF16View either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, this was copied from the CharacterView and UnicodeScalarView code, but I didn't include it in the proposal since there's no need to ever create this yourself especially since it's immutable - I'll remove this from the implementation


public func index(before i: AttributedString.Index) -> AttributedString.Index {
precondition(i >= startIndex && i <= endIndex, "AttributedString index out of bounds")
let j = Index(_guts.string.utf8.index(before: i._value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this throw if i == startIndex? I believe calling string.utf8.index(before: string.utf8.startIndex) throws the index out of bound error. If so, should we just enforce this at the precondition above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still do hit a precondition from swift-collections:

_RopeModule/BigString+Contents.swift:292: Precondition failed: Can't advance below start index

But we might as well account for this here, good call - I'll update the above >= to be >

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld requested a review from itingliu December 13, 2024 23:39
@jmschonfeld jmschonfeld merged commit b01f9c6 into swiftlang:main Dec 17, 2024
3 checks passed
@jmschonfeld jmschonfeld deleted the attrstr/utf8-utf16 branch December 17, 2024 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Change Any changes to Foundation's public API surface enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants