You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've recently had the problem of using bytes2hex on the reverse of a large vector that mustn't be modified (and copying it would be unnecessary, as bytes2hex already allocates). The arguments for bytes2hex are restricted by Union{NTuple{Any, UInt8}, AbstractArray{UInt8}} though, so using Iterators.reverse to save the copying wasn't an option. bytes2hex doesn't require its argument to be an array though - being iterable, having a length and having an eltype of UInt8 is enough.
For my specific problem, adding the following method was enough to fix it:
function bytes2hex(a::Base.Iterators.Reverse{<:AbstractVector{UInt8}})
b = Base.StringVector(2*length(a))
@inbounds for (i, x) in enumerate(a)
b[2i - 1] = hex_chars[1 + x >> 4]
b[2i ] = hex_chars[1 + x & 0xf]
end
return String(b)
end
This won't work for general iterables of course, which is why I opened this issue instead of a PR widening the type signature. I'm mostly looking for ideas on how to best widen the signature and would submit a PR implementing it once we've decided on a design.
The text was updated successfully, but these errors were encountered:
Yes, that's one part - I guess ArgumentError("eltype of iterable not UInt8") would be best?
Hm, general iterables with undefined length would have to return an iterator as well, but those with HasLength could use it to keep the current interface non-breaking 🤔 And the lazy variant could live in Iterators, though I'm not sure we should add that version if the n2x interface is planned to change #14418
I've recently had the problem of using
bytes2hex
on the reverse of a large vector that mustn't be modified (and copying it would be unnecessary, asbytes2hex
already allocates). The arguments forbytes2hex
are restricted byUnion{NTuple{Any, UInt8}, AbstractArray{UInt8}}
though, so usingIterators.reverse
to save the copying wasn't an option.bytes2hex
doesn't require its argument to be an array though - being iterable, having a length and having an eltype ofUInt8
is enough.For my specific problem, adding the following method was enough to fix it:
This won't work for general iterables of course, which is why I opened this issue instead of a PR widening the type signature. I'm mostly looking for ideas on how to best widen the signature and would submit a PR implementing it once we've decided on a design.
The text was updated successfully, but these errors were encountered: