Skip to content

Commit dd0f1f6

Browse files
authored
move repr(::UUID) to string(::UUID) (#23777)
This is the typical place for such a method. fix #17026
1 parent 1c0573e commit dd0f1f6

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

base/random/misc.jl

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -435,33 +435,36 @@ uuid_version(u::UUID) = Int((u.value >> 76) & 0xf)
435435

436436
Base.convert(::Type{UInt128}, u::UUID) = u.value
437437

438-
function Base.convert(::Type{UUID}, s::AbstractString)
439-
s = lowercase(s)
438+
let groupings = [1:8; 10:13; 15:18; 20:23; 25:36]
439+
function Base.convert(::Type{UUID}, s::AbstractString)
440+
s = lowercase(s)
440441

441-
if !ismatch(r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$", s)
442-
throw(ArgumentError("Malformed UUID string"))
443-
end
442+
if !ismatch(r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$", s)
443+
throw(ArgumentError("Malformed UUID string"))
444+
end
444445

445-
u = UInt128(0)
446-
for i in [1:8; 10:13; 15:18; 20:23; 25:36]
447-
u <<= 4
448-
d = s[i]-'0'
449-
u |= 0xf & (d-39*(d>9))
446+
u = UInt128(0)
447+
for i in groupings
448+
u <<= 4
449+
d = s[i] - '0'
450+
u |= 0xf & (d - 39*(d > 9))
451+
end
452+
return UUID(u)
450453
end
451-
return UUID(u)
452454
end
453455

454-
function Base.repr(u::UUID)
455-
u = u.value
456-
a = Vector{UInt8}(36)
457-
for i = [36:-1:25; 23:-1:20; 18:-1:15; 13:-1:10; 8:-1:1]
458-
d = u & 0xf
459-
a[i] = '0'+d+39*(d>9)
460-
u >>= 4
456+
let groupings = [36:-1:25; 23:-1:20; 18:-1:15; 13:-1:10; 8:-1:1]
457+
function Base.string(u::UUID)
458+
u = u.value
459+
a = Base.StringVector(36)
460+
for i in groupings
461+
d = u & 0xf
462+
a[i] = '0' + d + 39*(d > 9)
463+
u >>= 4
464+
end
465+
a[24] = a[19] = a[14] = a[9] = '-'
466+
return String(a)
461467
end
462-
a[[24,19,14,9]] = '-'
463-
464-
return String(a)
465468
end
466469

467-
Base.show(io::IO, u::UUID) = write(io, Base.repr(u))
470+
Base.show(io::IO, u::UUID) = write(io, string(u))

0 commit comments

Comments
 (0)