Skip to content

Commit 67e086a

Browse files
lilyballdguenther
authored andcommitted
---
yaml --- r: 149025 b: refs/heads/try2 c: c1cc7e5 h: refs/heads/master i: 149023: 2e87924 v: v3
1 parent 82d4090 commit 67e086a

11 files changed

+209
-19
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 58985e168be7d271cba4d3fd1d95e10ee8839d51
8+
refs/heads/try2: c1cc7e5f164b0119fcd60d6c9ade31fbfcff4b55
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/io/mem.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,7 @@ impl Seek for MemReader {
177177

178178
impl Buffer for MemReader {
179179
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
180-
if self.pos < self.buf.len() {
181-
Ok(self.buf.slice_from(self.pos))
182-
} else {
183-
Err(io::standard_error(io::EndOfFile))
184-
}
180+
Ok(self.buf.slice_from(self.pos))
185181
}
186182
fn consume(&mut self, amt: uint) { self.pos += amt; }
187183
}
@@ -312,11 +308,7 @@ impl<'a> Seek for BufReader<'a> {
312308

313309
impl<'a> Buffer for BufReader<'a> {
314310
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
315-
if self.pos < self.buf.len() {
316-
Ok(self.buf.slice_from(self.pos))
317-
} else {
318-
Err(io::standard_error(io::EndOfFile))
319-
}
311+
Ok(self.buf.slice_from(self.pos))
320312
}
321313
fn consume(&mut self, amt: uint) { self.pos += amt; }
322314
}
@@ -429,10 +421,6 @@ mod test {
429421
assert_eq!(reader.read(buf), Ok(3));
430422
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
431423
assert!(reader.read(buf).is_err());
432-
let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
433-
assert_eq!(reader.read_until(3).unwrap(), ~[0, 1, 2, 3]);
434-
assert_eq!(reader.read_until(3).unwrap(), ~[4, 5, 6, 7]);
435-
assert!(reader.read(buf).is_err());
436424
}
437425

438426
#[test]
@@ -453,10 +441,6 @@ mod test {
453441
assert_eq!(reader.read(buf), Ok(3));
454442
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
455443
assert!(reader.read(buf).is_err());
456-
let mut reader = BufReader::new(in_buf);
457-
assert_eq!(reader.read_until(3).unwrap(), ~[0, 1, 2, 3]);
458-
assert_eq!(reader.read_until(3).unwrap(), ~[4, 5, 6, 7]);
459-
assert!(reader.read(buf).is_err());
460444
}
461445

462446
#[test]

