Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bytes2hex(io, a) method #27124

Merged
merged 1 commit into from
May 20, 2018
Merged

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented May 16, 2018

Fixes #27036.

I don't really care about allocating an extra string when calling print on an SHA1 object, but I can easily imagine cases where you might be writing a long block of hexadecimal text to a file and then it would be mildly annoying to be forced to allocate an extra string.

Actually, the old code allocated two extra strings rather than one, because it wasn't using StringVector. #27036 also mentioned UUID printing, but this doesn't go through bytes2hex so I left it as allocating an extra string (I doubt it's a big deal in practice) but I did slightly clean up the code to use the same hex_chars array as bytes2hex.

@stevengj
Copy link
Member Author

Travis Mac failure (running casher push took longer than 180 seconds and has been aborted) looks unrelated.

@@ -575,6 +580,11 @@ function bytes2hex(a::AbstractArray{UInt8})
return String(b)
end

bytes2hex(io::IO, a::AbstractArray{UInt8}) =
for x in a
print(io, Char(hex_chars[1 + x >> 4]), Char(hex_chars[1 + x & 0xf]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write(io, hex_chars[1 + (x >> 4)]) would probably be faster, since it'll save us from needing to decode the Char into the stream

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faster but wrong, unfortunately. Writing bytes directly assumes that io uses an ASCII-compatible encoding (#26286). We would need an ASCIIChar type to get both encoding-independence and maximum performance here.

@JeffBezanson JeffBezanson merged commit ca9eca7 into JuliaLang:master May 20, 2018
@stevengj stevengj deleted the bytes2hex_io branch May 20, 2018 20:23
staticfloat pushed a commit that referenced this pull request May 20, 2018
staticfloat pushed a commit that referenced this pull request May 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants