@@ -595,15 +595,20 @@ replace(s::AbstractString, pat_f::Pair; count=typemax(Int)) =
595
595
# hex <-> bytes conversion
596
596
597
597
"""
598
- hex2bytes(s::Union{AbstractString,AbstractVector{UInt8}} )
598
+ hex2bytes(itr )
599
599
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
601
601
`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.
603
603
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 `.
605
605
See also [`hex2bytes!`](@ref) for an in-place version, and [`bytes2hex`](@ref) for the inverse.
606
606
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
+
607
612
# Examples
608
613
```jldoctest
609
614
julia> s = string(12345, base = 16)
@@ -632,13 +637,12 @@ julia> hex2bytes(a)
632
637
"""
633
638
function hex2bytes end
634
639
635
- hex2bytes (s:: String ) = hex2bytes (transcode (UInt8, s))
636
640
hex2bytes (s) = hex2bytes! (Vector {UInt8} (undef, length (s) >> 1 ), s)
637
641
638
642
# special case - valid bytes are checked in the generic implementation
639
643
function hex2bytes! (dest:: AbstractArray{UInt8} , s:: String )
640
644
sizeof (s) != length (s) && throw (ArgumentError (" input string must consist of hexadecimal characters only" ))
641
-
645
+
642
646
hex2bytes! (dest, transcode (UInt8, s))
643
647
end
644
648
648
652
Convert an iterable `itr` of bytes representing a hexadecimal string to its binary
649
653
representation, similar to [`hex2bytes`](@ref) except that the output is written in-place
650
654
to `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.
651
660
"""
652
661
function hex2bytes! (dest:: AbstractArray{UInt8} , itr)
653
662
isodd (length (itr)) && throw (ArgumentError (" length of iterable must be even" ))
@@ -659,14 +668,15 @@ function hex2bytes!(dest::AbstractArray{UInt8}, itr)
659
668
x,state = next
660
669
y,state = iterate (itr, state)
661
670
next = iterate (itr, state)
662
- dest[i] = nfh (x) << 4 + nfh (y)
671
+ dest[i] = number_from_hex (x) << 4 + number_from_hex (y)
663
672
end
664
673
665
674
return dest
666
675
end
667
676
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 )
670
680
UInt8 (' 0' ) <= c <= UInt8 (' 9' ) && return c - UInt8 (' 0' )
671
681
c |= 0b0100000
672
682
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
681
691
returning a `String` via `bytes2hex(itr)` or writing the string to an `io` stream
682
692
via `bytes2hex(io, itr)`. The hexadecimal characters are all lowercase.
683
693
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
+
684
699
# Examples
685
700
```jldoctest
686
701
julia> a = string(12345, base = 16)
0 commit comments