module Text.Regex.Posix.String(
Regex,
MatchOffset,
MatchLength,
ReturnCode,
WrapError,
unusedOffset,
compile,
regexec,
execute,
CompOption(CompOption),
compBlank,
compExtended,
compIgnoreCase,
compNoSub,
compNewline,
ExecOption(ExecOption),
execBlank,
execNotBOL,
execNotEOL
) where
import Data.Array(listArray, Array)
import Data.List(map, length, genericDrop, genericTake)
import Foreign.C.String(withCAString)
import System.IO.Unsafe(unsafePerformIO)
import Text.Regex.Base.RegexLike(RegexContext(..),RegexMaker(..),RegexLike(..),MatchOffset,MatchLength)
import Text.Regex.Posix.Wrap
import Text.Regex.Base.Impl(polymatch,polymatchM)
instance RegexContext Regex String String where
match = polymatch
matchM = polymatchM
unusedOffset :: Int
unusedOffset = fromIntegral unusedRegOffset
unwrap :: (Show e) => Either e v -> IO v
unwrap x = case x of Left err -> fail ("Text.Regex.Posix.String died: "++ show err)
Right v -> return v
instance RegexMaker Regex CompOption ExecOption String where
makeRegexOpts c e pattern = unsafePerformIO $
(compile c e pattern >>= unwrap)
instance RegexLike Regex String where
matchTest regex str = unsafePerformIO $ do
withCAString str (wrapTest regex) >>= unwrap
matchOnce regex str = unsafePerformIO $
execute regex str >>= unwrap
matchAll regex str = unsafePerformIO $
withCAString str (wrapMatchAll regex) >>= unwrap
matchCount regex str = unsafePerformIO $
withCAString str (wrapCount regex) >>= unwrap
compile :: CompOption
-> ExecOption
-> String
-> IO (Either WrapError Regex)
compile flags e pattern = withCAString pattern (wrapCompile flags e)
execute :: Regex
-> String
-> IO (Either WrapError (Maybe (Array Int (MatchOffset,MatchLength))))
execute regex str = do
maybeStartEnd <- withCAString str (wrapMatch regex)
case maybeStartEnd of
Right Nothing -> return (Right Nothing)
Right (Just parts) ->
return . Right . Just . listArray (0,pred (length parts))
. map (\(s,e)->(fromIntegral s, fromIntegral (es)))
$ parts
Left err -> return (Left err)
regexec :: Regex
-> String
-> IO (Either WrapError (Maybe (String, String, String, [String])))
regexec regex str = do
let getSub (start,stop) | start == unusedRegOffset = ""
| otherwise =
genericTake (stopstart) . genericDrop start $ str
matchedParts [] = (str,"","",[])
matchedParts (matchedStartStop@(start,stop):subStartStop) =
(genericTake start str
,getSub matchedStartStop
,genericDrop stop str
,map getSub subStartStop)
maybeStartEnd <- withCAString str (wrapMatch regex)
case maybeStartEnd of
Right Nothing -> return (Right Nothing)
Right (Just parts) -> return . Right . Just . matchedParts $ parts
Left err -> return (Left err)