From 04f6162e190751fb0c068c31c5b3fe98bd95af00 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 6 Jul 2014 10:11:02 -0700 Subject: [PATCH] extract regular expression utils module --- hledger-lib/Hledger/Utils.hs | 47 ++---------------------- hledger-lib/Hledger/Utils/Regex.hs | 59 ++++++++++++++++++++++++++++++ hledger-lib/hledger-lib.cabal | 1 + 3 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 hledger-lib/Hledger/Utils/Regex.hs diff --git a/hledger-lib/Hledger/Utils.hs b/hledger-lib/Hledger/Utils.hs index b355c6d34..fed7ee063 100644 --- a/hledger-lib/Hledger/Utils.hs +++ b/hledger-lib/Hledger/Utils.hs @@ -20,6 +20,7 @@ module Hledger.Utils (---- provide these frequently used modules - or not, for c -- module Text.Printf, ---- all of this one: module Hledger.Utils, + module Hledger.Utils.Regex, Debug.Trace.trace, -- module Data.PPrint, -- module Hledger.Utils.UTF8IOCompat @@ -36,7 +37,7 @@ import Control.Monad.IO.Class (liftIO) import Data.Char import Data.List import qualified Data.Map as M -import Data.Maybe +-- import Data.Maybe -- import Data.PPrint import Data.Time.Clock import Data.Time.LocalTime @@ -52,10 +53,9 @@ import System.IO.Unsafe (unsafePerformIO) import Test.HUnit import Text.ParserCombinators.Parsec import Text.Printf -import Text.Regex.TDFA -import Text.RegexPR -- import qualified Data.Map as Map --- + +import Hledger.Utils.Regex -- import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn) -- import Hledger.Utils.UTF8IOCompat (readFile,writeFile,appendFile,getContents,putStr,putStrLn) import Hledger.Utils.UTF8IOCompat (SystemString,fromSystemString,toSystemString,error',userError') @@ -248,45 +248,6 @@ fifth5 (_,_,_,_,x) = x difforzero :: (Num a, Ord a) => a -> a -> a difforzero a b = maximum [(a - b), 0] --- regexps --- Note many of these will die on malformed regexps. - --- regexMatch :: String -> String -> MatchFun Maybe -regexMatch r s = matchRegexPR r s - --- regexMatchCI :: String -> String -> MatchFun Maybe -regexMatchCI r s = regexMatch (regexToCaseInsensitive r) s - -regexMatches :: String -> String -> Bool -regexMatches r s = isJust $ matchRegexPR r s - -regexMatchesCI :: String -> String -> Bool -regexMatchesCI r s = regexMatches (regexToCaseInsensitive r) s - -containsRegex = regexMatchesCI - -regexReplace :: String -> String -> String -> String -regexReplace r repl s = gsubRegexPR r repl s - -regexReplaceCI :: String -> String -> String -> String -regexReplaceCI r s = regexReplace (regexToCaseInsensitive r) s - -regexReplaceBy :: String -> (String -> String) -> String -> String -regexReplaceBy r replfn s = gsubRegexPRBy r replfn s - -regexToCaseInsensitive :: String -> String -regexToCaseInsensitive r = "(?i)"++ r - -regexSplit :: String -> String -> [String] -regexSplit = splitRegexPR - --- regex-compat (regex-posix) functions that perform better than regexpr. -regexMatchesRegexCompat :: String -> String -> Bool -regexMatchesRegexCompat = flip (=~) - -regexMatchesCIRegexCompat :: String -> String -> Bool -regexMatchesCIRegexCompat r = match (makeRegexOpts defaultCompOpt { multiline = True, caseSensitive = False, newSyntax = True } defaultExecOpt r) - -- lists splitAtElement :: Eq a => a -> [a] -> [[a]] diff --git a/hledger-lib/Hledger/Utils/Regex.hs b/hledger-lib/Hledger/Utils/Regex.hs new file mode 100644 index 000000000..bbb2fd39a --- /dev/null +++ b/hledger-lib/Hledger/Utils/Regex.hs @@ -0,0 +1,59 @@ +-- Regular expression helpers. +-- Currently using mostly regexpr and some regex-tdfa. +-- Note many of these will die on malformed regexps. + +module Hledger.Utils.Regex ( + regexMatch + ,regexMatchCI + ,regexMatches + ,regexMatchesCI + ,containsRegex + ,regexReplace + ,regexReplaceCI + ,regexReplaceBy + ,regexToCaseInsensitive + ,regexSplit + ,regexMatchesRegexCompat + ,regexMatchesCIRegexCompat + ) +where +import Data.Maybe +import Text.Regex.TDFA +import Text.RegexPR + +-- regexMatch :: String -> String -> MatchFun Maybe +regexMatch r s = matchRegexPR r s + +-- regexMatchCI :: String -> String -> MatchFun Maybe +regexMatchCI r s = regexMatch (regexToCaseInsensitive r) s + +regexMatches :: String -> String -> Bool +regexMatches r s = isJust $ matchRegexPR r s + +regexMatchesCI :: String -> String -> Bool +regexMatchesCI r s = regexMatches (regexToCaseInsensitive r) s + +containsRegex = regexMatchesCI + +regexReplace :: String -> String -> String -> String +regexReplace r repl s = gsubRegexPR r repl s + +regexReplaceCI :: String -> String -> String -> String +regexReplaceCI r s = regexReplace (regexToCaseInsensitive r) s + +regexReplaceBy :: String -> (String -> String) -> String -> String +regexReplaceBy r replfn s = gsubRegexPRBy r replfn s + +regexToCaseInsensitive :: String -> String +regexToCaseInsensitive r = "(?i)"++ r + +regexSplit :: String -> String -> [String] +regexSplit = splitRegexPR + +-- regex-compat (regex-posix) functions that perform better than regexpr. +regexMatchesRegexCompat :: String -> String -> Bool +regexMatchesRegexCompat = flip (=~) + +regexMatchesCIRegexCompat :: String -> String -> Bool +regexMatchesCIRegexCompat r = match (makeRegexOpts defaultCompOpt { multiline = True, caseSensitive = False, newSyntax = True } defaultExecOpt r) + diff --git a/hledger-lib/hledger-lib.cabal b/hledger-lib/hledger-lib.cabal index 5a00d621e..a27e3c127 100644 --- a/hledger-lib/hledger-lib.cabal +++ b/hledger-lib/hledger-lib.cabal @@ -64,6 +64,7 @@ library Hledger.Reports.PostingsReport Hledger.Reports.TransactionsReports Hledger.Utils + Hledger.Utils.Regex Hledger.Utils.UTF8IOCompat build-depends: base >= 4.3 && < 5