summaryrefslogtreecommitdiff
path: root/ghc/lib/misc/RegexString.lhs
blob: 8bc98a5a02c9e2f14e753446f9d676e6d4431545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-----------------------------------------------------------------------------
RegexString.lhs

A simple high-level interface to Regex

(c) Simon Marlow 1997
-----------------------------------------------------------------------------

> module RegexString (Regex(..), mkRegex, matchRegex) where

> import Regex
> import PackedString
> import Array
> import GlaExts

> type Regex = PatBuffer
> 
> mkRegex :: String -> Regex
> mkRegex s = unsafePerformPrimIO (
> 	  re_compile_pattern (packString s) False False)
> 
> matchRegex :: Regex -> String -> Maybe [String]
> matchRegex p s = unsafePerformPrimIO (
> 	  re_match p str 0 True >>= \m ->
> 	  case m of
> 		  Nothing -> return Nothing
> 		  Just m  -> return (Just (matches m str))
> 	  )
>    where
>	  str = packString s
> 
> matches (REmatch arr _ _ _ _) s = 
> 	  [ unpackPS (substrPS s beg (end-1)) | 
> 		  index <- [1..], let (beg,end) = arr ! index ]