← Back to Pull requests

Small fixes to hex2bytes #64379

Open
jakobnissen opened this pull request with 86 lines diff across 2 files
View on GitHub
jakobnissen jakobnissen commented on Oct 27, 2026 13:18 UTC
Author

Consistently throw ArgumentError for non-ASCII inputs
Do not mention misleading "element" count in error messages when actually counting codeunits of strings
Use the fast codeunits path for all strings with codeunit(s) === UInt8
Optimize char-to-hex parsing slightly

  • Avoid repeated length computation for strings
  • Alternative to #64358

    Assisted by: Claude Code (Opus 5.5)

    Diff: 86 lines (+72 / -14) across 2 files Reviewer: vtjnash Review duration: 27084m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.36) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.36 · 86 lines diff across 2 files
    0.36
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.90 · 5 substantive comment(s)
    0.90
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    91%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.267
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    9 items
    vtjnash vtjnash commented on code on Oct 27, 2026 18:23 UTC
    Code Review
    base/strings/util.jl:1
    @@ -1236,11 +1236,17 @@ function hex2bytes end
     
     hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s)::Int >> 1), s)
     
    -# special case - valid bytes are checked in the generic implementation
    -function hex2bytes!(dest::AbstractArray{UInt8}, s::String)
    -    sizeof(s) != length(s) && throw(ArgumentError("input string must consist of hexadecimal characters only"))
    +# Strings with `UInt8` codeunits are decoded via their codeunits, since a non-ASCII
    +# character consists only of bytes >= 0x80, which are rejected as hex digits.
    +# Otherwise, iterate the characters, computing the O(n) `length` only once.
    +function hex_digits(s::AbstractString)
    +    codeunit(s) === UInt8 && return (codeunits(s), ncodeunits(s))

    A codeunit isn't a character though, but I guess maybe it agrees over ascii? There's a utf8units helper instead if you want to specifically get utf8 code units

    jakobnissen jakobnissen commented on code on Oct 27, 2026 18:57 UTC
    Code Review
    base/strings/util.jl:1
    @@ -1236,11 +1236,17 @@ function hex2bytes end
     
     hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s)::Int >> 1), s)
     
    -# special case - valid bytes are checked in the generic implementation
    -function hex2bytes!(dest::AbstractArray{UInt8}, s::String)
    -    sizeof(s) != length(s) && throw(ArgumentError("input string must consist of hexadecimal characters only"))
    +# Strings with `UInt8` codeunits are decoded via their codeunits, since a non-ASCII
    +# character consists only of bytes >= 0x80, which are rejected as hex digits.
    +# Otherwise, iterate the characters, computing the O(n) `length` only once.
    +function hex_digits(s::AbstractString)
    +    codeunit(s) === UInt8 && return (codeunits(s), ncodeunits(s))

    Yeah they agree for ASCII, so this should be correct

    vtjnash vtjnash commented on code on Oct 28, 2026 03:43 UTC
    Code Review
    base/strings/util.jl:1
    @@ -1236,11 +1236,17 @@ function hex2bytes end
     
     hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s)::Int >> 1), s)
     
    -# special case - valid bytes are checked in the generic implementation
    -function hex2bytes!(dest::AbstractArray{UInt8}, s::String)
    -    sizeof(s) != length(s) && throw(ArgumentError("input string must consist of hexadecimal characters only"))
    +# Strings with `UInt8` codeunits are decoded via their codeunits, since a non-ASCII
    +# character consists only of bytes >= 0x80, which are rejected as hex digits.
    +# Otherwise, iterate the characters, computing the O(n) `length` only once.
    +function hex_digits(s::AbstractString)
    +    codeunit(s) === UInt8 && return (codeunits(s), ncodeunits(s))

    Yeah, I guess most byte-sized string encodings are supersets of ASCII, particularly when considering just the hex digit characters (or isalphanum)

    jakobnissen jakobnissen commented on code on Oct 28, 2026 06:19 UTC
    Code Review
    base/strings/util.jl:1
    @@ -1236,11 +1236,17 @@ function hex2bytes end
     
     hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s)::Int >> 1), s)
     
    -# special case - valid bytes are checked in the generic implementation
    -function hex2bytes!(dest::AbstractArray{UInt8}, s::String)
    -    sizeof(s) != length(s) && throw(ArgumentError("input string must consist of hexadecimal characters only"))
    +# Strings with `UInt8` codeunits are decoded via their codeunits, since a non-ASCII
    +# character consists only of bytes >= 0x80, which are rejected as hex digits.
    +# Otherwise, iterate the characters, computing the O(n) `length` only once.
    +function hex_digits(s::AbstractString)
    +    codeunit(s) === UInt8 && return (codeunits(s), ncodeunits(s))

    Specifically, the AbstractString docstring says that codepoints are Unicode. So it may be possible to have a string type with UInt8 codeunits that do not do a one-to-one mapping from single-byte Unicode codepoints to codeunits, but I struggle to see how that encoding could work. It’s not impossible - one could easily make an AbstractString that maps arbitrarily between codeunits and codepoints, but that seems unlikely and almost adversarial.
    Though I could make this more defensive and only dispatch to String, StringView and substrings of those.
    I also think there is an open issue about having a trait to dispatch on the encoding scheme of strings.

    vtjnash vtjnash commented on code on Oct 28, 2026 18:05 UTC
    Code Review
    base/strings/util.jl:1
    @@ -1236,11 +1236,17 @@ function hex2bytes end
     
     hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s)::Int >> 1), s)
     
    -# special case - valid bytes are checked in the generic implementation
    -function hex2bytes!(dest::AbstractArray{UInt8}, s::String)
    -    sizeof(s) != length(s) && throw(ArgumentError("input string must consist of hexadecimal characters only"))
    +# Strings with `UInt8` codeunits are decoded via their codeunits, since a non-ASCII
    +# character consists only of bytes >= 0x80, which are rejected as hex digits.
    +# Otherwise, iterate the characters, computing the O(n) `length` only once.
    +function hex_digits(s::AbstractString)
    +    codeunit(s) === UInt8 && return (codeunits(s), ncodeunits(s))

    Codepoints are unicode, codeunits are not (e.g. they could be latin1), and only UTF32String has a 1-to-1 mapping between them.

    JeffBezanson JeffBezanson commented on code on Nov 04, 2026 22:36 UTC
    Code Review
    base/strings/util.jl:1
    @@ -1236,11 +1236,17 @@ function hex2bytes end
     
     hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s)::Int >> 1), s)
     
    -# special case - valid bytes are checked in the generic implementation
    -function hex2bytes!(dest::AbstractArray{UInt8}, s::String)
    -    sizeof(s) != length(s) && throw(ArgumentError("input string must consist of hexadecimal characters only"))
    +# Strings with `UInt8` codeunits are decoded via their codeunits, since a non-ASCII
    +# character consists only of bytes >= 0x80, which are rejected as hex digits.
    +# Otherwise, iterate the characters, computing the O(n) `length` only once.
    +function hex_digits(s::AbstractString)
    +    codeunit(s) === UInt8 && return (codeunits(s), ncodeunits(s))

    I also don't really like this way of writing it; I think it's confusing to have a function that returns either a string or its codeunits.

    jakobnissen jakobnissen commented on Nov 05, 2026 19:39 UTC
    Author

    I addressed your reviews with two changes:
    1. The code no longer assumes that UInt8 codeunits implies UTF8 encoding. Instead, it dispatches on String, StringView and substrings of those, which are known to be UTF8. This is a closed set of types, and e.g. does not include InlineStrings.jl types, but it is more conservative.
    2. I have addressed Jeff's point about the type confused helper by simply having distinct paths for UTF8 and non-UTF8 strings.

    ScottPJones ScottPJones commented on code on Nov 06, 2026 19:14 UTC
    Code Review
    base/strings/util.jl:1
    @@ -1236,11 +1236,17 @@ function hex2bytes end
     
     hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s)::Int >> 1), s)
     
    -# special case - valid bytes are checked in the generic implementation
    -function hex2bytes!(dest::AbstractArray{UInt8}, s::String)
    -    sizeof(s) != length(s) && throw(ArgumentError("input string must consist of hexadecimal characters only"))
    +# Strings with `UInt8` codeunits are decoded via their codeunits, since a non-ASCII
    +# character consists only of bytes >= 0x80, which are rejected as hex digits.
    +# Otherwise, iterate the characters, computing the O(n) `length` only once.
    +function hex_digits(s::AbstractString)
    +    codeunit(s) === UInt8 && return (codeunits(s), ncodeunits(s))

    I think the docstring for AbstractString is not correct, because String allows non-Unicode Char values for it's codepoints.
    It's also very restrictive, there are many 8-bit character sets still in use (not just ISO Latin 1). All of those codepoints can be represented in Unicode, but need to be translated to the Unicode character set.

    Reviewers
    Assignees
    jakobnissen jakobnissen
    Labels
    risk: low confidence: 91.2% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 36.2% (MEDIUM) · Higher = larger blast radius
    36.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 91.2% · Lower = rushed or shallow review
    91.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.2% · Safe to merge
    3.2%