Skip to content

Commit

Permalink
remove public array_peak
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Oct 31, 2023
1 parent 2961795 commit 0fe7466
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 75 deletions.
40 changes: 20 additions & 20 deletions benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ fn jiter_iter_big(path: &str, bench: &mut Bencher) {

loop {
let mut v_inner = Vec::new();
if jiter.array_first().unwrap().is_some() {
let i = jiter.next_float().unwrap();
if let Some(peak) = jiter.array_first().unwrap() {
let i = jiter.known_float(peak).unwrap();
v_inner.push(i);
while jiter.array_step().unwrap() {
let i = jiter.next_float().unwrap();
while let Some(peak) = jiter.array_step().unwrap() {
let i = jiter.known_float(peak).unwrap();
v_inner.push(i);
}
}
v_outer.push(v_inner);
if !jiter.array_step().unwrap() {
if jiter.array_step().unwrap().is_none() {
break;
}
}
Expand All @@ -58,7 +58,7 @@ fn find_string(jiter: &mut Jiter) -> String {
Peak::Array => {
assert!(jiter.array_first().unwrap().is_some());
let s = find_string(jiter).to_string();
assert!(!jiter.array_step().unwrap());
assert!(jiter.array_step().unwrap().is_none());
s
}
_ => panic!("Expected string or array"),
Expand Down Expand Up @@ -86,8 +86,8 @@ fn jiter_iter_string_array(path: &str, bench: &mut Bencher) {
let i = jiter.next_str().unwrap();
// record len instead of allocating the string to simulate something like constructing a PyString
v.push(i.len());
while jiter.array_step().unwrap() {
let i = jiter.next_str().unwrap();
while jiter.array_step().unwrap().is_some() {
let i = jiter.known_str().unwrap();
v.push(i.len());
}
jiter.finish().unwrap();
Expand All @@ -101,11 +101,11 @@ fn jiter_iter_true_array(path: &str, bench: &mut Bencher) {
bench.iter(|| {
let mut jiter = Jiter::new(json_data);
let mut v = Vec::new();
jiter.array_first().unwrap();
let i = jiter.next_bool().unwrap();
let first_peak = jiter.array_first().unwrap().unwrap();
let i = jiter.known_bool(first_peak).unwrap();
v.push(i);
while jiter.array_step().unwrap() {
let i = jiter.next_bool().unwrap();
while let Some(peak) = jiter.array_step().unwrap() {
let i = jiter.known_bool(peak).unwrap();
v.push(i);
}
black_box(v)
Expand Down Expand Up @@ -138,11 +138,11 @@ fn jiter_iter_ints_array(path: &str, bench: &mut Bencher) {
bench.iter(|| {
let mut jiter = Jiter::new(json_data);
let mut v = Vec::new();
jiter.array_first().unwrap();
let i = jiter.next_int().unwrap();
let first_peak = jiter.array_first().unwrap().unwrap();
let i = jiter.known_int(first_peak).unwrap();
v.push(i);
while jiter.array_step().unwrap() {
let i = jiter.next_int().unwrap();
while let Some(peak) = jiter.array_step().unwrap() {
let i = jiter.known_int(peak).unwrap();
v.push(i);
}
black_box(v)
Expand All @@ -155,11 +155,11 @@ fn jiter_iter_floats_array(path: &str, bench: &mut Bencher) {
bench.iter(|| {
let mut jiter = Jiter::new(json_data);
let mut v = Vec::new();
jiter.array_first().unwrap();
let i = jiter.next_float().unwrap();
let first_peak = jiter.array_first().unwrap().unwrap();
let i = jiter.known_float(first_peak).unwrap();
v.push(i);
while jiter.array_step().unwrap() {
let i = jiter.next_float().unwrap();
while let Some(peak) = jiter.array_step().unwrap() {
let i = jiter.known_float(peak).unwrap();
v.push(i);
}
black_box(v)
Expand Down
20 changes: 14 additions & 6 deletions src/jiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,22 @@ impl<'a> Jiter<'a> {

pub fn next_int(&mut self) -> JiterResult<NumberInt> {
let peak = self.peak()?;
self.known_int(peak)
}

pub fn known_int(&mut self, peak: Peak) -> JiterResult<NumberInt> {
match peak {
Peak::Num(first) => self.known_int(first),
Peak::Num(first) => self.parser.consume_number::<NumberInt>(first).map_err(Into::into),
_ => Err(self.wrong_type(JsonType::Int, peak)),
}
}

pub fn known_int(&mut self, first: u8) -> JiterResult<NumberInt> {
self.parser.consume_number::<NumberInt>(first).map_err(Into::into)
}

pub fn next_float(&mut self) -> JiterResult<f64> {
let peak = self.peak()?;
self.known_float(peak)
}

pub fn known_float(&mut self, peak: Peak) -> JiterResult<f64> {
match peak {
Peak::Num(first) => self.parser.consume_number::<NumberFloat>(first).map_err(Into::into),
_ => Err(self.wrong_type(JsonType::Int, peak)),
Expand Down Expand Up @@ -129,6 +133,10 @@ impl<'a> Jiter<'a> {

pub fn next_value(&mut self) -> JiterResult<JsonValue> {
let peak = self.peak()?;
self.known_value(peak)
}

Check warning on line 137 in src/jiter.rs

View check run for this annotation

Codecov / codecov/patch

src/jiter.rs#L136-L137

Added lines #L136 - L137 were not covered by tests

pub fn known_value(&mut self, peak: Peak) -> JiterResult<JsonValue> {

Check warning on line 139 in src/jiter.rs

View check run for this annotation

Codecov / codecov/patch

src/jiter.rs#L139

Added line #L139 was not covered by tests
take_value(peak, &mut self.parser, &mut self.tape, DEFAULT_RECURSION_LIMIT).map_err(Into::into)
}

Expand All @@ -144,7 +152,7 @@ impl<'a> Jiter<'a> {
self.parser.array_first().map_err(Into::into)
}

pub fn array_step(&mut self) -> JiterResult<bool> {
pub fn array_step(&mut self) -> JiterResult<Option<Peak>> {
self.parser.array_step().map_err(Into::into)
}

Expand Down
44 changes: 22 additions & 22 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,48 +78,30 @@ impl<'a> Parser<'a> {
}
}

pub fn peak_array_step(&mut self) -> JsonResult<Peak> {
if let Some(next) = self.eat_whitespace() {
match Peak::new(next) {
Some(p) => Ok(p),
None => {
// if next is a `]`, we have a "trailing comma" error
if next == b']' {
json_err!(TrailingComma, self.index + 1)
} else {
json_err!(ExpectedSomeValue, self.index + 2)
}
}
}
} else {
json_err!(EofWhileParsingValue, self.index)
}
}

pub fn array_first(&mut self) -> JsonResult<Option<Peak>> {
self.index += 1;
if let Some(next) = self.eat_whitespace() {
if next == b']' {
self.index += 1;
Ok(None)
} else {
self.peak_array_step().map(Some)
self.array_peak()
}
} else {
json_err!(EofWhileParsingList, self.index + 1)
}
}

pub fn array_step(&mut self) -> JsonResult<bool> {
pub fn array_step(&mut self) -> JsonResult<Option<Peak>> {
if let Some(next) = self.eat_whitespace() {
match next {
b',' => {
self.index += 1;
Ok(true)
self.array_peak()
}
b']' => {
self.index += 1;
Ok(false)
Ok(None)
}
_ => json_err!(ExpectedListCommaOrEnd, self.index + 1),

Check warning on line 106 in src/parse.rs

View check run for this annotation

Codecov / codecov/patch

src/parse.rs#L106

Added line #L106 was not covered by tests
}
Expand Down Expand Up @@ -258,6 +240,24 @@ impl<'a> Parser<'a> {
}
}

fn array_peak(&mut self) -> JsonResult<Option<Peak>> {
if let Some(next) = self.eat_whitespace() {
match Peak::new(next) {
Some(p) => Ok(Some(p)),
None => {
// if next is a `]`, we have a "trailing comma" error
if next == b']' {
json_err!(TrailingComma, self.index + 1)
} else {
json_err!(ExpectedSomeValue, self.index + 2)
}
}
}
} else {
json_err!(EofWhileParsingValue, self.index)
}
}

fn eat_whitespace(&mut self) -> Option<u8> {
while let Some(next) = self.data.get(self.index) {
match next {
Expand Down
3 changes: 1 addition & 2 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ impl<'j> PythonParser<'j> {
let mut vec: SmallVec<[PyObject; 8]> = SmallVec::with_capacity(8);
let v = self._check_take_value(py, peak_first)?;
vec.push(v);
while self.parser.array_step().map_err(mje)? {
let peak = self.parser.peak_array_step().map_err(mje)?;
while let Some(peak) = self.parser.array_step().map_err(mje)? {
let v = self._check_take_value(py, peak)?;
vec.push(v);
}
Expand Down
3 changes: 1 addition & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ pub(crate) fn take_value(
let v = take_value(peak_first, parser, tape, recursion_limit)?;
);
array.push(v);
while parser.array_step()? {
let peak = parser.peak_array_step()?;
while let Some(peak) = parser.array_step()? {
check_recursion!(recursion_limit, parser.index,
let v = take_value(peak, parser, tape, recursion_limit)?;
);
Expand Down
45 changes: 22 additions & 23 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use jiter::{
NumberInt, Parser, Peak, StringDecoder, StringDecoderRange,
};

fn json_vec(parser: &mut Parser, in_array: bool) -> JsonResult<Vec<String>> {
fn json_vec(parser: &mut Parser, peak: Option<Peak>) -> JsonResult<Vec<String>> {
let mut v = Vec::new();
let mut tape: Vec<u8> = Vec::new();
let peak = match in_array {
true => parser.peak_array_step()?,
false => parser.peak()?,
let peak = match peak {
Some(peak) => peak,
None => parser.peak()?,
};

let position = parser.current_position().short();
Expand All @@ -43,13 +43,12 @@ fn json_vec(parser: &mut Parser, in_array: bool) -> JsonResult<Vec<String>> {
}
Peak::Array => {
v.push(format!("[ @ {position}"));
if parser.array_first()?.is_some() {
loop {
let el_vec = json_vec(parser, true)?;
if let Some(peak) = parser.array_first()? {
let el_vec = json_vec(parser, Some(peak))?;
v.extend(el_vec);
while let Some(peak) = parser.array_step()? {
let el_vec = json_vec(parser, Some(peak))?;
v.extend(el_vec);
if !parser.array_step()? {
break;
}
}
}
v.push("]".to_string());
Expand All @@ -58,11 +57,11 @@ fn json_vec(parser: &mut Parser, in_array: bool) -> JsonResult<Vec<String>> {
v.push(format!("{{ @ {position}"));
if let Some(key) = parser.object_first::<StringDecoderRange>(&mut tape)? {
v.push(format!("Key({key:?})"));
let value_vec = json_vec(parser, false)?;
let value_vec = json_vec(parser, None)?;
v.extend(value_vec);
while let Some(key) = parser.object_step::<StringDecoderRange>(&mut tape)? {
v.push(format!("Key({key:?}"));
let value_vec = json_vec(parser, false)?;
let value_vec = json_vec(parser, None)?;
v.extend(value_vec);
}
}
Expand Down Expand Up @@ -96,7 +95,7 @@ macro_rules! single_expect_ok_or_error {
#[test]
fn [< single_element_ok__ $name >]() {
let mut parser = Parser::new($json.as_bytes());
let elements = json_vec(&mut parser, false).unwrap().join(", ");
let elements = json_vec(&mut parser, None).unwrap().join(", ");
assert_eq!(elements, $expected);
parser.finish().unwrap();
}
Expand All @@ -109,7 +108,7 @@ macro_rules! single_expect_ok_or_error {
#[test]
fn [< single_element_xerror__ $name >]() {
let mut parser = Parser::new($json.as_bytes());
let result = json_vec(&mut parser, false);
let result = json_vec(&mut parser, None);
let first_value = match result {
Ok(v) => v,
Err(e) => {
Expand Down Expand Up @@ -511,11 +510,11 @@ fn jiter_object() {
assert_eq!(jiter.next_key().unwrap(), Some("spam"));
assert_eq!(jiter.next_array().unwrap(), Some(Peak::Num(b'1')));
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(1));
assert!(jiter.array_step().unwrap());
assert_eq!(jiter.array_step().unwrap(), Some(Peak::Num(b'2')));
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(2));
assert!(jiter.array_step().unwrap());
assert_eq!(jiter.array_step().unwrap(), Some(Peak::String));
assert_eq!(jiter.next_bytes().unwrap(), b"x");
assert!(!jiter.array_step().unwrap());
assert!(jiter.array_step().unwrap().is_none());
assert_eq!(jiter.next_key().unwrap(), None);
jiter.finish().unwrap();
}
Expand All @@ -535,15 +534,15 @@ fn jiter_number() {
let mut jiter = Jiter::new(br#" [1, 2.2, 3, 4.1, 5.67]"#);
assert_eq!(jiter.next_array().unwrap(), Some(Peak::Num(b'1')));
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(1));
assert_eq!(jiter.array_step().unwrap(), true);
assert_eq!(jiter.array_step().unwrap(), Some(Peak::Num(b'2')));
assert_eq!(jiter.next_float().unwrap(), 2.2);
assert_eq!(jiter.array_step().unwrap(), true);
assert_eq!(jiter.array_step().unwrap(), Some(Peak::Num(b'3')));
assert_eq!(jiter.next_number().unwrap(), NumberAny::Int(NumberInt::Int(3)));
assert_eq!(jiter.array_step().unwrap(), true);
assert_eq!(jiter.array_step().unwrap(), Some(Peak::Num(b'4')));
assert_eq!(jiter.next_number().unwrap(), NumberAny::Float(4.1));
assert_eq!(jiter.array_step().unwrap(), true);
assert_eq!(jiter.array_step().unwrap(), Some(Peak::Num(b'5')));
assert_eq!(jiter.next_number_bytes().unwrap(), b"5.67");
assert_eq!(jiter.array_step().unwrap(), false);
assert_eq!(jiter.array_step().unwrap(), None);
jiter.finish().unwrap();
}

Expand Down Expand Up @@ -575,7 +574,7 @@ fn jiter_trailing_bracket() {
let mut jiter = Jiter::new(b"[1]]");
assert_eq!(jiter.next_array().unwrap(), Some(Peak::Num(b'1')));
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(1));
assert!(!jiter.array_step().unwrap());
assert!(jiter.array_step().unwrap().is_none());
let result = jiter.finish();
match result {
Ok(t) => panic!("unexpectedly valid: {:?}", t),
Expand Down

0 comments on commit 0fe7466

Please sign in to comment.