extract regular expression utils module
This commit is contained in:
parent
8fe26fe345
commit
04f6162e19
@ -20,6 +20,7 @@ module Hledger.Utils (---- provide these frequently used modules - or not, for c
|
|||||||
-- module Text.Printf,
|
-- module Text.Printf,
|
||||||
---- all of this one:
|
---- all of this one:
|
||||||
module Hledger.Utils,
|
module Hledger.Utils,
|
||||||
|
module Hledger.Utils.Regex,
|
||||||
Debug.Trace.trace,
|
Debug.Trace.trace,
|
||||||
-- module Data.PPrint,
|
-- module Data.PPrint,
|
||||||
-- module Hledger.Utils.UTF8IOCompat
|
-- module Hledger.Utils.UTF8IOCompat
|
||||||
@ -36,7 +37,7 @@ import Control.Monad.IO.Class (liftIO)
|
|||||||
import Data.Char
|
import Data.Char
|
||||||
import Data.List
|
import Data.List
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Data.Maybe
|
-- import Data.Maybe
|
||||||
-- import Data.PPrint
|
-- import Data.PPrint
|
||||||
import Data.Time.Clock
|
import Data.Time.Clock
|
||||||
import Data.Time.LocalTime
|
import Data.Time.LocalTime
|
||||||
@ -52,10 +53,9 @@ import System.IO.Unsafe (unsafePerformIO)
|
|||||||
import Test.HUnit
|
import Test.HUnit
|
||||||
import Text.ParserCombinators.Parsec
|
import Text.ParserCombinators.Parsec
|
||||||
import Text.Printf
|
import Text.Printf
|
||||||
import Text.Regex.TDFA
|
|
||||||
import Text.RegexPR
|
|
||||||
-- import qualified Data.Map as Map
|
-- import qualified Data.Map as Map
|
||||||
--
|
|
||||||
|
import Hledger.Utils.Regex
|
||||||
-- import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
-- import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
||||||
-- import Hledger.Utils.UTF8IOCompat (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')
|
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 :: (Num a, Ord a) => a -> a -> a
|
||||||
difforzero a b = maximum [(a - b), 0]
|
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
|
-- lists
|
||||||
|
|
||||||
splitAtElement :: Eq a => a -> [a] -> [[a]]
|
splitAtElement :: Eq a => a -> [a] -> [[a]]
|
||||||
|
|||||||
59
hledger-lib/Hledger/Utils/Regex.hs
Normal file
59
hledger-lib/Hledger/Utils/Regex.hs
Normal file
@ -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)
|
||||||
|
|
||||||
@ -64,6 +64,7 @@ library
|
|||||||
Hledger.Reports.PostingsReport
|
Hledger.Reports.PostingsReport
|
||||||
Hledger.Reports.TransactionsReports
|
Hledger.Reports.TransactionsReports
|
||||||
Hledger.Utils
|
Hledger.Utils
|
||||||
|
Hledger.Utils.Regex
|
||||||
Hledger.Utils.UTF8IOCompat
|
Hledger.Utils.UTF8IOCompat
|
||||||
build-depends:
|
build-depends:
|
||||||
base >= 4.3 && < 5
|
base >= 4.3 && < 5
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user