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 = CompOption -> ExecOption -> String -> Regex
forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
compOpt -> execOpt -> source -> regex
makeRegexOpts CompOption
opt ExecOption
forall regex compOpt execOpt.
RegexOptions regex compOpt execOpt =>
execOpt
defaultExecOpt String
s
where
opt :: CompOption
opt = CompOption
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 = CompOption
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 CompOption -> ExecOption -> String -> Regex
forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
compOpt -> execOpt -> source -> regex
makeRegexOpts CompOption
opt ExecOption
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 = ((String, String, String, [String]) -> [String])
-> Maybe (String, String, String, [String]) -> Maybe [String]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String, String, String, [String]) -> [String]
go (Regex -> String -> Maybe (String, String, String, [String])
forall regex source target (m :: * -> *).
(RegexContext regex source target, MonadFail m) =>
regex -> source -> m target
forall (m :: * -> *).
MonadFail m =>
Regex -> String -> m (String, String, String, [String])
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