Skip to content

Commit 7a2afb7

Browse files
committed
Made bytes!() accept a list of string, integer or char literals
1 parent 916942d commit 7a2afb7

9 files changed

+152
-8
lines changed

src/libsyntax/ext/bytes.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,58 @@ use ext::base::*;
1616
use ext::base;
1717
use ext::build::{mk_u8, mk_slice_vec_e};
1818

19-
pub fn expand_syntax_ext(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
20-
-> base::MacResult {
21-
let var = get_single_str_from_tts(cx, sp, tts, "bytes!");
19+
pub fn expand_syntax_ext(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree]) -> base::MacResult {
20+
// Gather all argument expressions
21+
let exprs = get_exprs_from_tts(cx, tts);
2222
let mut bytes = ~[];
23-
for var.each |byte| {
24-
bytes.push(mk_u8(cx, sp, byte));
23+
24+
for exprs.each |expr| {
25+
match expr.node {
26+
// expression is a literal
27+
ast::expr_lit(lit) => match lit.node {
28+
// string literal, push each byte to vector expression
29+
ast::lit_str(s) => {
30+
for s.each |byte| {
31+
bytes.push(mk_u8(cx, sp, byte));
32+
}
33+
}
34+
35+
// u8 literal, push to vector expression
36+
ast::lit_uint(v, ast::ty_u8) => {
37+
if v > 0xFF {
38+
cx.span_err(sp, "Too large u8 literal in bytes!")
39+
} else {
40+
bytes.push(mk_u8(cx, sp, v as u8));
41+
}
42+
}
43+
44+
// integer literal, push to vector expression
45+
ast::lit_int_unsuffixed(v) => {
46+
if v > 0xFF {
47+
cx.span_err(sp, "Too large integer literal in bytes!")
48+
} else if v < 0 {
49+
cx.span_err(sp, "Negative integer literal in bytes!")
50+
} else {
51+
bytes.push(mk_u8(cx, sp, v as u8));
52+
}
53+
}
54+
55+
// char literal, push to vector expression
56+
ast::lit_int(v, ast::ty_char) => {
57+
if (v as char).is_ascii() {
58+
bytes.push(mk_u8(cx, sp, v as u8));
59+
} else {
60+
cx.span_err(sp, "Non-ascii char literal in bytes!")
61+
}
62+
}
63+
64+
_ => cx.span_err(sp, "Unsupported literal in bytes!")
65+
},
66+
67+
_ => cx.span_err(sp, "Non-literal in bytes!")
68+
}
2569
}
70+
2671
let e = mk_slice_vec_e(cx, sp, bytes);
2772
MRExpr(e)
2873
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
fn main() {
12+
let vec = bytes!('λ'); //~ ERROR Non-ascii char literal in bytes!
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
fn main() {
12+
let vec = bytes!(foo); //~ ERROR Non-literal in bytes!
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
fn main() {
12+
let vec = bytes!(1024); //~ ERROR Too large integer literal in bytes!
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
fn main() {
12+
let vec = bytes!(1024u8); //~ ERROR Too large u8 literal in bytes!
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
fn main() {
12+
let vec = bytes!(-1024); //~ ERROR Non-literal in bytes
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
fn main() {
12+
let vec = bytes!(-1024u8); //~ ERROR Non-literal in bytes
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
fn main() {
12+
let vec = bytes!(45f); //~ ERROR Unsupported literal in bytes!
13+
}

src/test/run-pass/syntax-extension-bytes.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
static static_vec: &'static [u8] = bytes!("abc", 0xFF, '!');
12+
1113
fn main() {
1214
let vec = bytes!("abc");
13-
assert_eq!(vec[0], 97);
14-
assert_eq!(vec[1], 98);
15-
assert_eq!(vec[2], 99);
15+
assert_eq!(vec, &[97_u8, 98_u8, 99_u8]);
16+
17+
let vec = bytes!("null", 0);
18+
assert_eq!(vec, &[110_u8, 117_u8, 108_u8, 108_u8, 0_u8]);
19+
20+
let vec = bytes!(' ', " ", 32, 32u8);
21+
assert_eq!(vec, &[32_u8, 32_u8, 32_u8, 32_u8]);
22+
23+
assert_eq!(static_vec, &[97_u8, 98_u8, 99_u8, 255_u8, 33_u8]);
1624
}

0 commit comments

Comments
 (0)