Skip to content

Commit 15d5c08

Browse files
committed
some code improvements in libfmt_macros
1 parent 69092ff commit 15d5c08

File tree

1 file changed

+74
-108
lines changed

1 file changed

+74
-108
lines changed

src/libfmt_macros/lib.rs

Lines changed: 74 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -151,28 +151,31 @@ impl<'a> Iterator for Parser<'a> {
151151
type Item = Piece<'a>;
152152

153153
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+
}
163165
}
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+
}
172174
}
175+
_ => Some(String(self.string(pos))),
173176
}
174-
Some(&(pos, _)) => { Some(String(self.string(pos))) }
175-
None => None
177+
} else {
178+
None
176179
}
177180
}
178181
}
@@ -198,61 +201,47 @@ impl<'a> Parser<'a> {
198201
/// the current position, then the current iterator isn't moved and false is
199202
/// returned, otherwise the character is consumed and true is returned.
200203
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
207208
}
208209
}
209210

210211
/// Forces consumption of the specified character. If the character is not
211212
/// found, an error is emitted.
212213
fn must_consume(&mut self, c: char) {
213214
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 {
216217
self.cur.next();
218+
} else {
219+
self.err(&format!("expected `{:?}`, found `{:?}`", c, maybe));
217220
}
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));
226223
}
227224
}
228225

229226
/// Consumes all whitespace characters until the first non-whitespace
230227
/// character
231228
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 }
237231
}
238232
}
239233

240234
/// Parses all of a string which is to be considered a "raw literal" in a
241235
/// format string. This is everything outside of the braces.
242236
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(); }
254242
}
255243
}
244+
&self.input[start..self.input.len()]
256245
}
257246

258247
/// Parses an Argument structure, or what's contained within braces inside
@@ -267,15 +256,14 @@ impl<'a> Parser<'a> {
267256
/// Parses a positional argument for a format. This could either be an
268257
/// integer index of an argument, a named argument, or a blank string.
269258
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())
278265
}
266+
_ => ArgumentNext
279267
}
280268
}
281269
}
@@ -294,17 +282,14 @@ impl<'a> Parser<'a> {
294282
if !self.consume(':') { return spec }
295283

296284
// 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();
305290
}
291+
_ => {}
306292
}
307-
None => {}
308293
}
309294
// Alignment
310295
if self.consume('<') {
@@ -361,29 +346,20 @@ impl<'a> Parser<'a> {
361346
/// for 'CountIsNextParam' because that is only used in precision, not
362347
/// width.
363348
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 {
366358
if self.consume('$') {
367-
CountIsParam(i)
359+
CountIsName(word)
368360
} 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
387363
}
388364
}
389365
}
@@ -394,23 +370,17 @@ impl<'a> Parser<'a> {
394370
/// characters.
395371
fn word(&mut self) -> &'a str {
396372
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]; }
402375
};
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];
411381
}
412382
}
413-
&self.input[start..end]
383+
&self.input[start..self.input.len()]
414384
}
415385

416386
/// Optionally parses an integer at the current position. This doesn't deal
@@ -427,11 +397,7 @@ impl<'a> Parser<'a> {
427397
break
428398
}
429399
}
430-
if found {
431-
Some(cur)
432-
} else {
433-
None
434-
}
400+
if found { Some(cur) } else { None }
435401
}
436402
}
437403

0 commit comments

Comments
 (0)