Skip to content

Commit

Permalink
latest pcre-light began to use pcre_free as finalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
lyokha committed Sep 13, 2023
1 parent 5424e8c commit 7e1f9c0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 40 deletions.
41 changes: 2 additions & 39 deletions NgxExport/Tools/PCRE.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{-# LANGUAGE TemplateHaskell, BangPatterns, OverloadedStrings #-}
{-# LANGUAGE ForeignFunctionInterface, CApiFFI #-}

{-# OPTIONS_GHC -fno-warn-dodgy-foreign-imports #-}

-----------------------------------------------------------------------------
-- |
Expand Down Expand Up @@ -43,16 +40,10 @@ import qualified Data.ByteString.Lazy as L
import Data.List
import Data.Maybe
import Data.IORef
import Text.Regex.PCRE.Light hiding (compile, compileM)
import Text.Regex.PCRE.Light.Base
import Text.Regex.PCRE.Heavy hiding (compileM)
import Text.Regex.PCRE.Light
import Text.Regex.PCRE.Heavy
import Control.Exception (Exception, throwIO)
import Control.Arrow
import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.C.String
import Foreign.Storable
import Foreign.Marshal.Alloc
import System.IO.Unsafe

-- $matchingPCRE
Expand Down Expand Up @@ -173,34 +164,6 @@ declareRegexes = ignitionService $ const $ return ""

ngxExportSimpleServiceTyped 'declareRegexes ''InputRegexes SingleShotService


{- SPLICE: compile with pcre_free finalizer, mostly adopted from pcre-light -}

foreign import capi "pcre.h value pcre_free" c_pcre_free' :: FinalizerPtr a

compile :: ByteString -> [PCREOption] -> Regex
compile s o = case compileM s o of
Right r -> r
Left e -> error ("Text.Regex.PCRE.Light: Error in regex: " ++ e)

compileM :: ByteString -> [PCREOption] -> Either String Regex
compileM str os = unsafePerformIO $
C8.useAsCString str $ \ptn ->
alloca $ \errptr ->
alloca $ \erroffset -> do
pcre_ptr <- c_pcre_compile ptn (combineOptions os)
errptr erroffset nullPtr
if pcre_ptr == nullPtr
then do
err <- peekCString =<< peek errptr
return (Left err)
else do
reg <- newForeignPtr c_pcre_free' pcre_ptr
return (Right (Regex reg str))

{- SPLICE: END -}


compileRegexes :: ByteString -> IO L.ByteString
compileRegexes = const $ do
!inputRegexes <- fromJust <$> readIORef storage_InputRegexes_declareRegexes
Expand Down
2 changes: 1 addition & 1 deletion ngx-export-tools-extra.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ library
, aeson >= 1.0.0.0 && < 2.0.0.0

if flag(PCRE)
build-depends: pcre-light >= 0.4
build-depends: pcre-light >= 0.4.1.2
, pcre-heavy

exposed-modules: NgxExport.Tools.Aggregate
Expand Down

0 comments on commit 7e1f9c0

Please sign in to comment.