GitHub PullRequestReviewCommentEvent at 2014-05-06 00:51:05
Repository: rfcs
Actor: chris-morgan
Payload: {"comment":{"url":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/12307623","id":12307623,"diff_hunk":"@@ -0,0 +1,111 @@\n+- Start Date: 2014-05-05\n+- RFC PR #:\n+- Rust Issue #:\n+\n+# Summary\n+\n+Add ASCII byte literals and ASCII byte string literals to the language,\n+similar to the existing (Unicode) character and string literals.\n+Before the RFC process was in place, \n+this was discussed in [#4334](https://github.com/mozilla/rust/issues/4334).\n+\n+\n+# Motivation\n+\n+Programs dealing with text usually should use Unicode,\n+represented in Rust by the `str` and `char` types.\n+In some cases however,\n+a program may be dealing with bytes that can not be interpreted as Unicode as a whole,\n+but still contain ASCII compatible bits.\n+\n+For example, the HTTP protocol was originally defined as Latin-1,\n+but in practice different pieces of the same request or response\n+can use different encodings.\n+The PDF file format is mostly ASCII,\n+but can contain UTF-16 strings and raw binary data.\n+\n+There is a precedent at least in Python, which has both Unicode and byte strings.\n+\n+\n+# Drawbacks\n+\n+The language becomes slightly more complex,\n+although that complexity should be limited to the parser.\n+\n+\n+# Detailed design\n+\n+Using terminology from [the Reference Manual](http://static.rust-lang.org/doc/master/rust.html#character-and-string-literals):\n+\n+Extend the syntax of expressions and patterns to add\n+byte literals of type `u8` and\n+byte string literals of type `&'static [u8]` (or `[u8]`, post-DST).\n+They are identical to the existing character and string literals, except that:\n+\n+* They are prefixed with a `b` (for \"binary\"), to distinguish them.\n+  This is similar to the `r` prefix for raw strings.\n+* Unescaped code points in the body must be in the ASCII range: U+0000 to U+007F.\n+* `'\\x5c' 'u' hex_digit 4` and `'\\x5c' 'U' hex_digit 8` escapes are not allowed.\n+* `'\\x5c' 'x' hex_digit 2` escapes represent a single byte rather than a code point.\n+  (They are the only way to express a non-ASCII byte.)\n+\n+Examples: `b'A' == 65u8`, `b'\\t' == 9u8`, `b'\\xFF' == 0xFFu8`,\n+`b\"A\\t\\xFF\" == [65u8, 9, 0xFF]`\n+\n+Assuming `buffer` of type `&[u8]`\n+```rust\n+match buffer[i] {\n+    b'a' .. b'z' => { /* ... */ }\n+    c => { /* ... */ }\n+}\n+```\n+\n+\n+# Alternatives\n+\n+Status quo: patterns must use numeric literals for ASCII values,\n+or (for a single byte, not a byte string) cast to char\n+\n+```rust\n+match buffer[i] {\n+    c @ 0x61 .. 0x7A => { /* ... */ }\n+    c => { /* ... */ }\n+}\n+match buffer[i] as char {\n+    // `c` is of the wrong type!\n+    c @ 'a' .. 'z' => { /* ... */ }\n+    c => { /* ... */ }\n+}\n+```\n+\n+Another option is to change the syntax so that macros such as\n+[`bytes!()`](http://static.rust-lang.org/doc/master/std/macros/builtin/macro.bytes.html)\n+can be used in patterns, and add a `byte!()` macro:\n+\n+```rust\n+match buffer[i] {\n+    c @ byte!('a') .. byte!('z') => { /* ... */ }\n+    c => { /* ... */ }\n+}q\n+```\n+\n+This RFC was written to align the syntax with Python,\n+but there could be many variations such as using a different prefix (maybe `a` for ASCII),\n+or using a suffix instead (maybe `u8`, as in integer literals).\n+\n+The code points from syntax could be encoded as UTF-8\n+rather than being mapped to bytes of the same value,\n+but assuming UTF-8 is not always appropriate when working with bytes.\n+\n+See also previous discussion in [#4334](https://github.com/mozilla/rust/issues/4334).\n+\n+\n+# Unresolved questions\n+\n+Should there be \"raw byte string\" literals?\n+E.g. `pdf_file.write(rb\"<< /Title (FizzBuzz \\(Part one\\)) >>\")`\n+\n+Should control characters (U+0000 to U+001F) be disallowed in syntax?\n+This should be consistent across all kinds of literals.\n+\n+Should the `bytes!()` macro be removed in favor of this?","path":"active/0000-ascii-literals.md","position":111,"original_position":111,"commit_id":"471fbe84b2873c9f9bab82e2ca84d5b311458bdb","original_commit_id":"471fbe84b2873c9f9bab82e2ca84d5b311458bdb","user":{"login":"chris-morgan","id":392868,"avatar_url":"https://avatars.githubusercontent.com/u/392868?","gravatar_id":"16a138ff3b9c8f1663f8a0613c47b60a","url":"https://api.github.com/users/chris-morgan","html_url":"https://github.com/chris-morgan","followers_url":"https://api.github.com/users/chris-morgan/followers","following_url":"https://api.github.com/users/chris-morgan/following{/other_user}","gists_url":"https://api.github.com/users/chris-morgan/gists{/gist_id}","starred_url":"https://api.github.com/users/chris-morgan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chris-morgan/subscriptions","organizations_url":"https://api.github.com/users/chris-morgan/orgs","repos_url":"https://api.github.com/users/chris-morgan/repos","events_url":"https://api.github.com/users/chris-morgan/events{/privacy}","received_events_url":"https://api.github.com/users/chris-morgan/received_events","type":"User","site_admin":false},"body":"Yes.","created_at":"2014-05-06T00:51:05Z","updated_at":"2014-05-06T00:51:05Z","html_url":"https://github.com/rust-lang/rfcs/pull/69#discussion_r12307623","pull_request_url":"https://api.github.com/repos/rust-lang/rfcs/pulls/69","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/12307623"},"html":{"href":"https://github.com/rust-lang/rfcs/pull/69#discussion_r12307623"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/69"}}}}