Skip to content

Commit 8e4f108

Browse files
committed
Widen type signature of bytes2hex
1 parent 7040ffb commit 8e4f108

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

base/strings/util.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,12 +666,12 @@ end
666666
throw(ArgumentError("byte is not an ASCII hexadecimal digit"))
667667

668668
"""
669-
bytes2hex(a::AbstractArray{UInt8}) -> String
670-
bytes2hex(io::IO, a::AbstractArray{UInt8})
669+
bytes2hex(itr) -> String
670+
bytes2hex(io::IO, itr)
671671
672-
Convert an array `a` of bytes to its hexadecimal string representation, either
673-
returning a `String` via `bytes2hex(a)` or writing the string to an `io` stream
674-
via `bytes2hex(io, a)`. The hexadecimal characters are all lowercase.
672+
Convert an iterator `itr` of bytes to its hexadecimal string representation, either
673+
returning a `String` via `bytes2hex(itr)` or writing the string to an `io` stream
674+
via `bytes2hex(io, itr)`. The hexadecimal characters are all lowercase.
675675
676676
# Examples
677677
```jldoctest
@@ -689,17 +689,19 @@ julia> bytes2hex(b)
689689
"""
690690
function bytes2hex end
691691

692-
function bytes2hex(a::Union{Tuple{Vararg{UInt8}}, AbstractArray{UInt8}})
693-
b = Base.StringVector(2*length(a))
694-
@inbounds for (i, x) in enumerate(a)
692+
function bytes2hex(itr)
693+
eltype(itr) === UInt8 || throw(ArgumentError("eltype of iterator not UInt8"))
694+
b = Base.StringVector(2*length(itr))
695+
@inbounds for (i, x) in enumerate(itr)
695696
b[2i - 1] = hex_chars[1 + x >> 4]
696697
b[2i ] = hex_chars[1 + x & 0xf]
697698
end
698699
return String(b)
699700
end
700701

701-
function bytes2hex(io::IO, a::Union{Tuple{Vararg{UInt8}}, AbstractArray{UInt8}})
702-
for x in a
702+
function bytes2hex(io::IO, itr)
703+
eltype(itr) != UInt8 && throw(ArgumentError("eltype of iterator not UInt8"))
704+
for x in itr
703705
print(io, Char(hex_chars[1 + x >> 4]), Char(hex_chars[1 + x & 0xf]))
704706
end
705707
end

test/strings/util.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ end
376376
#non-hex characters
377377
@test_throws ArgumentError hex2bytes(b"0123456789abcdefABCDEFGH")
378378
end
379+
380+
@testset "Issue 39284" begin
381+
@test "efcdabefcdab8967452301" == bytes2hex(Iterators.reverse(hex2bytes("0123456789abcdefABCDEF")))
382+
end
379383
end
380384

381385
# b"" should be immutable

0 commit comments

Comments
 (0)