From 8423c1fe64082b40e83e73c40553ebb58cba4c53 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 29 Nov 2019 13:37:34 +0100 Subject: [PATCH] cli/completion: use match statements and .contains() `match` is a bit more readable than the if-else chains, also replace space_chars.iter().any(|s| c == *s) with space_chars.contains(&c) which is also more readable. Signed-off-by: Wolfgang Bumiller --- src/cli/completion.rs | 62 ++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/src/cli/completion.rs b/src/cli/completion.rs index 78e38e58..ede5547f 100644 --- a/src/cli/completion.rs +++ b/src/cli/completion.rs @@ -36,19 +36,21 @@ pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec, Option for (index, c) in char_indices { match mode { - ParseMode::Space => { - if c == '"' { + ParseMode::Space => match c { + '"' => { mode = ParseMode::DoubleQuote; field_start = Some((index, Quote::Double)); - } else if c == '\\' { + } + '\\' => { mode = ParseMode::EscapeNormal; field_start = Some((index, Quote::None)); - } else if c == '\'' { + } + '\'' => { mode = ParseMode::SingleQuote; field_start = Some((index, Quote::Single)); - } else if space_chars.iter().any(|s| c == *s) { - /* skip space */ - } else { + } + c if space_chars.contains(&c) => (), // skip space + c => { mode = ParseMode::Normal; field_start = Some((index, Quote::None)); field.push(c); @@ -62,45 +64,33 @@ pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec, Option // Within double quoted strings, backslashes are only // treated as metacharacters when followed by one of // the following characters: $ ' " \ newline - if c == '$' || c == '\'' || c == '"' || c == '\\' || c == '\n' { - field.push(c); - } else { - field.push('\\'); - field.push(c); + match c { + '$' | '\'' | '"' | '\\' | '\n' => (), + _ => field.push('\\'), } + field.push(c); mode = ParseMode::DoubleQuote; } - ParseMode::Normal => { - if c == '"' { - mode = ParseMode::DoubleQuote; - } else if c == '\'' { - mode = ParseMode::SingleQuote; - } else if c == '\\' { - mode = ParseMode::EscapeNormal; - } else if space_chars.iter().any(|s| c == *s) { + ParseMode::Normal => match c { + '"' => mode = ParseMode::DoubleQuote, + '\'' => mode = ParseMode::SingleQuote, + '\\' => mode = ParseMode::EscapeNormal, + c if space_chars.contains(&c) => { mode = ParseMode::Space; let (_start, _quote) = field_start.take().unwrap(); args.push(field.split_off(0)); - } else { - field.push(c); // continue } + c => field.push(c), // continue } - ParseMode::DoubleQuote => { - if c == '"' { - mode = ParseMode::Normal; - } else if c == '\\' { - mode = ParseMode::EscapeInDoubleQuote; - } else { - field.push(c); // continue - } + ParseMode::DoubleQuote => match c { + '"' => mode = ParseMode::Normal, + '\\' => mode = ParseMode::EscapeInDoubleQuote, + c => field.push(c), // continue } - ParseMode::SingleQuote => { + ParseMode::SingleQuote => match c { // Note: no escape in single quotes - if c == '\'' { - mode = ParseMode::Normal; - } else { - field.push(c); // continue - } + '\'' => mode = ParseMode::Normal, + c => field.push(c), // continue } } }