fix a regexReplaceCI bug

This commit is contained in:
Simon Michael 2014-10-24 15:04:21 -07:00
parent bfedf367c4
commit be9b637e0c

View File

@ -32,7 +32,7 @@ module Hledger.Utils.Regex (
,regexReplace
,regexReplaceCI
,regexReplaceBy
,regexToCaseInsensitive
-- ,regexpToCI
,regexSplit
,toRegex
)
@ -52,9 +52,6 @@ type Replacement = String
containsRegex :: Regexp -> String -> Bool
containsRegex = regexMatchesCI
regexToCaseInsensitive :: Regexp -> Regexp
regexToCaseInsensitive r = "(?i)"++ r
-- regexpr - may be slow
regexSplit :: Regexp -> String -> [Regexp]
@ -64,19 +61,19 @@ regexSplit = splitRegexPR
-- regexMatch r s = matchRegexPR r s
-- regexMatchCI :: Regexp -> String -> MatchFun Maybe
-- regexMatchCI r s = regexMatch (regexToCaseInsensitive r) s
-- regexMatchCI r s = regexMatch (regexpToCI r) s
-- regexMatches :: Regexp -> String -> Bool
-- regexMatches r s = isJust $ matchRegexPR r s
-- regexMatchesCI :: Regexp -> String -> Bool
-- regexMatchesCI r s = regexMatches (regexToCaseInsensitive r) s
-- regexMatchesCI r s = regexMatches (regexpToCI r) s
-- regexReplace :: Regexp -> Replacement -> String -> String
-- regexReplace r repl s = gsubRegexPR r repl s
-- regexReplaceCI :: Regexp -> Replacement -> String -> String
-- regexReplaceCI r s = regexReplace (regexToCaseInsensitive r) s
-- regexReplaceCI r s = regexReplace (regexpToCI r) s
-- regexReplaceBy :: Regexp -> (String -> Replacement) -> String -> String
-- regexReplaceBy r replfn s = gsubRegexPRBy r replfn s
@ -101,18 +98,31 @@ regexMatches :: Regexp -> String -> Bool
regexMatches = flip (=~)
regexMatchesCI :: Regexp -> String -> Bool
regexMatchesCI r = match (makeRegexOpts compOpt{caseSensitive=False} execOpt r)
regexMatchesCI r = match (toRegexCI r)
regexReplace :: Regexp -> Replacement -> String -> String
regexReplace r repl = regexReplaceBy r (const repl)
regexReplaceCI :: Regexp -> Replacement -> String -> String
regexReplaceCI r s = regexReplace (regexToCaseInsensitive r) s
regexReplaceCI r repl = regexReplaceByCI r (const repl)
regexReplaceBy :: Regexp -> (String -> Replacement) -> String -> String
regexReplaceBy r = replaceAll (toRegex r)
-- from http://stackoverflow.com/questions/9071682/replacement-substition-with-haskell-regex-libraries
regexReplaceByCI :: Regexp -> (String -> Replacement) -> String -> String
regexReplaceByCI r = replaceAll (toRegexCI r)
toRegexCI :: Regexp -> Regex
toRegexCI = makeRegexOpts compOpt{caseSensitive=False} execOpt
-- regexpToCI :: Regexp -> Regexp
-- regexpToCI r = "(?i)"++ r
-- from
-- http://stackoverflow.com/questions/9071682/replacement-substition-with-haskell-regex-libraries
-- | Replace all occurrences of a regexp in a string using a replacer
-- function, which receives the matched string as its argument.
-- Does not support standard RE syntax such as \1.
replaceAll :: Regex -> (String -> Replacement) -> String -> String
replaceAll re f s = start end
where