Feature #22665
openImplement Encoding#ascii_superset?
Description
Context¶
All Ruby encodings currently have a #ascii_compatible? metadata.
The notion of ASCII compatibility can be a bit ambiguous, in the case of Ruby it means that ASCII text can be directly concatenated / embedded inside the string.
But it doesn't mean the string can be scanned like an ASCII string. e.g. with UTF-8 if you are searching for \ (0x5c) you can do a simple 1B search,
but with some of the encodings currently marked as ASCII-compatible, like Shift-JS, you can't because 0x5c is potentially a continuation byte of another character.
This difference has resulted in some bugs / security vulnerability in the past, e.g. in Trilogy.
Proposal¶
I'd like Ruby to have the metadata necessary to distinguish these two distinct cases, so that's it's easier for Ruby code that is only ASCII aware to only accept encodings that are strict ASCII supersets.
The proposed method would be Encoding#ascii_superset?.
Updated by duerst (Martin Dürst) about 17 hours ago
I think we should seriously consider this proposal.
But first, it's important to note that programmers using Ruby should first and foremost use operations on Strings that work independently of the underlying representation, rather than trying to be clever, and leave the details of the encoding to Ruby itself. If some String operations would benefit from additional speedups, we should look at that.
Second, we should update the documentation (@burdettelamar (Burdette Lamar)). https://docs.ruby-lang.org/en/master/Encoding.html#method-i-ascii_compatible-3F currently just says "Returns whether ASCII-compatible or not.", which should be more precise (and include the above point).
Third, what Google AI overview returns when asked for "ruby Encoding#ascii_compatible?" is also not helpful:
Encoding#ascii_compatible? returns true if the encoding is ASCII-compatible, and false otherwise.
What It Means
An encoding is ASCII-compatible if bytes in the 0x00–0x7F range always represent their corresponding US-ASCII characters and never appear as part of multi-byte sequences or shift states.
As for your proposed method name, ascii_superset?, I'm not sure I like it because even Shift_JIS (please note the spelling; there's no Shift-JIS or Shift-JS) encodes a superset of the characters that US-ASCII encodes.
Updated by byroot (Jean Boussier) about 11 hours ago
sing Ruby should first and foremost use operations on Strings that work independently of the underlying representation, rather than trying to be clever, and leave the details of the encoding to Ruby itself
Yes, I wasn't implying the issue was with Ruby itself, it was in C extensions where using Ruby strings wasn't possible.
What I'm requesting is a helpful metadata for these cases.
Second, we should update the documentation
Yes, definitely.
As for your proposed method name, ascii_superset?, I'm not sure I like it
Right, I'm not convinced it's the best name either, I'm more than open to suggestions.
Updated by byroot (Jean Boussier) about 10 hours ago
Two possible names:
#strict_ascii_compatible?#ascii_byte_safe?