Skip to content

Commit 0f95988

Browse files
authored
create UUIDs stdlib package (#25819)
closes #25590
1 parent 54adf21 commit 0f95988

File tree

8 files changed

+106
-100
lines changed

8 files changed

+106
-100
lines changed

base/deprecated.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,9 @@ end
496496
# related items to remove in: abstractarray.jl, dates/periods.jl, compiler.jl
497497
# also remove all uses of is_default_method
498498

499+
@deprecate convert(::Type{UInt128}, u::UUID) UInt128(u)
500+
@deprecate convert(::Type{UUID}, s::AbstractString) UUID(s)
501+
499502
# Issue #19923
500503
@deprecate ror circshift
501504
@deprecate ror! circshift!

base/uuid.jl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,6 @@ function convert(::Type{NTuple{4, UInt32}}, uuid::UUID)
2323
return (hh, hl, lh, ll)
2424
end
2525

26-
# TODO: update documentation for new location
27-
"""
28-
uuid_version(u::UUID) -> Int
29-
30-
Inspects the given UUID and returns its version
31-
(see [RFC 4122](https://www.ietf.org/rfc/rfc4122)).
32-
33-
# Examples
34-
```jldoctest
35-
julia> rng = MersenneTwister(1234);
36-
37-
julia> Random.uuid_version(Random.uuid4(rng))
38-
4
39-
```
40-
"""
41-
uuid_version(u::UUID) = Int((u.value >> 76) & 0xf)
42-
4326
UInt128(u::UUID) = u.value
4427

4528
let groupings = [1:8; 10:13; 15:18; 20:23; 25:36]

stdlib/Random/src/deprecated.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ end
1616

1717
@deprecate randjump(mt::MersenneTwister, jumps::Integer) randjump(mt, big(10)^20, jumps)
1818

19-
@deprecate convert(::Type{UInt128}, u::UUID) UInt128(u)
20-
@deprecate convert(::Type{UUID}, s::AbstractString) UUID(s)
21-
2219
# PR #25429
2320
@deprecate rand(r::AbstractRNG, dims::Dims) rand(r, Float64, dims)
2421
@deprecate rand( dims::Dims) rand(Float64, dims)

stdlib/Random/src/misc.jl

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -341,67 +341,3 @@ function randcycle!(r::AbstractRNG, a::Array{<:Integer})
341341
end
342342

343343
randcycle!(a::Array{<:Integer}) = randcycle!(GLOBAL_RNG, a)
344-
345-
346-
## random UUID generation
347-
348-
import Base: UUID, uuid_version
349-
350-
"""
351-
uuid1([rng::AbstractRNG=GLOBAL_RNG]) -> UUID
352-
353-
Generates a version 1 (time-based) universally unique identifier (UUID), as specified
354-
by RFC 4122. Note that the Node ID is randomly generated (does not identify the host)
355-
according to section 4.5 of the RFC.
356-
357-
# Examples
358-
```jldoctest
359-
julia> rng = MersenneTwister(1234);
360-
361-
julia> Random.uuid1(rng)
362-
2cc938da-5937-11e7-196e-0f4ef71aa64b
363-
```
364-
"""
365-
function uuid1(rng::AbstractRNG=GLOBAL_RNG)
366-
u = rand(rng, UInt128)
367-
368-
# mask off clock sequence and node
369-
u &= 0x00000000000000003fffffffffffffff
370-
371-
# set the unicast/multicast bit and version
372-
u |= 0x00000000000010000000010000000000
373-
374-
# 0x01b21dd213814000 is the number of 100 nanosecond intervals
375-
# between the UUID epoch and Unix epoch
376-
timestamp = round(UInt64, time() * 1e7) + 0x01b21dd213814000
377-
ts_low = timestamp & typemax(UInt32)
378-
ts_mid = (timestamp >> 32) & typemax(UInt16)
379-
ts_hi = (timestamp >> 48) & 0x0fff
380-
381-
u |= UInt128(ts_low) << 96
382-
u |= UInt128(ts_mid) << 80
383-
u |= UInt128(ts_hi) << 64
384-
385-
UUID(u)
386-
end
387-
388-
"""
389-
uuid4([rng::AbstractRNG=GLOBAL_RNG]) -> UUID
390-
391-
Generates a version 4 (random or pseudo-random) universally unique identifier (UUID),
392-
as specified by RFC 4122.
393-
394-
# Examples
395-
```jldoctest
396-
julia> rng = MersenneTwister(1234);
397-
398-
julia> Random.uuid4(rng)
399-
82015f10-44cc-4827-996e-0f4ef71aa64b
400-
```
401-
"""
402-
function uuid4(rng::AbstractRNG=GLOBAL_RNG)
403-
u = rand(rng, UInt128)
404-
u &= 0xffffffffffff0fff3fffffffffffffff
405-
u |= 0x00000000000040008000000000000000
406-
UUID(u)
407-
end

stdlib/Random/test/runtests.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -236,22 +236,6 @@ for U in (Int64, UInt64)
236236
end
237237

238238

239-
import Random: uuid1, uuid4, UUID, uuid_version
240-
241-
# UUID
242-
u1 = uuid1()
243-
u4 = uuid4()
244-
@test uuid_version(u1) == 1
245-
@test uuid_version(u4) == 4
246-
@test u1 == UUID(string(u1)) == UUID(GenericString(string(u1)))
247-
@test u4 == UUID(string(u4)) == UUID(GenericString(string(u4)))
248-
@test u1 == UUID(UInt128(u1))
249-
@test u4 == UUID(UInt128(u4))
250-
@test uuid4(MersenneTwister(0)) == uuid4(MersenneTwister(0))
251-
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-446655440000")
252-
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-44665544000098")
253-
@test_throws ArgumentError UUID("z50e8400-e29b-41d4-a716-446655440000")
254-
255239
#issue 8257
256240
let i8257 = 1:1/3:100
257241
for i = 1:100

stdlib/UUIDs/docs/src/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# UUIDs
2+
3+
```@docs
4+
UUIDs.uuid1
5+
UUIDs.uuid4
6+
UUIDs.uuid_version
7+
```

stdlib/UUIDs/src/UUIDs.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module UUIDs
2+
3+
using Random
4+
5+
export UUID, uuid1, uuid4, uuid_version
6+
7+
import Base: UUID
8+
9+
"""
10+
uuid_version(u::UUID) -> Int
11+
12+
Inspects the given UUID and returns its version
13+
(see [RFC 4122](https://www.ietf.org/rfc/rfc4122)).
14+
15+
# Examples
16+
```jldoctest
17+
julia> uuid_version(uuid4())
18+
4
19+
```
20+
"""
21+
uuid_version(u::UUID) = Int((u.value >> 76) & 0xf)
22+
23+
"""
24+
uuid1([rng::AbstractRNG=GLOBAL_RNG]) -> UUID
25+
26+
Generates a version 1 (time-based) universally unique identifier (UUID), as specified
27+
by RFC 4122. Note that the Node ID is randomly generated (does not identify the host)
28+
according to section 4.5 of the RFC.
29+
30+
# Examples
31+
```jldoctest
32+
julia> rng = MersenneTwister(1234);
33+
34+
julia> uuid1(rng)
35+
2cc938da-5937-11e7-196e-0f4ef71aa64b
36+
```
37+
"""
38+
function uuid1(rng::AbstractRNG=Random.GLOBAL_RNG)
39+
u = rand(rng, UInt128)
40+
41+
# mask off clock sequence and node
42+
u &= 0x00000000000000003fffffffffffffff
43+
44+
# set the unicast/multicast bit and version
45+
u |= 0x00000000000010000000010000000000
46+
47+
# 0x01b21dd213814000 is the number of 100 nanosecond intervals
48+
# between the UUID epoch and Unix epoch
49+
timestamp = round(UInt64, time() * 1e7) + 0x01b21dd213814000
50+
ts_low = timestamp & typemax(UInt32)
51+
ts_mid = (timestamp >> 32) & typemax(UInt16)
52+
ts_hi = (timestamp >> 48) & 0x0fff
53+
54+
u |= UInt128(ts_low) << 96
55+
u |= UInt128(ts_mid) << 80
56+
u |= UInt128(ts_hi) << 64
57+
58+
UUID(u)
59+
end
60+
61+
"""
62+
uuid4([rng::AbstractRNG=GLOBAL_RNG]) -> UUID
63+
64+
Generates a version 4 (random or pseudo-random) universally unique identifier (UUID),
65+
as specified by RFC 4122.
66+
67+
# Examples
68+
```jldoctest
69+
julia> rng = MersenneTwister(1234);
70+
71+
julia> uuid4(rng)
72+
82015f10-44cc-4827-996e-0f4ef71aa64b
73+
```
74+
"""
75+
function uuid4(rng::AbstractRNG=Random.GLOBAL_RNG)
76+
u = rand(rng, UInt128)
77+
u &= 0xffffffffffff0fff3fffffffffffffff
78+
u |= 0x00000000000040008000000000000000
79+
UUID(u)
80+
end
81+
82+
end

stdlib/UUIDs/test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using UUIDs, Random
2+
3+
u1 = uuid1()
4+
u4 = uuid4()
5+
@test uuid_version(u1) == 1
6+
@test uuid_version(u4) == 4
7+
@test u1 == UUID(string(u1)) == UUID(GenericString(string(u1)))
8+
@test u4 == UUID(string(u4)) == UUID(GenericString(string(u4)))
9+
@test u1 == UUID(UInt128(u1))
10+
@test u4 == UUID(UInt128(u4))
11+
@test uuid4(MersenneTwister(0)) == uuid4(MersenneTwister(0))
12+
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-446655440000")
13+
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-44665544000098")
14+
@test_throws ArgumentError UUID("z50e8400-e29b-41d4-a716-446655440000")

0 commit comments

Comments
 (0)