diff options
| author | Yuri Kunde Schlesner <yuriks@yuriks.net> | 2014-02-06 19:14:42 -0200 |
|---|---|---|
| committer | Derek Guenther <dguenther9@gmail.com> | 2014-02-08 23:40:17 -0600 |
| commit | 337e62e4d65e24682ba323b65283a59715a0c8af (patch) | |
| tree | f16040fc72557ccedbd0d4c7a6655b6ce28d3a0a | |
| parent | 6381daab773ca97ef6553d4d244cc9a8f49475a4 (diff) | |
| download | rust-337e62e4d65e24682ba323b65283a59715a0c8af.tar.gz rust-337e62e4d65e24682ba323b65283a59715a0c8af.zip | |
Allow codepoints 128-255 in fourc!!
Codepoints with those values will be interpreted as bytes with their
raw codepoint value. ('\xAB' -> 0xABu8, etc.) Codepoints > 255 remain
forbidden.
| -rw-r--r-- | src/libfourcc/lib.rs | 27 | ||||
| -rw-r--r-- | src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs | 2 | ||||
| -rw-r--r-- | src/test/run-pass-fulldeps/syntax-extension-fourcc.rs | 2 |
3 files changed, 17 insertions, 14 deletions
diff --git a/src/libfourcc/lib.rs b/src/libfourcc/lib.rs index 2b017b501c9..ea82d31bbe7 100644 --- a/src/libfourcc/lib.rs +++ b/src/libfourcc/lib.rs @@ -70,8 +70,6 @@ pub fn macro_registrar(register: |Name, SyntaxExtension|) { None)); } -use std::ascii::AsciiCast; - pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult { let (expr, endian) = parse_tts(cx, tts); @@ -93,9 +91,7 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> ast::ExprLit(lit) => match lit.node { // string literal ast::LitStr(ref s, _) => { - if !s.get().is_ascii() { - cx.span_err(expr.span, "non-ascii string literal in fourcc!"); - } else if s.get().len() != 4 { + if s.get().char_len() != 4 { cx.span_err(expr.span, "string literal with len != 4 in fourcc!"); } s @@ -112,14 +108,19 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> }; let mut val = 0u32; - if little { - for byte in s.get().bytes_rev().take(4) { - val = (val << 8) | (byte as u32); - } - } else { - for byte in s.get().bytes().take(4) { - val = (val << 8) | (byte as u32); - } + for codepoint in s.get().chars().take(4) { + let byte = if codepoint as u32 > 0xFF { + cx.span_err(expr.span, "fourcc! literal character out of range 0-255"); + 0u8 + } else { + codepoint as u8 + }; + + val = if little { + (val >> 8) | ((byte as u32) << 24) + } else { + (val << 8) | (byte as u32) + }; } let e = cx.expr_lit(sp, ast::LitUint(val as u64, ast::TyU32)); MRExpr(e) diff --git a/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs b/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs index 18ec4547ddf..5cc29b9d49f 100644 --- a/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs +++ b/src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs @@ -18,5 +18,5 @@ extern mod fourcc; fn main() { - let v = fourcc!("fooλ"); //~ ERROR non-ascii string literal in fourcc! + let v = fourcc!("fooλ"); //~ ERROR fourcc! literal character out of range 0-255 } diff --git a/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs b/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs index be5e24d09d5..f22626fb3a1 100644 --- a/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs +++ b/src/test/run-pass-fulldeps/syntax-extension-fourcc.rs @@ -40,4 +40,6 @@ fn main() { assert_eq!(static_val_le, 0x206f6f66u32); let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 }; assert_eq!(static_val_target, exp); + + assert_eq!(fourcc!("\xC0\xFF\xEE!"), 0xC0FFEE21); } |
