{-# OPTIONS_GHC -fno-warn-orphans #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Text.Regex.Posix.String
-- Copyright   :  (c) Chris Kuklewicz 2006
-- License     :  BSD-3-Clause
--
-- Maintainer  :  hvr@gnu.org, Andreas Abel
-- Stability   :  stable
-- Portability :  non-portable (regex-base needs MPTC+FD)
--
-- This provides 'String' instances for 'RegexMaker' and 'RegexLike' based
-- on "Text.Regex.Posix.Wrap", and a ('RegexContext' 'Regex' 'String' 'String')
-- instance.
--
-- To use these instance, you would normally import
-- "Text.Regex.Posix".  You only need to import this module to use
-- the medium level API of the compile, regexec, and execute
-- functions.  All of these report error by returning Left values
-- instead of undefined or error or fail.
--
-----------------------------------------------------------------------------

module Text.Regex.Posix.String(
  -- ** Types
  Regex,
  MatchOffset,
  MatchLength,
  ReturnCode,
  WrapError,
  -- ** Miscellaneous
  unusedOffset,
  -- ** Medium level API functions
  compile,
  regexec,
  execute,
  -- ** Compilation options
  CompOption(CompOption),
  compBlank,
  compExtended,   -- use extended regex syntax
  compIgnoreCase, -- ignore case when matching
  compNoSub,      -- no substring matching needed
  compNewline,    -- '.' doesn't match newline
  -- ** Execution options
  ExecOption(ExecOption),
  execBlank,
  execNotBOL,     -- not at begining of line
  execNotEOL     -- not at end of line
  ) where

import Prelude hiding (fail)
import Control.Monad.Fail (MonadFail(fail))

import Data.Array(listArray, Array)
import Data.List(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 :: Regex -> String -> String
match = Regex -> String -> String
forall a b. RegexLike a b => a -> b -> b
polymatch
  matchM :: forall (m :: * -> *). MonadFail m => Regex -> String -> m String
matchM = Regex -> String -> m String
forall a b (m :: * -> *).
(RegexLike a b, MonadFail m) =>
a -> b -> m b
polymatchM

unusedOffset :: Int
unusedOffset :: Int
unusedOffset = RegOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral RegOffset
unusedRegOffset

unwrap :: (Show e) => Either e v -> IO v
unwrap :: forall e v. Show e => Either e v -> IO v
unwrap Either e v
x = case Either e v
x of Left e
err -> String -> IO v
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"Text.Regex.Posix.String died: "String -> String -> String
forall a. [a] -> [a] -> [a]
++ e -> String
forall a. Show a => a -> String
show e
err)
                     Right v
v -> v -> IO v
forall (m :: * -> *) a. Monad m => a -> m a
return v
v

instance RegexMaker Regex CompOption ExecOption String where
  makeRegexOpts :: CompOption -> ExecOption -> String -> Regex
makeRegexOpts CompOption
c ExecOption
e String
pattern = IO Regex -> Regex
forall a. IO a -> a
unsafePerformIO (IO Regex -> Regex) -> IO Regex -> Regex
forall a b. (a -> b) -> a -> b
$
    (CompOption -> ExecOption -> String -> IO (Either WrapError Regex)
compile CompOption
c ExecOption
e String
pattern IO (Either WrapError Regex)
-> (Either WrapError Regex -> IO Regex) -> IO Regex
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either WrapError Regex -> IO Regex
forall e v. Show e => Either e v -> IO v
unwrap)
  makeRegexOptsM :: forall (m :: * -> *).
MonadFail m =>
CompOption -> ExecOption -> String -> m Regex
makeRegexOptsM CompOption
c ExecOption
e String
pattern = (WrapError -> m Regex)
-> (Regex -> m Regex) -> Either WrapError Regex -> m Regex
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> m Regex
forall (m :: * -> *) a. MonadFail m => String -> m a
fail(String -> m Regex)
-> (WrapError -> String) -> WrapError -> m Regex
forall b c a. (b -> c) -> (a -> b) -> a -> c
.WrapError -> String
forall a. Show a => a -> String
show) Regex -> m Regex
forall (m :: * -> *) a. Monad m => a -> m a
return (Either WrapError Regex -> m Regex)
-> Either WrapError Regex -> m Regex
forall a b. (a -> b) -> a -> b
$ IO (Either WrapError Regex) -> Either WrapError Regex
forall a. IO a -> a
unsafePerformIO (IO (Either WrapError Regex) -> Either WrapError Regex)
-> IO (Either WrapError Regex) -> Either WrapError Regex
forall a b. (a -> b) -> a -> b
$
    (CompOption -> ExecOption -> String -> IO (Either WrapError Regex)
compile CompOption
c ExecOption
e String
pattern)

