From: simonm Date: Mon, 26 Apr 1999 15:19:28 +0000 (+0000) Subject: [project @ 1999-04-26 15:19:28 by simonm] X-Git-Tag: Approximately_9120_patches~6306 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=0755a7d9a4ba807d3a5d47ac84224bd146882a08;p=ghc-hetmet.git [project @ 1999-04-26 15:19:28 by simonm] Add simple high-level interface to Regex (snaffled from my nofib log analyser). --- diff --git a/ghc/lib/misc/RegexString.lhs b/ghc/lib/misc/RegexString.lhs new file mode 100644 index 0000000..8bc98a5 --- /dev/null +++ b/ghc/lib/misc/RegexString.lhs @@ -0,0 +1,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 ]