-
Notifications
You must be signed in to change notification settings - Fork 128
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
Port _CShims UUID to Swift #129
Draft
tonyarnold
wants to merge
4
commits into
apple:main
Choose a base branch
from
tonyarnold:uuid-to-swift
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
+87
−380
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
16f6f9f
Initial attempt to replace C-based UUID functions with Swift implemen…
tonyarnold f4e1e2b
Remove UUID code from _CShims
tonyarnold c6fab7a
Begin to separate the UUIDv4 specific code
tonyarnold c3cc89c
Restore the original UUID initializers
tonyarnold File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,52 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2022 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension UUID { | ||
static func v4_generatedRandom() -> uuid_t { | ||
var randomBits = (0 ... 15).map { _ in UInt8.random(in: .min ... .max) } | ||
randomBits[6] = (randomBits[6] & 0x0F) | 0x40 | ||
randomBits[8] = (randomBits[8] & 0x3F) | 0x80 | ||
|
||
return randomBits.withUnsafeBytes { buffer in | ||
return buffer.bindMemory(to: uuid_t.self)[0] | ||
} | ||
} | ||
|
||
static func v4_parse(uuidString string: __shared String) -> uuid_t? { | ||
let components = string | ||
.replacing("-", with: "") | ||
.split(by: 2) | ||
.compactMap { UInt8($0, radix: 16) } | ||
|
||
guard components.count == 16 else { | ||
return nil | ||
} | ||
|
||
return components.withUnsafeBytes { buffer in | ||
return buffer.bindMemory(to: uuid_t.self)[0] | ||
} | ||
} | ||
} | ||
|
||
private extension String { | ||
func split(by length: Int) -> [String] { | ||
var startIndex = self.startIndex | ||
var results = [Substring]() | ||
|
||
while startIndex < self.endIndex { | ||
let endIndex = self.index(startIndex, offsetBy: length, limitedBy: self.endIndex) ?? self.endIndex | ||
results.append(self[startIndex..<endIndex]) | ||
startIndex = endIndex | ||
} | ||
|
||
return results.map { String($0) } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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.
Based on #129 (comment), one obvious optimization we can do here is to
guard
againststring.count
. However, bothString.count
andString.UTF8View.count
is not O(1), so I guess we should parse the string manually andthrow
if it’s malformed, including too short or too long?