Skip to content

Commit 3c731ae

Browse files
alerquesimonmichael
authored andcommitted
fix: Only escape special characters by single quoting, not escaping *and* quoting
1 parent 8cbe4c6 commit 3c731ae

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

hledger-lib/Hledger/Utils/String.hs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
--
170169
quoteForCommandLine :: String -> String
171170
quoteForCommandLine 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

183181
quotechars, whitespacechars, redirectchars, shellchars :: [Char]
184182
quotechars = "'\""
185183
whitespacechars = " \t\n\r"
186184
redirectchars = "<>"
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.
206200
stripquotes :: String -> String
207201
stripquotes s = if isSingleQuoted s || isDoubleQuoted s then init $ tailErr s else s -- PARTIAL tailErr won't fail because isDoubleQuoted

hledger-lib/Hledger/Utils/Text.hs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ module Hledger.Utils.Text
2323
-- escapeSingleQuotes,
2424
-- escapeQuotes,
2525
-- words',
26-
-- unwords',
2726
stripquotes,
2827
-- isSingleQuoted,
2928
-- isDoubleQuoted,
@@ -162,10 +161,6 @@ escapeBackslash = T.replace "\\" "\\\\"
162161
-- singleQuotedPattern = between (char '\'') (char '\'') (many $ noneOf "'")
163162
-- doubleQuotedPattern = between (char '"') (char '"') (many $ noneOf "\"")
164163

165-
-- -- | Quote-aware version of unwords - single-quote strings which contain whitespace
166-
-- unwords' :: [Text] -> Text
167-
-- unwords' = T.unwords . map quoteIfNeeded
168-
169164
-- | Strip one matching pair of single or double quotes on the ends of a string.
170165
stripquotes :: Text -> Text
171166
stripquotes s = if isSingleQuoted s || isDoubleQuoted s then T.init $ T.tail s else s

0 commit comments

Comments
 (0)