1
1
use std:: sync:: { Arc , RwLock } ;
2
2
3
- use cirru_edn:: { Edn , EdnAnyRef , EdnListView } ;
3
+ use cirru_edn:: { DynEq , Edn , EdnAnyRef , EdnListView } ;
4
4
use regex:: Regex ;
5
5
6
+ #[ derive( Debug ) ]
7
+ struct RegexWrapper ( pub Regex ) ;
8
+
9
+ impl DynEq for RegexWrapper {
10
+ fn as_any ( & self ) -> & dyn std:: any:: Any {
11
+ & self . 0
12
+ }
13
+
14
+ fn do_eq ( & self , rhs : & dyn DynEq ) -> bool {
15
+ if let Some ( _rhs_concrete) = rhs. as_any ( ) . downcast_ref :: < Regex > ( ) {
16
+ // does not compare the regex pattern
17
+ false
18
+ } else {
19
+ false
20
+ }
21
+ }
22
+ }
23
+
6
24
#[ no_mangle]
7
25
pub fn abi_version ( ) -> String {
8
26
String :: from ( "0.0.9" )
@@ -14,8 +32,7 @@ pub fn re_pattern(args: Vec<Edn>) -> Result<Edn, String> {
14
32
match & args[ 0 ] {
15
33
Edn :: Str ( s) => match Regex :: new ( s) {
16
34
Ok ( pattern) => {
17
- let p = Arc :: from ( RwLock :: new ( pattern) ) ;
18
-
35
+ let p = Arc :: from ( RwLock :: new ( RegexWrapper ( pattern) ) ) ;
19
36
Ok ( Edn :: AnyRef ( EdnAnyRef ( p) ) )
20
37
}
21
38
Err ( e) => Err ( format ! ( "re-pattern failed, {}" , e) ) ,
@@ -36,7 +53,7 @@ pub fn re_matches(args: Vec<Edn>) -> Result<Edn, String> {
36
53
Err ( e) => Err ( format ! ( "re-matches failed, {}" , e) ) ,
37
54
} ,
38
55
( Edn :: Str ( s) , Edn :: AnyRef ( EdnAnyRef ( p) ) ) => {
39
- if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. downcast_ref :: < Regex > ( ) {
56
+ if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. as_any ( ) . downcast_ref :: < Regex > ( ) {
40
57
Ok ( Edn :: Bool ( pattern. is_match ( s) ) )
41
58
} else {
42
59
Err ( format ! ( "re-matches expected a regex, got {:?}" , p) )
@@ -63,7 +80,7 @@ pub fn re_find_index(args: Vec<Edn>) -> Result<Edn, String> {
63
80
}
64
81
}
65
82
( Edn :: Str ( s) , Edn :: AnyRef ( EdnAnyRef ( p) ) ) => {
66
- if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. downcast_ref :: < Regex > ( ) {
83
+ if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. as_any ( ) . downcast_ref :: < Regex > ( ) {
67
84
match pattern. find ( s) {
68
85
Some ( matched) => Ok ( Edn :: Number ( matched. start ( ) as f64 ) ) ,
69
86
None => Ok ( Edn :: Number ( -1.0 ) ) , // TODO maybe nil
@@ -97,7 +114,7 @@ pub fn re_find(args: Vec<Edn>) -> Result<Edn, String> {
97
114
}
98
115
}
99
116
( Edn :: Str ( s) , Edn :: AnyRef ( EdnAnyRef ( p) ) ) => {
100
- if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. downcast_ref :: < Regex > ( ) {
117
+ if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. as_any ( ) . downcast_ref :: < Regex > ( ) {
101
118
let mut matched = pattern. find_iter ( s) ;
102
119
match matched. next ( ) {
103
120
Some ( v) => Ok ( Edn :: str ( v. as_str ( ) . to_string ( ) ) ) ,
@@ -129,7 +146,7 @@ pub fn re_find_all(args: Vec<Edn>) -> Result<Edn, String> {
129
146
Err ( e) => Err ( format ! ( "re-find-all failed, {}" , e) ) ,
130
147
} ,
131
148
( Edn :: Str ( s) , Edn :: AnyRef ( EdnAnyRef ( p) ) ) => {
132
- if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. downcast_ref :: < Regex > ( ) {
149
+ if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. as_any ( ) . downcast_ref :: < Regex > ( ) {
133
150
let mut ys: Vec < Edn > = vec ! [ ] ;
134
151
for v in pattern. find_iter ( s) {
135
152
ys. push ( Edn :: Str ( v. as_str ( ) . to_string ( ) . into ( ) ) )
@@ -161,7 +178,7 @@ pub fn re_split(args: Vec<Edn>) -> Result<Edn, String> {
161
178
Err ( e) => Err ( format ! ( "re-split failed, {}" , e) ) ,
162
179
} ,
163
180
( Edn :: Str ( s) , Edn :: AnyRef ( EdnAnyRef ( p) ) ) => {
164
- if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. downcast_ref :: < Regex > ( ) {
181
+ if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. as_any ( ) . downcast_ref :: < Regex > ( ) {
165
182
let mut ys: Vec < Edn > = vec ! [ ] ;
166
183
for piece in pattern. split ( s) {
167
184
ys. push ( Edn :: str ( piece) ) ;
@@ -187,7 +204,7 @@ pub fn re_replace_all(args: Vec<Edn>) -> Result<Edn, String> {
187
204
Err ( e) => Err ( format ! ( "re-replace-all failed, {}" , e) ) ,
188
205
} ,
189
206
( Edn :: Str ( s) , Edn :: AnyRef ( EdnAnyRef ( p) ) , Edn :: Str ( next) ) => {
190
- if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. downcast_ref :: < Regex > ( ) {
207
+ if let Some ( pattern) = p. read ( ) . map_err ( |e| e. to_string ( ) ) ?. as_any ( ) . downcast_ref :: < Regex > ( ) {
191
208
Ok ( Edn :: str ( pattern. replace_all ( s, & * * next) . into_owned ( ) ) )
192
209
} else {
193
210
Err ( format ! ( "re-replace-all expected a regex, got {:?}" , p) )
0 commit comments