aboutsummaryrefslogtreecommitdiff
path: root/Regex.icl
blob: e9ee1f504db161ff4701a6f6dcfb04affb09b28e (plain) (blame)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
implementation module Regex

import StdEnv
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
	# (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

freeRegex :: !Regex -> String
freeRegex {ptr,str} = free ptr str
where
	free :: !Int !String -> String
	free ptr pass = code {
		ccall cleanrgx_free "p:V:S"
	}

match :: !Regex !Flags !String -> Maybe Bool
match {ptr} flags s = case match` ptr flags s of
  0 = Just False
  1 = Just True
  _ = Nothing
where
	match` :: !Int !Int !String -> Int
	match` ptr flags s = code {
		ccall cleanrgx_exec "pIS:I"
	}