diff options
Diffstat (limited to 'Regex.icl')
-rw-r--r-- | Regex.icl | 50 |
1 files changed, 28 insertions, 22 deletions
@@ -1,39 +1,45 @@ implementation module Regex import StdEnv -import StdMaybe -import code from "regex.o" +import Data.Maybe +import code from "cleanregex.o" + +instance zero Flags where zero = 0 :: 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" - } +where + toRegex flags s + # (ok,r) = c_compile s flags + | ok <> 0 = Nothing + | r == 0 = Nothing + | otherwise = Just {ptr=r, str=s} + where + c_compile :: !String !Int -> (!Int,!Int) + c_compile reg flags = code { + ccall cleanrgx_compile "SI:VIp" + } 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 +freeRegex :: !Regex -> String +freeRegex {ptr,str} = free ptr str where - match` :: !Int !String -> Int - match` _ _ = code { - ccall cleanregex_match "pS:I" - } + free :: !Int !String -> String + free ptr pass = code { + ccall cleanrgx_free "p:V:S" + } +match :: !Regex !String -> Maybe Bool +match {ptr} s = case match` ptr s of 0 = Just False; 1 = Just True; _ = Nothing +where + match` :: !Int !String -> Int + match` ptr s = code { + ccall cleanrgx_exec "pS:I" + } |