6
6
*/
7
7
use regex:: { Match , Regex } ;
8
8
9
- use crate :: function:: PyFuncArgs ;
10
- use crate :: obj:: objstr;
11
9
use crate :: obj:: objstr:: PyStringRef ;
12
10
use crate :: obj:: objtype:: PyClassRef ;
13
- use crate :: pyobject:: { PyContext , PyObjectRef , PyRef , PyResult , PyValue , TypeProtocol } ;
11
+ use crate :: pyobject:: { PyContext , PyObjectRef , PyRef , PyResult , PyValue } ;
14
12
use crate :: vm:: VirtualMachine ;
15
13
16
14
impl PyValue for Regex {
@@ -19,73 +17,39 @@ impl PyValue for Regex {
19
17
}
20
18
}
21
19
22
- /// Create the python `re` module with all its members.
23
- pub fn make_module ( ctx : & PyContext ) -> PyObjectRef {
24
- let match_type = py_class ! ( ctx, "Match" , ctx. object( ) , {
25
- "start" => ctx. new_rustfunc( match_start) ,
26
- "end" => ctx. new_rustfunc( match_end)
27
- } ) ;
28
-
29
- let pattern_type = py_class ! ( ctx, "Pattern" , ctx. object( ) , {
30
- "match" => ctx. new_rustfunc( pattern_match) ,
31
- "search" => ctx. new_rustfunc( pattern_search)
32
- } ) ;
20
+ /// Inner data for a match object.
21
+ #[ derive( Debug ) ]
22
+ struct PyMatch {
23
+ start : usize ,
24
+ end : usize ,
25
+ }
33
26
34
- py_module ! ( ctx, "re" , {
35
- "compile" => ctx. new_rustfunc( re_compile) ,
36
- "Match" => match_type,
37
- "match" => ctx. new_rustfunc( re_match) ,
38
- "Pattern" => pattern_type,
39
- "search" => ctx. new_rustfunc( re_search)
40
- } )
27
+ impl PyValue for PyMatch {
28
+ fn class ( vm : & VirtualMachine ) -> PyClassRef {
29
+ vm. class ( "re" , "Match" )
30
+ }
41
31
}
42
32
43
- /// Implement re.match
44
- /// See also:
45
- /// https://docs.python.org/3/library/re.html#re.match
46
- fn re_match ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
47
- arg_check ! (
48
- vm,
49
- args,
50
- required = [
51
- ( pattern, Some ( vm. ctx. str_type( ) ) ) ,
52
- ( string, Some ( vm. ctx. str_type( ) ) )
53
- ]
54
- ) ;
55
- let pattern_str = objstr:: get_value ( & pattern) ;
56
- let regex = make_regex ( vm, & pattern_str) ?;
57
- let search_text = objstr:: get_value ( string) ;
58
-
59
- do_match ( vm, & regex, search_text)
33
+ type PyRegexRef = PyRef < Regex > ;
34
+ type PyMatchRef = PyRef < PyMatch > ;
35
+
36
+ fn re_match ( pattern : PyStringRef , string : PyStringRef , vm : & VirtualMachine ) -> PyResult {
37
+ let regex = make_regex ( vm, & pattern. value ) ?;
38
+ do_match ( vm, & regex, & string. value )
60
39
}
61
40
62
- /// Implement re.search
63
- /// See also:
64
- /// https://docs.python.org/3/library/re.html#re.search
65
- fn re_search ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
66
- arg_check ! (
67
- vm,
68
- args,
69
- required = [
70
- ( pattern, Some ( vm. ctx. str_type( ) ) ) ,
71
- ( string, Some ( vm. ctx. str_type( ) ) )
72
- ]
73
- ) ;
74
-
75
- let pattern_str = objstr:: get_value ( & pattern) ;
76
- let regex = make_regex ( vm, & pattern_str) ?;
77
- let search_text = objstr:: get_value ( string) ;
78
-
79
- do_search ( vm, & regex, search_text)
41
+ fn re_search ( pattern : PyStringRef , string : PyStringRef , vm : & VirtualMachine ) -> PyResult {
42
+ let regex = make_regex ( vm, & pattern. value ) ?;
43
+ do_search ( vm, & regex, & string. value )
80
44
}
81
45
82
- fn do_match ( vm : & VirtualMachine , regex : & Regex , search_text : String ) -> PyResult {
46
+ fn do_match ( vm : & VirtualMachine , regex : & Regex , search_text : & str ) -> PyResult {
83
47
// TODO: implement match!
84
48
do_search ( vm, regex, search_text)
85
49
}
86
50
87
- fn do_search ( vm : & VirtualMachine , regex : & Regex , search_text : String ) -> PyResult {
88
- match regex. find ( & search_text) {
51
+ fn do_search ( vm : & VirtualMachine , regex : & Regex , search_text : & str ) -> PyResult {
52
+ match regex. find ( search_text) {
89
53
None => Ok ( vm. get_none ( ) ) ,
90
54
Some ( result) => create_match ( vm, & result) ,
91
55
}
@@ -98,19 +62,6 @@ fn make_regex(vm: &VirtualMachine, pattern: &str) -> PyResult<Regex> {
98
62
}
99
63
}
100
64
101
- /// Inner data for a match object.
102
- #[ derive( Debug ) ]
103
- struct PyMatch {
104
- start : usize ,
105
- end : usize ,
106
- }
107
-
108
- impl PyValue for PyMatch {
109
- fn class ( vm : & VirtualMachine ) -> PyClassRef {
110
- vm. class ( "re" , "Match" )
111
- }
112
- }
113
-
114
65
/// Take a found regular expression and convert it to proper match object.
115
66
fn create_match ( vm : & VirtualMachine , match_value : & Match ) -> PyResult {
116
67
// let mo = vm.invoke(match_class, PyFuncArgs::default())?;
@@ -124,68 +75,45 @@ fn create_match(vm: &VirtualMachine, match_value: &Match) -> PyResult {
124
75
. into_object ( ) )
125
76
}
126
77
127
- /// Compile a regular expression into a Pattern object.
128
- /// See also:
129
- /// https://docs.python.org/3/library/re.html#re.compile
130
- fn re_compile ( pattern : PyStringRef , vm : & VirtualMachine ) -> PyResult < PyRef < Regex > > {
131
- let regex = make_regex ( vm, & pattern. value ) ?;
132
-
133
- Ok ( regex. into_ref ( vm) )
78
+ fn re_compile ( pattern : PyStringRef , vm : & VirtualMachine ) -> PyResult < Regex > {
79
+ make_regex ( vm, & pattern. value )
134
80
}
135
81
136
- fn pattern_match ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
137
- arg_check ! (
138
- vm,
139
- args,
140
- required = [ ( zelf, None ) , ( text, Some ( vm. ctx. str_type( ) ) ) ]
141
- ) ;
142
-
143
- let regex = get_regex ( zelf) ;
144
- let search_text = objstr:: get_value ( text) ;
145
- do_match ( vm, & regex, search_text)
146
- }
147
-
148
- fn pattern_search ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
149
- arg_check ! (
150
- vm,
151
- args,
152
- required = [ ( zelf, None ) , ( text, Some ( vm. ctx. str_type( ) ) ) ]
153
- ) ;
154
-
155
- let regex = get_regex ( zelf) ;
156
- let search_text = objstr:: get_value ( text) ;
157
- do_search ( vm, & regex, search_text)
82
+ impl PyRegexRef {
83
+ fn match_ ( self , text : PyStringRef , vm : & VirtualMachine ) -> PyResult {
84
+ do_match ( vm, & self , & text. value )
85
+ }
86
+ fn search ( self , text : PyStringRef , vm : & VirtualMachine ) -> PyResult {
87
+ do_search ( vm, & self , & text. value )
88
+ }
158
89
}
159
90
160
- /// Returns start of match
161
- /// see: https://docs.python.org/3/library/re.html#re.Match. start
162
- fn match_start ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
163
- arg_check ! ( vm , args , required = [ ( zelf , None ) ] ) ;
164
- // TODO: implement groups
165
- let m = get_match ( zelf ) ;
166
- Ok ( vm . new_int ( m . start ) )
91
+ impl PyMatchRef {
92
+ fn start ( self , _vm : & VirtualMachine ) -> usize {
93
+ self . start
94
+ }
95
+ fn end ( self , _vm : & VirtualMachine ) -> usize {
96
+ self . end
97
+ }
167
98
}
168
99
169
- fn match_end ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
170
- arg_check ! ( vm , args , required = [ ( zelf , None ) ] ) ;
171
- // TODO: implement groups
172
- let m = get_match ( zelf ) ;
173
- Ok ( vm . new_int ( m . end ) )
174
- }
100
+ /// Create the python `re` module with all its members.
101
+ pub fn make_module ( ctx : & PyContext ) -> PyObjectRef {
102
+ let match_type = py_class ! ( ctx , "Match" , ctx . object ( ) , {
103
+ "start" => ctx . new_rustfunc ( PyMatchRef :: start ) ,
104
+ "end" => ctx . new_rustfunc ( PyMatchRef :: end)
105
+ } ) ;
175
106
176
- /// Retrieve inner rust regex from python object:
177
- fn get_regex ( obj : & PyObjectRef ) -> & Regex {
178
- // TODO: Regex shouldn't be stored in payload directly, create newtype wrapper
179
- if let Some ( regex) = obj. payload :: < Regex > ( ) {
180
- return regex;
181
- }
182
- panic ! ( "Inner error getting regex {:?}" , obj) ;
183
- }
107
+ let pattern_type = py_class ! ( ctx, "Pattern" , ctx. object( ) , {
108
+ "match" => ctx. new_rustfunc( PyRegexRef :: match_) ,
109
+ "search" => ctx. new_rustfunc( PyRegexRef :: search)
110
+ } ) ;
184
111
185
- /// Retrieve inner rust match from python object:
186
- fn get_match ( obj : & PyObjectRef ) -> & PyMatch {
187
- if let Some ( value) = obj. payload :: < PyMatch > ( ) {
188
- return value;
189
- }
190
- panic ! ( "Inner error getting match {:?}" , obj) ;
112
+ py_module ! ( ctx, "re" , {
113
+ "compile" => ctx. new_rustfunc( re_compile) ,
114
+ "Match" => match_type,
115
+ "match" => ctx. new_rustfunc( re_match) ,
116
+ "Pattern" => pattern_type,
117
+ "search" => ctx. new_rustfunc( re_search)
118
+ } )
191
119
}
0 commit comments