forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move UUID from stdlib/Random into Base
- Loading branch information
1 parent
67ee6c6
commit 8da905b
Showing
3 changed files
with
64 additions
and
57 deletions.
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,62 @@ | ||
# This file is a part of Julia. License is MIT: https://julialang.org/license | ||
|
||
struct UUID | ||
value::UInt128 | ||
end | ||
UUID(u::NTuple{2, UInt64}) = UUID((UInt128(u[1]) << 64) | UInt128(u[2])) | ||
UUID(u::NTuple{4, UInt32}) = UUID((UInt128(u[1]) << 96) | (UInt128(u[2]) << 64) | | ||
(UInt128(u[3]) << 32) | UInt128(u[4])) | ||
|
||
# TODO: update documentation for new location | ||
""" | ||
uuid_version(u::UUID) -> Int | ||
Inspects the given UUID and returns its version | ||
(see [RFC 4122](https://www.ietf.org/rfc/rfc4122)). | ||
# Examples | ||
```jldoctest | ||
julia> rng = MersenneTwister(1234); | ||
julia> Random.uuid_version(Random.uuid4(rng)) | ||
4 | ||
``` | ||
""" | ||
uuid_version(u::UUID) = Int((u.value >> 76) & 0xf) | ||
|
||
UInt128(u::UUID) = u.value | ||
|
||
let groupings = [1:8; 10:13; 15:18; 20:23; 25:36] | ||
global UUID | ||
function UUID(s::AbstractString) | ||
s = lowercase(s) | ||
|
||
if !contains(s, r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$") | ||
throw(ArgumentError("Malformed UUID string")) | ||
end | ||
|
||
u = UInt128(0) | ||
for i in groupings | ||
u <<= 4 | ||
d = s[i] - '0' | ||
u |= 0xf & (d - 39*(d > 9)) | ||
end | ||
return UUID(u) | ||
end | ||
end | ||
|
||
let groupings = [36:-1:25; 23:-1:20; 18:-1:15; 13:-1:10; 8:-1:1] | ||
function string(u::UUID) | ||
u = u.value | ||
a = Base.StringVector(36) | ||
for i in groupings | ||
d = u & 0xf | ||
a[i] = '0' + d + 39*(d > 9) | ||
u >>= 4 | ||
end | ||
a[24] = a[19] = a[14] = a[9] = '-' | ||
return String(a) | ||
end | ||
end | ||
|
||
show(io::IO, u::UUID) = write(io, string(u)) |
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