|
32 | 32 | UInt128(u::UUID) = u.value
|
33 | 33 |
|
34 | 34 | let
|
35 |
| -@noinline throw_malformed_uuid(s) = throw(ArgumentError("Malformed UUID string: $(repr(s))")) |
36 | 35 | @inline function uuid_kernel(s, i, u)
|
37 | 36 | _c = UInt32(@inbounds codeunit(s, i))
|
38 | 37 | d = __convert_digit(_c, UInt32(16))
|
39 |
| - d >= 16 && throw_malformed_uuid(s) |
| 38 | + d >= 16 && return nothing |
40 | 39 | u <<= 4
|
41 |
| - u | d |
| 40 | + return u | d |
42 | 41 | end
|
43 | 42 |
|
44 |
| -global UUID |
45 |
| -function UUID(s::AbstractString) |
| 43 | +function Base.tryparse(::Type{UUID}, s::AbstractString) |
46 | 44 | u = UInt128(0)
|
47 |
| - ncodeunits(s) != 36 && throw_malformed_uuid(s) |
| 45 | + ncodeunits(s) != 36 && return nothing |
48 | 46 | for i in 1:8
|
49 | 47 | u = uuid_kernel(s, i, u)
|
| 48 | + u === nothing && return nothing |
50 | 49 | end
|
51 |
| - @inbounds codeunit(s, 9) == UInt8('-') || @goto error |
| 50 | + @inbounds codeunit(s, 9) == UInt8('-') || return nothing |
52 | 51 | for i in 10:13
|
53 | 52 | u = uuid_kernel(s, i, u)
|
| 53 | + u === nothing && return nothing |
54 | 54 | end
|
55 |
| - @inbounds codeunit(s, 14) == UInt8('-') || @goto error |
| 55 | + @inbounds codeunit(s, 14) == UInt8('-') || return nothing |
56 | 56 | for i in 15:18
|
57 | 57 | u = uuid_kernel(s, i, u)
|
| 58 | + u === nothing && return nothing |
58 | 59 | end
|
59 |
| - @inbounds codeunit(s, 19) == UInt8('-') || @goto error |
| 60 | + @inbounds codeunit(s, 19) == UInt8('-') || return nothing |
60 | 61 | for i in 20:23
|
61 | 62 | u = uuid_kernel(s, i, u)
|
| 63 | + u === nothing && return nothing |
62 | 64 | end
|
63 |
| - @inbounds codeunit(s, 24) == UInt8('-') || @goto error |
| 65 | + @inbounds codeunit(s, 24) == UInt8('-') || return nothing |
64 | 66 | for i in 25:36
|
65 | 67 | u = uuid_kernel(s, i, u)
|
| 68 | + u === nothing && return nothing |
66 | 69 | end
|
67 | 70 | return Base.UUID(u)
|
68 |
| - @label error |
69 |
| - throw_malformed_uuid(s) |
70 | 71 | end
|
71 | 72 | end
|
72 | 73 |
|
73 |
| -parse(::Type{UUID}, s::AbstractString) = UUID(s) |
74 |
| -function tryparse(::Type{UUID}, s::AbstractString) |
75 |
| - try |
76 |
| - return parse(UUID, s) |
77 |
| - catch e |
78 |
| - if isa(e, ArgumentError) |
79 |
| - return nothing |
80 |
| - end |
81 |
| - rethrow(e) |
| 74 | +let |
| 75 | + @noinline throw_malformed_uuid(s) = throw(ArgumentError("Malformed UUID string: $(repr(s))")) |
| 76 | + function Base.parse(::Type{UUID}, s::AbstractString) |
| 77 | + uuid = tryparse(UUID, s) |
| 78 | + return uuid === nothing ? throw_malformed_uuid(s) : uuid |
82 | 79 | end
|
83 | 80 | end
|
84 | 81 |
|
| 82 | +UUID(s::AbstractString) = parse(UUID, s) |
| 83 | + |
85 | 84 | let groupings = [36:-1:25; 23:-1:20; 18:-1:15; 13:-1:10; 8:-1:1]
|
86 | 85 | global string
|
87 | 86 | function string(u::UUID)
|
|
0 commit comments