@@ -15,7 +15,6 @@ module Hledger.Utils.String (
1515 -- quotechars,
1616 -- whitespacechars,
1717 words' ,
18- unwords' ,
1918 stripAnsi ,
2019 -- * single-line layout
2120 strip ,
@@ -165,26 +164,25 @@ singleQuote s = "'"++s++"'"
165164-- >>> quoteForCommandLine "\""
166165-- "'\"'"
167166-- >>> quoteForCommandLine "$"
168- -- "'\\ $'"
167+ -- "'$'"
169168--
170169quoteForCommandLine :: String -> String
171170quoteForCommandLine s
172- | any (`elem` s) (quotechars++ whitespacechars++ shellchars) = singleQuote $ quoteShellChars s
171+ | any (`elem` s) (quotechars++ whitespacechars++ shellchars) = singleQuote $ escapeSingleQuotes s
173172 | otherwise = s
174173
175- -- | Try to backslash-quote common shell-significant characters in this string.
176- -- Doesn't handle single quotes, & probably others.
177- quoteShellChars :: String -> String
178- quoteShellChars = concatMap escapeShellChar
174+ -- | Escape single quotes appearing in a string we're protecting by wrapping in single quotes
175+ escapeSingleQuotes :: String -> String
176+ escapeSingleQuotes = concatMap escapeSingleQuote
179177 where
180- escapeShellChar c | c `elem` shellchars = [' \\ ' ,c]
181- escapeShellChar c = [c]
178+ escapeSingleQuote c | c `elem` " ' " = [' \\ ' ,c]
179+ escapeSingleQuote c = [c]
182180
183181quotechars , whitespacechars , redirectchars , shellchars :: [Char ]
184182quotechars = " '\" "
185183whitespacechars = " \t\n\r "
186184redirectchars = " <>"
187- shellchars = " <>(){}[]$&?#!~`"
185+ shellchars = " <>(){}[]$&?#!~`*+ \\ "
188186
189187-- | Quote-aware version of words - don't split on spaces which are inside quotes.
190188-- NB correctly handles "a'b" but not "''a''". Can raise an error if parsing fails.
@@ -198,10 +196,6 @@ words' s = map stripquotes $ fromparse $ parsewithString p s -- PARTIAL
198196 singleQuotedPattern = between (char ' \' ' ) (char ' \' ' ) (many $ noneOf " '" )
199197 doubleQuotedPattern = between (char ' "' ) (char ' "' ) (many $ noneOf " \" " )
200198
201- -- | Quote-aware version of unwords - single-quote strings which contain whitespace
202- unwords' :: [String ] -> String
203- unwords' = unwords . map quoteIfNeeded
204-
205199-- | Strip one matching pair of single or double quotes on the ends of a string.
206200stripquotes :: String -> String
207201stripquotes s = if isSingleQuoted s || isDoubleQuoted s then init $ tailErr s else s -- PARTIAL tailErr won't fail because isDoubleQuoted
0 commit comments