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 <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-11-29 13:37:34 +01:00
parent e399398444
commit 8423c1fe64
1 changed files with 26 additions and 36 deletions

View File

@ -36,19 +36,21 @@ pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec<String>, Option
for (index, c) in char_indices { for (index, c) in char_indices {
match mode { match mode {
ParseMode::Space => { ParseMode::Space => match c {
if c == '"' { '"' => {
mode = ParseMode::DoubleQuote; mode = ParseMode::DoubleQuote;
field_start = Some((index, Quote::Double)); field_start = Some((index, Quote::Double));
} else if c == '\\' { }
'\\' => {
mode = ParseMode::EscapeNormal; mode = ParseMode::EscapeNormal;
field_start = Some((index, Quote::None)); field_start = Some((index, Quote::None));
} else if c == '\'' { }
'\'' => {
mode = ParseMode::SingleQuote; mode = ParseMode::SingleQuote;
field_start = Some((index, Quote::Single)); field_start = Some((index, Quote::Single));
} else if space_chars.iter().any(|s| c == *s) { }
/* skip space */ c if space_chars.contains(&c) => (), // skip space
} else { c => {
mode = ParseMode::Normal; mode = ParseMode::Normal;
field_start = Some((index, Quote::None)); field_start = Some((index, Quote::None));
field.push(c); field.push(c);
@ -62,45 +64,33 @@ pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec<String>, Option
// Within double quoted strings, backslashes are only // Within double quoted strings, backslashes are only
// treated as metacharacters when followed by one of // treated as metacharacters when followed by one of
// the following characters: $ ' " \ newline // the following characters: $ ' " \ newline
if c == '$' || c == '\'' || c == '"' || c == '\\' || c == '\n' { match c {
field.push(c); '$' | '\'' | '"' | '\\' | '\n' => (),
} else { _ => field.push('\\'),
field.push('\\');
field.push(c);
} }
field.push(c);
mode = ParseMode::DoubleQuote; mode = ParseMode::DoubleQuote;
} }
ParseMode::Normal => { ParseMode::Normal => match c {
if c == '"' { '"' => mode = ParseMode::DoubleQuote,
mode = ParseMode::DoubleQuote; '\'' => mode = ParseMode::SingleQuote,
} else if c == '\'' { '\\' => mode = ParseMode::EscapeNormal,
mode = ParseMode::SingleQuote; c if space_chars.contains(&c) => {
} else if c == '\\' {
mode = ParseMode::EscapeNormal;
} else if space_chars.iter().any(|s| c == *s) {
mode = ParseMode::Space; mode = ParseMode::Space;
let (_start, _quote) = field_start.take().unwrap(); let (_start, _quote) = field_start.take().unwrap();
args.push(field.split_off(0)); args.push(field.split_off(0));
} else {
field.push(c); // continue
} }
c => field.push(c), // continue
} }
ParseMode::DoubleQuote => { ParseMode::DoubleQuote => match c {
if c == '"' { '"' => mode = ParseMode::Normal,
mode = ParseMode::Normal; '\\' => mode = ParseMode::EscapeInDoubleQuote,
} else if c == '\\' { c => field.push(c), // continue
mode = ParseMode::EscapeInDoubleQuote;
} else {
field.push(c); // continue
}
} }
ParseMode::SingleQuote => { ParseMode::SingleQuote => match c {
// Note: no escape in single quotes // Note: no escape in single quotes
if c == '\'' { '\'' => mode = ParseMode::Normal,
mode = ParseMode::Normal; c => field.push(c), // continue
} else {
field.push(c); // continue
}
} }
} }
} }