-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ mod deserializer; | |
mod error; | ||
mod number; | ||
mod object; | ||
mod skip; | ||
mod token; | ||
|
||
extern crate alloc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |