[project @ 1999-04-26 15:19:28 by simonm]
[ghc-hetmet.git] / ghc / lib / misc / RegexString.lhs
1 -----------------------------------------------------------------------------
2 RegexString.lhs
3
4 A simple high-level interface to Regex
5
6 (c) Simon Marlow 1997
7 -----------------------------------------------------------------------------
8
9 > module RegexString (Regex(..), mkRegex, matchRegex) where
10
11 > import Regex
12 > import PackedString
13 > import Array
14 > import GlaExts
15
16 > type Regex = PatBuffer
17
18 > mkRegex :: String -> Regex
19 > mkRegex s = unsafePerformPrimIO (
20 >         re_compile_pattern (packString s) False False)
21
22 > matchRegex :: Regex -> String -> Maybe [String]
23 > matchRegex p s = unsafePerformPrimIO (
24 >         re_match p str 0 True >>= \m ->
25 >         case m of
26 >                 Nothing -> return Nothing
27 >                 Just m  -> return (Just (matches m str))
28 >         )
29 >    where
30 >         str = packString s
31
32 > matches (REmatch arr _ _ _ _) s = 
33 >         [ unpackPS (substrPS s beg (end-1)) | 
34 >                 index <- [1..], let (beg,end) = arr ! index ]