diff options
author | Alex Crichton <alex@alexcrichton.com> | 2014-07-15 18:36:39 -0700 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2014-07-15 18:43:00 -0700 |
commit | 5b6053fdaca465ceee451ece2c775ff8ba9fa887 (patch) | |
tree | 8e543cc990090e1bc9c3c2dde97ef8372db57dce /src | |
parent | b4a4ed72d7b13fb8012878a191c7ca15de995030 (diff) | |
download | milf-rs-5b6053fdaca465ceee451ece2c775ff8ba9fa887.tar.gz milf-rs-5b6053fdaca465ceee451ece2c775ff8ba9fa887.zip |
Support \U escapes in strings
Diffstat (limited to 'src')
-rw-r--r-- | src/parser.rs | 30 |
1 files changed, 20 insertions, 10 deletions
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")); + } } |