From 3d4655798e6168205ebc03d6623740de7301d39f Mon Sep 17 00:00:00 2001 From: Heptazhou Date: Sat, 6 Apr 2024 22:21:23 +0000 Subject: [PATCH] Update --- src/UUID4.jl | 35 +++++++++++++++++++++++------------ test/runtests.jl | 4 +++- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/UUID4.jl b/src/UUID4.jl index 68e510f..454d38d 100644 --- a/src/UUID4.jl +++ b/src/UUID4.jl @@ -78,18 +78,18 @@ function uuid_formats()::Vector{Int} end """ - uuid_parse(str::AbstractString; fmt::Int = length(str)) -> Tuple{Int, UUID} + uuid_parse(str::AbstractString; fmt::Int = ncodeunits(str)) -> Tuple{Int, UUID} """ function uuid_parse end function uuid_parse(id::UUID)::Tuple{Int, UUID} - (length("$id"), id) # (36, id) + (36, id) end function uuid_parse(str::AbstractString; fmt::Int = 0)::Tuple{Int, UUID} - len = length(str) + len = ncodeunits(str) (id = uuid_tryparse(str)) |> isnothing && - argumenterror("Invalid id `$str` with length = $len") + argumenterror("Invalid id `$str` with ncodeunits = $len") if 0 < fmt ≠ len - argumenterror("Invalid id `$str` with length = $len (should be $fmt)") + argumenterror("Invalid id `$str` with ncodeunits = $len (should be $fmt)") elseif fmt < 0 argumenterror("Invalid format `$fmt` (should be positive)") end @@ -150,24 +150,35 @@ function uuid_string(id::UUID, fmt::Int)::String fmt ≡ 22 ? string(id.value, base = 62, pad = fmt) : fmt ≡ 25 ? string(id.value, base = 36, pad = fmt) : fmt ≡ 32 ? string(id.value, base = 16, pad = fmt) : - fmt ≡ 24 ? replace(uuid_string(id, 22), r"(.{7})" => s"\1-", count = fmt - 22) : - fmt ≡ 29 ? replace(uuid_string(id, 25), r"(.{5})" => s"\1-", count = fmt - 25) : - fmt ≡ 39 ? replace(uuid_string(id, 32), r"(.{4})" => s"\1-", count = fmt - 32) : + fmt ≡ 24 ? replace(uuid_string(id, 22), r"(.{7})" => s"\1-", count = 2) : + fmt ≡ 29 ? replace(uuid_string(id, 25), r"(.{5})" => s"\1-", count = 4) : + fmt ≡ 39 ? replace(uuid_string(id, 32), r"(.{4})" => s"\1-", count = 7) : argumenterror("Invalid format `$fmt` (not defined)") end """ uuid_tryparse(s::AbstractString) -> Maybe{Union{UInt128, UUID}} """ +function uuid_tryparse end +let +#! format: noindent +function dropdash(s::AbstractString; step::Int, count::Int) + for i ∈ 1:count + @inbounds UInt8('-') ≠ codeunit(s, (1 + step)i) && return "" + end + replace(s, "-" => ""; count) +end function uuid_tryparse(str::AbstractString)::Maybe{Union{UInt128, UUID}} len = ncodeunits(str) - len ≡ 24 ? uuid_tryparse(replace(str, "-" => "")) : - len ≡ 29 ? uuid_tryparse(replace(str, "-" => "")) : - len ≡ 39 ? uuid_tryparse(replace(str, "-" => "")) : + len ≡ 24 ? uuid_tryparse(dropdash(str, step = 7, count = 2)) : + len ≡ 29 ? uuid_tryparse(dropdash(str, step = 5, count = 4)) : + len ≡ 39 ? uuid_tryparse(dropdash(str, step = 4, count = 7)) : len ≡ 22 ? Base.tryparse(UInt128, str, base = 62) : len ≡ 25 ? Base.tryparse(UInt128, str, base = 36) : len ≡ 32 ? Base.tryparse(UInt128, str, base = 16) : - len ≡ 36 ? Base.tryparse(UUID, str) : nothing + len ≡ 36 ? Base.tryparse(UUID, str) : (nothing) +end +global uuid_tryparse end """ diff --git a/test/runtests.jl b/test/runtests.jl index c2083f6..0591ec4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -42,7 +42,9 @@ str = "22b4a8a1-e548-4eeb-9270-60426d66a48e" @test_throws ArgumentError UUID("22b4a8a1-e548-4eeb-9270a60426d66a48e") @test UUID(uppercase(str)) == UUID(str) for r ∈ rand(UInt128, 10^3) - @test UUID(r) == UUID(string(UUID(r))) + s = string(UUID(r)) + @test UUID(r) == UUID(s) + @test ncodeunits(s) === length(s) === 36 end fmt = [22, 24, 25, 29, 32, 36, 39]