mirror of
https://github.com/simonmichael/hledger.git
synced 2024-12-26 03:42:25 +03:00
whitespace/quoting utilities
This commit is contained in:
parent
bea6fb7464
commit
b344f797d3
@ -69,6 +69,31 @@ underline s = s' ++ replicate (length s) '-' ++ "\n"
|
|||||||
| last s == '\n' = s
|
| last s == '\n' = s
|
||||||
| otherwise = s ++ "\n"
|
| otherwise = s ++ "\n"
|
||||||
|
|
||||||
|
-- | Wrap a string in single quotes, and \-prefix any embedded single
|
||||||
|
-- quotes, if it contains whitespace and is not already single- or
|
||||||
|
-- double-quoted.
|
||||||
|
quoteIfSpaced :: String -> String
|
||||||
|
quoteIfSpaced s | isSingleQuoted s || isDoubleQuoted s = s
|
||||||
|
| not $ any (`elem` s) whitespacechars = s
|
||||||
|
| otherwise = "'"++escapeSingleQuotes s++"'"
|
||||||
|
where escapeSingleQuotes = regexReplace "'" "\'"
|
||||||
|
|
||||||
|
-- | Quote-aware version of words - don't split on spaces which are inside quotes.
|
||||||
|
words' :: String -> [String]
|
||||||
|
words' = map stripquotes . fromparse . parsewith ((quotedPattern <|> pattern) `sepBy` many1 spacenonewline)
|
||||||
|
where
|
||||||
|
pattern = many (noneOf whitespacechars)
|
||||||
|
quotedPattern = between (oneOf "'\"") (oneOf "'\"") $ many $ noneOf "'\""
|
||||||
|
|
||||||
|
whitespacechars = " \t\n\r\'\""
|
||||||
|
|
||||||
|
-- | Strip one matching pair of single or double quotes on the ends of a string.
|
||||||
|
stripquotes :: String -> String
|
||||||
|
stripquotes s = if isSingleQuoted s || isDoubleQuoted s then init $ tail s else s
|
||||||
|
|
||||||
|
isSingleQuoted s = head s == '\'' && last s == '\''
|
||||||
|
isDoubleQuoted s = head s == '"' && last s == '"'
|
||||||
|
|
||||||
unbracket :: String -> String
|
unbracket :: String -> String
|
||||||
unbracket s
|
unbracket s
|
||||||
| (head s == '[' && last s == ']') || (head s == '(' && last s == ')') = init $ tail s
|
| (head s == '[' && last s == ']') || (head s == '(' && last s == ')') = init $ tail s
|
||||||
|
@ -453,11 +453,4 @@ accountRegexToAccountName = gsubRegexPR "^\\^(.*?)\\(:\\|\\$\\)$" "\\1"
|
|||||||
isAccountRegex :: String -> Bool
|
isAccountRegex :: String -> Bool
|
||||||
isAccountRegex s = take 1 s == "^" && (take 5 $ reverse s) == ")$|:("
|
isAccountRegex s = take 1 s == "^" && (take 5 $ reverse s) == ")$|:("
|
||||||
|
|
||||||
-- | Quote-aware version of words - don't split on spaces which are inside quotes.
|
|
||||||
words' :: String -> [String]
|
|
||||||
words' = fromparse . parsewith ((quotedPattern <|> pattern) `sepBy` many1 spacenonewline)
|
|
||||||
where
|
|
||||||
pattern = many (noneOf " \n\r\"")
|
|
||||||
quotedPattern = between (oneOf "'\"") (oneOf "'\"") $ many $ noneOf "'\""
|
|
||||||
|
|
||||||
numbered = zip [1..]
|
numbered = zip [1..]
|
||||||
|
Loading…
Reference in New Issue
Block a user