Skip to content

Commit 337e62e

Browse files
yuriksdguenther
authored andcommitted
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.
1 parent 6381daa commit 337e62e

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/libfourcc/lib.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ pub fn macro_registrar(register: |Name, SyntaxExtension|) {
7070
None));
7171
}
7272

73-
use std::ascii::AsciiCast;
74-
7573
pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult {
7674
let (expr, endian) = parse_tts(cx, tts);
7775

@@ -93,9 +91,7 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) ->
9391
ast::ExprLit(lit) => match lit.node {
9492
// string literal
9593
ast::LitStr(ref s, _) => {
96-
if !s.get().is_ascii() {
97-
cx.span_err(expr.span, "non-ascii string literal in fourcc!");
98-
} else if s.get().len() != 4 {
94+
if s.get().char_len() != 4 {
9995
cx.span_err(expr.span, "string literal with len != 4 in fourcc!");
10096
}
10197
s
@@ -112,14 +108,19 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) ->
112108
};
113109

114110
let mut val = 0u32;
115-
if little {
116-
for byte in s.get().bytes_rev().take(4) {
117-
val = (val << 8) | (byte as u32);
118-
}
119-
} else {
120-
for byte in s.get().bytes().take(4) {
121-
val = (val << 8) | (byte as u32);
122-
}
111+
for codepoint in s.get().chars().take(4) {
112+
let byte = if codepoint as u32 > 0xFF {
113+
cx.span_err(expr.span, "fourcc! literal character out of range 0-255");
114+
0u8
115+
} else {
116+
codepoint as u8
117+
};
118+
119+
val = if little {
120+
(val >> 8) | ((byte as u32) << 24)
121+
} else {
122+
(val << 8) | (byte as u32)
123+
};
123124
}
124125
let e = cx.expr_lit(sp, ast::LitUint(val as u64, ast::TyU32));
125126
MRExpr(e)

src/test/compile-fail/syntax-extension-fourcc-non-ascii-str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
extern mod fourcc;
1919

2020
fn main() {
21-
let v = fourcc!("fooλ"); //~ ERROR non-ascii string literal in fourcc!
21+
let v = fourcc!("fooλ"); //~ ERROR fourcc! literal character out of range 0-255
2222
}

src/test/run-pass-fulldeps/syntax-extension-fourcc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ fn main() {
4040
assert_eq!(static_val_le, 0x206f6f66u32);
4141
let exp = if cfg!(target_endian = "big") { 0x666f6f20u32 } else { 0x206f6f66u32 };
4242
assert_eq!(static_val_target, exp);
43+
44+
assert_eq!(fourcc!("\xC0\xFF\xEE!"), 0xC0FFEE21);
4345
}

0 commit comments

Comments
 (0)