1 -----------------------------------------------------------------------------
3 -- Module : Text.Regex.Posix
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable (only on platforms that provide POSIX regexps)
11 -- Interface to the POSIX regular expression library.
12 -- ToDo: should have an interface using PackedStrings.
14 -----------------------------------------------------------------------------
16 module Text.Regex.Posix (
19 regcomp, -- :: String -> Int -> IO Regex
21 regexec, -- :: Regex -- pattern
22 -- -> String -- string to match
23 -- -> IO (Maybe (String, -- everything before match
24 -- String, -- matched portion
25 -- String, -- everything after match
26 -- [String])) -- subexpression matches
28 regExtended, -- (flag to regcomp) use extended regex syntax
29 regIgnoreCase, -- (flag to regcomp) ignore case when matching
30 regNewline -- (flag to regcomp) '.' doesn't match newline
33 #include <sys/types.h>
41 newtype Regex = Regex (ForeignPtr CRegex)
43 -- -----------------------------------------------------------------------------
46 regcomp :: String -> Int -> IO Regex
47 regcomp pattern flags = do
48 regex_ptr <- mallocBytes (#const sizeof(regex_t))
49 regex_fptr <- newForeignPtr regex_ptr (regfree regex_ptr)
50 r <- withCString pattern $ \cstr ->
51 withForeignPtr regex_fptr $ \p ->
52 c_regcomp p cstr (fromIntegral flags)
54 then return (Regex regex_fptr)
55 else error "Text.Regex.Posix.regcomp: error in pattern" -- ToDo
57 regfree :: Ptr CRegex -> IO ()
62 -- -----------------------------------------------------------------------------
65 regexec :: Regex -- pattern
66 -> String -- string to match
67 -> IO (Maybe (String, -- everything before match
68 String, -- matched portion
69 String, -- everything after match
70 [String])) -- subexpression matches
72 regexec (Regex regex_fptr) str = do
73 withCString str $ \cstr -> do
74 withForeignPtr regex_fptr $ \regex_ptr -> do
75 nsub <- (#peek regex_t, re_nsub) regex_ptr
76 let nsub_int = fromIntegral (nsub :: CSize)
77 allocaBytes ((1 + nsub_int) * (#const sizeof(regmatch_t))) $ \p_match -> do
78 -- add one because index zero covers the whole match
79 r <- c_regexec regex_ptr cstr (1 + nsub) p_match 0{-no flags for now-}
81 if (r /= 0) then return Nothing else do
83 (before,match,after) <- matched_parts str p_match
86 mapM (unpack str) $ take nsub_int $ tail $
87 iterate (`plusPtr` (#const sizeof(regmatch_t))) p_match
89 return (Just (before, match, after, sub_strs))
91 matched_parts :: String -> Ptr CRegMatch -> IO (String, String, String)
92 matched_parts string p_match = do
93 start <- (#peek regmatch_t, rm_so) p_match :: IO CInt
94 end <- (#peek regmatch_t, rm_eo) p_match :: IO CInt
95 let s = fromIntegral start; e = fromIntegral end
96 return ( take (s-1) string,
97 take (e-s) (drop s string),
100 unpack :: String -> Ptr CRegMatch -> IO (String)
101 unpack string p_match = do
102 start <- (#peek regmatch_t, rm_so) p_match :: IO CInt
103 end <- (#peek regmatch_t, rm_eo) p_match :: IO CInt
104 -- the subexpression may not have matched at all, perhaps because it
105 -- was optional. In this case, the offsets are set to -1.
106 if (start == -1) then return "" else do
107 return (take (fromIntegral (end-start)) (drop (fromIntegral start) string))
109 -- -----------------------------------------------------------------------------
110 -- The POSIX regex C interface
117 -- Return values from regexec
125 regIgnoreCase = REG_ICASE, \
129 -- Error codes from regcomp
147 foreign import ccall unsafe "regcomp"
148 c_regcomp :: Ptr CRegex -> CString -> CInt -> IO CInt
150 foreign import ccall unsafe "regfree"
151 c_regfree :: Ptr CRegex -> IO ()
153 foreign import ccall unsafe "regexec"
154 c_regexec :: Ptr CRegex -> CString -> CSize
155 -> Ptr CRegMatch -> CInt -> IO CInt