Remove the need for the unicode feature flag
This commit is contained in:
parent
5f6838130d
commit
8ba1e7aceb
@ -1,7 +1,5 @@
|
|||||||
//! Generic deserialization framework.
|
//! Generic deserialization framework.
|
||||||
|
|
||||||
use std::str;
|
|
||||||
|
|
||||||
pub mod impls;
|
pub mod impls;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|
||||||
@ -204,10 +202,9 @@ pub trait Visitor {
|
|||||||
fn visit_char<E>(&mut self, v: char) -> Result<Self::Value, E>
|
fn visit_char<E>(&mut self, v: char) -> Result<Self::Value, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
// The unwraps in here should be safe.
|
// FIXME: this allocation is required in order to be compatible with stable rust, which
|
||||||
let mut s = &mut [0; 4];
|
// doesn't support encoding a `char` into a stack buffer.
|
||||||
let len = v.encode_utf8(s).unwrap();
|
self.visit_string(v.to_string())
|
||||||
self.visit_str(str::from_utf8(&s[..len]).unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E>(&mut self, _v: &str) -> Result<Self::Value, E>
|
fn visit_str<E>(&mut self, _v: &str) -> Result<Self::Value, E>
|
||||||
|
@ -2,8 +2,6 @@ use std::char;
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use unicode::str::Utf16Item;
|
|
||||||
|
|
||||||
use de;
|
use de;
|
||||||
use super::error::{Error, ErrorCode};
|
use super::error::{Error, ErrorCode};
|
||||||
|
|
||||||
@ -345,35 +343,44 @@ impl<Iter> Deserializer<Iter>
|
|||||||
// Non-BMP characters are encoded as a sequence of
|
// Non-BMP characters are encoded as a sequence of
|
||||||
// two hex escapes, representing UTF-16 surrogates.
|
// two hex escapes, representing UTF-16 surrogates.
|
||||||
n1 @ 0xD800 ... 0xDBFF => {
|
n1 @ 0xD800 ... 0xDBFF => {
|
||||||
let c1 = try!(self.next_char());
|
match (try!(self.next_char()), try!(self.next_char())) {
|
||||||
let c2 = try!(self.next_char());
|
|
||||||
match (c1, c2) {
|
|
||||||
(Some(b'\\'), Some(b'u')) => (),
|
(Some(b'\\'), Some(b'u')) => (),
|
||||||
_ => {
|
_ => {
|
||||||
return Err(self.error(ErrorCode::UnexpectedEndOfHexEscape));
|
return Err(self.error(ErrorCode::UnexpectedEndOfHexEscape));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let buf = &[n1, try!(self.decode_hex_escape())];
|
let n2 = try!(self.decode_hex_escape());
|
||||||
match ::unicode::str::utf16_items(buf).next() {
|
|
||||||
Some(Utf16Item::ScalarValue(c)) => c,
|
if n2 < 0xDC00 || n2 > 0xDFFF {
|
||||||
_ => {
|
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||||
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 => match char::from_u32(n as u32) {
|
n => {
|
||||||
Some(c) => c,
|
match char::from_u32(n as u32) {
|
||||||
None => {
|
Some(c) => c,
|
||||||
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
None => {
|
||||||
|
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let buf = &mut [0; 4];
|
// FIXME: this allocation is required in order to be compatible with stable
|
||||||
let len = c.encode_utf8(buf).unwrap_or(0);
|
// rust, which doesn't support encoding a `char` into a stack buffer.
|
||||||
self.str_buf.extend(buf[..len].iter().map(|b| *b));
|
let buf = c.to_string();
|
||||||
|
self.str_buf.extend(buf.bytes());
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(self.error(ErrorCode::InvalidEscape));
|
return Err(self.error(ErrorCode::InvalidEscape));
|
||||||
|
@ -386,9 +386,9 @@ pub fn escape_str<W>(wr: &mut W, value: &str) -> io::Result<()>
|
|||||||
fn escape_char<W>(wr: &mut W, value: char) -> io::Result<()>
|
fn escape_char<W>(wr: &mut W, value: char) -> io::Result<()>
|
||||||
where W: io::Write
|
where W: io::Write
|
||||||
{
|
{
|
||||||
let buf = &mut [0; 4];
|
// FIXME: this allocation is required in order to be compatible with stable
|
||||||
value.encode_utf8(buf);
|
// rust, which doesn't support encoding a `char` into a stack buffer.
|
||||||
escape_bytes(wr, buf)
|
escape_bytes(wr, value.to_string().as_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_f32_or_null<W>(wr: &mut W, value: f32) -> io::Result<()>
|
fn fmt_f32_or_null<W>(wr: &mut W, value: f32) -> io::Result<()>
|
||||||
|
@ -6,9 +6,8 @@
|
|||||||
//! leaving serde to perform roughly the same speed as a hand written serializer for a specific
|
//! leaving serde to perform roughly the same speed as a hand written serializer for a specific
|
||||||
//! type.
|
//! type.
|
||||||
|
|
||||||
#![feature(collections, core, std_misc, unicode)]
|
#![feature(collections, core, std_misc)]
|
||||||
|
|
||||||
extern crate unicode;
|
|
||||||
|
|
||||||
pub use ser::{Serialize, Serializer};
|
pub use ser::{Serialize, Serializer};
|
||||||
pub use de::{Deserialize, Deserializer, Error};
|
pub use de::{Deserialize, Deserializer, Error};
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
//! Generic serialization framework.
|
//! Generic serialization framework.
|
||||||
|
|
||||||
use std::str;
|
|
||||||
|
|
||||||
pub mod impls;
|
pub mod impls;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@ -97,10 +95,9 @@ pub trait Serializer {
|
|||||||
/// single character.
|
/// single character.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_char(&mut self, v: char) -> Result<(), Self::Error> {
|
fn visit_char(&mut self, v: char) -> Result<(), Self::Error> {
|
||||||
// The unwraps in here should be safe.
|
// FIXME: this allocation is required in order to be compatible with stable rust, which
|
||||||
let mut s = &mut [0; 4];
|
// doesn't support encoding a `char` into a stack buffer.
|
||||||
let len = v.encode_utf8(s).unwrap();
|
self.visit_str(&v.to_string())
|
||||||
self.visit_str(str::from_utf8(&s[..len]).unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `visit_str` serializes a `&str`.
|
/// `visit_str` serializes a `&str`.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user