Support \U escapes in strings

This commit is contained in:
Alex Crichton 2014-07-15 18:36:39 -07:00
parent b4a4ed72d7
commit 5b6053fdac

View file

@ -306,9 +306,11 @@ impl<'a> Parser<'a> {
Some((_, '"')) => Some('\u0022'), Some((_, '"')) => Some('\u0022'),
Some((_, '/')) => Some('\u002f'), Some((_, '/')) => Some('\u002f'),
Some((_, '\\')) => Some('\u005c'), Some((_, '\\')) => Some('\u005c'),
Some((pos, 'u')) => { Some((pos, c @ 'u')) |
let num = if me.input.is_char_boundary(pos + 5) { Some((pos, c @ 'U')) => {
me.input.slice(pos + 1, pos + 5) 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 { } else {
"invalid" "invalid"
}; };
@ -316,10 +318,9 @@ impl<'a> Parser<'a> {
Some(n) => { Some(n) => {
match char::from_u32(n) { match char::from_u32(n) {
Some(c) => { Some(c) => {
for _ in range(0, len) {
me.cur.next(); me.cur.next();
me.cur.next(); }
me.cur.next();
me.cur.next();
return Some(c) return Some(c)
} }
None => { None => {
@ -337,8 +338,8 @@ impl<'a> Parser<'a> {
me.errors.push(Error { me.errors.push(Error {
lo: pos, lo: pos,
hi: pos + 1, hi: pos + 1,
desc: format!("expected four hex digits \ desc: format!("expected {} hex digits \
after a `u` escape"), after a `u` escape", len),
}) })
} }
} }
@ -681,7 +682,7 @@ impl<'a> Parser<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Parser; use {Table, Parser};
#[test] #[test]
fn crlf() { fn crlf() {
@ -710,4 +711,13 @@ name = \"splay\"\r\n\
assert_eq!(p.to_linecol(7), (2, 0)); 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"));
}
} }