Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed Apr 7, 2024
1 parent f868340 commit ff3a853
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
1 change: 0 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ DocMeta.setdocmeta!(UUID4, :DocTestSetup, quote
#! format: noindent
using OrderedCollections
using UUID4
using UUID4: Random
end)

@info "doctest"
Documenter.doctest(UUID4, fix = true, manual = false)

@info "makedocs"
Documenter.makedocs(
doctest = false,
format = Documenter.HTML(),
modules = [UUID4],
pages = ["Manual" => "index.md"],
Expand Down
13 changes: 13 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
## API reference
```@autodocs
Modules = [UUID4]
Order = [:module]
```

## Types
```@autodocs
Modules = [UUID4]
Order = [:constant, :type]
```

## Functions
```@autodocs
Modules = [UUID4]
Order = [:function, :macro]
```

13 changes: 13 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,18 @@ help?> uuid
## API reference
```@autodocs
Modules = [UUID4]
Order = [:module]
```

## Types
```@autodocs
Modules = [UUID4]
Order = [:constant, :type]
```

## Functions
```@autodocs
Modules = [UUID4]
Order = [:function, :macro]
```

28 changes: 21 additions & 7 deletions src/UUID4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,53 @@ export uuid_string
export uuid_tryparse
export uuid_version

export AbstractRNG, MersenneTwister, RandomDevice
export UUID

const Maybe{T} = Union{Nothing, T}
const UUID = Base.UUID
using Random: AbstractRNG, MersenneTwister, RandomDevice
using Random: Random

@doc " Maybe{T} -> Union{Nothing, T}" Maybe
@doc " UUID -> Base.UUID" UUID

"""
uuid(rng::AbstractRNG = Random.RandomDevice()) -> UUID
uuid(rng::Random.AbstractRNG = Random.RandomDevice()) -> UUID
Generate a version 4 (random or pseudo-random) universally unique identifier (UUID),
as specified by [RFC 4122](https://tools.ietf.org/html/rfc4122).
# Examples
```jldoctest
julia> rng = MersenneTwister(42);
julia> rng = Random.MersenneTwister(42);
julia> uuid(rng)
UUID("bc8f8f98-a497-45c4-817b-b034d1a24a0e")
```
"""
function uuid(rng::AbstractRNG = RandomDevice())::UUID
function uuid(rng::Random.AbstractRNG = Random.RandomDevice())::UUID
id = rand(rng, UInt128)
id &= 0xffffffffffff0fff3fffffffffffffff
id |= 0x00000000000040008000000000000000
id |> UUID
end
const uuid4 = uuid
@doc " uuid4 -> uuid" const uuid4 = uuid

"""
uuid_formats() -> Vector{Int}
Return all supported UUID string formats.
Return all $(length(UUID4.uuid_formats())) supported UUID string formats.
```julia
# case-sensitive
22 # (base62) 22
24 # (base62) 7-7-8
# case-insensitive
25 # (base36) 25
29 # (base36) 5-5-5-5-5
32 # (base16) 32
36 # (base16) 8-4-4-4-12
39 # (base16) 4-4-4-4-4-4-4-4
```
"""
function uuid_formats()::Vector{Int}
[
Expand Down
1 change: 0 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

using OrderedCollections: LittleDict, OrderedDict
using Random: Random
using Test, UUID4
using UUID4: Random

parse(Bool, get(ENV, "CI", "0")) || cd(@__DIR__) do
cp("./Project.toml", "../docs/Project.toml", force = true)
Expand All @@ -37,7 +37,7 @@ u4 = uuid4()
@test 4 == uuid_version(u4 |> string |> s -> replace(s, "-" => ""))
@test u4 == UUID(string(u4)) == UUID(GenericString(string(u4)))
@test u4 == UUID(UInt128(u4))
@test uuid4(MersenneTwister(0)) == uuid4(MersenneTwister(0))
@test uuid4(Random.MersenneTwister(0)) == uuid4(Random.MersenneTwister(0))
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-446655440000")
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-44665544000098")
@test_throws ArgumentError UUID("z50e8400-e29b-41d4-a716-446655440000")
Expand Down

0 comments on commit ff3a853

Please sign in to comment.