X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Text%2FRegex%2FPosix.hsc;h=1035518752b238f1a64d324b3f3631c55c640fec;hb=9fa9bc17072a58c0bae2cce4764d38677e96ac29;hp=2b2dc9b7f6bec93c83df566ce9634dfe3cee4b3e;hpb=1e93f575b6111cf00d2cd9d9a19f905002d1d4c8;p=ghc-base.git diff --git a/Text/Regex/Posix.hsc b/Text/Regex/Posix.hsc index 2b2dc9b..1035518 100644 --- a/Text/Regex/Posix.hsc +++ b/Text/Regex/Posix.hsc @@ -1,5 +1,5 @@ ----------------------------------------------------------------------------- --- +-- | -- Module : Text.Regex.Posix -- Copyright : (c) The University of Glasgow 2001 -- License : BSD-style (see the file libraries/core/LICENSE) @@ -8,10 +8,9 @@ -- Stability : experimental -- Portability : non-portable (only on platforms that provide POSIX regexps) -- --- $Id: Posix.hsc,v 1.1 2001/08/02 11:20:50 simonmar Exp $ +-- $Id: Posix.hsc,v 1.7 2002/04/24 16:31:47 simonmar Exp $ -- -- Interface to the POSIX regular expression library. --- ToDo: detect regex library with configure. -- ToDo: should have an interface using PackedStrings. -- ----------------------------------------------------------------------------- @@ -33,6 +32,7 @@ module Text.Regex.Posix ( regNewline -- (flag to regcomp) '.' doesn't match newline ) where +#include #include "regex.h" import Prelude @@ -49,11 +49,12 @@ regcomp :: String -> Int -> IO Regex regcomp pattern flags = do regex_ptr <- mallocBytes (#const sizeof(regex_t)) regex_fptr <- newForeignPtr regex_ptr (regfree regex_ptr) - withCString pattern $ \cstr -> do - r <- c_regcomp regex_fptr cstr (fromIntegral flags) - if (r == 0) - then return (Regex regex_fptr) - else error "Text.Regex.Posix.regcomp: error in pattern" -- ToDo + r <- withCString pattern $ \cstr -> + withForeignPtr regex_fptr $ \p -> + c_regcomp p cstr (fromIntegral flags) + if (r == 0) + then return (Regex regex_fptr) + else error "Text.Regex.Posix.regcomp: error in pattern" -- ToDo regfree :: Ptr CRegex -> IO () regfree p_regex = do @@ -71,22 +72,23 @@ regexec :: Regex -- pattern [String])) -- subexpression matches regexec (Regex regex_fptr) str = do - withUnsafeCString str $ \cstr -> do - nsub <- withForeignPtr regex_fptr $ \p -> (#peek regex_t, re_nsub) p - let nsub_int = fromIntegral (nsub :: CSize) - allocaBytes ((1 + nsub_int) * (#const sizeof(regmatch_t))) $ \p_match -> do + withCString str $ \cstr -> do + withForeignPtr regex_fptr $ \regex_ptr -> do + nsub <- (#peek regex_t, re_nsub) regex_ptr + let nsub_int = fromIntegral (nsub :: CSize) + allocaBytes ((1 + nsub_int) * (#const sizeof(regmatch_t))) $ \p_match -> do -- add one because index zero covers the whole match - r <- c_regexec regex_fptr cstr (1 + nsub) p_match 0{-no flags for now-} + r <- c_regexec regex_ptr cstr (1 + nsub) p_match 0{-no flags for now-} - if (r /= 0) then return Nothing else do + if (r /= 0) then return Nothing else do - (before,match,after) <- matched_parts str p_match + (before,match,after) <- matched_parts str p_match - sub_strs <- + sub_strs <- mapM (unpack str) $ take nsub_int $ tail $ iterate (`plusPtr` (#const sizeof(regmatch_t))) p_match - return (Just (before, match, after, sub_strs)) + return (Just (before, match, after, sub_strs)) matched_parts :: String -> Ptr CRegMatch -> IO (String, String, String) matched_parts string p_match = do @@ -144,12 +146,12 @@ unpack string p_match = do type CRegex = () type CRegMatch = () -foreign import "regcomp" unsafe - c_regcomp :: ForeignPtr CRegex -> CString -> CInt -> IO CInt +foreign import ccall unsafe "regcomp" + c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt -foreign import "regfree" unsafe +foreign import ccall unsafe "regfree" c_regfree :: Ptr CRegex -> IO () -foreign import "regexec" unsafe - c_regexec :: ForeignPtr CRegex -> UnsafeCString -> CSize +foreign import ccall unsafe "regexec" + c_regexec :: Ptr CRegex -> CString -> CSize -> Ptr CRegMatch -> CInt -> IO CInt