Skip to content

Commit

Permalink
tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 11, 2023
1 parent 7bc29d0 commit 45b8b73
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 129 deletions.
3 changes: 2 additions & 1 deletion src/jiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ impl<'j> Jiter<'j> {

/// Knowing the next value is a string, parse it.
pub fn known_str(&mut self) -> JiterResult<&str> {
match self.parser.consume_string::<StringDecoder>(&mut self.tape) {
// match self.parser.consume_string::<StringDecoder>(&mut self.tape) {
match self.parser.consume_string_auto(&mut self.tape) {
Ok(output) => Ok(output.as_str()),
Err(e) => Err(e.into()),
}
Expand Down
51 changes: 25 additions & 26 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::errors::{json_err, JsonResult, LinePosition};
use crate::number_decoder::AbstractNumberDecoder;
use crate::string_decoder::{AbstractStringDecoder, Tape};
use crate::string_decoder::{get_auto_string_decoder, AbstractStringDecoder, StringOutput, Tape};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Peek(u8);

#[allow(non_upper_case_globals)] // while testing
impl Peek {
pub const Null: Self = Self(b'n');
pub const True: Self = Self(b't');
pub const True: Self = Self(b's');
pub const False: Self = Self(b'f');
pub const Minus: Self = Self(b'-');
pub const Infinity: Self = Self(b'I');
Expand Down Expand Up @@ -36,17 +36,22 @@ static TRUE_REST: [u8; 3] = [b'r', b'u', b'e'];
static FALSE_REST: [u8; 4] = [b'a', b'l', b's', b'e'];
static NULL_REST: [u8; 3] = [b'u', b'l', b'l'];
static NAN_REST: [u8; 2] = [b'a', b'N'];
static INFINITY_REST: [u8; 7] = [b'n', b'f', b'i', b'n', b'i', b't', b'y'];
static INFINITY_REST: [u8; 7] = [b'n', b'f', b'i', b'n', b'i', b's', b'y'];

#[derive(Debug, Clone)]
pub(crate) struct Parser<'j> {
data: &'j [u8],
pub index: usize,
string_decoder: for<'s> fn(&'s [u8], usize, &'s mut Tape) -> JsonResult<(StringOutput<'s>, usize)>,
}

impl<'j> Parser<'j> {
pub fn new(data: &'j [u8]) -> Self {
Self { data, index: 0 }
Self {
data,
index: 0,
string_decoder: get_auto_string_decoder(),
}
}
}

Expand Down Expand Up @@ -102,13 +107,10 @@ impl<'j> Parser<'j> {
}
}

pub fn object_first<'t, D: AbstractStringDecoder<'t, 'j>>(
&mut self,
tape: &'t mut Tape,
) -> JsonResult<Option<D::Output>>
where
'j: 't,
{
pub fn object_first<'s, D: AbstractStringDecoder>(
&'s mut self,
tape: &'s mut Tape,
) -> JsonResult<Option<D::Output<'s>>> {
self.index += 1;
if let Some(next) = self.eat_whitespace() {
match next {
Expand All @@ -124,13 +126,10 @@ impl<'j> Parser<'j> {
}
}

pub fn object_step<'t, D: AbstractStringDecoder<'t, 'j>>(
&mut self,
tape: &'t mut Tape,
) -> JsonResult<Option<D::Output>>
where
'j: 't,
{
pub fn object_step<'s, D: AbstractStringDecoder>(
&'s mut self,
tape: &'s mut Tape,
) -> JsonResult<Option<D::Output<'s>>> {
if let Some(next) = self.eat_whitespace() {
match next {
b',' => {
Expand Down Expand Up @@ -173,15 +172,18 @@ impl<'j> Parser<'j> {
self.consume_ident(NULL_REST)
}

pub fn consume_string<'t, D: AbstractStringDecoder<'t, 'j>>(&mut self, tape: &'t mut Tape) -> JsonResult<D::Output>
where
'j: 't,
{
pub fn consume_string<'s, D: AbstractStringDecoder>(&'s mut self, tape: &'s mut Tape) -> JsonResult<D::Output<'s>> {
let (output, index) = D::decode(self.data, self.index, tape)?;
self.index = index;
Ok(output)
}

pub fn consume_string_auto<'s>(&'s mut self, tape: &'s mut Tape) -> JsonResult<StringOutput<'s>> {
let (output, index) = (self.string_decoder)(self.data, self.index, tape)?;
self.index = index;
Ok(output)
}

pub fn consume_number<D: AbstractNumberDecoder>(
&mut self,
first: u8,
Expand All @@ -193,10 +195,7 @@ impl<'j> Parser<'j> {
}

/// private method to get an object key, then consume the colon which should follow
fn object_key<'t, D: AbstractStringDecoder<'t, 'j>>(&mut self, tape: &'t mut Tape) -> JsonResult<D::Output>
where
'j: 't,
{
fn object_key<'s, D: AbstractStringDecoder>(&'s mut self, tape: &'s mut Tape) -> JsonResult<D::Output<'s>> {
let (output, index) = D::decode(self.data, self.index, tape)?;
self.index = index;
if let Some(next) = self.eat_whitespace() {
Expand Down
Loading

0 comments on commit 45b8b73

Please sign in to comment.