Skip to content

Commit 07e9e09

Browse files
authored
avoid a trycatch in tryparse(::Type{UUID}, ...) (#37696)
1 parent 367f5dc commit 07e9e09

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

base/uuid.jl

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,55 @@ end
3232
UInt128(u::UUID) = u.value
3333

3434
let
35-
@noinline throw_malformed_uuid(s) = throw(ArgumentError("Malformed UUID string: $(repr(s))"))
3635
@inline function uuid_kernel(s, i, u)
3736
_c = UInt32(@inbounds codeunit(s, i))
3837
d = __convert_digit(_c, UInt32(16))
39-
d >= 16 && throw_malformed_uuid(s)
38+
d >= 16 && return nothing
4039
u <<= 4
41-
u | d
40+
return u | d
4241
end
4342

44-
global UUID
45-
function UUID(s::AbstractString)
43+
function Base.tryparse(::Type{UUID}, s::AbstractString)
4644
u = UInt128(0)
47-
ncodeunits(s) != 36 && throw_malformed_uuid(s)
45+
ncodeunits(s) != 36 && return nothing
4846
for i in 1:8
4947
u = uuid_kernel(s, i, u)
48+
u === nothing && return nothing
5049
end
51-
@inbounds codeunit(s, 9) == UInt8('-') || @goto error
50+
@inbounds codeunit(s, 9) == UInt8('-') || return nothing
5251
for i in 10:13
5352
u = uuid_kernel(s, i, u)
53+
u === nothing && return nothing
5454
end
55-
@inbounds codeunit(s, 14) == UInt8('-') || @goto error
55+
@inbounds codeunit(s, 14) == UInt8('-') || return nothing
5656
for i in 15:18
5757
u = uuid_kernel(s, i, u)
58+
u === nothing && return nothing
5859
end
59-
@inbounds codeunit(s, 19) == UInt8('-') || @goto error
60+
@inbounds codeunit(s, 19) == UInt8('-') || return nothing
6061
for i in 20:23
6162
u = uuid_kernel(s, i, u)
63+
u === nothing && return nothing
6264
end
63-
@inbounds codeunit(s, 24) == UInt8('-') || @goto error
65+
@inbounds codeunit(s, 24) == UInt8('-') || return nothing
6466
for i in 25:36
6567
u = uuid_kernel(s, i, u)
68+
u === nothing && return nothing
6669
end
6770
return Base.UUID(u)
68-
@label error
69-
throw_malformed_uuid(s)
7071
end
7172
end
7273

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
8279
end
8380
end
8481

82+
UUID(s::AbstractString) = parse(UUID, s)
83+
8584
let groupings = [36:-1:25; 23:-1:20; 18:-1:15; 13:-1:10; 8:-1:1]
8685
global string
8786
function string(u::UUID)

0 commit comments

Comments
 (0)