Project

General

Profile

Actions

Feature #10391

open

Provide %eISO-8859-1'string \xAA literal' string literals with explicit encoding

Added by duerst (Martin Dürst) about 10 years ago. Updated about 10 years ago.

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

Description

There is occasionally a need to use a string literal with an Encoding different from the source encoding.
This proposes to use %e (e for encoding) to introduce such string literals.

The syntax used in the subject relies on the fact that the set of characters used in Encoding names and the set of characters used to surround the actual string in a %-literal are completely disjoint (or if they currently aren't, can be made completely disjoint). Alternatives would be to use % as a separator before and/or after the encoding, e.g. like this:

  • %eISO-8859-1'string \xAA literal' # original proposal
  • %e%ISO-8859-1%'string \xAA literal' # before and after
  • %e%ISO-8859-1'string \xAA literal' # before only
  • %eISO-8859-1%'string \xAA literal' # after only
  • %e(ISO-8859-1)(string \xAA literal) # surrounding the encoding name

The most frequent use of this would be with binary, so we probably want to allow a shortcut for binary, e.g.

  • %eB'binary \x80 string'
    or even just
  • %b'binary \x08 string'
    We could then in the long term deprecate String#b, and go back to check string validity at creation.

The upper/lowercase distinction can be used to distinguish single-quoted strings (%e) and double-quoted strings (%E). We probably also want something for regular expressions, but I'm not sure which letter is best.

There is one question about semantics: What's the meaning of e.g. %eGB2312'松本' in a program with a source encoding of UTF-8 or Shift_JIS? In some cases, it might be convenient to have the result contain the same characters. But that would mean that the data needs to be transcoded, and that could fail. The easier way to define this is that the result is the same as '松本'.force_encoding('GB2312'), i.e. just using the byte values.


Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #8848: Syntax for binary stringsOpenActions
Actions

Also available in: Atom PDF

Like0
Like0Like0