1
1
module Data.String.Regex (
2
2
Regex (..),
3
+ RegexFlags (..),
3
4
regex ,
5
+ source ,
6
+ flags ,
7
+ renderFlags ,
8
+ parseFlags ,
4
9
test ,
5
10
match ,
6
11
replace ,
7
12
replace' ,
8
13
search
9
14
) where
10
15
16
+ import Data.String (indexOf )
17
+
11
18
foreign import data Regex :: *
12
19
13
20
foreign import showRegex'
@@ -18,13 +25,57 @@ foreign import showRegex'
18
25
instance showRegex :: Show Regex where
19
26
show = showRegex'
20
27
21
- foreign import regex
22
- " function regex(s1) {\
28
+ type RegexFlags =
29
+ { global :: Boolean
30
+ , ignoreCase :: Boolean
31
+ , multiline :: Boolean
32
+ , sticky :: Boolean
33
+ , unicode :: Boolean
34
+ }
35
+
36
+ foreign import regex'
37
+ " function regex$prime(s1) {\
23
38
\ return function(s2) {\
24
39
\ return new RegExp(s1, s2);\
25
40
\ };\
26
41
\}" :: String -> String -> Regex
27
42
43
+ regex :: String -> RegexFlags -> Regex
44
+ regex source flags = regex' source $ renderFlags flags
45
+
46
+ foreign import source
47
+ " function source(r) {\
48
+ \ return r.source;\
49
+ \}" :: Regex -> String
50
+
51
+ foreign import flags
52
+ " function source(r) {\
53
+ \ return {\
54
+ \ multiline: r.multiline,\
55
+ \ ignoreCase: r.ignoreCase,\
56
+ \ global: r.global,\
57
+ \ sticky: !!r.sticky,\
58
+ \ unicode: !!r.unicode\
59
+ \ };\
60
+ \}" :: Regex -> RegexFlags
61
+
62
+ renderFlags :: RegexFlags -> String
63
+ renderFlags flags =
64
+ (if flags.global then " g" else " " ) ++
65
+ (if flags.ignoreCase then " i" else " " ) ++
66
+ (if flags.multiline then " m" else " " ) ++
67
+ (if flags.sticky then " y" else " " ) ++
68
+ (if flags.unicode then " u" else " " )
69
+
70
+ parseFlags :: String -> RegexFlags
71
+ parseFlags s =
72
+ { global: indexOf " g" s >= 0
73
+ , ignoreCase: indexOf " i" s >= 0
74
+ , multiline: indexOf " m" s >= 0
75
+ , sticky: indexOf " y" s >= 0
76
+ , unicode: indexOf " u" s >= 0
77
+ }
78
+
28
79
foreign import test
29
80
" function test(r) {\
30
81
\ return function (s) {\
0 commit comments