From 5b6053fdaca465ceee451ece2c775ff8ba9fa887 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 15 Jul 2014 18:36:39 -0700 Subject: Support \U escapes in strings --- src/parser.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs index ea1b3eb..49f7a4c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -306,9 +306,11 @@ impl<'a> Parser<'a> { Some((_, '"')) => Some('\u0022'), Some((_, '/')) => Some('\u002f'), Some((_, '\\')) => Some('\u005c'), - Some((pos, 'u')) => { - let num = if me.input.is_char_boundary(pos + 5) { - me.input.slice(pos + 1, pos + 5) + Some((pos, c @ 'u')) | + Some((pos, c @ 'U')) => { + let len = if c == 'u' {4} else {8}; + let num = if me.input.is_char_boundary(pos + 1 + len) { + me.input.slice(pos + 1, pos + 1 + len) } else { "invalid" }; @@ -316,10 +318,9 @@ impl<'a> Parser<'a> { Some(n) => { match char::from_u32(n) { Some(c) => { - me.cur.next(); - me.cur.next(); - me.cur.next(); - me.cur.next(); + for _ in range(0, len) { + me.cur.next(); + } return Some(c) } None => { @@ -337,8 +338,8 @@ impl<'a> Parser<'a> { me.errors.push(Error { lo: pos, hi: pos + 1, - desc: format!("expected four hex digits \ - after a `u` escape"), + desc: format!("expected {} hex digits \ + after a `u` escape", len), }) } } @@ -681,7 +682,7 @@ impl<'a> Parser<'a> { #[cfg(test)] mod tests { - use super::Parser; + use {Table, Parser}; #[test] fn crlf() { @@ -710,4 +711,13 @@ name = \"splay\"\r\n\ assert_eq!(p.to_linecol(7), (2, 0)); } + #[test] + fn fun_with_strings() { + let mut p = Parser::new(r#" +[foo] +bar = "\U00000000" +"#); + let table = Table(p.parse().unwrap()); + assert_eq!(table.lookup("foo.bar").and_then(|k| k.as_str()), Some("\0")); + } } -- cgit v1.2.3