{-# LANGUAGE BangPatterns, EmptyDataDecls, MagicHash, RecordWildCards,
ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.Text.ICU.Regex
(
MatchOption(..)
, ParseError(errError, errLine, errOffset)
, Regex
, regex
, regex'
, clone
, setText
, getUTextPtr
, pattern
, find
, findNext
, groupCount
, start
, end
, start_
, end_
) where
import Data.Text.ICU.Regex.Internal
import qualified Control.Exception as E
import Data.IORef (newIORef, readIORef, writeIORef)
import Data.Text (Text)
import Data.Text.ICU.Internal (asBool, UTextPtr, asUTextPtr, emptyUTextPtr, TextI, withUTextPtr, fromUCharPtr, newICUPtr)
import Data.Text.ICU.Error.Internal (ParseError(..), handleError)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Storable (peek)
import System.IO.Unsafe (unsafePerformIO)
instance Show Regex where
show :: Regex -> String
show Regex
re = String
"Regex " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Regex -> Text
pattern Regex
re)
regex' :: [MatchOption] -> Text -> IO (Either ParseError Regex)
regex' :: [MatchOption] -> Text -> IO (Either ParseError Regex)
regex' [MatchOption]
opts Text
pat = (forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` [MatchOption] -> Text -> IO Regex
regex [MatchOption]
opts Text
pat) forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` \(ParseError
err::ParseError) ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left ParseError
err)
setText :: Regex -> Text -> IO ()
setText :: Regex -> Text -> IO ()
setText Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
..} Text
t = do
UTextPtr
hayfp <- Text -> IO UTextPtr
asUTextPtr Text
t
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr URegularExpression
reRe forall a b. (a -> b) -> a -> b
$ \Ptr URegularExpression
rePtr ->
forall a. UTextPtr -> (Ptr UText -> IO a) -> IO a
withUTextPtr UTextPtr
hayfp forall a b. (a -> b) -> a -> b
$ \Ptr UText
hayPtr -> forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError forall a b. (a -> b) -> a -> b
$
Ptr URegularExpression -> Ptr UText -> Ptr UErrorCode -> IO ()
uregex_setUText Ptr URegularExpression
rePtr Ptr UText
hayPtr
forall a. IORef a -> a -> IO ()
writeIORef IORef UTextPtr
reText UTextPtr
hayfp
getUTextPtr :: Regex -> IO UTextPtr
getUTextPtr :: Regex -> IO UTextPtr
getUTextPtr Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
..} = forall a. IORef a -> IO a
readIORef IORef UTextPtr
reText
pattern :: Regex -> Text
pattern :: Regex -> Text
pattern Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
..} = forall a. IO a -> a
unsafePerformIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr URegularExpression
reRe forall a b. (a -> b) -> a -> b
$ \Ptr URegularExpression
rePtr ->
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr Int32
lenPtr -> do
Ptr UChar
textPtr <- forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError forall a b. (a -> b) -> a -> b
$ Ptr URegularExpression
-> Ptr Int32 -> Ptr UErrorCode -> IO (Ptr UChar)
uregex_pattern Ptr URegularExpression
rePtr Ptr Int32
lenPtr
(Ptr UChar -> TextI -> IO Text
fromUCharPtr Ptr UChar
textPtr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. Storable a => Ptr a -> IO a
peek Ptr Int32
lenPtr
find :: Regex -> TextI -> IO Bool
find :: Regex -> TextI -> IO Bool
find Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
..} TextI
n =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Integral a => a -> Bool
asBool forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr URegularExpression
reRe forall a b. (a -> b) -> a -> b
$ \Ptr URegularExpression
rePtr -> forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError forall a b. (a -> b) -> a -> b
$
Ptr URegularExpression -> Int32 -> Ptr UErrorCode -> IO UBool
uregex_find Ptr URegularExpression
rePtr (forall a b. (Integral a, Num b) => a -> b
fromIntegral TextI
n)
findNext :: Regex -> IO Bool
findNext :: Regex -> IO Bool
findNext Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
..} =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Integral a => a -> Bool
asBool forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr URegularExpression
reRe forall a b. (a -> b) -> a -> b
$ forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr URegularExpression -> Ptr UErrorCode -> IO UBool
uregex_findNext
clone :: Regex -> IO Regex
{-# INLINE clone #-}
clone :: Regex -> IO Regex
clone Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
..} = do
forall a i.
(ForeignPtr a -> i) -> FinalizerPtr a -> IO (Ptr a) -> IO i
newICUPtr ForeignPtr URegularExpression -> IORef UTextPtr -> Regex
Regex FunPtr (Ptr URegularExpression -> IO ())
uregex_close
(forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr URegularExpression
reRe (forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr URegularExpression
-> Ptr UErrorCode -> IO (Ptr URegularExpression)
uregex_clone))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>
forall a. a -> IO (IORef a)
newIORef UTextPtr
emptyUTextPtr
groupCount :: Regex -> IO Int
groupCount :: Regex -> IO Int
groupCount Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
..} =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr URegularExpression
reRe forall a b. (a -> b) -> a -> b
$ forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr URegularExpression -> Ptr UErrorCode -> IO Int32
uregex_groupCount
start_ :: Regex -> Int -> IO TextI
start_ :: Regex -> Int -> IO TextI
start_ Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
..} Int
n =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr URegularExpression
reRe forall a b. (a -> b) -> a -> b
$ \Ptr URegularExpression
rePtr -> forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError forall a b. (a -> b) -> a -> b
$
Ptr URegularExpression -> Int32 -> Ptr UErrorCode -> IO Int32
uregex_start Ptr URegularExpression
rePtr (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
end_ :: Regex -> Int -> IO TextI
end_ :: Regex -> Int -> IO TextI
end_ Regex{ForeignPtr URegularExpression
IORef UTextPtr
reText :: IORef UTextPtr
reRe :: ForeignPtr URegularExpression
reText :: Regex -> IORef UTextPtr
reRe :: Regex -> ForeignPtr URegularExpression
..} Int
n =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr URegularExpression
reRe forall a b. (a -> b) -> a -> b
$ \Ptr URegularExpression
rePtr -> forall a. (Ptr UErrorCode -> IO a) -> IO a
handleError forall a b. (a -> b) -> a -> b
$
Ptr URegularExpression -> Int32 -> Ptr UErrorCode -> IO Int32
uregex_end Ptr URegularExpression
rePtr (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
start :: Regex -> Int -> IO (Maybe TextI)
start :: Regex -> Int -> IO (Maybe TextI)
start Regex
r Int
n = TextI -> Maybe TextI
check forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Regex -> Int -> IO TextI
start_ Regex
r Int
n
end :: Regex -> Int -> IO (Maybe TextI)
end :: Regex -> Int -> IO (Maybe TextI)
end Regex
r Int
n = TextI -> Maybe TextI
check forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Regex -> Int -> IO TextI
end_ Regex
r Int
n
check :: TextI -> Maybe TextI
check :: TextI -> Maybe TextI
check (-1) = forall a. Maybe a
Nothing
check TextI
k = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! forall a b. (Integral a, Num b) => a -> b
fromIntegral TextI
k