{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveDataTypeable #-}
module Text.RE.ZeInternals.Types.Capture
( Capture(..)
, hasCaptured
, capturePrefix
, captureSuffix
) where
-- | the matching of a single sub-expression against part of the source
-- text
data Capture a =
Capture
{ captureSource :: !a -- ^ the whole text that was searched
, capturedText :: !a -- ^ the text that was matched
, captureOffset :: !Int -- ^ the number of characters preceding the
-- match with -1 used if no text was captured
-- by the RE (not even the empty string)
, captureLength :: !Int -- ^ the number of chacter in the captured
-- sub-string
}
deriving (Show,Eq)
instance Functor Capture where
fmap f c@Capture{..} =
c
{ captureSource = f captureSource
, capturedText = f capturedText
}
-- | test if the capture has matched any text
hasCaptured :: Capture a -> Bool
hasCaptured = (>=0) . captureOffset
-- | returns the text preceding the match
capturePrefix :: Extract a => Capture a -> a
capturePrefix Capture{..} = before captureOffset captureSource
-- | returns the text after the match
captureSuffix :: Extract a => Capture a -> a
captureSuffix Capture{..} = after (captureOffset+captureLength) captureSource