Skip to content

Commit

Permalink
chore: yoink from #2437
Browse files Browse the repository at this point in the history
  • Loading branch information
indietyp committed Apr 19, 2023
1 parent fd8737a commit f2602b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/deer/json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod deserializer;
mod error;
mod number;
mod object;
mod skip;
mod token;

extern crate alloc;
45 changes: 45 additions & 0 deletions libs/deer/json/src/skip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::{deserializer::Deserializer, Token};

fn scan_object(deserializer: &Deserializer, stop: &Token) -> usize {
let mut objects: usize = 0;
let mut arrays: usize = 0;

let mut n = 0;

loop {
let Some(token) = deserializer.peek_n(n) else {
// we're at the end
return n;
};

if token == *stop && arrays == 0 && objects == 0 {
// we're at the outer layer, meaning we can know where we end
// need to increment by one as we want to also skip the ObjectEnd
return n + 1;
}

match token {
Token::Array { .. } => arrays += 1,
Token::ArrayEnd => arrays = arrays.saturating_sub(1),
Token::Object { .. } => objects += 1,
Token::ObjectEnd => objects = objects.saturating_sub(1),
_ => {}
}

n += 1;
}
}

/// Skips all tokens required for the start token, be aware that the start token should no longer be
/// on the tape
pub(crate) fn skip_tokens(deserializer: &mut Deserializer, start: &Token) {
let n = match start {
Token::Array { .. } => scan_object(&*deserializer, &Token::ArrayEnd),
Token::Object { .. } => scan_object(&*deserializer, &Token::ObjectEnd),
_ => 0,
};

if n > 0 {
deserializer.tape_mut().bump_n(n);
}
}

0 comments on commit f2602b3

Please sign in to comment.