From d5dd4f6cff943f987f8a2765e6d65033035de9a9 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Fri, 27 Aug 2021 10:57:41 +0200 Subject: [PATCH] ls: only quote ~ and # when they appear at the start of the name For example, we quote '~a' and '~' but not a~ --- src/uu/ls/src/quoting_style.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/uu/ls/src/quoting_style.rs b/src/uu/ls/src/quoting_style.rs index f9ba55f7b..5f421b2ee 100644 --- a/src/uu/ls/src/quoting_style.rs +++ b/src/uu/ls/src/quoting_style.rs @@ -1,6 +1,9 @@ use std::char::from_digit; -const SPECIAL_SHELL_CHARS: &str = "~`#$&*()|[]{};\\'\"<>?! "; +// These are characters with special meaning in the shell (e.g. bash). +// The first const contains characters that only have a special meaning when they appear at the beginning of a name. +const SPECIAL_SHELL_CHARS_START: &[char] = &['~', '#']; +const SPECIAL_SHELL_CHARS: &str = "`$&*()|[]{};\\'\"<>?! "; pub(super) enum QuotingStyle { Shell { @@ -198,6 +201,8 @@ fn shell_without_escape(name: &str, quotes: Quotes, show_control_chars: bool) -> } } } + + must_quote = must_quote || name.starts_with(SPECIAL_SHELL_CHARS_START); (escaped_str, must_quote) } @@ -246,6 +251,7 @@ fn shell_with_escape(name: &str, quotes: Quotes) -> (String, bool) { } } } + must_quote = must_quote || name.starts_with(SPECIAL_SHELL_CHARS_START); (escaped_str, must_quote) } @@ -659,4 +665,29 @@ mod tests { ], ); } + + #[test] + fn test_tilde_and_hash() { + check_names("~", vec![("'~'", "shell"), ("'~'", "shell-escape")]); + check_names( + "~name", + vec![("'~name'", "shell"), ("'~name'", "shell-escape")], + ); + check_names( + "some~name", + vec![("some~name", "shell"), ("some~name", "shell-escape")], + ); + check_names("name~", vec![("name~", "shell"), ("name~", "shell-escape")]); + + check_names("#", vec![("'#'", "shell"), ("'#'", "shell-escape")]); + check_names( + "#name", + vec![("'#name'", "shell"), ("'#name'", "shell-escape")], + ); + check_names( + "some#name", + vec![("some#name", "shell"), ("some#name", "shell-escape")], + ); + check_names("name#", vec![("name#", "shell"), ("name#", "shell-escape")]); + } }