Skip to content

Commit 6004b0a

Browse files
authored
Merge pull request #354 from dtolnay/bom
Handle parsing input that starts with byte order mark
2 parents f26128d + 1d068c8 commit 6004b0a

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/fallback.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,13 @@ impl FromStr for TokenStream {
182182

183183
fn from_str(src: &str) -> Result<TokenStream, LexError> {
184184
// Create a dummy file & add it to the source map
185-
let cursor = get_cursor(src);
185+
let mut cursor = get_cursor(src);
186+
187+
// Strip a byte order mark if present
188+
const BYTE_ORDER_MARK: &str = "\u{feff}";
189+
if cursor.starts_with(BYTE_ORDER_MARK) {
190+
cursor = cursor.advance(BYTE_ORDER_MARK.len());
191+
}
186192

187193
parse::token_stream(cursor)
188194
}

src/parse.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(crate) struct Cursor<'a> {
1414
}
1515

1616
impl<'a> Cursor<'a> {
17-
fn advance(&self, bytes: usize) -> Cursor<'a> {
17+
pub fn advance(&self, bytes: usize) -> Cursor<'a> {
1818
let (_front, rest) = self.rest.split_at(bytes);
1919
Cursor {
2020
rest,
@@ -23,7 +23,7 @@ impl<'a> Cursor<'a> {
2323
}
2424
}
2525

26-
fn starts_with(&self, s: &str) -> bool {
26+
pub fn starts_with(&self, s: &str) -> bool {
2727
self.rest.starts_with(s)
2828
}
2929

tests/test.rs

+13
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,16 @@ fn check_spans_internal(ts: TokenStream, lines: &mut &[(usize, usize, usize, usi
630630
}
631631
}
632632
}
633+
634+
#[test]
635+
fn byte_order_mark() {
636+
let string = "\u{feff}foo";
637+
let tokens = string.parse::<TokenStream>().unwrap();
638+
match tokens.into_iter().next().unwrap() {
639+
TokenTree::Ident(ident) => assert_eq!(ident, "foo"),
640+
_ => unreachable!(),
641+
}
642+
643+
let string = "foo\u{feff}";
644+
string.parse::<TokenStream>().unwrap_err();
645+
}

0 commit comments

Comments
 (0)