use std::ptr;
use std::io::Write;
use json::JsonValue;
use json::number::Number;
use json::object::Object;
use std::io;
use super::print_dec;
const QU: u8 = b'"';
const BS: u8 = b'\\';
const BB: u8 = b'b';
const TT: u8 = b't';
const NN: u8 = b'n';
const FF: u8 = b'f';
const RR: u8 = b'r';
const UU: u8 = b'u';
const __: u8 = 0;
static ESCAPED: [u8; 256] = [
UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, ];
pub trait Generator {
type T: Write;
fn get_writer(&mut self) -> &mut Self::T;
#[inline(always)]
fn write(&mut self, slice: &[u8]) -> io::Result<()> {
self.get_writer().write_all(slice)
}
#[inline(always)]
fn write_char(&mut self, ch: u8) -> io::Result<()> {
self.get_writer().write_all(&[ch])
}
fn write_min(&mut self, slice: &[u8], min: u8) -> io::Result<()>;
#[inline(never)]
fn write_string_complex(&mut self, string: &str, mut start: usize) -> io::Result<()> {
self.write(string[ .. start].as_bytes())?;
for (index, ch) in string.bytes().enumerate().skip(start) {
let escape = ESCAPED[ch as usize];
if escape > 0 {
self.write(string[start .. index].as_bytes())?;
self.write(&[b'\\', escape])?;
start = index + 1;
}
if escape == b'u' {
write!(self.get_writer(), "{:04x}", ch)?;
}
}
self.write(string[start ..].as_bytes())?;
self.write_char(b'"')
}
#[inline(always)]
fn write_string(&mut self, string: &str) -> io::Result<()> {
self.write_char(b'"')?;
for (index, ch) in string.bytes().enumerate() {
if ESCAPED[ch as usize] > 0 {
return self.write_string_complex(string, index)
}
}
self.write(string.as_bytes())?;
self.write_char(b'"')
}
#[inline(always)]
fn write_number(&mut self, num: &Number) -> io::Result<()> {
if num.is_nan() {
return self.write(b"null");
}
let (positive, mantissa, exponent) = num.as_parts();
unsafe {
print_dec::write(
self.get_writer(),
positive,
mantissa,
exponent
)
}
}
#[inline(always)]
fn write_object(&mut self, object: &Object) -> io::Result<()> {
self.write_char(b'{')?;
let mut iter = object.iter();
if let Some((key, value)) = iter.next() {
self.write_string(key)?;
self.write_min(b": ", b':')?;
self.write_json(value)?;
} else {
self.write_char(b'}')?;
return Ok(());
}
for (key, value) in iter {
self.write_char(b',')?;
self.write_string(key)?;
self.write_min(b": ", b':')?;
self.write_json(value)?;
}
self.write_char(b'}')
}
fn write_json(&mut self, json: &JsonValue) -> io::Result<()> {
match *json {
JsonValue::Null => self.write(b"null"),
JsonValue::Short(ref short) => self.write_string(short.as_str()),
JsonValue::String(ref string) => self.write_string(string),
JsonValue::Number(ref number) => self.write_number(number),
JsonValue::Boolean(true) => self.write(b"true"),
JsonValue::Boolean(false) => self.write(b"false"),
JsonValue::Array(ref array) => {
self.write_char(b'[')?;
let mut iter = array.iter();
if let Some(item) = iter.next() {
self.write_json(item)?;
} else {
self.write_char(b']')?;
return Ok(());
}
for item in iter {
self.write_char(b',')?;
self.write_json(item)?;
}
self.write_char(b']')
},
JsonValue::Object(ref object) => {
self.write_object(object)
}
}
}
}
#[inline]
pub fn extend_from_slice(dst: &mut Vec<u8>, src: &[u8]) {
let dst_len = dst.len();
let src_len = src.len();
dst.reserve(src_len);
unsafe {
dst.set_len(dst_len + src_len);
ptr::copy_nonoverlapping(
src.as_ptr(),
dst.as_mut_ptr().offset(dst_len as isize),
src_len);
}
}