@@ -154,15 +154,7 @@ pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::D
154
154
/// Split off the next token from the input
155
155
fn next_token ( & mut self ) -> Option < Self :: Token > ;
156
156
/// Split off the next token from the input
157
- #[ inline( always) ]
158
- fn peek_token ( & self ) -> Option < ( Self , Self :: Token ) >
159
- where
160
- Self : Clone ,
161
- {
162
- let mut peek = self . clone ( ) ;
163
- let token = peek. next_token ( ) ?;
164
- Some ( ( peek, token) )
165
- }
157
+ fn peek_token ( & self ) -> Option < Self :: Token > ;
166
158
167
159
/// Finds the offset of the next matching token
168
160
fn offset_for < P > ( & self , predicate : P ) -> Option < usize >
@@ -195,15 +187,7 @@ pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::D
195
187
///
196
188
fn next_slice ( & mut self , offset : usize ) -> Self :: Slice ;
197
189
/// Split off a slice of tokens from the input
198
- #[ inline( always) ]
199
- fn peek_slice ( & self , offset : usize ) -> ( Self , Self :: Slice )
200
- where
201
- Self : Clone ,
202
- {
203
- let mut peek = self . clone ( ) ;
204
- let slice = peek. next_slice ( offset) ;
205
- ( peek, slice)
206
- }
190
+ fn peek_slice ( & self , offset : usize ) -> Self :: Slice ;
207
191
208
192
/// Advance to the end of the stream
209
193
#[ inline( always) ]
@@ -212,13 +196,11 @@ pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::D
212
196
}
213
197
/// Advance to the end of the stream
214
198
#[ inline( always) ]
215
- fn peek_finish ( & self ) -> ( Self , Self :: Slice )
199
+ fn peek_finish ( & self ) -> Self :: Slice
216
200
where
217
201
Self : Clone ,
218
202
{
219
- let mut peek = self . clone ( ) ;
220
- let slice = peek. finish ( ) ;
221
- ( peek, slice)
203
+ self . peek_slice ( self . eof_offset ( ) )
222
204
}
223
205
224
206
/// Save the current parse location within the stream
@@ -261,6 +243,15 @@ where
261
243
Some ( token. clone ( ) )
262
244
}
263
245
246
+ #[ inline( always) ]
247
+ fn peek_token ( & self ) -> Option < Self :: Token > {
248
+ if self . is_empty ( ) {
249
+ None
250
+ } else {
251
+ Some ( self [ 0 ] . clone ( ) )
252
+ }
253
+ }
254
+
264
255
#[ inline( always) ]
265
256
fn offset_for < P > ( & self , predicate : P ) -> Option < usize >
266
257
where
@@ -282,6 +273,11 @@ where
282
273
* self = next;
283
274
slice
284
275
}
276
+ #[ inline( always) ]
277
+ fn peek_slice ( & self , offset : usize ) -> Self :: Slice {
278
+ let ( slice, _next) = self . split_at ( offset) ;
279
+ slice
280
+ }
285
281
286
282
#[ inline( always) ]
287
283
fn checkpoint ( & self ) -> Self :: Checkpoint {
@@ -323,6 +319,11 @@ impl<'i> Stream for &'i str {
323
319
Some ( c)
324
320
}
325
321
322
+ #[ inline( always) ]
323
+ fn peek_token ( & self ) -> Option < Self :: Token > {
324
+ self . chars ( ) . next ( )
325
+ }
326
+
326
327
#[ inline( always) ]
327
328
fn offset_for < P > ( & self , predicate : P ) -> Option < usize >
328
329
where
@@ -357,6 +358,11 @@ impl<'i> Stream for &'i str {
357
358
* self = next;
358
359
slice
359
360
}
361
+ #[ inline( always) ]
362
+ fn peek_slice ( & self , offset : usize ) -> Self :: Slice {
363
+ let ( slice, _next) = self . split_at ( offset) ;
364
+ slice
365
+ }
360
366
361
367
#[ inline( always) ]
362
368
fn checkpoint ( & self ) -> Self :: Checkpoint {
@@ -406,6 +412,11 @@ where
406
412
next_bit ( self )
407
413
}
408
414
415
+ #[ inline( always) ]
416
+ fn peek_token ( & self ) -> Option < Self :: Token > {
417
+ peek_bit ( self )
418
+ }
419
+
409
420
#[ inline( always) ]
410
421
fn offset_for < P > ( & self , predicate : P ) -> Option < usize >
411
422
where
@@ -434,6 +445,14 @@ where
434
445
self . 1 = end_offset;
435
446
( s, start_offset, end_offset)
436
447
}
448
+ #[ inline( always) ]
449
+ fn peek_slice ( & self , offset : usize ) -> Self :: Slice {
450
+ let byte_offset = ( offset + self . 1 ) / 8 ;
451
+ let end_offset = ( offset + self . 1 ) % 8 ;
452
+ let s = self . 0 . peek_slice ( byte_offset) ;
453
+ let start_offset = self . 1 ;
454
+ ( s, start_offset, end_offset)
455
+ }
437
456
438
457
#[ inline( always) ]
439
458
fn checkpoint ( & self ) -> Self :: Checkpoint {
@@ -496,6 +515,27 @@ where
496
515
}
497
516
}
498
517
518
+ fn peek_bit < I > ( i : & ( I , usize ) ) -> Option < bool >
519
+ where
520
+ I : Stream < Token = u8 > + Clone ,
521
+ {
522
+ if i. eof_offset ( ) == 0 {
523
+ return None ;
524
+ }
525
+ let offset = i. 1 ;
526
+
527
+ let mut next_i = i. 0 . clone ( ) ;
528
+ let byte = next_i. next_token ( ) ?;
529
+ let bit = ( byte >> offset) & 0x1 == 0x1 ;
530
+
531
+ let next_offset = offset + 1 ;
532
+ if next_offset == 8 {
533
+ Some ( bit)
534
+ } else {
535
+ Some ( bit)
536
+ }
537
+ }
538
+
499
539
/// Current parse locations offset
500
540
///
501
541
/// See [`LocatingSlice`] for adding location tracking to your [`Stream`]
0 commit comments