instance RegexLike Regex String where
  matchTest :: Regex -> String -> Bool
matchTest Regex
regex String
str = IO Bool -> Bool
forall a. IO a -> a
unsafePerformIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$ do
    String
-> (CString -> IO (Either WrapError Bool))
-> IO (Either WrapError Bool)
forall a. String -> (CString -> IO a) -> IO a
withCAString String
str (Regex -> CString -> IO (Either WrapError Bool)
wrapTest Regex
regex) IO (Either WrapError Bool)
-> (Either WrapError Bool -> IO Bool) -> IO Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either WrapError Bool -> IO Bool
forall e v. Show e => Either e v -> IO v
unwrap
  matchOnce :: Regex -> String -> Maybe MatchArray
matchOnce Regex
regex String
str = IO (Maybe MatchArray) -> Maybe MatchArray
forall a. IO a -> a
unsafePerformIO (IO (Maybe MatchArray) -> Maybe MatchArray)
-> IO (Maybe MatchArray) -> Maybe MatchArray
forall a b. (a -> b) -> a -> b
$
    Regex -> String -> IO (Either WrapError (Maybe MatchArray))
execute Regex
regex String
str IO (Either WrapError (Maybe MatchArray))
-> (Either WrapError (Maybe MatchArray) -> IO (Maybe MatchArray))
-> IO (Maybe MatchArray)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either WrapError (Maybe MatchArray) -> IO (Maybe MatchArray)
forall e v. Show e => Either e v -> IO v
unwrap
  matchAll :: Regex -> String -> [MatchArray]
matchAll Regex
regex String
str = IO [MatchArray] -> [MatchArray]
forall a. IO a -> a
unsafePerformIO (IO [MatchArray] -> [MatchArray])
-> IO [MatchArray] -> [MatchArray]
forall a b. (a -> b) -> a -> b
$
    String
-> (CString -> IO (Either WrapError [MatchArray]))
-> IO (Either WrapError [MatchArray])
forall a. String -> (CString -> IO a) -> IO a
withCAString String
str (Regex -> CString -> IO (Either WrapError [MatchArray])
wrapMatchAll Regex
regex) IO (Either WrapError [MatchArray])
-> (Either WrapError [MatchArray] -> IO [MatchArray])
-> IO [MatchArray]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either WrapError [MatchArray] -> IO [MatchArray]
forall e v. Show e => Either e v -> IO v
unwrap
  matchCount :: Regex -> String -> Int
matchCount Regex
regex String
str = IO Int -> Int
forall a. IO a -> a
unsafePerformIO (IO Int -> Int) -> IO Int -> Int
forall a b. (a -> b) -> a -> b
$
    String
-> (CString -> IO (Either WrapError Int))
-> IO (Either WrapError Int)
forall a. String -> (CString -> IO a) -> IO a
withCAString String
str (Regex -> CString -> IO (Either WrapError Int)
wrapCount Regex
regex) IO (Either WrapError Int)
-> (Either WrapError Int -> IO Int) -> IO Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either WrapError Int -> IO Int
forall e v. Show e => Either e v -> IO v
unwrap

-- compile
compile  :: CompOption -- ^ Flags (summed together)
         -> ExecOption -- ^ Flags (summed together)
         -> String     -- ^ The regular expression to compile (ASCII only, no null bytes)
         -> IO (Either WrapError Regex) -- ^ Returns: the compiled regular expression
compile :: CompOption -> ExecOption -> String -> IO (Either WrapError Regex)
compile CompOption
flags ExecOption
e String
pattern =  String
-> (CString -> IO (Either WrapError Regex))
-> IO (Either WrapError Regex)
forall a. String -> (CString -> IO a) -> IO a
withCAString String
pattern (CompOption -> ExecOption -> CString -> IO (Either WrapError Regex)
wrapCompile CompOption
flags ExecOption
e)

-- -----------------------------------------------------------------------------
-- regexec

-- | Matches a regular expression against a string
execute :: Regex      -- ^ Compiled regular expression
        -> String     -- ^ String to match against
        -> IO (Either WrapError (Maybe (Array Int (MatchOffset,MatchLength))))
                -- ^ Returns: 'Nothing' if the regex did not match the
                -- string, or:
                --
                -- @
                --   'Just' (array of offset length pairs)
                -- @
