1 -----------------------------------------------------------------------------
3 -- Module : Text.Regex.Posix
4 -- Copyright : (c) The University of Glasgow 2002
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : portable
11 -- Interface to the POSIX regular expression library.
13 -----------------------------------------------------------------------------
15 -- ToDo: should have an interface using PackedStrings.
17 #include "HsBaseConfig.h"
19 #define HAVE_REGEX_H 1
20 #define HAVE_REGCOMP 1
23 module Text.Regex.Posix (
27 -- * Compiling a regular expression
28 regcomp, -- :: String -> Int -> IO Regex
30 -- ** Flags for regcomp
31 regExtended, -- (flag to regcomp) use extended regex syntax
32 regIgnoreCase, -- (flag to regcomp) ignore case when matching
33 regNewline, -- (flag to regcomp) '.' doesn't match newline
35 -- * Matching a regular expression
36 regexec, -- :: Regex -- pattern
37 -- -> String -- string to match
38 -- -> IO (Maybe (String, -- everything before match
39 -- String, -- matched portion
40 -- String, -- everything after match
41 -- [String])) -- subexpression matches
45 #include <sys/types.h>
47 #if HAVE_REGEX_H && HAVE_REGCOMP
50 #include "regex/regex.h"
52 -- CFILES stuff is Hugs only
53 {-# CFILES cbits/regex/reallocf.c #-}
54 {-# CFILES cbits/regex/regcomp.c #-}
55 {-# CFILES cbits/regex/regerror.c #-}
56 {-# CFILES cbits/regex/regexec.c #-}
57 {-# CFILES cbits/regex/regfree.c #-}
67 -- | A compiled regular expression
68 newtype Regex = Regex (ForeignPtr CRegex)
70 -- -----------------------------------------------------------------------------
73 -- | Compiles a regular expression
75 :: String -- ^ The regular expression to compile
76 -> Int -- ^ Flags (summed together)
77 -> IO Regex -- ^ Returns: the compiled regular expression
78 regcomp pattern flags = do
79 regex_fptr <- mallocForeignPtrBytes (#const sizeof(regex_t))
80 r <- withCString pattern $ \cstr ->
81 withForeignPtr regex_fptr $ \p ->
82 c_regcomp p cstr (fromIntegral flags)
84 then do addForeignPtrFinalizer ptr_regfree regex_fptr
85 return (Regex regex_fptr)
86 else error "Text.Regex.Posix.regcomp: error in pattern" -- ToDo
88 -- -----------------------------------------------------------------------------
91 -- | Matches a regular expression against a string
92 regexec :: Regex -- ^ Compiled regular expression
93 -> String -- ^ String to match against
94 -> IO (Maybe (String, String, String, [String]))
95 -- ^ Returns: 'Nothing' if the regex did not match the
99 -- 'Just' (everything before match,
101 -- everything after match,
102 -- subexpression matches)
105 regexec (Regex regex_fptr) str = do
106 withCString str $ \cstr -> do
107 withForeignPtr regex_fptr $ \regex_ptr -> do
108 nsub <- (#peek regex_t, re_nsub) regex_ptr
109 let nsub_int = fromIntegral (nsub :: CSize)
110 allocaBytes ((1 + nsub_int) * (#const sizeof(regmatch_t))) $ \p_match -> do
111 -- add one because index zero covers the whole match
112 r <- c_regexec regex_ptr cstr (1 + nsub) p_match 0{-no flags for now-}
114 if (r /= 0) then return Nothing else do
116 (before,match,after) <- matched_parts str p_match
119 mapM (unpack str) $ take nsub_int $ tail $
120 iterate (`plusPtr` (#const sizeof(regmatch_t))) p_match
122 return (Just (before, match, after, sub_strs))
124 matched_parts :: String -> Ptr CRegMatch -> IO (String, String, String)
125 matched_parts string p_match = do
126 start <- (#peek regmatch_t, rm_so) p_match :: IO (#type regoff_t)
127 end <- (#peek regmatch_t, rm_eo) p_match :: IO (#type regoff_t)
128 let s = fromIntegral start; e = fromIntegral end
129 return ( take s string,
130 take (e-s) (drop s string),
133 unpack :: String -> Ptr CRegMatch -> IO (String)
134 unpack string p_match = do
135 start <- (#peek regmatch_t, rm_so) p_match :: IO (#type regoff_t)
136 end <- (#peek regmatch_t, rm_eo) p_match :: IO (#type regoff_t)
137 -- the subexpression may not have matched at all, perhaps because it
138 -- was optional. In this case, the offsets are set to -1.
139 if (start == -1) then return "" else do
140 return (take (fromIntegral (end-start)) (drop (fromIntegral start) string))
142 -- -----------------------------------------------------------------------------
143 -- The POSIX regex C interface
150 -- Return values from regexec
158 regIgnoreCase = REG_ICASE, \
162 -- Error codes from regcomp
179 -- GHC and Hugs get the appropriate include file from the OPTIONS
180 -- pragma generated by hsc2hs from the above #include.
181 -- Implementations following the FFI spec have to specify it in the
182 -- foreign import, which is awkward because some systems provide
183 -- regex.h and the rest of the regex library, but otherwise we
184 -- need to use our own copy, regex/regex.h.
186 #if __GLASGOW_HASKELL__ || __HUGS__
187 foreign import ccall unsafe "regcomp"
188 c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt
190 foreign import ccall unsafe "®free"
191 ptr_regfree :: FunPtr (Ptr CRegex -> IO ())
193 foreign import ccall unsafe "regexec"
194 c_regexec :: Ptr CRegex -> CString -> CSize
195 -> Ptr CRegMatch -> CInt -> IO CInt
196 #elif HAVE_REGEX_H && HAVE_REGCOMP
197 foreign import ccall unsafe "regex.h regcomp"
198 c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt
200 foreign import ccall unsafe "regex.h ®free"
201 ptr_regfree :: FunPtr (Ptr CRegex -> IO ())
203 foreign import ccall unsafe "regex.h regexec"
204 c_regexec :: Ptr CRegex -> CString -> CSize
205 -> Ptr CRegMatch -> CInt -> IO CInt
207 foreign import ccall unsafe "regex/regex.h regcomp"
208 c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt
210 foreign import ccall unsafe "regex/regex.h ®free"
211 ptr_regfree :: FunPtr (Ptr CRegex -> IO ())
213 foreign import ccall unsafe "regex/regex.h regexec"
214 c_regexec :: Ptr CRegex -> CString -> CSize
215 -> Ptr CRegMatch -> CInt -> IO CInt