GitHub PullRequestReviewCommentEvent at 2014-05-06 00:05:32
Repository: rfcs
Actor: nick29581
Payload: {"comment":{"url":"https://api.github.com/repos/rust-lang/rfcs/pulls/comments/12306418","id":12306418,"diff_hunk":"@@ -0,0 +1,110 @@\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+* 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:","path":"active/0000-ascii-literals.md","position":82,"original_position":82,"commit_id":"4ea0ec9b4eb952d56d58e0e8c33358ba99638db4","original_commit_id":"4ea0ec9b4eb952d56d58e0e8c33358ba99638db4","user":{"login":"nick29581","id":762626,"avatar_url":"https://avatars.githubusercontent.com/u/762626?","gravatar_id":"fbd53a3405ed31e6e3a1fe235ca797be","url":"https://api.github.com/users/nick29581","html_url":"https://github.com/nick29581","followers_url":"https://api.github.com/users/nick29581/followers","following_url":"https://api.github.com/users/nick29581/following{/other_user}","gists_url":"https://api.github.com/users/nick29581/gists{/gist_id}","starred_url":"https://api.github.com/users/nick29581/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nick29581/subscriptions","organizations_url":"https://api.github.com/users/nick29581/orgs","repos_url":"https://api.github.com/users/nick29581/repos","events_url":"https://api.github.com/users/nick29581/events{/privacy}","received_events_url":"https://api.github.com/users/nick29581/received_events","type":"User","site_admin":false},"body":"I think I prefer having a macro rather than a prefix for this, maybe `ascii!` rather than byte. I kind of want to make it ugly so it is clear this is the exceptional situation and you should be using utf8 unless you have a good reason not to.\r\n\r\nSo, `ascii!('t')` for `b't'` and `ascii!(\"foo\")` for `b\"foo\"`.","created_at":"2014-05-06T00:05:32Z","updated_at":"2014-05-06T00:05:32Z","html_url":"https://github.com/rust-lang/rfcs/pull/69#discussion_r12306418","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/12306418"},"html":{"href":"https://github.com/rust-lang/rfcs/pull/69#discussion_r12306418"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rfcs/pulls/69"}}}}