module Text.Regex.TDFA.ByteString.Lazy(
Regex
,CompOption
,ExecOption
,compile
,execute
,regexec
) where
import Data.Array((!),elems)
import qualified Data.ByteString.Lazy.Char8 as L
import Text.Regex.Base(MatchArray,RegexContext(..),RegexMaker(..),RegexLike(..))
import Text.Regex.Base.Impl(polymatch,polymatchM)
import Text.Regex.TDFA.ReadRegex(parseRegex)
import Text.Regex.TDFA.String()
import Text.Regex.TDFA.TDFA(patternToDFA)
import Text.Regex.TDFA.MutRunLBS(findMatch,findMatchAll,countMatchAll)
import Text.Regex.TDFA.Wrap(Regex(..),CompOption,ExecOption)
instance RegexContext Regex L.ByteString L.ByteString where
match = polymatch
matchM = polymatchM
instance RegexMaker Regex CompOption ExecOption L.ByteString where
makeRegexOptsM c e source = makeRegexOptsM c e (L.unpack source)
instance RegexLike Regex L.ByteString where
matchOnce = findMatch
matchAll = findMatchAll
matchCount = countMatchAll
compile :: CompOption
-> ExecOption
-> L.ByteString
-> Either String Regex
compile compOpt execOpt bs =
case parseRegex (L.unpack bs) of
Left err -> Left ("parseRegex for Text.Regex.TDFA.ByteString failed:"++show err)
Right pattern ->
let (dfa,i,tags,groups) = patternToDFA compOpt pattern
in Right (Regex dfa i tags groups compOpt execOpt)
execute :: Regex
-> L.ByteString
-> Either String (Maybe MatchArray)
execute r bs = Right (matchOnce r bs)
regexec :: Regex
-> L.ByteString
-> Either String (Maybe (L.ByteString, L.ByteString, L.ByteString, [L.ByteString]))
regexec r bs =
case matchOnceText r bs of
Nothing -> Right (Nothing)
Just (pre,mt,post) ->
let main = fst (mt!0)
rest = map fst (tail (elems mt))
in Right (Just (pre,main,post,rest))