@@ -15,7 +15,6 @@ module Hledger.Utils.String (
15
15
-- quotechars,
16
16
-- whitespacechars,
17
17
words' ,
18
- unwords' ,
19
18
stripAnsi ,
20
19
-- * single-line layout
21
20
strip ,
@@ -165,26 +164,25 @@ singleQuote s = "'"++s++"'"
165
164
-- >>> quoteForCommandLine "\""
166
165
-- "'\"'"
167
166
-- >>> quoteForCommandLine "$"
168
- -- "'\\ $'"
167
+ -- "'$'"
169
168
--
170
169
quoteForCommandLine :: String -> String
171
170
quoteForCommandLine s
172
- | any (`elem` s) (quotechars++ whitespacechars++ shellchars) = singleQuote $ quoteShellChars s
171
+ | any (`elem` s) (quotechars++ whitespacechars++ shellchars) = singleQuote $ escapeSingleQuotes s
173
172
| otherwise = s
174
173
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
179
177
where
180
- escapeShellChar c | c `elem` shellchars = [' \\ ' ,c]
181
- escapeShellChar c = [c]
178
+ escapeSingleQuote c | c `elem` " ' " = [' \\ ' ,c]
179
+ escapeSingleQuote c = [c]
182
180
183
181
quotechars , whitespacechars , redirectchars , shellchars :: [Char ]
184
182
quotechars = " '\" "
185
183
whitespacechars = " \t\n\r "
186
184
redirectchars = " <>"
187
- shellchars = " <>(){}[]$&?#!~`"
185
+ shellchars = " <>(){}[]$&?#!~`*+ \\ "
188
186
189
187
-- | Quote-aware version of words - don't split on spaces which are inside quotes.
190
188
-- 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
198
196
singleQuotedPattern = between (char ' \' ' ) (char ' \' ' ) (many $ noneOf " '" )
199
197
doubleQuotedPattern = between (char ' "' ) (char ' "' ) (many $ noneOf " \" " )
200
198
201
- -- | Quote-aware version of unwords - single-quote strings which contain whitespace
202
- unwords' :: [String ] -> String
203
- unwords' = unwords . map quoteIfNeeded
204
-
205
199
-- | Strip one matching pair of single or double quotes on the ends of a string.
206
200
stripquotes :: String -> String
207
201
stripquotes s = if isSingleQuoted s || isDoubleQuoted s then init $ tailErr s else s -- PARTIAL tailErr won't fail because isDoubleQuoted
0 commit comments