Bryan O'Sullivan avatar 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
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.