Skip to content

Commit 9c60f2d

Browse files
committed
Clean code
1 parent caca409 commit 9c60f2d

File tree

5 files changed

+25
-144
lines changed

5 files changed

+25
-144
lines changed

README.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ Value has a number of methods that meet your different needs.
151151
```rust
152152
value.get(&str) -> Option<Value>
153153
value.as_str() -> &str
154-
value.to_u64() -> u64
155-
value.to_i64() -> i64
156-
value.to_f64() -> f64
157-
value.to_bool() -> bool
158-
value.to_vec() -> Vec<Value>
159-
value.to_object() -> HashMap<String, Value>
154+
value.as_u64() -> u64
155+
value.as_i64() -> i64
156+
value.as_f64() -> f64
157+
value.as_bool() -> bool
158+
value.as_vec() -> Vec<Value>
159+
value.as_object() -> HashMap<String, Value>
160160
```
161161

162162

@@ -231,13 +231,9 @@ Found 5 outliers among 100 measurements (5.00%)
231231
1 (1.00%) high severe
232232
```
233233

234-
* MacBook Pro (13-inch, 2018, Four Thunderbolt 3 Ports)
235-
* 2.7 GHz Intel Core i7
236-
* 16 GB 2133 MHz LPDDR3
237-
238-
## problems
239-
240-
AJSON has just been finished, there may be some bugs and shortcomings, please feel free to issue. Also, Rust is a new language for me, and maybe ajson isn't rust enough, so I hope you have some suggestions.
234+
* MacBook Pro (14-inch, 2021)
235+
* Apple M1 Pro
236+
* 16 GB
241237

242238
## License
243239
MIT License.

benches/traversing.rs

-3
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ pub fn traverse_benchmark(c: &mut Criterion) {
218218
c.bench_function("traversing", |b| {
219219
b.iter(|| traversing(black_box(BENCH_DATA.as_bytes())))
220220
});
221-
c.bench_function("chunk traversing", |b| {
222-
b.iter(|| ajson::compound(black_box(BENCH_DATA)))
223-
});
224221

225222
c.bench_function("chunk traversing u8", |b| {
226223
b.iter(|| ajson::compound_u8(black_box(BENCH_DATA.as_bytes())))

src/element.rs

+14-124
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, collections::HashMap, str};
1+
use std::{borrow::Cow, collections::HashMap};
22

33
use crate::{unescape, value::Value, Number, Result};
44

@@ -152,11 +152,6 @@ pub fn null_u8(bytes: &[u8]) -> Result<(&[u8], &[u8])> {
152152
Ok(split_at_u8(bytes, 4))
153153
}
154154

155-
#[inline(always)]
156-
pub fn split_at(s: &str, mid: usize) -> (&str, &str) {
157-
unsafe { (s.get_unchecked(..mid), s.get_unchecked(mid..s.len())) }
158-
}
159-
160155
#[inline(always)]
161156
pub fn split_at_u8(s: &[u8], mid: usize) -> (&[u8], &[u8]) {
162157
unsafe { (s.get_unchecked(..mid), s.get_unchecked(mid..s.len())) }
@@ -201,60 +196,21 @@ pub fn string_u8(bytes: &[u8]) -> Result<(&[u8], &[u8], bool)> {
201196
Ok((a, b, esc))
202197
}
203198

204-
pub fn string(input: &str) -> Result<(&str, &str)> {
205-
let mut i = 1;
206-
let bytes = input.as_bytes();
207-
const CHUNK: usize = 4;
208-
209-
'outer: while i + CHUNK < bytes.len() {
210-
for _ in 0..CHUNK {
211-
let &b = unsafe { bytes.get_unchecked(i) };
212-
i += 1;
213-
match b {
214-
b'"' => {
215-
return Ok(split_at(input, i));
216-
}
217-
b'\\' => {
218-
i += 1;
219-
continue 'outer;
220-
}
221-
_ => {}
222-
}
223-
}
224-
}
225-
226-
while i < bytes.len() {
227-
let b = unsafe { *bytes.get_unchecked(i) };
228-
229-
match b {
230-
b'"' => {
231-
i += 1;
232-
break;
233-
}
234-
b'\\' => {
235-
i += 1;
236-
}
237-
_ => {}
238-
}
239-
240-
i += 1;
241-
}
242-
243-
return Ok(split_at(input, i));
244-
}
245-
246199
#[cfg(test)]
247-
mod test_string {
248-
use super::string;
200+
mod test_strin_u8 {
201+
use super::string_u8;
249202

250203
#[test]
251204
fn test_string() {
252205
assert_eq!(
253-
string(r#""hello": "tom""#),
254-
Ok((r#""hello""#, r#": "tom""#))
206+
string_u8(r#""hello": "tom""#.as_bytes()),
207+
Ok((r#""hello""#.as_bytes(), r#": "tom""#.as_bytes(), false))
255208
);
256209

257-
assert_eq!(string(r#""hello"#), Ok((r#""hello"#, r#""#)));
210+
assert_eq!(
211+
string_u8(r#""hello"#.as_bytes()),
212+
Ok((r#""hello"#.as_bytes(), r#""#.as_bytes(), false))
213+
);
258214
}
259215
}
260216

@@ -322,83 +278,17 @@ pub fn compound_u8(bytes: &[u8]) -> Result<(&[u8], &[u8])> {
322278
return Ok(split_at_u8(bytes, i));
323279
}
324280

325-
// object or array
326-
pub fn compound(input: &str) -> Result<(&str, &str)> {
327-
let bytes = input.as_bytes();
328-
let mut i = 1;
329-
let mut depth = 1;
330-
331-
const CHUNK_SIZE: usize = 32;
332-
333-
'outer: while i + CHUNK_SIZE < bytes.len() {
334-
for _ in 0..CHUNK_SIZE {
335-
let &b = unsafe { bytes.get_unchecked(i) };
336-
337-
match b {
338-
b'\\' => {
339-
i += 2;
340-
continue 'outer;
341-
}
342-
b'"' => {
343-
let input = unsafe { input.get_unchecked(i..) };
344-
let (s, _) = string(input).unwrap();
345-
346-
i += s.len();
347-
continue 'outer;
348-
}
349-
b'[' | b'{' => depth += 1,
350-
b']' | b'}' => {
351-
depth -= 1;
352-
if depth == 0 {
353-
i += 1;
354-
return Ok(split_at(input, i));
355-
}
356-
}
357-
_ => (),
358-
}
359-
i += 1;
360-
}
361-
}
362-
363-
while i < bytes.len() {
364-
let &b = unsafe { bytes.get_unchecked(i) };
365-
match b {
366-
b'\\' => {
367-
i += 1;
368-
}
369-
b'"' => {
370-
let input = unsafe { input.get_unchecked(i..) };
371-
let (s, _) = string(input).unwrap();
372-
i += s.len();
373-
continue;
374-
}
375-
b'[' | b'{' => depth += 1,
376-
b']' | b'}' => {
377-
depth -= 1;
378-
if depth == 0 {
379-
i += 1;
380-
break;
381-
}
382-
}
383-
_ => (),
384-
}
385-
i += 1;
386-
}
387-
388-
return Ok(split_at(input, i));
389-
}
390-
391281
#[cfg(test)]
392-
mod test_compound {
393-
use super::{compound, Result};
282+
mod test_compound_u8 {
283+
use super::{compound_u8, Result};
394284

395285
#[test]
396286
fn test_compound() -> Result<()> {
397287
const JSON: &str = r#"{"1":"2", "name": "jack"}xxxx"#;
398-
let r = compound(JSON)?;
288+
let r = compound_u8(JSON.as_bytes())?;
399289

400-
assert_eq!(r.0, r#"{"1":"2", "name": "jack"}"#);
401-
assert_eq!(r.1, "xxxx");
290+
assert_eq!(r.0, r#"{"1":"2", "name": "jack"}"#.as_bytes());
291+
assert_eq!(r.1, "xxxx".as_bytes());
402292

403293
Ok(())
404294
}

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ mod value;
104104

105105
use std::result;
106106

107-
#[doc(hidden)]
108-
pub use element::compound;
109107
#[doc(hidden)]
110108
pub use element::compound_u8;
111109
pub use number::Number;

src/path/sub_selector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ pub fn parse_selectors(v: &[u8]) -> (Vec<SubSelector>, usize, bool) {
8181
i += 1;
8282
}
8383
b'"' => {
84-
let input = unsafe { std::str::from_utf8_unchecked(v.get_unchecked(i..)) };
85-
let (a, _) = element::string(input).unwrap();
84+
let input = unsafe { v.get_unchecked(i..) };
85+
let (a, _, _) = element::string_u8(input).unwrap();
8686
i += a.len();
8787

8888
continue;

0 commit comments

Comments
 (0)