branches/try2/src/libsyntax/ext/base.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ pub fn syntax_expander_table() -> SyntaxEnv {
194194
syntax_expanders.insert(intern("bytes"),
195195
builtin_normal_expander(
196196
ext::bytes::expand_syntax_ext));
197+
syntax_expanders.insert(intern("fourcc"),
198+
builtin_normal_tt_no_ctxt(
199+
ext::fourcc::expand_syntax_ext));
197200
syntax_expanders.insert(intern("concat_idents"),
198201
builtin_normal_expander(
199202
ext::concat_idents::expand_syntax_ext));
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
/* The compiler code necessary to support the fourcc! extension. */
12+
13+
// fourcc!() is called with a single 4-character string, and an optional ident
14+
// that is either `big` or `little`. If the ident is omitted it is assumed to
15+
// be the platform-native value. It returns a u32.
16+
17+
use ast;
18+
use attr::contains;
19+
use codemap::{Span, mk_sp};
20+
use ext::base::*;
21+
use ext::base;
22+
use ext::build::AstBuilder;
23+
use parse;
24+
use parse::token;
25+
26+
use std::ascii::AsciiCast;
27+
28+
pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult {
29+
let (expr, endian) = parse_tts(cx, tts);
30+
31+
let little = match endian {
32+
None => target_endian_little(cx, sp),
33+
Some(Ident{ident, span}) => match cx.str_of(ident).as_slice() {
34+
"little" => true,
35+
"big" => false,
36+
_ => {
37+
cx.span_err(span, "invalid endian directive in fourcc!");
38+
target_endian_little(cx, sp)
39+
}
40+
}
41+
};
42+
43+
let s = match expr.node {
44+
// expression is a literal
45+
ast::ExprLit(lit) => match lit.node {
46+
// string literal
47+
ast::lit_str(s) => {
48+
if !s.is_ascii() {
49+
cx.span_err(expr.span, "non-ascii string literal in fourcc!");
50+
} else if s.len() != 4 {
51+
cx.span_err(expr.span, "string literal with len != 4 in fourcc!");
52+
}
53+
s
54+
}
55+
_ => {
56+
cx.span_err(expr.span, "unsupported literal in fourcc!");
57+
return MRExpr(cx.expr_lit(sp, ast::lit_uint(0u64, ast::ty_u32)));
58+
}
59+
},
60+
_ => {
61+
cx.span_err(expr.span, "non-literal in fourcc!");
62+
return MRExpr(cx.expr_lit(sp, ast::lit_uint(0u64, ast::ty_u32)));
63+
}
64+
};
65+
66+
let mut val = 0u32;
67+
if little {
68+
for byte in s.byte_rev_iter().take(4) {
69+
val = (val << 8) | (byte as u32);
70+
}
71+
} else {
72+
for byte in s.byte_iter().take(4) {
73+
val = (val << 8) | (byte as u32);
74+
}
75+
}
76+
let e = cx.expr_lit(sp, ast::lit_uint(val as u64, ast::ty_u32));
77+
MRExpr(e)
78+
}
79+
80+
struct Ident {
81+
ident: ast::Ident,
82+
span: Span
83+
}
84+
85+
fn parse_tts(cx: @ExtCtxt, tts: &[ast::token_tree]) -> (@ast::Expr, Option<Ident>) {
86+
let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
87+
let ex = p.parse_expr();
88+
let id = if *p.token == token::EOF {
89+
None
90+
} else {
91+
p.expect(&token::COMMA);
92+
let lo = p.span.lo;
93+
let ident = p.parse_ident();
94+
let hi = p.last_span.hi;
95+
Some(Ident{ident: ident, span: mk_sp(lo, hi)})
96+
};
97+
if *p.token != token::EOF {
98+
p.unexpected();
99+
}
100+
(ex, id)
101+
}
102+
103+
fn target_endian_little(cx: @ExtCtxt, sp: Span) -> bool {
104+
let meta = cx.meta_name_value(sp, @"target_endian", ast::lit_str(@"little"));
105+
contains(cx.cfg(), meta)
106+
}

branches/try2/src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub mod ext {
9595
pub mod bytes;
9696
pub mod concat;
9797
pub mod concat_idents;
98+
pub mod fourcc;
9899
pub mod log_syntax;
99100
pub mod source_util;
100101

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 val = fourcc!("foo"); //~ ERROR string literal with len != 4 in fourcc!
13+
let val2 = fourcc!("fooba"); //~ ERROR string literal with len != 4 in fourcc!
14+
}
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 val = fourcc!("foo ", bork); //~ ERROR invalid endian directive in fourcc!
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 v = fourcc!("fooλ"); //~ ERROR non-ascii string literal in fourcc!
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 val = fourcc!(foo); //~ ERROR non-literal in fourcc!
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 val = fourcc!(45f); //~ ERROR unsupported literal in fourcc!
13+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
static static_val: u32 = fourcc!("foo ");
12+
static static_val_le: u32 = fourcc!("foo ", little);
13+
static static_val_be: u32 = fourcc!("foo ", big);
14+
15+
fn main() {
16+
let val = fourcc!("foo ");
17+
let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
18+
assert_eq!(val, exp);
19+
20+
let val = fourcc!("foo ", big);
21+
assert_eq!(val, 0x666f6f20u32);
22+
23+
let val = fourcc!("foo ", little);
24+
assert_eq!(val, 0x206f6f66u32);
25+
26+
let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
27+
assert_eq!(static_val, exp);
28+
assert_eq!(static_val_le, 0x206f6f66u32);
29+
assert_eq!(static_val_be, 0x666f6f20u32);
30+
}

0 commit comments

Comments
 (0)