Skip to content

Commit 69092ff

Browse files
committed
Changed libfmt_macros Parse iterator to Peekable
1 parent 1a1e6b8 commit 69092ff

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/libfmt_macros/lib.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub use self::Count::*;
3737

3838
use std::str;
3939
use std::string;
40+
use std::iter;
4041

4142
/// A piece is a portion of the format string which represents the next part
4243
/// to emit. These are emitted as a stream by the `Parser` class.
@@ -141,7 +142,7 @@ pub enum Count<'a> {
141142
/// necessary there's probably lots of room for improvement performance-wise.
142143
pub struct Parser<'a> {
143144
input: &'a str,
144-
cur: str::CharIndices<'a>,
145+
cur: iter::Peekable<str::CharIndices<'a>>,
145146
/// Error messages accumulated during parsing
146147
pub errors: Vec<string::String>,
147148
}
@@ -150,8 +151,8 @@ impl<'a> Iterator for Parser<'a> {
150151
type Item = Piece<'a>;
151152

152153
fn next(&mut self) -> Option<Piece<'a>> {
153-
match self.cur.clone().next() {
154-
Some((pos, '{')) => {
154+
match self.cur.peek() {
155+
Some(&(pos, '{')) => {
155156
self.cur.next();
156157
if self.consume('{') {
157158
Some(String(self.string(pos + 1)))
@@ -161,7 +162,7 @@ impl<'a> Iterator for Parser<'a> {
161162
ret
162163
}
163164
}
164-
Some((pos, '}')) => {
165+
Some(&(pos, '}')) => {
165166
self.cur.next();
166167
if self.consume('}') {
167168
Some(String(self.string(pos + 1)))
@@ -170,7 +171,7 @@ impl<'a> Iterator for Parser<'a> {
170171
None
171172
}
172173
}
173-
Some((pos, _)) => { Some(String(self.string(pos))) }
174+
Some(&(pos, _)) => { Some(String(self.string(pos))) }
174175
None => None
175176
}
176177
}
@@ -181,7 +182,7 @@ impl<'a> Parser<'a> {
181182
pub fn new(s: &'a str) -> Parser<'a> {
182183
Parser {
183184
input: s,
184-
cur: s.char_indices(),
185+
cur: s.char_indices().peekable(),
185186
errors: vec!(),
186187
}
187188
}
@@ -197,8 +198,8 @@ impl<'a> Parser<'a> {
197198
/// the current position, then the current iterator isn't moved and false is
198199
/// returned, otherwise the character is consumed and true is returned.
199200
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 => {
202203
self.cur.next();
203204
true
204205
}
@@ -210,11 +211,11 @@ impl<'a> Parser<'a> {
210211
/// found, an error is emitted.
211212
fn must_consume(&mut self, c: char) {
212213
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 => {
215216
self.cur.next();
216217
}
217-
Some((_, other)) => {
218+
Some(&(_, other)) => {
218219
self.err(&format!("expected `{:?}`, found `{:?}`", c,
219220
other));
220221
}
@@ -229,8 +230,8 @@ impl<'a> Parser<'a> {
229230
/// character
230231
fn ws(&mut self) {
231232
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(); }
234235
Some(..) | None => { return }
235236
}
236237
}
@@ -241,8 +242,8 @@ impl<'a> Parser<'a> {
241242
fn string(&mut self, start: usize) -> &'a str {
242243
loop {
243244
// 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, '{')) => {
246247
return &self.input[start..pos];
247248
}
248249
Some(..) => { self.cur.next(); }
@@ -269,8 +270,8 @@ impl<'a> Parser<'a> {
269270
match self.integer() {
270271
Some(i) => { ArgumentIs(i) }
271272
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() => {
274275
ArgumentNamed(self.word())
275276
}
276277
_ => ArgumentNext
@@ -293,8 +294,8 @@ impl<'a> Parser<'a> {
293294
if !self.consume(':') { return spec }
294295

295296
// fill character
296-
match self.cur.clone().next() {
297-
Some((_, c)) => {
297+
match self.cur.peek() {
298+
Some(&(_, c)) => {
298299
match self.cur.clone().skip(1).next() {
299300
Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => {
300301
spec.fill = Some(c);
@@ -392,20 +393,20 @@ impl<'a> Parser<'a> {
392393
/// be an alphabetic character followed by any number of alphanumeric
393394
/// characters.
394395
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() => {
397398
self.cur.next();
398399
pos
399400
}
400401
Some(..) | None => { return &self.input[..0]; }
401402
};
402403
let end;
403404
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() => {
406407
self.cur.next();
407408
}
408-
Some((pos, _)) => { end = pos; break }
409+
Some(&(pos, _)) => { end = pos; break }
409410
None => { end = self.input.len(); break }
410411
}
411412
}
@@ -417,7 +418,7 @@ impl<'a> Parser<'a> {
417418
fn integer(&mut self) -> Option<usize> {
418419
let mut cur = 0;
419420
let mut found = false;
420-
while let Some((_, c)) = self.cur.clone().next() {
421+
while let Some(&(_, c)) = self.cur.peek() {
421422
if let Some(i) = c.to_digit(10) {
422423
cur = cur * 10 + i as usize;
423424
found = true;

0 commit comments

Comments
 (0)