Small fixes to hex2bytes #64379
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
Yeah they agree for ASCII, so this should be correct
Yeah, I guess most byte-sized string encodings are supersets of ASCII, particularly when considering just the hex digit characters (or isalphanum)
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.
Codepoints are unicode, codeunits are not (e.g. they could be latin1), and only UTF32String has a 1-to-1 mapping between them.
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.
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.
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.
vtjnash
jakobnissen
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) === UInt8Optimize char-to-hex parsing slightly
Alternative to #64358
Assisted by: Claude Code (Opus 5.5)