Project

General

Profile

Actions

Feature #22665

open

Implement Encoding#ascii_transparent?

Feature #22665: Implement Encoding#ascii_transparent?
2

Added by byroot (Jean Boussier) 2 days ago. Updated about 4 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:127782]

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_transparent?.

Updated by duerst (Martin Dürst) 1 day ago Actions #1 [ruby-core:127789]

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) 1 day ago Actions #2 [ruby-core:127792]

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) 1 day ago Actions #3 [ruby-core:127793]

Two possible names:

  • #strict_ascii_compatible?
  • #ascii_byte_safe?

Updated by jhawthorn (John Hawthorn) about 19 hours ago Actions #4 [ruby-core:127799]

I'm also in favour of this. I was looking for whether anyone else had named this and found this in the unicode spec https://www.unicode.org/versions/Unicode16.0.0/core-spec/chapter-2/#G11174

ASCII Transparency. The UTF-8 encoding form maintains transparency for all of the ASCII code points (0x00..0x7F). That means Unicode code points U+0000..U+007F are converted to single bytes 0x00..0x7F in UTF-8 and are thus indistinguishable from ASCII itself. Furthermore, the values 0x00..0x7F do not appear in any byte for the representation of any other Unicode code point, so that there can be no ambiguity. Beyond the ASCII range of Unicode, many of the non-ideographic scripts are represented by two bytes per code point in UTF-8; all non-surrogate code points between U+0800 and U+FFFF are represented by three bytes; and supplementary code points above U+FFFF require four bytes.

So how about #ascii_transparent?

Updated by kddnewton (Kevin Newton) about 19 hours ago Actions #5 [ruby-core:127800]

I have also needed this exact information before in Prism (https://github.com/ruby/prism/blob/12fe57bda488ce53c7a71786d4dde884ea186980/src/memchr.c#L154-L180). When searching for the next occurrence of a specific byte, I have needed to know if the encoding could potentially use that byte as a trailing byte in another character.

Updated by byroot (Jean Boussier) about 19 hours ago Actions #6 [ruby-core:127801]

  • Subject changed from Implement Encoding#ascii_superset? to Implement Encoding#ascii_transparent?
  • Description updated (diff)

So how about #ascii_transparent?

Sold!

Updated by Dan0042 (Daniel DeLorme) about 12 hours ago Actions #7 [ruby-core:127808]

A few years ago I suggested this as "ascii_safe", so the notion of "ascii_transparent" feels a bit weird and not quite intuitive, but if it's the official unicode.org terminology, what can I say. Nitpicking aside, it would be great to have.

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.

Wow, that's beyond "not helpful", it's completely false.

Updated by duerst (Martin Dürst) about 4 hours ago Actions #8 [ruby-core:127814]

As for names, I was thinking about Encoding#7bit_bytes_are_ASCII and Encoding#ASCII_is_7bit_bytes, or something similar. The advantage is that they are understandable without an additional lookup. The problem is that method names cannot start with a digit, and both of them are a bit long.

With respect to ascii_transparent, please note that this term doesn't turn up in the glossary at https://www.unicode.org/glossary/. Also, searching for it only turns up things like "there are no transparent characters in ASCII".

But I think the main problem is making sure the documentation is clear. The earlier we start fixing the documentation for #ascii_compatible?, which we can do without a decision on this issue, the better.

Minor nit: Please use the latest reference: https://www.unicode.org/versions/Unicode18.0.0/core-spec/chapter-2/#G11174

Actions

Also available in: PDF Atom