diff options
author | Camil Staps | 2016-03-21 19:31:24 +0100 |
---|---|---|
committer | Camil Staps | 2016-03-21 19:42:59 +0100 |
commit | 58ea61a19cb9bd0f6c600ebbb643e209fdf9d7cb (patch) | |
tree | 864264245f5f1ec8d8af0162830afd9bb5a63473 /Regex.icl | |
parent | Initial commit (diff) |
Matching works
Diffstat (limited to 'Regex.icl')
-rw-r--r-- | Regex.icl | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Regex.icl b/Regex.icl new file mode 100644 index 0000000..87ab703 --- /dev/null +++ b/Regex.icl @@ -0,0 +1,39 @@ +implementation module Regex + +import StdEnv +import StdMaybe +import code from "regex.o" + +:: Regex = { ptr :: Int // pointer to a pcre2_code object + , str :: String // string representation + } + +instance toRegex String +where toRegex flags s + # r = c_compile s flags + | r == 0 = Nothing + | otherwise = Just {ptr=r, str=s} + where + c_compile :: !String !Int -> Int + c_compile reg flags = code { + ccall cleanregex_pcre2_compile "SI:p" + } + +instance fromRegex String where fromRegex {str} = str + +instance toString Regex where toString r = fromRegex r +instance fromString (Maybe Regex) where fromString s = toRegex zero s + +instance zero Flags where zero = 0 + +match :: !Regex !String -> Maybe Int +match {ptr} s +# res = match` ptr s +| res < 0 = Nothing +| otherwise = Just res +where + match` :: !Int !String -> Int + match` _ _ = code { + ccall cleanregex_match "pS:I" + } + |