From 666218dd62b207c6ebba638145bb8259c7035ab0 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 15 Aug 2011 13:58:16 +0000 Subject: [PATCH] more quoted string splitting/joining utils --- hledger-lib/Hledger/Utils.hs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Hledger/Utils.hs b/hledger-lib/Hledger/Utils.hs index f92e08dfd..e827149eb 100644 --- a/hledger-lib/Hledger/Utils.hs +++ b/hledger-lib/Hledger/Utils.hs @@ -74,13 +74,24 @@ quoteIfSpaced s | isSingleQuoted s || isDoubleQuoted s = s where escapeSingleQuotes = regexReplace "'" "\'" -- | Quote-aware version of words - don't split on spaces which are inside quotes. +-- NB correctly handles "a'b" but not "''a''". words' :: String -> [String] -words' = map stripquotes . fromparse . parsewith ((quotedPattern <|> pattern) `sepBy` many1 spacenonewline) +words' = map stripquotes . fromparse . parsewith p where + p = do ss <- (quotedPattern <|> pattern) `sepBy` many1 spacenonewline + -- eof + return ss pattern = many (noneOf whitespacechars) quotedPattern = between (oneOf "'\"") (oneOf "'\"") $ many $ noneOf "'\"" -whitespacechars = " \t\n\r\'\"" +-- | Quote-aware version of unwords - single-quote strings which contain whitespace +unwords' :: [String] -> String +unwords' = unwords . map singleQuoteIfNeeded + +singleQuoteIfNeeded s | any (`elem` s) whitespacechars = "'"++s++"'" + | otherwise = s + +whitespacechars = " \t\n\r" -- | Strip one matching pair of single or double quotes on the ends of a string. stripquotes :: String -> String