Skip to content

Commit 77307ce

Browse files
committed
0.7.1 starting to optimize codegen
1 parent c0e49f9 commit 77307ce

File tree

2 files changed

+77
-37
lines changed

2 files changed

+77
-37
lines changed

src/codegen.rs

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
use JsonValue;
22

3-
pub struct Generator {
4-
pub minify: bool,
5-
code: String,
6-
dent: u16,
7-
spaces_per_indent: u16,
8-
}
3+
pub trait Generator {
4+
fn new_line(&mut self) {}
95

10-
impl Generator {
11-
pub fn new(minify: bool, spaces: u16) -> Self {
12-
Generator {
13-
minify: minify,
14-
code: String::with_capacity(1024),
15-
dent: 0,
16-
spaces_per_indent: spaces
17-
}
18-
}
6+
fn write(&mut self, slice: &str);
197

20-
pub fn new_line(&mut self) {
21-
if !self.minify {
22-
self.code.push('\n');
23-
for _ in 0..(self.dent * self.spaces_per_indent) {
24-
self.code.push(' ');
25-
}
26-
}
27-
}
8+
fn write_min(&mut self, slice: &str, minslice: &str);
9+
10+
fn write_char(&mut self, ch: char);
2811

29-
pub fn write_json(&mut self, json: &JsonValue) {
12+
fn indent(&mut self) {}
13+
14+
fn dedent(&mut self) {}
15+
16+
fn write_json(&mut self, json: &JsonValue) {
3017
match *json {
3118
JsonValue::String(ref string) => {
3219
self.write_char('"');
@@ -92,31 +79,84 @@ impl Generator {
9279
}
9380
}
9481

95-
pub fn write(&mut self, slice: &str) {
82+
fn consume(self) -> String;
83+
}
84+
85+
pub struct DumpGenerator {
86+
code: String,
87+
}
88+
89+
impl DumpGenerator {
90+
pub fn new() -> Self {
91+
DumpGenerator {
92+
code: String::with_capacity(1024),
93+
}
94+
}
95+
}
96+
97+
impl Generator for DumpGenerator {
98+
fn write(&mut self, slice: &str) {
9699
self.code.push_str(slice);
97100
}
98101

99-
pub fn write_min(&mut self, slice: &str, minslice: &str) {
100-
if self.minify {
101-
self.write(minslice);
102-
} else {
103-
self.write(slice);
102+
fn write_min(&mut self, _: &str, minslice: &str) {
103+
self.write(minslice);
104+
}
105+
106+
fn write_char(&mut self, ch: char) {
107+
self.code.push(ch);
108+
}
109+
110+
fn consume(self) -> String {
111+
self.code
112+
}
113+
}
114+
115+
pub struct PrettyGenerator {
116+
code: String,
117+
dent: u16,
118+
spaces_per_indent: u16,
119+
}
120+
121+
impl PrettyGenerator {
122+
pub fn new(spaces: u16) -> Self {
123+
PrettyGenerator {
124+
code: String::with_capacity(1024),
125+
dent: 0,
126+
spaces_per_indent: spaces
127+
}
128+
}
129+
}
130+
131+
impl Generator for PrettyGenerator {
132+
fn new_line(&mut self) {
133+
self.code.push('\n');
134+
for _ in 0..(self.dent * self.spaces_per_indent) {
135+
self.code.push(' ');
104136
}
105137
}
106138

107-
pub fn write_char(&mut self, ch: char) {
139+
fn write(&mut self, slice: &str) {
140+
self.code.push_str(slice);
141+
}
142+
143+
fn write_min(&mut self, slice: &str, _: &str) {
144+
self.write(slice);
145+
}
146+
147+
fn write_char(&mut self, ch: char) {
108148
self.code.push(ch);
109149
}
110150

111-
pub fn indent(&mut self) {
151+
fn indent(&mut self) {
112152
self.dent += 1;
113153
}
114154

115-
pub fn dedent(&mut self) {
155+
fn dedent(&mut self) {
116156
self.dent -= 1;
117157
}
118158

119-
pub fn consume(self) -> String {
159+
fn consume(self) -> String {
120160
self.code
121161
}
122162
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub use value::JsonValue::Null;
194194
pub type JsonResult<T> = Result<T, JsonError>;
195195

196196
pub use parser::parse;
197-
use codegen::Generator;
197+
use codegen::{ Generator, PrettyGenerator, DumpGenerator };
198198

199199
use std::collections::HashMap;
200200
use std::collections::BTreeMap;
@@ -206,15 +206,15 @@ pub type Object = BTreeMap<String, JsonValue>;
206206
impl JsonValue {
207207
/// Prints out the value as JSON string.
208208
pub fn dump(&self) -> String {
209-
let mut gen = Generator::new(true, 0);
209+
let mut gen = DumpGenerator::new();
210210
gen.write_json(self);
211211
gen.consume()
212212
}
213213

214214
/// Pretty prints out the value as JSON string. Takes an argument that's
215215
/// number of spaces to indent new blocks with.
216216
pub fn pretty(&self, spaces: u16) -> String {
217-
let mut gen = Generator::new(false, spaces);
217+
let mut gen = PrettyGenerator::new(spaces);
218218
gen.write_json(self);
219219
gen.consume()
220220
}

0 commit comments

Comments
 (0)