Skip to content

Commit 1550f9c

Browse files
authored
Merge pull request #49 from maciejhirsz/0.8.1
0.8.1 performance and utf8
2 parents 72c6a13 + 31620ee commit 1550f9c

File tree

6 files changed

+312
-224
lines changed

6 files changed

+312
-224
lines changed

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
[package]
22
name = "json"
3-
version = "0.8.0"
3+
version = "0.8.1"
44
authors = ["Maciej Hirsz <[email protected]>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/maciejhirsz/json-rust"
77
documentation = "http://terhix.com/doc/json/"
88
license = "MIT"
9+
10+
[dependencies]
11+
itoa = "0.1"

src/codegen.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::io::Write;
22
use std::num::FpCategory;
33
use JsonValue;
44

5+
extern crate itoa;
6+
57
pub trait Generator {
68
fn get_buffer(&mut self) -> &mut Vec<u8>;
79

@@ -27,10 +29,10 @@ pub trait Generator {
2729

2830
fn dedent(&mut self) {}
2931

30-
fn write_string(&mut self, string: &str) {
31-
self.write_char(b'"');
32+
fn write_string_complex(&mut self, string: &str, from: usize) {
33+
self.write(string[0 .. from].as_bytes());
3234

33-
for ch in string.bytes() {
35+
for ch in string.bytes().skip(from) {
3436
match ch {
3537
b'\\' | b'"' => {
3638
self.write_char(b'\\');
@@ -41,20 +43,33 @@ pub trait Generator {
4143
b'\t' => self.write(b"\\t"),
4244
0xC => self.write(b"\\f"),
4345
0x8 => self.write(b"\\b"),
44-
_ => self.write_char(ch)
46+
_ => self.write_char(ch),
4547
}
4648
}
4749

4850
self.write_char(b'"');
4951
}
5052

51-
fn write_digits_from_u64(&mut self, mut num: u64) {
52-
let digit = (num % 10) as u8;
53-
if num > 9 {
54-
num /= 10;
55-
self.write_digits_from_u64(num);
53+
fn write_string(&mut self, string: &str) {
54+
self.write_char(b'"');
55+
56+
for (index, ch) in string.bytes().enumerate() {
57+
match ch {
58+
b'\\' |
59+
b'"' |
60+
b'\n' |
61+
b'\r' |
62+
b'\t' |
63+
0xC |
64+
0x8 => {
65+
return self.write_string_complex(string, index)
66+
},
67+
_ => {}
68+
}
5669
}
57-
self.write_char(digit + b'0');
70+
71+
self.write(string.as_bytes());
72+
self.write_char(b'"');
5873
}
5974

6075
fn write_number(&mut self, mut num: f64) {
@@ -87,12 +102,12 @@ pub trait Generator {
87102
return;
88103
}
89104

90-
if num > 1e19 || num < 1e-15 {
105+
if num > 1e19 {
91106
write!(self.get_buffer(), "{:e}", num).unwrap();
92107
return;
93108
}
94109

95-
self.write_digits_from_u64(num as u64);
110+
itoa::write(self.get_buffer(), num as u64).unwrap();
96111
}
97112

98113
fn write_json(&mut self, json: &JsonValue) {

src/error.rs

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ impl JsonError {
2020
pub fn wrong_type(expected: &str) -> Self {
2121
JsonError::WrongType(expected.into())
2222
}
23-
24-
pub fn undefined(field: &str) -> Self {
25-
JsonError::UndefinedField(field.into())
26-
}
2723
}
2824

2925
impl fmt::Display for JsonError {

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
//!
5555
//! ## First class citizen
5656
//!
57-
//! Using macros and easy indexing, it's easy to work with the data.
57+
//! Using macros and indexing, it's easy to work with the data.
5858
//!
5959
//! ```rust
6060
//! # #[macro_use] extern crate json;

0 commit comments

Comments
 (0)