mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 06:41:03 +00:00
Merge pull request #71 from lifthrasiir/json-split-branch
Replace a redundant `escape` variable with nested matches.
This commit is contained in:
+62
-64
@@ -307,89 +307,87 @@ impl<Iter> Deserializer<Iter>
|
|||||||
fn parse_string(&mut self) -> Result<(), Error> {
|
fn parse_string(&mut self) -> Result<(), Error> {
|
||||||
self.str_buf.clear();
|
self.str_buf.clear();
|
||||||
|
|
||||||
let mut escape = false;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let ch = match try!(self.next_char()) {
|
let ch = match try!(self.next_char()) {
|
||||||
Some(ch) => ch,
|
Some(ch) => ch,
|
||||||
None => { return Err(self.error(ErrorCode::EOFWhileParsingString)); }
|
None => { return Err(self.error(ErrorCode::EOFWhileParsingString)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
if escape {
|
match ch {
|
||||||
match ch {
|
b'"' => {
|
||||||
b'"' => self.str_buf.push(b'"'),
|
try!(self.bump());
|
||||||
b'\\' => self.str_buf.push(b'\\'),
|
return Ok(());
|
||||||
b'/' => self.str_buf.push(b'/'),
|
}
|
||||||
b'b' => self.str_buf.push(b'\x08'),
|
b'\\' => {
|
||||||
b'f' => self.str_buf.push(b'\x0c'),
|
let ch = match try!(self.next_char()) {
|
||||||
b'n' => self.str_buf.push(b'\n'),
|
Some(ch) => ch,
|
||||||
b'r' => self.str_buf.push(b'\r'),
|
None => { return Err(self.error(ErrorCode::EOFWhileParsingString)); }
|
||||||
b't' => self.str_buf.push(b'\t'),
|
};
|
||||||
b'u' => {
|
|
||||||
let c = match try!(self.decode_hex_escape()) {
|
|
||||||
0xDC00 ... 0xDFFF => {
|
|
||||||
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non-BMP characters are encoded as a sequence of
|
match ch {
|
||||||
// two hex escapes, representing UTF-16 surrogates.
|
b'"' => self.str_buf.push(b'"'),
|
||||||
n1 @ 0xD800 ... 0xDBFF => {
|
b'\\' => self.str_buf.push(b'\\'),
|
||||||
match (try!(self.next_char()), try!(self.next_char())) {
|
b'/' => self.str_buf.push(b'/'),
|
||||||
(Some(b'\\'), Some(b'u')) => (),
|
b'b' => self.str_buf.push(b'\x08'),
|
||||||
_ => {
|
b'f' => self.str_buf.push(b'\x0c'),
|
||||||
return Err(self.error(ErrorCode::UnexpectedEndOfHexEscape));
|
b'n' => self.str_buf.push(b'\n'),
|
||||||
}
|
b'r' => self.str_buf.push(b'\r'),
|
||||||
}
|
b't' => self.str_buf.push(b'\t'),
|
||||||
|
b'u' => {
|
||||||
let n2 = try!(self.decode_hex_escape());
|
let c = match try!(self.decode_hex_escape()) {
|
||||||
|
0xDC00 ... 0xDFFF => {
|
||||||
if n2 < 0xDC00 || n2 > 0xDFFF {
|
|
||||||
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||||
}
|
}
|
||||||
|
|
||||||
let n = (((n1 - 0xD800) as u32) << 10 |
|
// Non-BMP characters are encoded as a sequence of
|
||||||
(n2 - 0xDC00) as u32) + 0x1_0000;
|
// two hex escapes, representing UTF-16 surrogates.
|
||||||
|
n1 @ 0xD800 ... 0xDBFF => {
|
||||||
|
match (try!(self.next_char()), try!(self.next_char())) {
|
||||||
|
(Some(b'\\'), Some(b'u')) => (),
|
||||||
|
_ => {
|
||||||
|
return Err(self.error(ErrorCode::UnexpectedEndOfHexEscape));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match char::from_u32(n as u32) {
|
let n2 = try!(self.decode_hex_escape());
|
||||||
Some(c) => c,
|
|
||||||
None => {
|
if n2 < 0xDC00 || n2 > 0xDFFF {
|
||||||
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||||
|
}
|
||||||
|
|
||||||
|
let n = (((n1 - 0xD800) as u32) << 10 |
|
||||||
|
(n2 - 0xDC00) as u32) + 0x1_0000;
|
||||||
|
|
||||||
|
match char::from_u32(n as u32) {
|
||||||
|
Some(c) => c,
|
||||||
|
None => {
|
||||||
|
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
n => {
|
n => {
|
||||||
match char::from_u32(n as u32) {
|
match char::from_u32(n as u32) {
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
None => {
|
None => {
|
||||||
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
// FIXME: this allocation is required in order to be compatible with stable
|
// FIXME: this allocation is required in order to be compatible with stable
|
||||||
// rust, which doesn't support encoding a `char` into a stack buffer.
|
// rust, which doesn't support encoding a `char` into a stack buffer.
|
||||||
let buf = c.to_string();
|
let buf = c.to_string();
|
||||||
self.str_buf.extend(buf.bytes());
|
self.str_buf.extend(buf.bytes());
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(self.error(ErrorCode::InvalidEscape));
|
return Err(self.error(ErrorCode::InvalidEscape));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
escape = false;
|
ch => {
|
||||||
} else {
|
self.str_buf.push(ch);
|
||||||
match ch {
|
|
||||||
b'"' => {
|
|
||||||
try!(self.bump());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
b'\\' => {
|
|
||||||
escape = true;
|
|
||||||
}
|
|
||||||
ch => {
|
|
||||||
self.str_buf.push(ch);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user