@@ -37,6 +37,7 @@ pub use self::Count::*;
37
37
38
38
use std:: str;
39
39
use std:: string;
40
+ use std:: iter;
40
41
41
42
/// A piece is a portion of the format string which represents the next part
42
43
/// to emit. These are emitted as a stream by the `Parser` class.
@@ -141,7 +142,7 @@ pub enum Count<'a> {
141
142
/// necessary there's probably lots of room for improvement performance-wise.
142
143
pub struct Parser < ' a > {
143
144
input : & ' a str ,
144
- cur : str:: CharIndices < ' a > ,
145
+ cur : iter :: Peekable < str:: CharIndices < ' a > > ,
145
146
/// Error messages accumulated during parsing
146
147
pub errors : Vec < string:: String > ,
147
148
}
@@ -150,8 +151,8 @@ impl<'a> Iterator for Parser<'a> {
150
151
type Item = Piece < ' a > ;
151
152
152
153
fn next ( & mut self ) -> Option < Piece < ' a > > {
153
- match self . cur . clone ( ) . next ( ) {
154
- Some ( ( pos, '{' ) ) => {
154
+ match self . cur . peek ( ) {
155
+ Some ( & ( pos, '{' ) ) => {
155
156
self . cur . next ( ) ;
156
157
if self . consume ( '{' ) {
157
158
Some ( String ( self . string ( pos + 1 ) ) )
@@ -161,7 +162,7 @@ impl<'a> Iterator for Parser<'a> {
161
162
ret
162
163
}
163
164
}
164
- Some ( ( pos, '}' ) ) => {
165
+ Some ( & ( pos, '}' ) ) => {
165
166
self . cur . next ( ) ;
166
167
if self . consume ( '}' ) {
167
168
Some ( String ( self . string ( pos + 1 ) ) )
@@ -170,7 +171,7 @@ impl<'a> Iterator for Parser<'a> {
170
171
None
171
172
}
172
173
}
173
- Some ( ( pos, _) ) => { Some ( String ( self . string ( pos) ) ) }
174
+ Some ( & ( pos, _) ) => { Some ( String ( self . string ( pos) ) ) }
174
175
None => None
175
176
}
176
177
}
@@ -181,7 +182,7 @@ impl<'a> Parser<'a> {
181
182
pub fn new ( s : & ' a str ) -> Parser < ' a > {
182
183
Parser {
183
184
input : s,
184
- cur : s. char_indices ( ) ,
185
+ cur : s. char_indices ( ) . peekable ( ) ,
185
186
errors : vec ! ( ) ,
186
187
}
187
188
}
@@ -197,8 +198,8 @@ impl<'a> Parser<'a> {
197
198
/// the current position, then the current iterator isn't moved and false is
198
199
/// returned, otherwise the character is consumed and true is returned.
199
200
fn consume ( & mut self , c : char ) -> bool {
200
- match self . cur . clone ( ) . next ( ) {
201
- Some ( ( _, maybe) ) if c == maybe => {
201
+ match self . cur . peek ( ) {
202
+ Some ( & ( _, maybe) ) if c == maybe => {
202
203
self . cur . next ( ) ;
203
204
true
204
205
}
@@ -210,11 +211,11 @@ impl<'a> Parser<'a> {
210
211
/// found, an error is emitted.
211
212
fn must_consume ( & mut self , c : char ) {
212
213
self . ws ( ) ;
213
- match self . cur . clone ( ) . next ( ) {
214
- Some ( ( _, maybe) ) if c == maybe => {
214
+ match self . cur . peek ( ) {
215
+ Some ( & ( _, maybe) ) if c == maybe => {
215
216
self . cur . next ( ) ;
216
217
}
217
- Some ( ( _, other) ) => {
218
+ Some ( & ( _, other) ) => {
218
219
self . err ( & format ! ( "expected `{:?}`, found `{:?}`" , c,
219
220
other) ) ;
220
221
}
@@ -229,8 +230,8 @@ impl<'a> Parser<'a> {
229
230
/// character
230
231
fn ws ( & mut self ) {
231
232
loop {
232
- match self . cur . clone ( ) . next ( ) {
233
- Some ( ( _, c) ) if c. is_whitespace ( ) => { self . cur . next ( ) ; }
233
+ match self . cur . peek ( ) {
234
+ Some ( & ( _, c) ) if c. is_whitespace ( ) => { self . cur . next ( ) ; }
234
235
Some ( ..) | None => { return }
235
236
}
236
237
}
@@ -241,8 +242,8 @@ impl<'a> Parser<'a> {
241
242
fn string ( & mut self , start : usize ) -> & ' a str {
242
243
loop {
243
244
// we may not consume the character, so clone the iterator
244
- match self . cur . clone ( ) . next ( ) {
245
- Some ( ( pos, '}' ) ) | Some ( ( pos, '{' ) ) => {
245
+ match self . cur . peek ( ) {
246
+ Some ( & ( pos, '}' ) ) | Some ( & ( pos, '{' ) ) => {
246
247
return & self . input [ start..pos] ;
247
248
}
248
249
Some ( ..) => { self . cur . next ( ) ; }
@@ -269,8 +270,8 @@ impl<'a> Parser<'a> {
269
270
match self . integer ( ) {
270
271
Some ( i) => { ArgumentIs ( i) }
271
272
None => {
272
- match self . cur . clone ( ) . next ( ) {
273
- Some ( ( _, c) ) if c. is_alphabetic ( ) => {
273
+ match self . cur . peek ( ) {
274
+ Some ( & ( _, c) ) if c. is_alphabetic ( ) => {
274
275
ArgumentNamed ( self . word ( ) )
275
276
}
276
277
_ => ArgumentNext
@@ -293,8 +294,8 @@ impl<'a> Parser<'a> {
293
294
if !self . consume ( ':' ) { return spec }
294
295
295
296
// fill character
296
- match self . cur . clone ( ) . next ( ) {
297
- Some ( ( _, c) ) => {
297
+ match self . cur . peek ( ) {
298
+ Some ( & ( _, c) ) => {
298
299
match self . cur . clone ( ) . skip ( 1 ) . next ( ) {
299
300
Some ( ( _, '>' ) ) | Some ( ( _, '<' ) ) | Some ( ( _, '^' ) ) => {
300
301
spec. fill = Some ( c) ;
@@ -392,20 +393,20 @@ impl<'a> Parser<'a> {
392
393
/// be an alphabetic character followed by any number of alphanumeric
393
394
/// characters.
394
395
fn word ( & mut self ) -> & ' a str {
395
- let start = match self . cur . clone ( ) . next ( ) {
396
- Some ( ( pos, c) ) if c. is_xid_start ( ) => {
396
+ let start = match self . cur . peek ( ) {
397
+ Some ( & ( pos, c) ) if c. is_xid_start ( ) => {
397
398
self . cur . next ( ) ;
398
399
pos
399
400
}
400
401
Some ( ..) | None => { return & self . input [ ..0 ] ; }
401
402
} ;
402
403
let end;
403
404
loop {
404
- match self . cur . clone ( ) . next ( ) {
405
- Some ( ( _, c) ) if c. is_xid_continue ( ) => {
405
+ match self . cur . peek ( ) {
406
+ Some ( & ( _, c) ) if c. is_xid_continue ( ) => {
406
407
self . cur . next ( ) ;
407
408
}
408
- Some ( ( pos, _) ) => { end = pos; break }
409
+ Some ( & ( pos, _) ) => { end = pos; break }
409
410
None => { end = self . input . len ( ) ; break }
410
411
}
411
412
}
@@ -417,7 +418,7 @@ impl<'a> Parser<'a> {
417
418
fn integer ( & mut self ) -> Option < usize > {
418
419
let mut cur = 0 ;
419
420
let mut found = false ;
420
- while let Some ( ( _, c) ) = self . cur . clone ( ) . next ( ) {
421
+ while let Some ( & ( _, c) ) = self . cur . peek ( ) {
421
422
if let Some ( i) = c. to_digit ( 10 ) {
422
423
cur = cur * 10 + i as usize ;
423
424
found = true ;
0 commit comments