Skip to content

Commit 1a513d1

Browse files
committed
Iterator indexing
1 parent def2ad2 commit 1a513d1

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/parser.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::char;
22
use std::str;
3+
use std::iter::Enumerate;
34
use std::str::Bytes;
45
use std::collections::BTreeMap;
56
use { JsonValue, JsonError, JsonResult };
@@ -21,7 +22,7 @@ pub enum Token {
2122
macro_rules! sequence {
2223
($tok:ident, $( $ch:pat ),*) => {
2324
$(
24-
match $tok.source.next() {
25+
match $tok.next_byte() {
2526
Some($ch) => {},
2627
Some(ch) => return Err(JsonError::unexpected_character(ch)),
2728
None => return Err(JsonError::UnexpectedEndOfJson)
@@ -48,29 +49,34 @@ macro_rules! read_num {
4849
}
4950

5051
struct Tokenizer<'a> {
51-
source: Bytes<'a>,
52+
source: Enumerate<Bytes<'a>>,
5253
buffer: Vec<u8>,
5354
left_over: Option<u8>,
55+
current_index: usize,
5456
}
5557

5658
impl<'a> Tokenizer<'a> {
5759
pub fn new(source: &'a str) -> Self {
5860
Tokenizer {
59-
source: source.bytes(),
61+
source: source.bytes().enumerate(),
6062
buffer: Vec::with_capacity(512),
6163
left_over: None,
64+
current_index: 0,
6265
}
6366
}
6467

6568
#[inline(always)]
6669
fn next_byte(&mut self) -> Option<u8> {
67-
self.source.next()
70+
self.source.next().map(|(index, byte)| {
71+
self.current_index = index;
72+
byte
73+
})
6874
}
6975

7076
#[inline(always)]
7177
fn peek_byte(&mut self) -> Option<u8> {
7278
if self.left_over.is_none() {
73-
self.left_over = self.source.next();
79+
self.left_over = self.next_byte();
7480
}
7581

7682
return self.left_over;
@@ -84,7 +90,7 @@ impl<'a> Tokenizer<'a> {
8490
return byte;
8591
}
8692

87-
self.source.next()
93+
self.next_byte()
8894
}
8995

9096
#[inline(always)]

0 commit comments

Comments
 (0)