module Z.Data.Text.Regex
(
Regex, regex, RegexOpts(..), defaultRegexOpts, regexOpts
, escape, regexCaptureNum, regexPattern
, RegexException(..)
, test
, match
, replace
, extract
) where
import Z.Foreign
import Control.Exception
import Data.Int
import Data.Word
import GHC.Stack
import GHC.Generics
import Foreign.ForeignPtr
import Foreign.Marshal.Utils (fromBool)
import System.IO.Unsafe
import qualified Z.Data.Text.Base as T
import qualified Z.Data.Text.ShowT as T
import qualified Z.Data.Vector.Base as V
import qualified Z.Data.Array as A
data Regex = Regex
{ Regex -> ForeignPtr Regex
regexPtr :: {-# UNPACK #-} !(ForeignPtr Regex)
, Regex -> Int
regexCaptureNum :: {-# UNPACK #-} !Int
, Regex -> Text
regexPattern :: T.Text
} deriving (Int -> Regex -> ShowS
[Regex] -> ShowS
Regex -> String
(Int -> Regex -> ShowS)
-> (Regex -> String) -> ([Regex] -> ShowS) -> Show Regex
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Regex] -> ShowS
$cshowList :: [Regex] -> ShowS
show :: Regex -> String
$cshow :: Regex -> String
showsPrec :: Int -> Regex -> ShowS
$cshowsPrec :: Int -> Regex -> ShowS
Show, (forall x. Regex -> Rep Regex x)
-> (forall x. Rep Regex x -> Regex) -> Generic Regex
forall x. Rep Regex x -> Regex
forall x. Regex -> Rep Regex x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Regex x -> Regex
$cfrom :: forall x. Regex -> Rep Regex x
Generic)
deriving anyclass Int -> Regex -> Builder ()
(Int -> Regex -> Builder ()) -> ShowT Regex
forall a. (Int -> a -> Builder ()) -> ShowT a
toUTF8BuilderP :: Int -> Regex -> Builder ()
$ctoUTF8BuilderP :: Int -> Regex -> Builder ()
T.ShowT
data RegexOpts = RegexOpts
{ RegexOpts -> Bool
posix_syntax :: Bool
, RegexOpts -> Bool
longest_match :: Bool
, RegexOpts -> Int64
max_mem :: {-# UNPACK #-} !Int64
, RegexOpts -> Bool
literal :: Bool
, RegexOpts -> Bool
never_nl :: Bool
, RegexOpts -> Bool
dot_nl :: Bool
, RegexOpts -> Bool
never_capture :: Bool
, RegexOpts -> Bool
case_sensitive :: Bool
, RegexOpts -> Bool
perl_classes :: Bool
, RegexOpts -> Bool
word_boundary :: Bool
, RegexOpts -> Bool
one_line :: Bool
} deriving (RegexOpts -> RegexOpts -> Bool
(RegexOpts -> RegexOpts -> Bool)
-> (RegexOpts -> RegexOpts -> Bool) -> Eq RegexOpts
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RegexOpts -> RegexOpts -> Bool
$c/= :: RegexOpts -> RegexOpts -> Bool
== :: RegexOpts -> RegexOpts -> Bool
$c== :: RegexOpts -> RegexOpts -> Bool
Eq, Eq RegexOpts
Eq RegexOpts
-> (RegexOpts -> RegexOpts -> Ordering)
-> (RegexOpts -> RegexOpts -> Bool)
-> (RegexOpts -> RegexOpts -> Bool)
-> (RegexOpts -> RegexOpts -> Bool)
-> (RegexOpts -> RegexOpts -> Bool)
-> (RegexOpts -> RegexOpts -> RegexOpts)
-> (RegexOpts -> RegexOpts -> RegexOpts)
-> Ord RegexOpts
RegexOpts -> RegexOpts -> Bool
RegexOpts -> RegexOpts -> Ordering
RegexOpts -> RegexOpts -> RegexOpts
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: RegexOpts -> RegexOpts -> RegexOpts
$cmin :: RegexOpts -> RegexOpts -> RegexOpts
max :: RegexOpts -> RegexOpts -> RegexOpts
$cmax :: RegexOpts -> RegexOpts -> RegexOpts
>= :: RegexOpts -> RegexOpts -> Bool
$c>= :: RegexOpts -> RegexOpts -> Bool
> :: RegexOpts -> RegexOpts -> Bool
$c> :: RegexOpts -> RegexOpts -> Bool
<= :: RegexOpts -> RegexOpts -> Bool
$c<= :: RegexOpts -> RegexOpts -> Bool
< :: RegexOpts -> RegexOpts -> Bool
$c< :: RegexOpts -> RegexOpts -> Bool
compare :: RegexOpts -> RegexOpts -> Ordering
$ccompare :: RegexOpts -> RegexOpts -> Ordering
$cp1Ord :: Eq RegexOpts
Ord, Int -> RegexOpts -> ShowS
[RegexOpts] -> ShowS
RegexOpts -> String
(Int -> RegexOpts -> ShowS)
-> (RegexOpts -> String)
-> ([RegexOpts] -> ShowS)
-> Show RegexOpts
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RegexOpts] -> ShowS
$cshowList :: [RegexOpts] -> ShowS
show :: RegexOpts -> String
$cshow :: RegexOpts -> String
showsPrec :: Int -> RegexOpts -> ShowS
$cshowsPrec :: Int -> RegexOpts -> ShowS
Show, (forall x. RegexOpts -> Rep RegexOpts x)
-> (forall x. Rep RegexOpts x -> RegexOpts) -> Generic RegexOpts
forall x. Rep RegexOpts x -> RegexOpts
forall x. RegexOpts -> Rep RegexOpts x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RegexOpts x -> RegexOpts
$cfrom :: forall x. RegexOpts -> Rep RegexOpts x
Generic)
deriving anyclass Int -> RegexOpts -> Builder ()
(Int -> RegexOpts -> Builder ()) -> ShowT RegexOpts
forall a. (Int -> a -> Builder ()) -> ShowT a
toUTF8BuilderP :: Int -> RegexOpts -> Builder ()
$ctoUTF8BuilderP :: Int -> RegexOpts -> Builder ()
T.ShowT
defaultRegexOpts :: RegexOpts
defaultRegexOpts :: RegexOpts
defaultRegexOpts = Bool
-> Bool
-> Int64
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> RegexOpts
RegexOpts
Bool
False Bool
False Int64
hs_re2_kDefaultMaxMem
Bool
False Bool
False Bool
False Bool
False
Bool
True Bool
False Bool
False Bool
False
data RegexException = InvalidRegexPattern T.Text CallStack deriving Int -> RegexException -> ShowS
[RegexException] -> ShowS
RegexException -> String
(Int -> RegexException -> ShowS)
-> (RegexException -> String)
-> ([RegexException] -> ShowS)
-> Show RegexException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RegexException] -> ShowS
$cshowList :: [RegexException] -> ShowS
show :: RegexException -> String
$cshow :: RegexException -> String
showsPrec :: Int -> RegexException -> ShowS
$cshowsPrec :: Int -> RegexException -> ShowS
Show
instance Exception RegexException
regex :: HasCallStack => T.Text -> Regex
{-# NOINLINE regex #-}
regex :: Text -> Regex
regex Text
t = IO Regex -> Regex
forall a. IO a -> a
unsafePerformIO (IO Regex -> Regex) -> IO Regex -> Regex
forall a b. (a -> b) -> a -> b
$ do
Ptr Regex
r <- PrimVector Word8
-> (BA# Word8 -> Int -> Int -> IO (Ptr Regex)) -> IO (Ptr Regex)
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe (Text -> PrimVector Word8
T.getUTF8Bytes Text
t) BA# Word8 -> Int -> Int -> IO (Ptr Regex)
hs_re2_compile_pattern_default
CInt
ok <- Ptr Regex -> IO CInt
hs_re2_ok Ptr Regex
r
if CInt
ok CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0
then do
ForeignPtr Regex
p <- FinalizerPtr Regex -> Ptr Regex -> IO (ForeignPtr Regex)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr Regex
p_hs_re2_delete_pattern Ptr Regex
r
Int
n <- Ptr Regex -> IO Int
hs_num_capture_groups Ptr Regex
r
Regex -> IO Regex
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr Regex -> Int -> Text -> Regex
Regex ForeignPtr Regex
p Int
n Text
t)
else do
Ptr Regex -> IO ()
hs_re2_delete_pattern Ptr Regex
r
RegexException -> IO Regex
forall e a. Exception e => e -> IO a
throwIO (Text -> CallStack -> RegexException
InvalidRegexPattern Text
t CallStack
HasCallStack => CallStack
callStack)
regexOpts :: HasCallStack => RegexOpts -> T.Text -> Regex
{-# NOINLINE regexOpts #-}
regexOpts :: RegexOpts -> Text -> Regex
regexOpts RegexOpts{Bool
Int64
one_line :: Bool
word_boundary :: Bool
perl_classes :: Bool
case_sensitive :: Bool
never_capture :: Bool
dot_nl :: Bool
never_nl :: Bool
literal :: Bool
max_mem :: Int64
longest_match :: Bool
posix_syntax :: Bool
one_line :: RegexOpts -> Bool
word_boundary :: RegexOpts -> Bool
perl_classes :: RegexOpts -> Bool
case_sensitive :: RegexOpts -> Bool
never_capture :: RegexOpts -> Bool
dot_nl :: RegexOpts -> Bool
never_nl :: RegexOpts -> Bool
literal :: RegexOpts -> Bool
max_mem :: RegexOpts -> Int64
longest_match :: RegexOpts -> Bool
posix_syntax :: RegexOpts -> Bool
..} Text
t = IO Regex -> Regex
forall a. IO a -> a
unsafePerformIO (IO Regex -> Regex) -> IO Regex -> Regex
forall a b. (a -> b) -> a -> b
$ do
Ptr Regex
r <- PrimVector Word8
-> (BA# Word8 -> Int -> Int -> IO (Ptr Regex)) -> IO (Ptr Regex)
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe (Text -> PrimVector Word8
T.getUTF8Bytes Text
t) ((BA# Word8 -> Int -> Int -> IO (Ptr Regex)) -> IO (Ptr Regex))
-> (BA# Word8 -> Int -> Int -> IO (Ptr Regex)) -> IO (Ptr Regex)
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
p Int
o Int
l ->
BA# Word8
-> Int
-> Int
-> CBool
-> CBool
-> Int64
-> CBool
-> CBool
-> CBool
-> CBool
-> CBool
-> CBool
-> CBool
-> CBool
-> IO (Ptr Regex)
hs_re2_compile_pattern BA# Word8
p Int
o Int
l
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
posix_syntax )
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
longest_match )
Int64
max_mem
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
literal )
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
never_nl )
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
dot_nl )
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
never_capture )
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
case_sensitive)
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
perl_classes )
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
word_boundary )
(Bool -> CBool
forall a. Num a => Bool -> a
fromBool Bool
one_line )
CInt
ok <- Ptr Regex -> IO CInt
hs_re2_ok Ptr Regex
r
if CInt
ok CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0
then do
ForeignPtr Regex
p <- FinalizerPtr Regex -> Ptr Regex -> IO (ForeignPtr Regex)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr Regex
p_hs_re2_delete_pattern Ptr Regex
r
Int
n <- Ptr Regex -> IO Int
hs_num_capture_groups Ptr Regex
r
Regex -> IO Regex
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr Regex -> Int -> Text -> Regex
Regex ForeignPtr Regex
p Int
n Text
t)
else do
Ptr Regex -> IO ()
hs_re2_delete_pattern Ptr Regex
r
RegexException -> IO Regex
forall e a. Exception e => e -> IO a
throwIO (Text -> CallStack -> RegexException
InvalidRegexPattern Text
t CallStack
HasCallStack => CallStack
callStack)
escape :: T.Text -> T.Text
{-# INLINABLE escape #-}
escape :: Text -> Text
escape Text
t = PrimVector Word8 -> Text
T.Text (PrimVector Word8 -> Text)
-> (IO (Ptr StdString) -> PrimVector Word8)
-> IO (Ptr StdString)
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (PrimVector Word8) -> PrimVector Word8
forall a. IO a -> a
unsafePerformIO (IO (PrimVector Word8) -> PrimVector Word8)
-> (IO (Ptr StdString) -> IO (PrimVector Word8))
-> IO (Ptr StdString)
-> PrimVector Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Ptr StdString) -> IO (PrimVector Word8)
fromStdString (IO (Ptr StdString) -> Text) -> IO (Ptr StdString) -> Text
forall a b. (a -> b) -> a -> b
$
PrimVector Word8
-> (BA# Word8 -> Int -> Int -> IO (Ptr StdString))
-> IO (Ptr StdString)
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe (Text -> PrimVector Word8
T.getUTF8Bytes Text
t) BA# Word8 -> Int -> Int -> IO (Ptr StdString)
hs_re2_quote_meta
test :: Regex -> T.Text -> Bool
{-# INLINABLE test #-}
test :: Regex -> Text -> Bool
test (Regex ForeignPtr Regex
fp Int
_ Text
_) (T.Text PrimVector Word8
bs) = IO Bool -> Bool
forall a. IO a -> a
unsafePerformIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$ do
ForeignPtr Regex -> (Ptr Regex -> IO Bool) -> IO Bool
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Regex
fp ((Ptr Regex -> IO Bool) -> IO Bool)
-> (Ptr Regex -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Ptr Regex
p ->
PrimVector Word8 -> (BA# Word8 -> Int -> Int -> IO Bool) -> IO Bool
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe PrimVector Word8
bs ((BA# Word8 -> Int -> Int -> IO Bool) -> IO Bool)
-> (BA# Word8 -> Int -> Int -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
ba# Int
s Int
l -> do
CInt
r <- Ptr Regex -> BA# Word8 -> Int -> Int -> IO CInt
hs_re2_test Ptr Regex
p BA# Word8
ba# Int
s Int
l
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> IO Bool) -> Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$! CInt
r CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0
match :: Regex -> T.Text -> (T.Text, [Maybe T.Text], T.Text)
{-# INLINABLE match #-}
match :: Regex -> Text -> (Text, [Maybe Text], Text)
match (Regex ForeignPtr Regex
fp Int
n Text
_) t :: Text
t@(T.Text bs :: PrimVector Word8
bs@(V.PrimVector PrimArray Word8
ba Int
_ Int
_)) = IO (Text, [Maybe Text], Text) -> (Text, [Maybe Text], Text)
forall a. IO a -> a
unsafePerformIO (IO (Text, [Maybe Text], Text) -> (Text, [Maybe Text], Text))
-> IO (Text, [Maybe Text], Text) -> (Text, [Maybe Text], Text)
forall a b. (a -> b) -> a -> b
$ do
ForeignPtr Regex
-> (Ptr Regex -> IO (Text, [Maybe Text], Text))
-> IO (Text, [Maybe Text], Text)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Regex
fp ((Ptr Regex -> IO (Text, [Maybe Text], Text))
-> IO (Text, [Maybe Text], Text))
-> (Ptr Regex -> IO (Text, [Maybe Text], Text))
-> IO (Text, [Maybe Text], Text)
forall a b. (a -> b) -> a -> b
$ \ Ptr Regex
p ->
PrimVector Word8
-> (BA# Word8 -> Int -> Int -> IO (Text, [Maybe Text], Text))
-> IO (Text, [Maybe Text], Text)
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe PrimVector Word8
bs ((BA# Word8 -> Int -> Int -> IO (Text, [Maybe Text], Text))
-> IO (Text, [Maybe Text], Text))
-> (BA# Word8 -> Int -> Int -> IO (Text, [Maybe Text], Text))
-> IO (Text, [Maybe Text], Text)
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
ba# Int
s Int
l -> do
(PrimArray Int
starts, (PrimArray Int
lens, CInt
r)) <- Int
-> (MBA# Int -> IO (PrimArray Int, CInt))
-> IO (PrimArray Int, (PrimArray Int, CInt))
forall a b.
Prim a =>
Int -> (MBA# Int -> IO b) -> IO (PrimArray a, b)
allocPrimArrayUnsafe Int
n ((MBA# Int -> IO (PrimArray Int, CInt))
-> IO (PrimArray Int, (PrimArray Int, CInt)))
-> (MBA# Int -> IO (PrimArray Int, CInt))
-> IO (PrimArray Int, (PrimArray Int, CInt))
forall a b. (a -> b) -> a -> b
$ \ MBA# Int
p_starts ->
Int -> (MBA# Int -> IO CInt) -> IO (PrimArray Int, CInt)
forall a b.
Prim a =>
Int -> (MBA# Int -> IO b) -> IO (PrimArray a, b)
allocPrimArrayUnsafe Int
n ((MBA# Int -> IO CInt) -> IO (PrimArray Int, CInt))
-> (MBA# Int -> IO CInt) -> IO (PrimArray Int, CInt)
forall a b. (a -> b) -> a -> b
$ \ MBA# Int
p_ends ->
Ptr Regex
-> BA# Word8
-> Int
-> Int
-> Int
-> MBA# Int
-> MBA# Int
-> IO CInt
hs_re2_match Ptr Regex
p BA# Word8
ba# Int
s Int
l Int
n MBA# Int
p_starts MBA# Int
p_ends
if CInt
r CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0
then (Text, [Maybe Text], Text) -> IO (Text, [Maybe Text], Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
T.empty, [], Text
t)
else do
let !s0 :: Int
s0 = PrimArray Int -> Int -> Int
forall (arr :: * -> *) a. Arr arr a => arr a -> Int -> a
A.indexArr PrimArray Int
starts Int
0
!l0 :: Int
l0 = PrimArray Int -> Int -> Int
forall (arr :: * -> *) a. Arr arr a => arr a -> Int -> a
A.indexArr PrimArray Int
lens Int
0
caps :: [Maybe Text]
caps = ((Int -> Maybe Text) -> [Int] -> [Maybe Text]
forall a b. (a -> b) -> [a] -> [b]
map (\ !Int
i ->
let !s' :: Int
s' = PrimArray Int -> Int -> Int
forall (arr :: * -> *) a. Arr arr a => arr a -> Int -> a
A.indexArr PrimArray Int
starts Int
i
!l' :: Int
l' = PrimArray Int -> Int -> Int
forall (arr :: * -> *) a. Arr arr a => arr a -> Int -> a
A.indexArr PrimArray Int
lens Int
i
in if Int
l' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== -Int
1
then Maybe Text
forall a. Maybe a
Nothing
else (Text -> Maybe Text
forall a. a -> Maybe a
Just (PrimVector Word8 -> Text
T.Text (PrimArray Word8 -> Int -> Int -> PrimVector Word8
forall a. PrimArray a -> Int -> Int -> PrimVector a
V.PrimVector PrimArray Word8
ba Int
s' Int
l')))) [Int
1..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1])
(Text, [Maybe Text], Text) -> IO (Text, [Maybe Text], Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (PrimVector Word8 -> Text
T.Text (PrimArray Word8 -> Int -> Int -> PrimVector Word8
forall a. PrimArray a -> Int -> Int -> PrimVector a
V.PrimVector PrimArray Word8
ba Int
s0 Int
l0)
, [Maybe Text]
caps
, PrimVector Word8 -> Text
T.Text (PrimArray Word8 -> Int -> Int -> PrimVector Word8
forall a. PrimArray a -> Int -> Int -> PrimVector a
V.PrimVector PrimArray Word8
ba (Int
s0Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l0) (Int
sInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
s0Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
l0)))
replace :: Regex
-> Bool
-> T.Text
-> T.Text
-> T.Text
{-# INLINABLE replace #-}
replace :: Regex -> Bool -> Text -> Text -> Text
replace (Regex ForeignPtr Regex
fp Int
_ Text
_) Bool
g Text
inp Text
rew = PrimVector Word8 -> Text
T.Text (PrimVector Word8 -> Text)
-> (IO (PrimVector Word8) -> PrimVector Word8)
-> IO (PrimVector Word8)
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (PrimVector Word8) -> PrimVector Word8
forall a. IO a -> a
unsafePerformIO (IO (PrimVector Word8) -> Text) -> IO (PrimVector Word8) -> Text
forall a b. (a -> b) -> a -> b
$ do
ForeignPtr Regex
-> (Ptr Regex -> IO (PrimVector Word8)) -> IO (PrimVector Word8)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Regex
fp ((Ptr Regex -> IO (PrimVector Word8)) -> IO (PrimVector Word8))
-> (Ptr Regex -> IO (PrimVector Word8)) -> IO (PrimVector Word8)
forall a b. (a -> b) -> a -> b
$ \ Ptr Regex
p ->
PrimVector Word8
-> (BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8)
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe (Text -> PrimVector Word8
T.getUTF8Bytes Text
inp) ((BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8))
-> (BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8)
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
inpp Int
inpoff Int
inplen ->
PrimVector Word8
-> (BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8)
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe (Text -> PrimVector Word8
T.getUTF8Bytes Text
rew) ((BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8))
-> (BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8)
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
rewp Int
rewoff Int
rewlen ->
IO (Ptr StdString) -> IO (PrimVector Word8)
fromStdString ((if Bool
g then Ptr Regex
-> BA# Word8
-> Int
-> Int
-> BA# Word8
-> Int
-> Int
-> IO (Ptr StdString)
hs_re2_replace_g else Ptr Regex
-> BA# Word8
-> Int
-> Int
-> BA# Word8
-> Int
-> Int
-> IO (Ptr StdString)
hs_re2_replace)
Ptr Regex
p BA# Word8
inpp Int
inpoff Int
inplen BA# Word8
rewp Int
rewoff Int
rewlen)
extract :: Regex
-> T.Text
-> T.Text
-> T.Text
{-# INLINABLE extract #-}
(Regex ForeignPtr Regex
fp Int
_ Text
_) Text
inp Text
rew = PrimVector Word8 -> Text
T.Text (PrimVector Word8 -> Text)
-> (IO (PrimVector Word8) -> PrimVector Word8)
-> IO (PrimVector Word8)
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (PrimVector Word8) -> PrimVector Word8
forall a. IO a -> a
unsafePerformIO (IO (PrimVector Word8) -> Text) -> IO (PrimVector Word8) -> Text
forall a b. (a -> b) -> a -> b
$ do
ForeignPtr Regex
-> (Ptr Regex -> IO (PrimVector Word8)) -> IO (PrimVector Word8)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Regex
fp ((Ptr Regex -> IO (PrimVector Word8)) -> IO (PrimVector Word8))
-> (Ptr Regex -> IO (PrimVector Word8)) -> IO (PrimVector Word8)
forall a b. (a -> b) -> a -> b
$ \ Ptr Regex
p ->
PrimVector Word8
-> (BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8)
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe (Text -> PrimVector Word8
T.getUTF8Bytes Text
inp) ((BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8))
-> (BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8)
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
inpp Int
inpoff Int
inplen ->
PrimVector Word8
-> (BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8)
forall a b.
Prim a =>
PrimVector a -> (BA# Word8 -> Int -> Int -> IO b) -> IO b
withPrimVectorUnsafe (Text -> PrimVector Word8
T.getUTF8Bytes Text
rew) ((BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8))
-> (BA# Word8 -> Int -> Int -> IO (PrimVector Word8))
-> IO (PrimVector Word8)
forall a b. (a -> b) -> a -> b
$ \ BA# Word8
rewp Int
rewoff Int
rewlen ->
IO (Ptr StdString) -> IO (PrimVector Word8)
fromStdString (Ptr Regex
-> BA# Word8
-> Int
-> Int
-> BA# Word8
-> Int
-> Int
-> IO (Ptr StdString)
hs_re2_extract Ptr Regex
p BA# Word8
inpp Int
inpoff Int
inplen BA# Word8
rewp Int
rewoff Int
rewlen)
foreign import ccall unsafe hs_re2_compile_pattern_default :: BA# Word8 -> Int -> Int -> IO (Ptr Regex)
foreign import ccall unsafe hs_re2_compile_pattern
:: BA# Word8 -> Int -> Int
-> CBool
-> CBool
-> Int64
-> CBool
-> CBool
-> CBool
-> CBool
-> CBool
-> CBool
-> CBool
-> CBool
-> IO (Ptr Regex)
foreign import ccall unsafe "&hs_re2_delete_pattern" p_hs_re2_delete_pattern :: FinalizerPtr Regex
foreign import ccall unsafe hs_re2_delete_pattern :: Ptr Regex -> IO ()
foreign import ccall unsafe hs_re2_ok :: Ptr Regex -> IO CInt
foreign import ccall unsafe hs_num_capture_groups :: Ptr Regex -> IO Int
foreign import ccall unsafe hs_re2_quote_meta :: BA# Word8 -> Int -> Int -> IO (Ptr StdString)
foreign import ccall unsafe hs_re2_match :: Ptr Regex
-> BA# Word8
-> Int
-> Int
-> Int
-> MBA# Int
-> MBA# Int
-> IO CInt
foreign import ccall unsafe hs_re2_test :: Ptr Regex
-> BA# Word8
-> Int
-> Int
-> IO CInt
foreign import ccall unsafe hs_re2_replace :: Ptr Regex
-> BA# Word8
-> Int
-> Int
-> BA# Word8
-> Int
-> Int
-> IO (Ptr StdString)
foreign import ccall unsafe hs_re2_replace_g :: Ptr Regex
-> BA# Word8
-> Int
-> Int
-> BA# Word8
-> Int
-> Int
-> IO (Ptr StdString)
foreign import ccall unsafe :: Ptr Regex
-> BA# Word8
-> Int
-> Int
-> BA# Word8
-> Int
-> Int
-> IO (Ptr StdString)
foreign import ccall unsafe hs_re2_kDefaultMaxMem :: Int64