execute :: Regex -> String -> IO (Either WrapError (Maybe MatchArray))
execute Regex
regex String
str = do
  Either WrapError (Maybe [(RegOffset, RegOffset)])
maybeStartEnd <- String
-> (CString
    -> IO (Either WrapError (Maybe [(RegOffset, RegOffset)])))
-> IO (Either WrapError (Maybe [(RegOffset, RegOffset)]))
forall a. String -> (CString -> IO a) -> IO a
withCAString String
str (Regex
-> CString
-> IO (Either WrapError (Maybe [(RegOffset, RegOffset)]))
wrapMatch Regex
regex)
  case Either WrapError (Maybe [(RegOffset, RegOffset)])
maybeStartEnd of
    Right Maybe [(RegOffset, RegOffset)]
Nothing -> Either WrapError (Maybe MatchArray)
-> IO (Either WrapError (Maybe MatchArray))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe MatchArray -> Either WrapError (Maybe MatchArray)
forall a b. b -> Either a b
Right Maybe MatchArray
forall a. Maybe a
Nothing)
--  Right (Just []) ->  fail "got [] back!" -- return wierd array instead
    Right (Just [(RegOffset, RegOffset)]
parts) ->
      Either WrapError (Maybe MatchArray)
-> IO (Either WrapError (Maybe MatchArray))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either WrapError (Maybe MatchArray)
 -> IO (Either WrapError (Maybe MatchArray)))
-> ([(RegOffset, RegOffset)]
    -> Either WrapError (Maybe MatchArray))
-> [(RegOffset, RegOffset)]
-> IO (Either WrapError (Maybe MatchArray))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe MatchArray -> Either WrapError (Maybe MatchArray)
forall a b. b -> Either a b
Right (Maybe MatchArray -> Either WrapError (Maybe MatchArray))
-> ([(RegOffset, RegOffset)] -> Maybe MatchArray)
-> [(RegOffset, RegOffset)]
-> Either WrapError (Maybe MatchArray)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatchArray -> Maybe MatchArray
forall a. a -> Maybe a
Just (MatchArray -> Maybe MatchArray)
-> ([(RegOffset, RegOffset)] -> MatchArray)
-> [(RegOffset, RegOffset)]
-> Maybe MatchArray
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, Int) -> [(Int, Int)] -> MatchArray
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0,Int -> Int
forall a. Enum a => a -> a
pred ([(RegOffset, RegOffset)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(RegOffset, RegOffset)]
parts))
       ([(Int, Int)] -> MatchArray)
-> ([(RegOffset, RegOffset)] -> [(Int, Int)])
-> [(RegOffset, RegOffset)]
-> MatchArray
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((RegOffset, RegOffset) -> (Int, Int))
-> [(RegOffset, RegOffset)] -> [(Int, Int)]
forall a b. (a -> b) -> [a] -> [b]
map (\(RegOffset
s,RegOffset
e)->(RegOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral RegOffset
s, RegOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (RegOffset
eRegOffset -> RegOffset -> RegOffset
forall a. Num a => a -> a -> a
-RegOffset
s)))
       ([(RegOffset, RegOffset)]
 -> IO (Either WrapError (Maybe MatchArray)))
-> [(RegOffset, RegOffset)]
-> IO (Either WrapError (Maybe MatchArray))
forall a b. (a -> b) -> a -> b
$ [(RegOffset, RegOffset)]
parts
    Left WrapError
err -> Either WrapError (Maybe MatchArray)
-> IO (Either WrapError (Maybe MatchArray))
forall (m :: * -> *) a. Monad m => a -> m a
return (WrapError -> Either WrapError (Maybe MatchArray)
forall a b. a -> Either a b
Left WrapError
err)

-- -----------------------------------------------------------------------------
-- regexec

-- | Matches a regular expression against a string
regexec :: Regex      -- ^ Compiled regular expression
        -> String     -- ^ String to match against
        -> IO (Either WrapError (Maybe (String, String, String, [String])))
                -- ^ Returns: 'Nothing' if the regex did not match the
                -- string, or:
                --
                -- @
                --   'Just' (everything before match,
                --         matched portion,
                --         everything after match,
                --         subexpression matches)
                -- @
regexec :: Regex
-> String
-> IO (Either WrapError (Maybe (String, String, String, [String])))
regexec Regex
regex String
str = do
  let getSub :: (RegOffset, RegOffset) -> String
getSub (RegOffset
start,RegOffset
stop) | RegOffset
start RegOffset -> RegOffset -> Bool
forall a. Eq a => a -> a -> Bool
== RegOffset
unusedRegOffset = String
""
                          | Bool
