@@ -595,15 +595,20 @@ replace(s::AbstractString, pat_f::Pair; count=typemax(Int)) =
595595# hex <-> bytes conversion
596596
597597"""
598- hex2bytes(s::Union{AbstractString,AbstractVector{UInt8}} )
598+ hex2bytes(itr )
599599
600- Given a string or array `s ` of ASCII codes for a sequence of hexadecimal digits, returns a
600+ Given an iterable `itr ` of ASCII codes for a sequence of hexadecimal digits, returns a
601601`Vector{UInt8}` of bytes corresponding to the binary representation: each successive pair
602- of hexadecimal digits in `s ` gives the value of one byte in the return vector.
602+ of hexadecimal digits in `itr ` gives the value of one byte in the return vector.
603603
604- The length of `s ` must be even, and the returned array has half of the length of `s `.
604+ The length of `itr ` must be even, and the returned array has half of the length of `itr `.
605605See also [`hex2bytes!`](@ref) for an in-place version, and [`bytes2hex`](@ref) for the inverse.
606606
607+ !!! compat "Julia 1.7"
608+ Calling hex2bytes with iterables producing UInt8 requires
609+ version 1.7. In earlier versions, you can collect the iterable
610+ before calling instead.
611+
607612# Examples
608613```jldoctest
609614julia> s = string(12345, base = 16)
@@ -632,13 +637,12 @@ julia> hex2bytes(a)
632637"""
633638function hex2bytes end
634639
635- hex2bytes (s:: String ) = hex2bytes (transcode (UInt8, s))
636640hex2bytes (s) = hex2bytes! (Vector {UInt8} (undef, length (s) >> 1 ), s)
637641
638642# special case - valid bytes are checked in the generic implementation
639643function hex2bytes! (dest:: AbstractArray{UInt8} , s:: String )
640644 sizeof (s) != length (s) && throw (ArgumentError (" input string must consist of hexadecimal characters only" ))
641-
645+
642646 hex2bytes! (dest, transcode (UInt8, s))
643647end
644648
648652Convert an iterable `itr` of bytes representing a hexadecimal string to its binary
649653representation, similar to [`hex2bytes`](@ref) except that the output is written in-place
650654to `dest`. The length of `dest` must be half the length of `itr`.
655+
656+ !!! compat "Julia 1.7"
657+ Calling hex2bytes! with iterators producing UInt8 requires
658+ version 1.7. In earlier versions, you can collect the iterable
659+ before calling instead.
651660"""
652661function hex2bytes! (dest:: AbstractArray{UInt8} , itr)
653662 isodd (length (itr)) && throw (ArgumentError (" length of iterable must be even" ))
@@ -659,14 +668,15 @@ function hex2bytes!(dest::AbstractArray{UInt8}, itr)
659668 x,state = next
660669 y,state = iterate (itr, state)
661670 next = iterate (itr, state)
662- dest[i] = nfh (x) << 4 + nfh (y)
671+ dest[i] = number_from_hex (x) << 4 + number_from_hex (y)
663672 end
664673
665674 return dest
666675end
667676
668- @inline nfh (c:: Char ) = nfh (UInt8 (c))
669- @inline function nfh (c:: UInt8 )
677+ @inline number_from_hex (c:: AbstractChar ) = number_from_hex (Char (c))
678+ @inline number_from_hex (c:: Char ) = number_from_hex (UInt8 (c))
679+ @inline function number_from_hex (c:: UInt8 )
670680 UInt8 (' 0' ) <= c <= UInt8 (' 9' ) && return c - UInt8 (' 0' )
671681 c |= 0b0100000
672682 UInt8 (' a' ) <= c <= UInt8 (' f' ) && return c - UInt8 (' a' ) + 0x0a
@@ -681,6 +691,11 @@ Convert an iterator `itr` of bytes to its hexadecimal string representation, eit
681691returning a `String` via `bytes2hex(itr)` or writing the string to an `io` stream
682692via `bytes2hex(io, itr)`. The hexadecimal characters are all lowercase.
683693
694+ !!! compat "Julia 1.7"
695+ Calling bytes2hex with iterators producing UInt8 requires
696+ version 1.7. In earlier versions, you can collect the iterable
697+ before calling instead.
698+
684699# Examples
685700```jldoctest
686701julia> a = string(12345, base = 16)
0 commit comments