Commits

Bryan O'Sullivan committed e3b2609

Work around ICU bug 8421 / text-icu bug #1.

We can't safely call uregex_setTimeLimit in ICU <= 4.6 unless we first supply
some dummy text to search against.

References:

https://bitbucket.org/bos/text-icu/issue/1
http://bugs.icu-project.org/trac/ticket/8421

Comments (0)

Files changed (1)

Data/Text/ICU/Regex/Internal.hsc

                                      handleError, handleParseError)
 import Data.Typeable (Typeable)
 import Data.Word (Word16, Word32)
-import Foreign.ForeignPtr (ForeignPtr, newForeignPtr, touchForeignPtr)
+import Foreign.ForeignPtr (ForeignPtr, newForeignPtr, touchForeignPtr, withForeignPtr)
 import Foreign.Ptr (FunPtr, Ptr)
 import Prelude hiding (catch)
 import System.IO.Unsafe (unsafePerformIO)
 
 -- | Compile a regular expression with the given options.  This
 -- function throws a 'ParseError' if the pattern is invalid.
+--
+-- The 'Regex' is initialized with empty text to search against.
 regex :: [MatchOption] -> Text -> IO Regex
 regex opts pat = T.useAsPtr pat $ \pptr plen -> do
   let (flags,workLimit,stackLimit) = toURegexpOpts opts
   ptr <- handleParseError isRegexError $
          uregex_open pptr (fromIntegral plen) flags
-  fp <- newForeignPtr uregex_close ptr
-  when (workLimit /= -1) .
+  refp <- newForeignPtr uregex_close ptr
+  (hayfp, hayLen) <- T.asForeignPtr T.empty
+  withForeignPtr refp $ \rePtr ->
+    withForeignPtr hayfp $ \hayPtr -> handleError $
+      uregex_setText rePtr hayPtr (fromIntegral hayLen)
+  when (workLimit > -1) .
     handleError $ uregex_setTimeLimit ptr (fromIntegral workLimit)
-  when (stackLimit /= -1) .
+  when (stackLimit > -1) .
     handleError $ uregex_setStackLimit ptr (fromIntegral stackLimit)
-  touchForeignPtr fp
-  Regex fp `fmap` newIORef emptyForeignPtr
+  touchForeignPtr refp
+  Regex refp `fmap` newIORef hayfp
 
 data URegularExpression