module Darcs.Util.Regex
( Regex
, mkRegex
, mkRegexWithOpts
, matchRegex
) where
import Darcs.Prelude
import Text.Regex.Base
( RegexContext(matchM)
, RegexMaker(makeRegexOpts)
, defaultCompOpt
, defaultExecOpt
)
import Text.Regex.TDFA (Regex, caseSensitive, multiline, newSyntax)
mkRegex :: String -> Regex
mkRegex :: String -> Regex
mkRegex String
s = forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
compOpt -> execOpt -> source -> regex
makeRegexOpts CompOption
opt forall regex compOpt execOpt.
RegexOptions regex compOpt execOpt =>
execOpt
defaultExecOpt String
s
where
opt :: CompOption
opt = forall regex compOpt execOpt.
RegexOptions regex compOpt execOpt =>
compOpt
defaultCompOpt {newSyntax :: Bool
newSyntax = Bool
True, multiline :: Bool
multiline = Bool
True}
mkRegexWithOpts
:: String
-> Bool
-> Bool
-> Regex
mkRegexWithOpts :: String -> Bool -> Bool -> Regex
mkRegexWithOpts String
s Bool
single_line Bool
case_sensitive
= let opt :: CompOption
opt = forall regex compOpt execOpt.
RegexOptions regex compOpt execOpt =>
compOpt
defaultCompOpt
{ multiline :: Bool
multiline = (if Bool
single_line then Bool
True else Bool
False)
, caseSensitive :: Bool
caseSensitive = (if Bool
case_sensitive then Bool
True else Bool
False)
, newSyntax :: Bool
newSyntax = Bool
True }
in forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
compOpt -> execOpt -> source -> regex
makeRegexOpts CompOption
opt forall regex compOpt execOpt.
RegexOptions regex compOpt execOpt =>
execOpt
defaultExecOpt String
s
matchRegex ::
Regex
-> String
-> Maybe [String]
matchRegex :: Regex -> String -> Maybe [String]
matchRegex Regex
p String
str = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String, String, String, [String]) -> [String]
go (forall regex source target (m :: * -> *).
(RegexContext regex source target, MonadFail m) =>
regex -> source -> m target
matchM Regex
p String
str)
where
go :: (String, String, String, [String]) -> [String]
go :: (String, String, String, [String]) -> [String]
go (String
_, String
_, String
_, [String]
ss) = [String]
ss