otherwise =
        RegOffset -> String -> String
forall i a. Integral i => i -> [a] -> [a]
genericTake (RegOffset
stopRegOffset -> RegOffset -> RegOffset
forall a. Num a => a -> a -> a
-RegOffset
start) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegOffset -> String -> String
forall i a. Integral i => i -> [a] -> [a]
genericDrop RegOffset
start (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
str
      matchedParts :: [(RegOffset, RegOffset)] -> (String, String, String, [String])
matchedParts [] = (String
str,String
"",String
"",[]) -- no information
      matchedParts (matchedStartStop :: (RegOffset, RegOffset)
matchedStartStop@(RegOffset
start,RegOffset
stop):[(RegOffset, RegOffset)]
subStartStop) =
        (RegOffset -> String -> String
forall i a. Integral i => i -> [a] -> [a]
genericTake RegOffset
start String
str
        ,(RegOffset, RegOffset) -> String
getSub (RegOffset, RegOffset)
matchedStartStop
        ,RegOffset -> String -> String
forall i a. Integral i => i -> [a] -> [a]
genericDrop RegOffset
stop String
str
        ,((RegOffset, RegOffset) -> String)
-> [(RegOffset, RegOffset)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (RegOffset, RegOffset) -> String
getSub [(RegOffset, RegOffset)]
subStartStop)
  Either WrapError (Maybe [(RegOffset, RegOffset)])
maybeStartEnd <- String
-> (CString
    -> IO (Either WrapError (Maybe [(RegOffset, RegOffset)])))
-> IO (Either WrapError (Maybe [(RegOffset, RegOffset)]))
forall a. String -> (CString -> IO a) -> IO a
withCAString String
str (Regex
-> CString
-> IO (Either WrapError (Maybe [(RegOffset, RegOffset)]))
wrapMatch Regex
regex)
  case Either WrapError (Maybe [(RegOffset, RegOffset)])
maybeStartEnd of
    Right Maybe [(RegOffset, RegOffset)]
Nothing -> Either WrapError (Maybe (String, String, String, [String]))
-> IO (Either WrapError (Maybe (String, String, String, [String])))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (String, String, String, [String])
-> Either WrapError (Maybe (String, String, String, [String]))
forall a b. b -> Either a b
Right Maybe (String, String, String, [String])
forall a. Maybe a
Nothing)
    Right (Just [(RegOffset, RegOffset)]
parts) -> Either WrapError (Maybe (String, String, String, [String]))
-> IO (Either WrapError (Maybe (String, String, String, [String])))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either WrapError (Maybe (String, String, String, [String]))
 -> IO
      (Either WrapError (Maybe (String, String, String, [String]))))
-> ([(RegOffset, RegOffset)]
    -> Either WrapError (Maybe (String, String, String, [String])))
-> [(RegOffset, RegOffset)]
-> IO (Either WrapError (Maybe (String, String, String, [String])))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (String, String, String, [String])
-> Either WrapError (Maybe (String, String, String, [String]))
forall a b. b -> Either a b
Right (Maybe (String, String, String, [String])
 -> Either WrapError (Maybe (String, String, String, [String])))
-> ([(RegOffset, RegOffset)]
    -> Maybe (String, String, String, [String]))
-> [(RegOffset, RegOffset)]
-> Either WrapError (Maybe (String, String, String, [String]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, String, String, [String])
-> Maybe (String, String, String, [String])
forall a. a -> Maybe a
Just ((String, String, String, [String])
 -> Maybe (String, String, String, [String]))
-> ([(RegOffset, RegOffset)] -> (String, String, String, [String]))
-> [(RegOffset, RegOffset)]
-> Maybe (String, String, String, [String])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(RegOffset, RegOffset)] -> (String, String, String, [String])
matchedParts ([(RegOffset, RegOffset)]
 -> IO
      (Either WrapError (Maybe (String, String, String, [String]))))
-> [(RegOffset, RegOffset)]
-> IO (Either WrapError (Maybe (String, String, String, [String])))
forall a b. (a -> b) -> a -> b
$ [(RegOffset, RegOffset)]
parts
    Left WrapError
err -> Either WrapError (Maybe (String, String, String, [String]))
-> IO (Either WrapError (Maybe (String, String, String, [String])))
forall (m :: * -> *) a. Monad m => a -> m a
return (WrapError
-> Either WrapError (Maybe (String, String, String, [String]))
forall a b. a -> Either a b
Left WrapError
err)