Skip to content

Commit 2d28d64

Browse files
committed
auto merge of #6569 : Kimundi/rust/ext-bytes, r=erickt
Also snug in some cosmetic changes in num.rs that aren't really important enough for an separate pr.
2 parents c34c505 + 7a2afb7 commit 2d28d64

10 files changed

+156
-13
lines changed

src/libcore/num/num.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,15 @@ pub trait FromStrRadix {
396396
/// - If code written to use this function doesn't care about it, it's
397397
/// probably assuming that `x^0` always equals `1`.
398398
///
399-
pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Div<T,T>+Mul<T,T>>(
400-
radix: uint, pow: uint) -> T {
399+
pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Div<T,T>+Mul<T,T>>(radix: uint, pow: uint) -> T {
401400
let _0: T = Zero::zero();
402401
let _1: T = One::one();
403402

404403
if pow == 0u { return _1; }
405404
if radix == 0u { return _0; }
406405
let mut my_pow = pow;
407406
let mut total = _1;
408-
let mut multiplier = cast(radix as int);
407+
let mut multiplier = cast(radix);
409408
while (my_pow > 0u) {
410409
if my_pow % 2u == 1u {
411410
total *= multiplier;
@@ -422,13 +421,13 @@ pub fn test_num<T:Num + NumCast>(ten: T, two: T) {
422421
assert_eq!(ten.add(&two), cast(12));
423422
assert_eq!(ten.sub(&two), cast(8));
424423
assert_eq!(ten.mul(&two), cast(20));
425-
assert_eq!(ten.div(&two), cast(5));
424+
assert_eq!(ten.div(&two), cast(5));
426425
assert_eq!(ten.rem(&two), cast(0));
427426

428427
assert_eq!(ten.add(&two), ten + two);
429428
assert_eq!(ten.sub(&two), ten - two);
430429
assert_eq!(ten.mul(&two), ten * two);
431-
assert_eq!(ten.div(&two), ten / two);
430+
assert_eq!(ten.div(&two), ten / two);
432431
assert_eq!(ten.rem(&two), ten % two);
433432
}
434433

src/libsyntax/ext/bytes.rs

+50-5
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
}
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+
}
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+
}
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+
}
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+
}
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+
}
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+
}
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

+11-3
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)