Skip to content

Add SystemUUID initializer from canonical UUID string - #211

Open
gakiwate wants to merge 1 commit into
apple:mainfrom
gakiwate:uuid-from-string
Open

gakiwate wants to merge 1 commit into
apple:mainfrom
gakiwate:uuid-from-string

Conversation

@gakiwate

@gakiwate gakiwate commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Add a failable init?(uuidString:) that parses the canonical hyphenated 8-4-4-4-12 hexadecimal UUID string form.

@tfpauly tfpauly added the 🆕 semver/minor Adds new public API. label Oct 2, 2026

@agnosticdev agnosticdev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you add a few tests here too?

/// `E621E1F8-C36C-495A-93FC-0C247A3E6E5F`), or nil when `uuidString` is not 36
/// characters in the 8-4-4-4-12 hexadecimal layout.
public init?(uuidString: String) {
let ascii = Array(uuidString.utf8)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we use uuidString.withCString here instead of creating a temporary array?
Maybe something like:

let isValid = uuidString.withCString { cString -> Bool in
    var byteIndex = 0
    var i = 0
    while i < 36 {
        let char = UInt8(bitPattern: cString[I])
        ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

withCString makes this unsafe, which we should avoid

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We do not want to take a heap hit each time we create UUID string though.

@tfpauly tfpauly left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add some unit tests for this too!

Add a failable init?(uuidString:) that parses the canonical hyphenated
8-4-4-4-12 hexadecimal UUID string form, with unit tests covering
round-trip, lowercase input, the NULL UUID, and malformed strings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants