Skip to content

Commit

Permalink
move UUID from stdlib/Random into Base
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jan 20, 2018
1 parent 67ee6c6 commit 8da905b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
1 change: 1 addition & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ include("pkg/pkg.jl")
include("threadcall.jl")

# code loading
include("uuid.jl")
include("loading.jl")

# set up load path to be able to find stdlib packages
Expand Down
62 changes: 62 additions & 0 deletions base/uuid.jl
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))
58 changes: 1 addition & 57 deletions stdlib/Random/src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,7 @@ randcycle!(a::Array{<:Integer}) = randcycle!(GLOBAL_RNG, a)

## random UUID generation

struct UUID
value::UInt128

UUID(u::UInt128) = new(u)
end
import Base: UUID

"""
uuid1([rng::AbstractRNG=GLOBAL_RNG]) -> UUID
Expand Down Expand Up @@ -409,55 +405,3 @@ function uuid4(rng::AbstractRNG=GLOBAL_RNG)
u |= 0x00000000000040008000000000000000
UUID(u)
end

"""
uuid_version(u::UUID) -> Integer
Inspects the given UUID and returns its version (see RFC 4122).
# 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 Base.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

Base.show(io::IO, u::UUID) = write(io, string(u))

0 comments on commit 8da905b

Please sign in to comment.