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:
parent
e399398444
commit
8423c1fe64
|
@ -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 => match c {
|
||||||
ParseMode::SingleQuote => {
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue