@@ -151,28 +151,31 @@ impl<'a> Iterator for Parser<'a> {
151
151
type Item = Piece < ' a > ;
152
152
153
153
fn next ( & mut self ) -> Option < Piece < ' a > > {
154
- match self . cur . peek ( ) {
155
- Some ( & ( pos, '{' ) ) => {
156
- self . cur . next ( ) ;
157
- if self . consume ( '{' ) {
158
- Some ( String ( self . string ( pos + 1 ) ) )
159
- } else {
160
- let ret = Some ( NextArgument ( self . argument ( ) ) ) ;
161
- self . must_consume ( '}' ) ;
162
- ret
154
+ if let Some ( & ( pos, c) ) = self . cur . peek ( ) {
155
+ match c {
156
+ '{' => {
157
+ self . cur . next ( ) ;
158
+ if self . consume ( '{' ) {
159
+ Some ( String ( self . string ( pos + 1 ) ) )
160
+ } else {
161
+ let ret = Some ( NextArgument ( self . argument ( ) ) ) ;
162
+ self . must_consume ( '}' ) ;
163
+ ret
164
+ }
163
165
}
164
- }
165
- Some ( & ( pos , '}' ) ) => {
166
- self . cur . next ( ) ;
167
- if self . consume ( '}' ) {
168
- Some ( String ( self . string ( pos + 1 ) ) )
169
- } else {
170
- self . err ( "unmatched `}` found" ) ;
171
- None
166
+ '}' => {
167
+ self . cur . next ( ) ;
168
+ if self . consume ( '}' ) {
169
+ Some ( String ( self . string ( pos + 1 ) ) )
170
+ } else {
171
+ self . err ( "unmatched `}` found" ) ;
172
+ None
173
+ }
172
174
}
175
+ _ => Some ( String ( self . string ( pos) ) ) ,
173
176
}
174
- Some ( & ( pos , _ ) ) => { Some ( String ( self . string ( pos ) ) ) }
175
- None => None
177
+ } else {
178
+ None
176
179
}
177
180
}
178
181
}
@@ -198,61 +201,47 @@ impl<'a> Parser<'a> {
198
201
/// the current position, then the current iterator isn't moved and false is
199
202
/// returned, otherwise the character is consumed and true is returned.
200
203
fn consume ( & mut self , c : char ) -> bool {
201
- match self . cur . peek ( ) {
202
- Some ( & ( _, maybe) ) if c == maybe => {
203
- self . cur . next ( ) ;
204
- true
205
- }
206
- Some ( ..) | None => false ,
204
+ if let Some ( & ( _, maybe) ) = self . cur . peek ( ) {
205
+ if c == maybe { self . cur . next ( ) ; true } else { false }
206
+ } else {
207
+ false
207
208
}
208
209
}
209
210
210
211
/// Forces consumption of the specified character. If the character is not
211
212
/// found, an error is emitted.
212
213
fn must_consume ( & mut self , c : char ) {
213
214
self . ws ( ) ;
214
- match self . cur . peek ( ) {
215
- Some ( & ( _ , maybe ) ) if c == maybe => {
215
+ if let Some ( & ( _ , maybe ) ) = self . cur . peek ( ) {
216
+ if c == maybe {
216
217
self . cur . next ( ) ;
218
+ } else {
219
+ self . err ( & format ! ( "expected `{:?}`, found `{:?}`" , c, maybe) ) ;
217
220
}
218
- Some ( & ( _, other) ) => {
219
- self . err ( & format ! ( "expected `{:?}`, found `{:?}`" , c,
220
- other) ) ;
221
- }
222
- None => {
223
- self . err ( & format ! ( "expected `{:?}` but string was terminated" ,
224
- c) ) ;
225
- }
221
+ } else {
222
+ self . err ( & format ! ( "expected `{:?}` but string was terminated" , c) ) ;
226
223
}
227
224
}
228
225
229
226
/// Consumes all whitespace characters until the first non-whitespace
230
227
/// character
231
228
fn ws ( & mut self ) {
232
- loop {
233
- match self . cur . peek ( ) {
234
- Some ( & ( _, c) ) if c. is_whitespace ( ) => { self . cur . next ( ) ; }
235
- Some ( ..) | None => { return }
236
- }
229
+ while let Some ( & ( _, c) ) = self . cur . peek ( ) {
230
+ if c. is_whitespace ( ) { self . cur . next ( ) ; } else { break }
237
231
}
238
232
}
239
233
240
234
/// Parses all of a string which is to be considered a "raw literal" in a
241
235
/// format string. This is everything outside of the braces.
242
236
fn string ( & mut self , start : usize ) -> & ' a str {
243
- loop {
244
- // we may not consume the character, so clone the iterator
245
- match self . cur . peek ( ) {
246
- Some ( & ( pos, '}' ) ) | Some ( & ( pos, '{' ) ) => {
247
- return & self . input [ start..pos] ;
248
- }
249
- Some ( ..) => { self . cur . next ( ) ; }
250
- None => {
251
- self . cur . next ( ) ;
252
- return & self . input [ start..self . input . len ( ) ] ;
253
- }
237
+ // we may not consume the character, peek the iterator
238
+ while let Some ( & ( pos, c) ) = self . cur . peek ( ) {
239
+ match c {
240
+ '{' | '}' => { return & self . input [ start..pos] ; }
241
+ _ => { self . cur . next ( ) ; }
254
242
}
255
243
}
244
+ & self . input [ start..self . input . len ( ) ]
256
245
}
257
246
258
247
/// Parses an Argument structure, or what's contained within braces inside
@@ -267,15 +256,14 @@ impl<'a> Parser<'a> {
267
256
/// Parses a positional argument for a format. This could either be an
268
257
/// integer index of an argument, a named argument, or a blank string.
269
258
fn position ( & mut self ) -> Position < ' a > {
270
- match self . integer ( ) {
271
- Some ( i) => { ArgumentIs ( i) }
272
- None => {
273
- match self . cur . peek ( ) {
274
- Some ( & ( _, c) ) if c. is_alphabetic ( ) => {
275
- ArgumentNamed ( self . word ( ) )
276
- }
277
- _ => ArgumentNext
259
+ if let Some ( i) = self . integer ( ) {
260
+ ArgumentIs ( i)
261
+ } else {
262
+ match self . cur . peek ( ) {
263
+ Some ( & ( _, c) ) if c. is_alphabetic ( ) => {
264
+ ArgumentNamed ( self . word ( ) )
278
265
}
266
+ _ => ArgumentNext
279
267
}
280
268
}
281
269
}
@@ -294,17 +282,14 @@ impl<'a> Parser<'a> {
294
282
if !self . consume ( ':' ) { return spec }
295
283
296
284
// fill character
297
- match self . cur . peek ( ) {
298
- Some ( & ( _, c) ) => {
299
- match self . cur . clone ( ) . skip ( 1 ) . next ( ) {
300
- Some ( ( _, '>' ) ) | Some ( ( _, '<' ) ) | Some ( ( _, '^' ) ) => {
301
- spec. fill = Some ( c) ;
302
- self . cur . next ( ) ;
303
- }
304
- Some ( ..) | None => { }
285
+ if let Some ( & ( _, c) ) = self . cur . peek ( ) {
286
+ match self . cur . clone ( ) . skip ( 1 ) . next ( ) {
287
+ Some ( ( _, '>' ) ) | Some ( ( _, '<' ) ) | Some ( ( _, '^' ) ) => {
288
+ spec. fill = Some ( c) ;
289
+ self . cur . next ( ) ;
305
290
}
291
+ _ => { }
306
292
}
307
- None => { }
308
293
}
309
294
// Alignment
310
295
if self . consume ( '<' ) {
@@ -361,29 +346,20 @@ impl<'a> Parser<'a> {
361
346
/// for 'CountIsNextParam' because that is only used in precision, not
362
347
/// width.
363
348
fn count ( & mut self ) -> Count < ' a > {
364
- match self . integer ( ) {
365
- Some ( i) => {
349
+ if let Some ( i) = self . integer ( ) {
350
+ if self . consume ( '$' ) { CountIsParam ( i) } else { CountIs ( i) }
351
+ } else {
352
+ let tmp = self . cur . clone ( ) ;
353
+ let word = self . word ( ) ;
354
+ if word. is_empty ( ) {
355
+ self . cur = tmp;
356
+ CountImplied
357
+ } else {
366
358
if self . consume ( '$' ) {
367
- CountIsParam ( i )
359
+ CountIsName ( word )
368
360
} else {
369
- CountIs ( i)
370
- }
371
- }
372
- None => {
373
- let tmp = self . cur . clone ( ) ;
374
- match self . word ( ) {
375
- word if !word. is_empty ( ) => {
376
- if self . consume ( '$' ) {
377
- CountIsName ( word)
378
- } else {
379
- self . cur = tmp;
380
- CountImplied
381
- }
382
- }
383
- _ => {
384
- self . cur = tmp;
385
- CountImplied
386
- }
361
+ self . cur = tmp;
362
+ CountImplied
387
363
}
388
364
}
389
365
}
@@ -394,23 +370,17 @@ impl<'a> Parser<'a> {
394
370
/// characters.
395
371
fn word ( & mut self ) -> & ' a str {
396
372
let start = match self . cur . peek ( ) {
397
- Some ( & ( pos, c) ) if c. is_xid_start ( ) => {
398
- self . cur . next ( ) ;
399
- pos
400
- }
401
- Some ( ..) | None => { return & self . input [ ..0 ] ; }
373
+ Some ( & ( pos, c) ) if c. is_xid_start ( ) => { self . cur . next ( ) ; pos }
374
+ _ => { return & self . input [ ..0 ] ; }
402
375
} ;
403
- let end;
404
- loop {
405
- match self . cur . peek ( ) {
406
- Some ( & ( _, c) ) if c. is_xid_continue ( ) => {
407
- self . cur . next ( ) ;
408
- }
409
- Some ( & ( pos, _) ) => { end = pos; break }
410
- None => { end = self . input . len ( ) ; break }
376
+ while let Some ( & ( pos, c) ) = self . cur . peek ( ) {
377
+ if c. is_xid_continue ( ) {
378
+ self . cur . next ( ) ;
379
+ } else {
380
+ return & self . input [ start..pos] ;
411
381
}
412
382
}
413
- & self . input [ start..end ]
383
+ & self . input [ start..self . input . len ( ) ]
414
384
}
415
385
416
386
/// Optionally parses an integer at the current position. This doesn't deal
@@ -427,11 +397,7 @@ impl<'a> Parser<'a> {
427
397
break
428
398
}
429
399
}
430
- if found {
431
- Some ( cur)
432
- } else {
433
- None
434
- }
400
+ if found { Some ( cur) } else { None }
435
401
}
436
402
}
437
403
0 commit comments