Skip to content

Commit 7c65b56

Browse files
authored
Merge pull request #50 from maciejhirsz/0.8.2
0.8.2 speed up strings even more with a LUT
2 parents 25c2143 + 2c6cbda commit 7c65b56

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/codegen.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ use JsonValue;
44

55
extern crate itoa;
66

7+
static ESCAPED: [u8; 256] = [
8+
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
9+
0, 0, 0, 0, 0, 0, 0, 0,b'b',b't',b'n', 0,b'f',b'r', 0, 0, // 0
10+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
11+
0, 0,b'"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2
12+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3
13+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4
14+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,b'\\', 0, 0, 0, // 5
15+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6
16+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7
17+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8
18+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9
19+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // A
20+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // B
21+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // C
22+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // D
23+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // E
24+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // F
25+
];
26+
727
pub trait Generator {
828
fn get_buffer(&mut self) -> &mut Vec<u8>;
929

@@ -29,21 +49,13 @@ pub trait Generator {
2949

3050
fn dedent(&mut self) {}
3151

32-
fn write_string_complex(&mut self, string: &str, from: usize) {
33-
self.write(string[0 .. from].as_bytes());
34-
35-
for ch in string.bytes().skip(from) {
36-
match ch {
37-
b'\\' | b'"' => {
38-
self.write_char(b'\\');
39-
self.write_char(ch);
40-
},
41-
b'\n' => self.write(b"\\n"),
42-
b'\r' => self.write(b"\\r"),
43-
b'\t' => self.write(b"\\t"),
44-
0xC => self.write(b"\\f"),
45-
0x8 => self.write(b"\\b"),
46-
_ => self.write_char(ch),
52+
fn write_string_complex(&mut self, string: &str, mut start: usize) {
53+
for (index, ch) in string.bytes().enumerate().skip(start) {
54+
let escape = ESCAPED[ch as usize];
55+
if escape > 0 {
56+
self.write(string[start .. index].as_bytes());
57+
self.write(&[b'\\', escape]);
58+
start = index + 1;
4759
}
4860
}
4961

@@ -54,17 +66,8 @@ pub trait Generator {
5466
self.write_char(b'"');
5567

5668
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-
_ => {}
69+
if ESCAPED[ch as usize] > 0 {
70+
return self.write_string_complex(string, index)
6871
}
6972
}
7073

tests/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn stringify_array_with_push() {
280280

281281
#[test]
282282
fn stringify_escaped_characters() {
283-
assert_eq!(stringify("\r\n\t\u{8}\u{c}\\\""), r#""\r\n\t\b\f\\\"""#);
283+
assert_eq!(stringify("\r____\n___\t\u{8}\u{c}\\\""), r#""\r____\n___\t\b\f\\\"""#);
284284
}
285285

286286
#[test]

0 commit comments

Comments
 (0)