lib: Fix quoteIfNeeded so it actually escapes quotes.
This commit is contained in:
parent
07dd30c1e5
commit
7d1e6d7d12
@ -13,7 +13,6 @@ module Hledger.Utils.String (
|
|||||||
singleQuoteIfNeeded,
|
singleQuoteIfNeeded,
|
||||||
-- quotechars,
|
-- quotechars,
|
||||||
-- whitespacechars,
|
-- whitespacechars,
|
||||||
escapeQuotes,
|
|
||||||
words',
|
words',
|
||||||
unwords',
|
unwords',
|
||||||
stripAnsi,
|
stripAnsi,
|
||||||
@ -120,8 +119,9 @@ underline s = s' ++ replicate (length s) '-' ++ "\n"
|
|||||||
-- | Double-quote this string if it contains whitespace, single quotes
|
-- | Double-quote this string if it contains whitespace, single quotes
|
||||||
-- or double-quotes, escaping the quotes as needed.
|
-- or double-quotes, escaping the quotes as needed.
|
||||||
quoteIfNeeded :: String -> String
|
quoteIfNeeded :: String -> String
|
||||||
quoteIfNeeded s | any (`elem` s) (quotechars++whitespacechars++redirectchars) = "\"" ++ escapeDoubleQuotes s ++ "\""
|
quoteIfNeeded s | any (`elem` s) (quotechars++whitespacechars++redirectchars) = show s
|
||||||
| otherwise = s
|
| otherwise = s
|
||||||
|
|
||||||
-- | Single-quote this string if it contains whitespace or double-quotes.
|
-- | Single-quote this string if it contains whitespace or double-quotes.
|
||||||
-- No good for strings containing single quotes.
|
-- No good for strings containing single quotes.
|
||||||
singleQuoteIfNeeded :: String -> String
|
singleQuoteIfNeeded :: String -> String
|
||||||
@ -133,12 +133,6 @@ quotechars = "'\""
|
|||||||
whitespacechars = " \t\n\r"
|
whitespacechars = " \t\n\r"
|
||||||
redirectchars = "<>"
|
redirectchars = "<>"
|
||||||
|
|
||||||
escapeDoubleQuotes :: String -> String
|
|
||||||
escapeDoubleQuotes = id -- regexReplace "\"" "\""
|
|
||||||
|
|
||||||
escapeQuotes :: String -> String
|
|
||||||
escapeQuotes = id -- regexReplace "([\"'])" "\\1"
|
|
||||||
|
|
||||||
-- | Quote-aware version of words - don't split on spaces which are inside quotes.
|
-- | Quote-aware version of words - don't split on spaces which are inside quotes.
|
||||||
-- NB correctly handles "a'b" but not "''a''". Can raise an error if parsing fails.
|
-- NB correctly handles "a'b" but not "''a''". Can raise an error if parsing fails.
|
||||||
words' :: String -> [String]
|
words' :: String -> [String]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user