rust_regexp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e186368d8d88a70355a0101812a8d8cd1fce54d02bbb910a316db7cf36800cd7
4
+ data.tar.gz: 7e08a707cd22e155ecde210a3e99f72eafe45efe5d998b3bdcafd0aab407e7f3
5
+ SHA512:
6
+ metadata.gz: 71b5dfce5fdc308ff46018f00608804429fa4a58d2d41f439a0add00f5b225669fd957149917be33beb43e1b3f4831ad618d51a9b62491e8cb5562130e762133
7
+ data.tar.gz: 71186a8b4fb00a89e53f4a1b736b54fdb3b0a3e3e682f04e96a10effade4704c0c602ebbbd45e150a066da574fc41f8fef7bb78f440e321d7161d486c191aa56
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dmytro Horoshko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # RustRegexp
2
+
3
+ Simple bindings for [rust/regex](https://docs.rs/regex/latest/regex/) library.
4
+
5
+ ## Installation
6
+
7
+ Install [Rust](https://www.rust-lang.org/) via [rustup](https://rustup.rs/) or in any other way.
8
+
9
+ Add as a dependency:
10
+
11
+ ```ruby
12
+ # In your Gemfile
13
+ gem "rust_regexp"
14
+
15
+ # Or without Bundler
16
+ gem install rust_regexp
17
+ ```
18
+
19
+ Include in your code:
20
+
21
+ ```ruby
22
+ require "rust_regexp"
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Regular expressions should pre-compiled before use:
28
+
29
+ ```ruby
30
+ re = RustRegexp.new('(\w+):(\d+)')
31
+ # => #<RustRegexp:...>
32
+ ```
33
+
34
+ > [!TIP]
35
+ > Note the use of *single quotes* when passing the regular expression as
36
+ > a string to `rust_regexp` so that the backslashes aren't interpreted as escapes.
37
+
38
+ To find a single match in the haystack:
39
+
40
+ ```ruby
41
+ re.match("ruby:123, rust:456")
42
+ # => ["ruby", "123"]
43
+ ```
44
+
45
+ To find all matches in the haystack:
46
+
47
+ ```ruby
48
+ re.scan("ruby:123, rust:456")
49
+ # => [["ruby", "123"], ["rust", "456"]]
50
+ ```
51
+
52
+ To check whether there is at least one match in the haystack:
53
+
54
+ ```ruby
55
+ re.match?("ruby:123")
56
+ # => true
57
+
58
+ re.match?("ruby")
59
+ # => false
60
+ ```
61
+
62
+ Inspect original pattern:
63
+
64
+ ```ruby
65
+ re.pattern
66
+ # => "(\\w+):(\\d+)"
67
+ ```
68
+
69
+ > [!WARNING]
70
+ > `rust/regex` regular expression syntax differs from Ruby's built-in
71
+ > [`Regexp`](https://docs.ruby-lang.org/en/3.4/Regexp.html) library, see the
72
+ > [official syntax page](https://docs.rs/regex/latest/regex/index.html#syntax) for more
73
+ > details.
74
+
75
+ ### Searching simultaneously
76
+
77
+ `RustRegexp::Set` represents a collection of
78
+ regular expressions that can be searched for simultaneously. Calling `RustRegexp::Set#match` will return an array containing the indices of all the patterns that matched.
79
+
80
+ ```ruby
81
+ set = RustRegexp::Set.new(["abc", "def", "ghi", "xyz"])
82
+
83
+ set.match("abcdefghi") # => [0, 1, 2]
84
+ set.match("ghidefabc") # => [0, 1, 2]
85
+ ```
86
+
87
+ > [!NOTE]
88
+ > Matches arrive in the order the constituent patterns were declared,
89
+ > not the order they appear in the haystack.
90
+
91
+ To check whether at least one pattern from the set matches the haystack:
92
+
93
+ ```ruby
94
+ set.match?("abc")
95
+ # => true
96
+
97
+ set.match?("123")
98
+ # => false
99
+ ```
100
+
101
+ Inspect original patterns:
102
+
103
+ ```ruby
104
+ set.patterns
105
+ # => ["abc", "def", "ghi", "xyz"]
106
+ ```
107
+
108
+ ## Development
109
+
110
+ ```sh
111
+ bin/setup # install deps
112
+ bin/console # interactive prompt to play around
113
+ rake compile # (re)compile extension
114
+ rake spec # run tests
115
+ ```
116
+
117
+ ## Contributing
118
+
119
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ocvit/rust_regexp.
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,348 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "aho-corasick"
7
+ version = "1.1.3"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
10
+ dependencies = [
11
+ "memchr",
12
+ ]
13
+
14
+ [[package]]
15
+ name = "bindgen"
16
+ version = "0.69.5"
17
+ source = "registry+https://github.com/rust-lang/crates.io-index"
18
+ checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
19
+ dependencies = [
20
+ "bitflags",
21
+ "cexpr",
22
+ "clang-sys",
23
+ "itertools",
24
+ "lazy_static",
25
+ "lazycell",
26
+ "proc-macro2",
27
+ "quote",
28
+ "regex",
29
+ "rustc-hash",
30
+ "shlex",
31
+ "syn",
32
+ ]
33
+
34
+ [[package]]
35
+ name = "bitflags"
36
+ version = "2.9.0"
37
+ source = "registry+https://github.com/rust-lang/crates.io-index"
38
+ checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
39
+
40
+ [[package]]
41
+ name = "cexpr"
42
+ version = "0.6.0"
43
+ source = "registry+https://github.com/rust-lang/crates.io-index"
44
+ checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
45
+ dependencies = [
46
+ "nom",
47
+ ]
48
+
49
+ [[package]]
50
+ name = "cfg-if"
51
+ version = "1.0.0"
52
+ source = "registry+https://github.com/rust-lang/crates.io-index"
53
+ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
54
+
55
+ [[package]]
56
+ name = "clang-sys"
57
+ version = "1.8.1"
58
+ source = "registry+https://github.com/rust-lang/crates.io-index"
59
+ checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
60
+ dependencies = [
61
+ "glob",
62
+ "libc",
63
+ "libloading",
64
+ ]
65
+
66
+ [[package]]
67
+ name = "either"
68
+ version = "1.15.0"
69
+ source = "registry+https://github.com/rust-lang/crates.io-index"
70
+ checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
71
+
72
+ [[package]]
73
+ name = "glob"
74
+ version = "0.3.2"
75
+ source = "registry+https://github.com/rust-lang/crates.io-index"
76
+ checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
77
+
78
+ [[package]]
79
+ name = "itertools"
80
+ version = "0.12.1"
81
+ source = "registry+https://github.com/rust-lang/crates.io-index"
82
+ checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
83
+ dependencies = [
84
+ "either",
85
+ ]
86
+
87
+ [[package]]
88
+ name = "lazy_static"
89
+ version = "1.5.0"
90
+ source = "registry+https://github.com/rust-lang/crates.io-index"
91
+ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
92
+
93
+ [[package]]
94
+ name = "lazycell"
95
+ version = "1.3.0"
96
+ source = "registry+https://github.com/rust-lang/crates.io-index"
97
+ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
98
+
99
+ [[package]]
100
+ name = "libc"
101
+ version = "0.2.172"
102
+ source = "registry+https://github.com/rust-lang/crates.io-index"
103
+ checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
104
+
105
+ [[package]]
106
+ name = "libloading"
107
+ version = "0.8.6"
108
+ source = "registry+https://github.com/rust-lang/crates.io-index"
109
+ checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
110
+ dependencies = [
111
+ "cfg-if",
112
+ "windows-targets",
113
+ ]
114
+
115
+ [[package]]
116
+ name = "magnus"
117
+ version = "0.7.1"
118
+ source = "registry+https://github.com/rust-lang/crates.io-index"
119
+ checksum = "3d87ae53030f3a22e83879e666cb94e58a7bdf31706878a0ba48752994146dab"
120
+ dependencies = [
121
+ "magnus-macros",
122
+ "rb-sys",
123
+ "rb-sys-env",
124
+ "seq-macro",
125
+ ]
126
+
127
+ [[package]]
128
+ name = "magnus-macros"
129
+ version = "0.6.0"
130
+ source = "registry+https://github.com/rust-lang/crates.io-index"
131
+ checksum = "5968c820e2960565f647819f5928a42d6e874551cab9d88d75e3e0660d7f71e3"
132
+ dependencies = [
133
+ "proc-macro2",
134
+ "quote",
135
+ "syn",
136
+ ]
137
+
138
+ [[package]]
139
+ name = "memchr"
140
+ version = "2.7.4"
141
+ source = "registry+https://github.com/rust-lang/crates.io-index"
142
+ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
143
+
144
+ [[package]]
145
+ name = "minimal-lexical"
146
+ version = "0.2.1"
147
+ source = "registry+https://github.com/rust-lang/crates.io-index"
148
+ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
149
+
150
+ [[package]]
151
+ name = "nom"
152
+ version = "7.1.3"
153
+ source = "registry+https://github.com/rust-lang/crates.io-index"
154
+ checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
155
+ dependencies = [
156
+ "memchr",
157
+ "minimal-lexical",
158
+ ]
159
+
160
+ [[package]]
161
+ name = "proc-macro2"
162
+ version = "1.0.95"
163
+ source = "registry+https://github.com/rust-lang/crates.io-index"
164
+ checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
165
+ dependencies = [
166
+ "unicode-ident",
167
+ ]
168
+
169
+ [[package]]
170
+ name = "quote"
171
+ version = "1.0.40"
172
+ source = "registry+https://github.com/rust-lang/crates.io-index"
173
+ checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
174
+ dependencies = [
175
+ "proc-macro2",
176
+ ]
177
+
178
+ [[package]]
179
+ name = "rb-sys"
180
+ version = "0.9.111"
181
+ source = "registry+https://github.com/rust-lang/crates.io-index"
182
+ checksum = "becea799ce051c16fb140be80f5e7cf781070f99ca099332383c2b17861249af"
183
+ dependencies = [
184
+ "rb-sys-build",
185
+ ]
186
+
187
+ [[package]]
188
+ name = "rb-sys-build"
189
+ version = "0.9.111"
190
+ source = "registry+https://github.com/rust-lang/crates.io-index"
191
+ checksum = "64691175abc704862f60a9ca8ef06174080cc50615f2bf1d4759f46db18b4d29"
192
+ dependencies = [
193
+ "bindgen",
194
+ "lazy_static",
195
+ "proc-macro2",
196
+ "quote",
197
+ "regex",
198
+ "shell-words",
199
+ "syn",
200
+ ]
201
+
202
+ [[package]]
203
+ name = "rb-sys-env"
204
+ version = "0.1.2"
205
+ source = "registry+https://github.com/rust-lang/crates.io-index"
206
+ checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb"
207
+
208
+ [[package]]
209
+ name = "regex"
210
+ version = "1.11.1"
211
+ source = "registry+https://github.com/rust-lang/crates.io-index"
212
+ checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
213
+ dependencies = [
214
+ "aho-corasick",
215
+ "memchr",
216
+ "regex-automata",
217
+ "regex-syntax",
218
+ ]
219
+
220
+ [[package]]
221
+ name = "regex-automata"
222
+ version = "0.4.9"
223
+ source = "registry+https://github.com/rust-lang/crates.io-index"
224
+ checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
225
+ dependencies = [
226
+ "aho-corasick",
227
+ "memchr",
228
+ "regex-syntax",
229
+ ]
230
+
231
+ [[package]]
232
+ name = "regex-syntax"
233
+ version = "0.8.5"
234
+ source = "registry+https://github.com/rust-lang/crates.io-index"
235
+ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
236
+
237
+ [[package]]
238
+ name = "rust_regexp"
239
+ version = "0.1.0"
240
+ dependencies = [
241
+ "magnus",
242
+ "regex",
243
+ ]
244
+
245
+ [[package]]
246
+ name = "rustc-hash"
247
+ version = "1.1.0"
248
+ source = "registry+https://github.com/rust-lang/crates.io-index"
249
+ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
250
+
251
+ [[package]]
252
+ name = "seq-macro"
253
+ version = "0.3.6"
254
+ source = "registry+https://github.com/rust-lang/crates.io-index"
255
+ checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc"
256
+
257
+ [[package]]
258
+ name = "shell-words"
259
+ version = "1.1.0"
260
+ source = "registry+https://github.com/rust-lang/crates.io-index"
261
+ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
262
+
263
+ [[package]]
264
+ name = "shlex"
265
+ version = "1.3.0"
266
+ source = "registry+https://github.com/rust-lang/crates.io-index"
267
+ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
268
+
269
+ [[package]]
270
+ name = "syn"
271
+ version = "2.0.100"
272
+ source = "registry+https://github.com/rust-lang/crates.io-index"
273
+ checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0"
274
+ dependencies = [
275
+ "proc-macro2",
276
+ "quote",
277
+ "unicode-ident",
278
+ ]
279
+
280
+ [[package]]
281
+ name = "unicode-ident"
282
+ version = "1.0.18"
283
+ source = "registry+https://github.com/rust-lang/crates.io-index"
284
+ checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
285
+
286
+ [[package]]
287
+ name = "windows-targets"
288
+ version = "0.52.6"
289
+ source = "registry+https://github.com/rust-lang/crates.io-index"
290
+ checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
291
+ dependencies = [
292
+ "windows_aarch64_gnullvm",
293
+ "windows_aarch64_msvc",
294
+ "windows_i686_gnu",
295
+ "windows_i686_gnullvm",
296
+ "windows_i686_msvc",
297
+ "windows_x86_64_gnu",
298
+ "windows_x86_64_gnullvm",
299
+ "windows_x86_64_msvc",
300
+ ]
301
+
302
+ [[package]]
303
+ name = "windows_aarch64_gnullvm"
304
+ version = "0.52.6"
305
+ source = "registry+https://github.com/rust-lang/crates.io-index"
306
+ checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
307
+
308
+ [[package]]
309
+ name = "windows_aarch64_msvc"
310
+ version = "0.52.6"
311
+ source = "registry+https://github.com/rust-lang/crates.io-index"
312
+ checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
313
+
314
+ [[package]]
315
+ name = "windows_i686_gnu"
316
+ version = "0.52.6"
317
+ source = "registry+https://github.com/rust-lang/crates.io-index"
318
+ checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
319
+
320
+ [[package]]
321
+ name = "windows_i686_gnullvm"
322
+ version = "0.52.6"
323
+ source = "registry+https://github.com/rust-lang/crates.io-index"
324
+ checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
325
+
326
+ [[package]]
327
+ name = "windows_i686_msvc"
328
+ version = "0.52.6"
329
+ source = "registry+https://github.com/rust-lang/crates.io-index"
330
+ checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
331
+
332
+ [[package]]
333
+ name = "windows_x86_64_gnu"
334
+ version = "0.52.6"
335
+ source = "registry+https://github.com/rust-lang/crates.io-index"
336
+ checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
337
+
338
+ [[package]]
339
+ name = "windows_x86_64_gnullvm"
340
+ version = "0.52.6"
341
+ source = "registry+https://github.com/rust-lang/crates.io-index"
342
+ checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
343
+
344
+ [[package]]
345
+ name = "windows_x86_64_msvc"
346
+ version = "0.52.6"
347
+ source = "registry+https://github.com/rust-lang/crates.io-index"
348
+ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
@@ -0,0 +1,11 @@
1
+ [package]
2
+ name = "rust_regexp"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+
6
+ [lib]
7
+ crate-type = ["cdylib"]
8
+
9
+ [dependencies]
10
+ magnus = "0.7.1"
11
+ regex = "1.11.1"
@@ -0,0 +1,4 @@
1
+ require "mkmf"
2
+ require "rb_sys/mkmf"
3
+
4
+ create_rust_makefile("rust_regexp/rust_regexp")
@@ -0,0 +1,179 @@
1
+ use magnus::{
2
+ class,
3
+ define_class,
4
+ encoding::RbEncoding,
5
+ exception,
6
+ function,
7
+ method,
8
+ prelude::*,
9
+ scan_args::scan_args,
10
+ Value,
11
+ Error,
12
+ RString,
13
+ RArray,
14
+ };
15
+ use regex::bytes::{Regex, RegexSet, Match};
16
+
17
+ #[magnus::wrap(class = "RustRegexp", free_immediately, size)]
18
+ pub struct RustRegexp(Regex);
19
+
20
+ impl RustRegexp {
21
+ pub fn new(args: &[Value]) -> Result<Self, Error> {
22
+ let args = scan_args::<(String,), (), (), (), (), ()>(args)?;
23
+ let pattern = args.required.0;
24
+
25
+ let regex = Regex::new(&pattern).map_err(|e| Error::new(exception::arg_error(), e.to_string()))?;
26
+
27
+ Ok(Self(regex))
28
+ }
29
+
30
+ pub fn find(&self, haystack: RString) -> RArray {
31
+ let result = RArray::new();
32
+
33
+ let regex = &self.0;
34
+ let haystack = unsafe { haystack.as_slice() };
35
+
36
+ if regex.captures_len() == 1 {
37
+ // speed optimization, `.find` is faster than `.captures`
38
+ if let Some(capture) = regex.find(haystack) {
39
+ result
40
+ .push(Self::capture_to_ruby_string(&capture))
41
+ .expect("Non-frozen array");
42
+ }
43
+ } else {
44
+ if let Some(captures) = regex.captures(haystack) {
45
+ for capture in captures.iter().skip(1) {
46
+ if let Some(capture) = capture {
47
+ result
48
+ .push(Self::capture_to_ruby_string(&capture))
49
+ .expect("Non-frozen array");
50
+ } else {
51
+ result
52
+ .push(()) // push `nil`
53
+ .expect("Non-frozen array");
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ result
60
+ }
61
+
62
+ pub fn scan(&self, haystack: RString) -> RArray {
63
+ let result = RArray::new();
64
+
65
+ let regex = &self.0;
66
+ let haystack = unsafe { haystack.as_slice() };
67
+
68
+ if regex.captures_len() == 1 {
69
+ // speed optimization, `.find_iter` is faster than `.captures_iter`
70
+ for capture in regex.find_iter(haystack) {
71
+ let group = RArray::with_capacity(1);
72
+
73
+ group
74
+ .push(Self::capture_to_ruby_string(&capture))
75
+ .expect("Non-frozen array");
76
+
77
+ result
78
+ .push(group)
79
+ .expect("Non-frozen array");
80
+ }
81
+ } else {
82
+ for captures in regex.captures_iter(haystack) {
83
+ let group = RArray::with_capacity(regex.captures_len());
84
+
85
+ for capture in captures.iter().skip(1) {
86
+ if let Some(capture) = capture {
87
+ group
88
+ .push(Self::capture_to_ruby_string(&capture))
89
+ .expect("Non-frozen array");
90
+ } else {
91
+ group
92
+ .push(()) // push `nil`
93
+ .expect("Non-frozen array");
94
+ }
95
+ }
96
+
97
+ result
98
+ .push(group)
99
+ .expect("Non-frozen array");
100
+ }
101
+ }
102
+
103
+ result
104
+ }
105
+
106
+ pub fn is_match(&self, haystack: RString) -> bool {
107
+ let regex = &self.0;
108
+ let haystack = unsafe { haystack.as_slice() };
109
+
110
+ regex.is_match(haystack)
111
+ }
112
+
113
+ pub fn pattern(&self) -> &str {
114
+ let regex = &self.0;
115
+
116
+ regex.as_str()
117
+ }
118
+
119
+ fn capture_to_ruby_string(capture: &Match) -> RString {
120
+ RString::enc_new(
121
+ capture.as_bytes(),
122
+ RbEncoding::utf8()
123
+ )
124
+ }
125
+ }
126
+
127
+ #[magnus::wrap(class = "RustRegexp::Set", free_immediately, size)]
128
+ pub struct RustRegexpSet(RegexSet);
129
+
130
+ impl RustRegexpSet {
131
+ pub fn new(args: &[Value]) -> Result<Self, Error> {
132
+ let args = scan_args::<(Vec<String>,), (), (), (), (), ()>(args)?;
133
+ let patterns = args.required.0;
134
+
135
+ let set = RegexSet::new(patterns).map_err(|e| Error::new(exception::arg_error(), e.to_string()))?;
136
+
137
+ Ok(Self(set))
138
+ }
139
+
140
+ pub fn matches(&self, haystack: RString) -> Vec<usize> {
141
+ let set = &self.0;
142
+ let haystack = unsafe { haystack.as_slice() };
143
+
144
+ set.matches(haystack).into_iter().collect()
145
+ }
146
+
147
+ pub fn is_match(&self, haystack: RString) -> bool {
148
+ let set = &self.0;
149
+ let haystack = unsafe { haystack.as_slice() };
150
+
151
+ set.is_match(haystack)
152
+ }
153
+
154
+ pub fn patterns(&self) -> Vec<String> {
155
+ let set = &self.0;
156
+
157
+ set.patterns().into()
158
+ }
159
+ }
160
+
161
+ #[magnus::init]
162
+ pub fn init() -> Result<(), Error> {
163
+ let regexp_class = define_class("RustRegexp", class::object())?;
164
+
165
+ regexp_class.define_singleton_method("new", function!(RustRegexp::new, -1))?;
166
+ regexp_class.define_method("match", method!(RustRegexp::find, 1))?;
167
+ regexp_class.define_method("match?", method!(RustRegexp::is_match, 1))?;
168
+ regexp_class.define_method("scan", method!(RustRegexp::scan, 1))?;
169
+ regexp_class.define_method("pattern", method!(RustRegexp::pattern, 0))?;
170
+
171
+ let regexp_set_class = regexp_class.define_class("Set", class::object())?;
172
+
173
+ regexp_set_class.define_singleton_method("new", function!(RustRegexpSet::new, -1))?;
174
+ regexp_set_class.define_method("match", method!(RustRegexpSet::matches, 1))?;
175
+ regexp_set_class.define_method("match?", method!(RustRegexpSet::is_match, 1))?;
176
+ regexp_set_class.define_method("patterns", method!(RustRegexpSet::patterns, 0))?;
177
+
178
+ Ok(())
179
+ }
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RustRegexp
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rust_regexp/version"
4
+ require_relative "rust_regexp/rust_regexp"
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rust_regexp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmytro Horoshko
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Simple bindings to rust/regex library.
13
+ email:
14
+ - electric.molfar@gmail.com
15
+ executables: []
16
+ extensions:
17
+ - ext/rust_regexp/extconf.rb
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - ext/rust_regexp/Cargo.lock
23
+ - ext/rust_regexp/Cargo.toml
24
+ - ext/rust_regexp/extconf.rb
25
+ - ext/rust_regexp/src/lib.rs
26
+ - lib/rust_regexp.rb
27
+ - lib/rust_regexp/version.rb
28
+ homepage: https://github.com/ocvit/rust_regexp
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ bug_tracker_uri: https://github.com/ocvit/rust_regexp/issues
33
+ homepage_uri: https://github.com/ocvit/rust_regexp
34
+ source_code_uri: https://github.com/ocvit/rust_regexp
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.7.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.6.7
50
+ specification_version: 4
51
+ summary: Simple bindings for rust/regex library
52
+ test_files: []