Skip to content

Commit 604667f

Browse files
Added support for a \0 escape sequence.
This commit adds support for `\0` escapes in character and string literals. Since `\0` is equivalent to `\x00`, this is a direct translation to the latter escape sequence. Future builds will be able to compile using `\0` directly. Also updated the grammar specification and added a test for NUL characters.
1 parent 4dc3a97 commit 604667f

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

doc/rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ string_body : non_double_quote
248248
| '\x5c' [ '\x22' | common_escape ] ;
249249
250250
common_escape : '\x5c'
251-
| 'n' | 'r' | 't'
251+
| 'n' | 'r' | 't' | '0'
252252
| 'x' hex_digit 2
253253
| 'u' hex_digit 4
254254
| 'U' hex_digit 8 ;

src/libsyntax/parse/lexer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
699699
'\\' => { c2 = '\\'; }
700700
'\'' => { c2 = '\''; }
701701
'"' => { c2 = '"'; }
702+
'0' => { c2 = '\x00'; }
702703
'x' => { c2 = scan_numeric_escape(rdr, 2u); }
703704
'u' => { c2 = scan_numeric_escape(rdr, 4u); }
704705
'U' => { c2 = scan_numeric_escape(rdr, 8u); }
@@ -738,6 +739,7 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
738739
'\'' => accum_str.push_char('\''),
739740
'"' => accum_str.push_char('"'),
740741
'\n' => consume_whitespace(rdr),
742+
'0' => accum_str.push_char('\x00'),
741743
'x' => {
742744
accum_str.push_char(scan_numeric_escape(rdr, 2u));
743745
}

src/test/run-pass/nul-characters.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main()
12+
{
13+
let all_nuls1 = "\0\x00\u0000\U00000000";
14+
let all_nuls2 = "\U00000000\u0000\x00\0";
15+
let all_nuls3 = "\u0000\U00000000\x00\0";
16+
let all_nuls4 = "\x00\u0000\0\U00000000";
17+
18+
// sizes for two should suffice
19+
assert_eq!(all_nuls1.len(), 4);
20+
assert_eq!(all_nuls2.len(), 4);
21+
22+
// string equality should pass between the strings
23+
assert_eq!(all_nuls1, all_nuls2);
24+
assert_eq!(all_nuls2, all_nuls3);
25+
assert_eq!(all_nuls3, all_nuls4);
26+
27+
// all extracted characters in all_nuls are equivalent to each other
28+
for c1 in all_nuls1.iter()
29+
{
30+
for c2 in all_nuls1.iter()
31+
{
32+
assert_eq!(c1,c2);
33+
}
34+
}
35+
36+
// testing equality between explicit character literals
37+
assert_eq!('\0', '\x00');
38+
assert_eq!('\u0000', '\x00');
39+
assert_eq!('\u0000', '\U00000000');
40+
41+
// NUL characters should make a difference
42+
assert!("Hello World" != "Hello \0World");
43+
assert!("Hello World" != "Hello World\0");
44+
}

0 commit comments

Comments
 (0)