Small improvements 🧺

This commit is contained in:
Laurenz 2020-10-02 20:22:08 +02:00
parent 904bc392ab
commit dc8d5d2f1e
4 changed files with 7 additions and 9 deletions

View File

@ -35,7 +35,7 @@ pub async fn align(_: Span, mut args: DictValue, ctx: LayoutContext<'_>) -> Pass
// if the alignment is `center` for a positional argument. Then we set // if the alignment is `center` for a positional argument. Then we set
// `deferred_center` to true and handle the situation once we know more. // `deferred_center` to true and handle the situation once we know more.
if let Some(axis) = axis { if let Some(axis) = axis {
if align.v.axis().map(|a| a != axis).unwrap_or(false) { if align.v.axis().map_or(false, |a| a != axis) {
error!( error!(
@f, align.span, @f, align.span,
"invalid alignment {} for {} axis", align.v, axis, "invalid alignment {} for {} axis", align.v, axis,

View File

@ -228,7 +228,7 @@ impl<'s> Parser<'s> {
/// ///
/// Returns `false` if there is no next token. /// Returns `false` if there is no next token.
pub fn check(&mut self, f: impl FnOnce(Token<'s>) -> bool) -> bool { pub fn check(&mut self, f: impl FnOnce(Token<'s>) -> bool) -> bool {
self.peek().map(f).unwrap_or(false) self.peek().map_or(false, f)
} }
/// Whether the end of the source string or group is reached. /// Whether the end of the source string or group is reached.
@ -278,7 +278,9 @@ impl<'s> Parser<'s> {
/// Set the position to the tokenizer's position and take the peeked token. /// Set the position to the tokenizer's position and take the peeked token.
fn bump(&mut self) -> Option<Token<'s>> { fn bump(&mut self) -> Option<Token<'s>> {
self.pos = self.tokens.pos(); self.pos = self.tokens.pos();
self.peeked.take() let token = self.peeked;
self.peeked = None;
token
} }
} }

View File

@ -89,12 +89,12 @@ fn trim_and_split_raw(raw: &str) -> (Vec<String>, bool) {
let is_whitespace = |line: &String| line.chars().all(char::is_whitespace); let is_whitespace = |line: &String| line.chars().all(char::is_whitespace);
// Trims a sequence of whitespace followed by a newline at the start. // Trims a sequence of whitespace followed by a newline at the start.
if lines.first().map(is_whitespace).unwrap_or(false) { if lines.first().map_or(false, is_whitespace) {
lines.remove(0); lines.remove(0);
} }
// Trims a newline followed by a sequence of whitespace at the end. // Trims a newline followed by a sequence of whitespace at the end.
if lines.last().map(is_whitespace).unwrap_or(false) { if lines.last().map_or(false, is_whitespace) {
lines.pop(); lines.pop();
} }

View File

@ -32,8 +32,6 @@ impl<'s> Scanner<'s> {
/// Returns whether the char was consumed. /// Returns whether the char was consumed.
pub fn eat_if(&mut self, c: char) -> bool { pub fn eat_if(&mut self, c: char) -> bool {
// Don't decode the char twice through peek() and eat(). // Don't decode the char twice through peek() and eat().
//
// TODO: Benchmark this vs. the naive version.
if self.iter.next() == Some(c) { if self.iter.next() == Some(c) {
self.index += c.len_utf8(); self.index += c.len_utf8();
true true
@ -71,8 +69,6 @@ impl<'s> Scanner<'s> {
if f(c) { if f(c) {
// Undo the previous `next()` without peeking all the time // Undo the previous `next()` without peeking all the time
// during iteration. // during iteration.
//
// TODO: Benchmark this vs. the naive peeking version.
self.reset(); self.reset();
break; break;
} }