Skip to content

Commit 41697f9

Browse files
committed
remove some uses of Vector{UInt8}(::String)
1 parent 2a56a37 commit 41697f9

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

base/strings/search.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function _search_bloom_mask(c)
147147
end
148148

149149
_nthbyte(s::String, i) = codeunit(s, i)
150-
_nthbyte(a::ByteArray, i) = a[i]
150+
_nthbyte(a::Union{AbstractVector{UInt8},AbstractVector{Int8}}, i) = a[i]
151151

152152
function _searchindex(s::Union{String,ByteArray}, t::Union{String,ByteArray}, i)
153153
n = sizeof(t)

base/strings/util.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,24 +475,29 @@ julia> hex2bytes(a)
475475
"""
476476
function hex2bytes end
477477

478-
hex2bytes(s::AbstractString) = hex2bytes(Vector{UInt8}(String(s)))
479-
hex2bytes(s::AbstractVector{UInt8}) = hex2bytes!(Vector{UInt8}(uninitialized, length(s) >> 1), s)
478+
hex2bytes(s::AbstractString) = hex2bytes(String(s))
479+
hex2bytes(s::Union{String,AbstractVector{UInt8}}) = hex2bytes!(Vector{UInt8}(uninitialized, length(s) >> 1), s)
480+
481+
_firstbyteidx(s::String) = 1
482+
_firstbyteidx(s::AbstractVector{UInt8}) = first(eachindex(s))
483+
_lastbyteidx(s::String) = sizeof(s)
484+
_lastbyteidx(s::AbstractVector{UInt8}) = endof(s)
480485

481486
"""
482-
hex2bytes!(d::AbstractVector{UInt8}, s::AbstractVector{UInt8})
487+
hex2bytes!(d::AbstractVector{UInt8}, s::Union{String,AbstractVector{UInt8}})
483488
484489
Convert an array `s` of bytes representing a hexadecimal string to its binary
485490
representation, similar to [`hex2bytes`](@ref) except that the output is written in-place
486491
in `d`. The length of `s` must be exactly twice the length of `d`.
487492
"""
488-
function hex2bytes!(d::AbstractVector{UInt8}, s::AbstractVector{UInt8})
489-
if 2length(d) != length(s)
490-
isodd(length(s)) && throw(ArgumentError("input hex array must have even length"))
493+
function hex2bytes!(d::AbstractVector{UInt8}, s::Union{String,AbstractVector{UInt8}})
494+
if 2length(d) != sizeof(s)
495+
isodd(sizeof(s)) && throw(ArgumentError("input hex array must have even length"))
491496
throw(ArgumentError("output array must be half length of input array"))
492497
end
493498
j = first(eachindex(d)) - 1
494-
for i = first(eachindex(s)):2:endof(s)
495-
@inbounds d[j += 1] = number_from_hex(s[i]) << 4 + number_from_hex(s[i+1])
499+
for i = _firstbyteidx(s):2:_lastbyteidx(s)
500+
@inbounds d[j += 1] = number_from_hex(_nthbyte(s,i)) << 4 + number_from_hex(_nthbyte(s,i+1))
496501
end
497502
return d
498503
end

0 commit comments

Comments
 (0)