-
Notifications
You must be signed in to change notification settings - Fork 233
Align UUID regex component with PR #1547 direction #1740
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
Open
beltradini
wants to merge
1
commit into
swiftlang:main
Choose a base branch
from
beltradini:feature/complete-work-on-issue-1470
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # UUID Regex Parser Component | ||
|
|
||
| * Proposal: [SF-NNNN](NNNN-uuid-regex-parser.md) | ||
| * Authors: [beltradini](https://github.com/beltradini) | ||
| * Review Manager: TBD | ||
| * Status: **Awaiting implementation** | ||
| * Bug: [swiftlang/swift-foundation#1470](https://github.com/swiftlang/swift-foundation/issues/1470) | ||
| * Implementation: [swiftlang/swift-foundation#1547](https://github.com/swiftlang/swift-foundation/pull/1547) | ||
|
|
||
| ## Introduction | ||
|
|
||
| This proposal adds a dedicated UUID parser component for Swift Regex so callers can match and capture UUID text directly as `UUID` values. | ||
|
|
||
| ## Motivation | ||
|
|
||
| Users can currently match UUID-shaped text with a string pattern and then manually construct `UUID(uuidString:)` in a follow-up step. This is repetitive and easy to get wrong when combining with larger regexes. | ||
|
|
||
| A first-class regex component improves ergonomics and makes captures strongly typed. | ||
|
|
||
| ## Proposed solution | ||
|
|
||
| Add a new nested type, `UUID.RegexComponent`, that conforms to `CustomConsumingRegexComponent`. | ||
|
|
||
| Also add a convenience regex factory: | ||
|
|
||
| ```swift | ||
| @available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
| extension RegexComponent where Self == UUID.RegexComponent { | ||
| public static var uuid: UUID.RegexComponent { UUID.RegexComponent() } | ||
| } | ||
| ``` | ||
|
|
||
| This enables regex usage like: | ||
|
|
||
| ```swift | ||
| let match = input.firstMatch(of: /id=\(.uuid)/) | ||
| ``` | ||
|
|
||
| ## Detailed design | ||
|
|
||
| `UUID.RegexComponent` consumes exactly 36 characters from the current regex index and uses `UUID(uuidString:)` to validate and produce a typed `UUID` capture. | ||
|
|
||
| If there are fewer than 36 characters remaining, or the 36-character slice is not a valid UUID, matching fails at that position. | ||
|
|
||
| ### API surface | ||
|
|
||
| ```swift | ||
| @available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
| extension UUID { | ||
| public struct RegexComponent : CustomConsumingRegexComponent { | ||
| public init() | ||
| public typealias RegexOutput = UUID | ||
| public func consuming( | ||
| _ input: String, | ||
| startingAt index: String.Index, | ||
| in bounds: Range<String.Index> | ||
| ) throws -> (upperBound: String.Index, output: UUID)? | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
| extension RegexComponent where Self == UUID.RegexComponent { | ||
| public static var uuid: UUID.RegexComponent | ||
| } | ||
| ``` | ||
|
|
||
| ## Source compatibility | ||
|
|
||
| This is an additive API change with no source break. | ||
|
|
||
| ## Implications on adoption | ||
|
|
||
| Adopters can gate use with standard availability checks for Regex APIs (`macOS 13 / iOS 16 / tvOS 16 / watchOS 9`). | ||
|
|
||
| ## Future directions | ||
|
|
||
| Future revisions could add parsing components for other common scalar identifiers that already have string initializers. | ||
|
|
||
| ## Alternatives considered | ||
|
|
||
| ### Keep using string captures + manual parsing | ||
|
|
||
| This remains possible but duplicates parsing work at call sites and weakens type-safety. | ||
|
|
||
| ### Make `UUID` itself the regex component | ||
|
|
||
| Using a dedicated parser type avoids making ordinary `UUID` values double as parser instances and keeps API intent explicit. | ||
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
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.
These should all be
FoundationPreview 6.4, I think.