Skip to content
This repository was archived by the owner on Nov 26, 2020. It is now read-only.

Commit 7c95010

Browse files
committed
Implement JSON hex escapes
1 parent d74d140 commit 7c95010

File tree

1 file changed

+8
-1
lines changed
  • examples/010-json/json_parser/src

1 file changed

+8
-1
lines changed

examples/010-json/json_parser/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ impl Parser {
114114
't' => s.push('\t'),
115115
'b' => s.push('\u{10}'),
116116
'f' => s.push('\u{14}'),
117-
'u' => unimplemented!(),
117+
'u' => s.push(std::char::from_u32(self.parse_hex_digit()? << 12 |
118+
self.parse_hex_digit()? << 8 |
119+
self.parse_hex_digit()? << 4 |
120+
self.parse_hex_digit()?).ok_or(ParseError)?),
118121
_ => return Err(ParseError)
119122
},
120123
x => s.push(x)
@@ -123,6 +126,10 @@ impl Parser {
123126
Ok(Value::String(s))
124127
}
125128

129+
fn parse_hex_digit(&mut self) -> ParseResult<u32> {
130+
self.next()?.to_digit(16).ok_or(ParseError)
131+
}
132+
126133
fn parse_number(&mut self) -> ParseResult<Value> {
127134
let mut s = String::new();
128135

0 commit comments

Comments
 (0)