grex_ruby 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a2dd5cd77e2c7169182cc883204cf332f79822fb906c459644b982f8a882614
4
- data.tar.gz: e8dfeb8f839b02283c62e2f0fc4c25d54dc505c8e10952d4405113fb9a898094
3
+ metadata.gz: 9eea98be32e92c0d87bc6675840278c2472767a7f57e48d01fde8bedb59b650d
4
+ data.tar.gz: 7bd0abf1026dbe6f9bb4e6c764952301c948da7b6647b9a2015b3a7553bb3e04
5
5
  SHA512:
6
- metadata.gz: 695f7ea72cf70580dae52d2d755281e408cc357e6ca772fde12b10fca2aa4efbcd9d88933f2395e26fb5c25d126daa5775470aa28fa6ccb70bd910d6f507baa9
7
- data.tar.gz: 1b1789d8ec092b42df5ecfd98fa8128412a22193c0e26c2890cf50ac325f4ca7bef79c7bd43ced3fc1121052ab783b1f272b207f4096104130e705d798aedb44
6
+ metadata.gz: 25bb8ab1dd9ac55dfdfcf1eb37841be34abd390cf2d27a4f541e1fb59735b780b5f4dabc0853b8463b2464cc0d7c222dfba91afa3c1f059196c5e7c0d4607f20
7
+ data.tar.gz: a45f66a23f259de711df1c84ce8e2df4335c2b568a493492cdddb4b7bce21f89fc072e29e88dff2ec2008cf43d824a6d45ebee51bf4ff0047a788823f08d66f2
data/CHANGELOG.md CHANGED
@@ -1,7 +1,20 @@
1
- # Changelog
2
-
3
- ## 0.1.0
4
-
5
- - Generate regular expression patterns from arrays of strings.
6
- - Support digit and repetition conversion.
7
- - Adapt digit classes for Unicode matching in Ruby.
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ - Expose the public API as `Grex`, keeping `GrexRuby` as a compatibility alias.
6
+ - Add conversion of non-digits, whitespace, non-whitespace, words and non-words.
7
+ - Adapt character classes for Unicode matching in Ruby, preserving escaped literals.
8
+ - Add minimum repetition count and minimum repeated substring length settings.
9
+ - Support case-insensitive matching, capturing groups and verbose patterns.
10
+ - Allow disabling both anchors or the start and end anchors individually.
11
+ - Support non-ASCII escaping with Unicode code points understood by Ruby Regexp.
12
+ - Document Ruby's line anchor semantics and local build prerequisites.
13
+ - Include grex's Apache-2.0 license and third-party attribution in the gem.
14
+ - Add regression coverage for combinations of conversion options.
15
+
16
+ ## 0.1.0
17
+
18
+ - Generate regular expression patterns from arrays of strings.
19
+ - Support digit and repetition conversion.
20
+ - Adapt digit classes for Unicode matching in Ruby.
data/Cargo.lock CHANGED
@@ -228,7 +228,7 @@ dependencies = [
228
228
 
229
229
  [[package]]
230
230
  name = "grex_ruby"
231
- version = "0.1.0"
231
+ version = "0.2.0"
232
232
  dependencies = [
233
233
  "grex",
234
234
  "magnus",
data/README.md CHANGED
@@ -1,56 +1,267 @@
1
- # GrexRuby
1
+ # Grex
2
2
 
3
- GrexRuby provides Ruby bindings for the Rust library [grex](https://github.com/pemistahl/grex).
3
+ Grex provides Ruby bindings for the Rust library [grex](https://github.com/pemistahl/grex).
4
4
  It generates a regular expression pattern from an array of example strings.
5
5
 
6
6
  ## Installation
7
7
 
8
- The gem is not published yet. To build it locally, follow the development instructions below.
8
+ Install from RubyGems:
9
+
10
+ ```bash
11
+ gem install grex_ruby
12
+ ```
13
+
14
+ Or add it to your application's bundle:
15
+
16
+ ```bash
17
+ bundle add grex_ruby
18
+ ```
19
+
20
+ Grex requires Ruby 3.2 or newer. The gem builds its native extension during
21
+ installation, so Rust (including Cargo), a C/C++ compiler, Make, libclang, and
22
+ matching Ruby development headers must be available.
23
+
24
+ Follow the dependency setup instructions for [Linux](#linux) or
25
+ [Windows](#windows-64-bit-rubyinstaller-with-ucrt) below before installing.
26
+ On Windows, set `LIBCLANG_PATH` and `RUST_TARGET` as shown there, then install with:
27
+
28
+ ```powershell
29
+ ridk.cmd exec gem install grex_ruby
30
+ ```
9
31
 
10
32
  ## Usage
11
33
 
12
- `GrexRuby.generate` accepts a non-empty array of strings and returns a Ruby `String`.
34
+ `Grex.generate` accepts a non-empty array of strings and returns a Ruby `String`.
35
+
36
+ The gem name and require path remain `grex_ruby`. `GrexRuby` is kept as an alias
37
+ of `Grex` for compatibility with version 0.1.0. The `Grex` module name is introduced
38
+ in 0.2.0; version 0.1.0 uses `GrexRuby`.
13
39
 
14
40
  ```ruby
15
41
  require 'grex_ruby'
16
42
 
17
- GrexRuby.generate(%w[a aa aaa])
43
+ Grex.generate(%w[a aa aaa])
18
44
  # => "^a(?:aa?)?$"
19
45
 
20
- GrexRuby.generate(%w[cat])
46
+ Grex.generate(%w[cat])
21
47
  # => "^cat$"
22
48
  ```
23
49
 
24
- Both keyword options default to `false`:
25
-
26
- | Option | Behavior when enabled |
27
- | --- | --- |
28
- | `convert_digits` | Generalizes decimal digits to the Unicode digit class `\p{Nd}`. |
29
- | `convert_repetitions` | Converts repeated substrings to quantifiers such as `{3}`. |
50
+ Keyword options:
51
+
52
+ | Option | Default | Behavior |
53
+ | -------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------- |
54
+ | `convert_digits` | `false` | Generalizes decimal digits to the Unicode digit class `\p{Nd}`. |
55
+ | `convert_non_digits` | `false` | Generalizes non-digit characters to the Unicode class `\P{Nd}`. |
56
+ | `convert_whitespace` | `false` | Generalizes whitespace characters to the Unicode class `\p{Space}`. |
57
+ | `convert_non_whitespace` | `false` | Generalizes non-whitespace characters to the Unicode class `\P{Space}`. |
58
+ | `convert_words` | `false` | Generalizes individual Unicode word characters, including letters, decimal digits, marks, connector punctuation and join controls. |
59
+ | `convert_non_words` | `false` | Generalizes characters outside the Unicode word class, including punctuation, whitespace and emoji. |
60
+ | `convert_repetitions` | `false` | Converts repeated substrings to quantifiers such as `{3}`. |
61
+ | `minimum_substring_length` | `1` | Minimum length of a repeated fragment eligible for conversion. |
62
+ | `minimum_repetitions` | `1` | Minimum number of repetitions after the fragment's first occurrence. |
63
+ | `anchors` | `true` | Keeps grex's `^` and `$` line anchors; set to `false` to omit both. |
64
+ | `start_anchor` | `true` | Keeps the initial `^` anchor; set to `false` to remove it. `anchors: false` overrides this setting. |
65
+ | `end_anchor` | `true` | Keeps the final `$` anchor; set to `false` to remove it. `anchors: false` overrides this setting. |
66
+ | `escape_non_ascii` | `false` | Writes non-ASCII characters as Unicode code point escapes understood by Ruby Regexp. |
67
+ | `ignore_case` | `false` | Generates a pattern that matches without regard to letter case. |
68
+ | `capturing_groups` | `false` | Uses capturing groups instead of non-capturing groups. |
69
+ | `verbose` | `false` | Formats the pattern across multiple lines with indentation and enables extended mode. |
70
+
71
+ The two minimum settings affect generation when `convert_repetitions: true`.
72
+ Both accept integers from `1` to `4_294_967_295`; zero raises `ArgumentError`.
30
73
 
31
74
  ```ruby
32
- GrexRuby.generate(%w[123])
75
+ Grex.generate(%w[123])
33
76
  # => "^123$"
34
77
 
35
- GrexRuby.generate(%w[123], convert_digits: true)
78
+ Grex.generate(%w[123], convert_digits: true)
36
79
  # => "^\\p{Nd}\\p{Nd}\\p{Nd}$"
37
80
 
38
- GrexRuby.generate(['aaa'], convert_repetitions: true)
81
+ Grex.generate(['aaa'], convert_repetitions: true)
39
82
  # => "^a{3}$"
40
83
 
41
- GrexRuby.generate(['111'], convert_digits: true, convert_repetitions: true)
84
+ Grex.generate(['111'], convert_digits: true, convert_repetitions: true)
42
85
  # => "^\\p{Nd}{3}$"
43
86
  ```
44
87
 
88
+ Control which repetitions are converted:
89
+
90
+ ```ruby
91
+ Grex.generate(['aaaa'], convert_repetitions: true, minimum_substring_length: 2)
92
+ # => "^aaaa$"
93
+
94
+ Grex.generate(['ababab'], convert_repetitions: true, minimum_substring_length: 2)
95
+ # => "^(?:ab){3}$"
96
+
97
+ Grex.generate(['aa'], convert_repetitions: true, minimum_repetitions: 2)
98
+ # => "^aa$"
99
+
100
+ Grex.generate(['aaa'], convert_repetitions: true, minimum_repetitions: 2)
101
+ # => "^a{3}$"
102
+ ```
103
+
104
+ For `minimum_repetitions`, the first occurrence is not counted as a repetition:
105
+ `'aa'` has one repetition of `'a'`, while `'aaa'` has two.
106
+
45
107
  Digit conversion allows other decimal digits to match too, including non-ASCII
46
- digits. GrexRuby adapts grex's `\d` tokens to `\p{Nd}` because Ruby's `\d` matches
108
+ digits. Grex adapts grex's `\d` tokens to `\p{Nd}` because Ruby's `\d` matches
47
109
  only ASCII digits.
48
110
 
111
+ Use `convert_non_digits: true` to match non-digit characters. Ruby's Unicode
112
+ class `\P{Nd}` excludes both ASCII and non-ASCII decimal digits:
113
+
114
+ ```ruby
115
+ pattern = Grex.generate(['abc'], convert_non_digits: true)
116
+ # => "^\\P{Nd}\\P{Nd}\\P{Nd}$"
117
+
118
+ regexp = Regexp.new(pattern)
119
+ regexp.match?('xyz')
120
+ # => true
121
+
122
+ regexp.match?('123')
123
+ # => false
124
+
125
+ regexp.match?('١٢٣')
126
+ # => false
127
+ ```
128
+
129
+ The adapter preserves escaped literal backslashes while
130
+ converting grex's `\D` tokens to `\P{Nd}`.
131
+
132
+ Use `convert_whitespace: true` to generalize spaces to other whitespace characters.
133
+ Grex adapts grex's `\s` tokens to Ruby's Unicode class `\p{Space}`, so tabs and
134
+ non-ASCII whitespace are recognized too:
135
+
136
+ ```ruby
137
+ pattern = Grex.generate(['a b'], convert_whitespace: true)
138
+ # => "^a\\p{Space}b$"
139
+
140
+ regexp = Regexp.new(pattern)
141
+ regexp.match?("a\tb")
142
+ # => true
143
+
144
+ regexp.match?("a\u00A0b") # NO-BREAK SPACE
145
+ # => true
146
+
147
+ regexp.match?('ab')
148
+ # => false
149
+ ```
150
+
151
+ By default, a literal space remains a literal space.
152
+
153
+ Use `convert_non_whitespace: true` to match any non-whitespace characters,
154
+ including digits. Grex adapts grex's `\S` tokens to Ruby's Unicode class
155
+ `\P{Space}`, which excludes both ASCII and Unicode whitespace:
156
+
157
+ ```ruby
158
+ pattern = Grex.generate(['abc'], convert_non_whitespace: true)
159
+ # => "^\\P{Space}\\P{Space}\\P{Space}$"
160
+
161
+ regexp = Regexp.new(pattern)
162
+ regexp.match?('xyz')
163
+ # => true
164
+
165
+ regexp.match?('123')
166
+ # => true
167
+
168
+ regexp.match?("a\tb")
169
+ # => false
170
+
171
+ regexp.match?("a\u00A0b") # NO-BREAK SPACE
172
+ # => false
173
+ ```
174
+
175
+ The `convert_non_whitespace` option defaults to `false`. Escaped literal
176
+ backslashes are preserved during adaptation.
177
+
178
+ ### Word conversion
179
+
180
+ `convert_words: true` generalizes
181
+ individual Unicode word characters, rather than entire words. Word characters
182
+ include letters, decimal digits, combining marks, connector punctuation such as
183
+ `_`, and join controls.
184
+
185
+ For example:
186
+
187
+ ```ruby
188
+ pattern = Grex.generate(['кот'], convert_words: true)
189
+ regexp = Regexp.new(pattern)
190
+
191
+ regexp.match?('мир')
192
+ # => true
193
+
194
+ regexp.match?('!!!')
195
+ # => false
196
+ ```
197
+
198
+ The default is `false`. Since Ruby's `\w` is ASCII-only, the adapter replaces
199
+ grex's `\w` tokens with the explicit Unicode character class
200
+ `[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_Control}]`. Escaped literal backslashes
201
+ are preserved. Unicode compatibility tests cover Cyrillic, non-ASCII digits,
202
+ combining marks and join controls.
203
+
204
+ ### Non-word conversion
205
+
206
+ `convert_non_words: true` generalizes individual characters outside the Unicode
207
+ word class, including punctuation, whitespace and emoji. The default is `false`.
208
+ The adapter replaces grex's `\W` tokens with
209
+ `[^\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_Control}]`, preserving escaped literal
210
+ backslashes.
211
+
212
+ ```ruby
213
+ pattern = Grex.generate(['!'], convert_non_words: true)
214
+ regexp = Regexp.new(pattern)
215
+
216
+ regexp.match?('?')
217
+ # => true
218
+
219
+ regexp.match?('🐈')
220
+ # => true
221
+
222
+ regexp.match?('к')
223
+ # => false
224
+
225
+ regexp.match?('١')
226
+ # => false
227
+ ```
228
+
229
+ Letters, decimal digits, combining marks, connector punctuation such as `_`,
230
+ and join controls are excluded.
231
+
232
+ ### Escaping non-ASCII characters
233
+
234
+ Set `escape_non_ascii: true` to represent non-ASCII characters using `\u{...}`
235
+ escapes. The resulting pattern still matches the original characters:
236
+
237
+ ```ruby
238
+ pattern = Grex.generate(['кот'], escape_non_ascii: true)
239
+ # => "^\\u{43a}\\u{43e}\\u{442}$"
240
+
241
+ Regexp.new(pattern).match?('кот')
242
+ # => true
243
+
244
+ pattern = Grex.generate(['🐈'], escape_non_ascii: true)
245
+ # => "^\\u{1f408}$"
246
+
247
+ Regexp.new(pattern).match?('🐈')
248
+ # => true
249
+
250
+ Grex.generate(['cat'], escape_non_ascii: true)
251
+ # => "^cat$"
252
+ ```
253
+
254
+ The default is `false`, leaving Unicode characters literal. Emoji are escaped
255
+ as code points rather than UTF-16 surrogate pairs. This changes the pattern's
256
+ representation, not the characters it matches.
257
+
258
+ ### Matching patterns
259
+
49
260
  Pass the returned pattern to `Regexp.new` to match strings. Special characters
50
261
  in the examples, such as the dot below, are escaped:
51
262
 
52
263
  ```ruby
53
- pattern = GrexRuby.generate(['a.b'])
264
+ pattern = Grex.generate(['a.b'])
54
265
  # => "^a\\.b$"
55
266
 
56
267
  regexp = Regexp.new(pattern)
@@ -62,6 +273,130 @@ regexp.match?('axb')
62
273
  # => false
63
274
  ```
64
275
 
276
+ ### Capturing groups
277
+
278
+ By default, groups introduced by grex are non-capturing. Set
279
+ `capturing_groups: true` to make their matches available through `MatchData#captures`:
280
+
281
+ ```ruby
282
+ pattern = Grex.generate(%w[cat dog], capturing_groups: true)
283
+ regexp = Regexp.new(pattern)
284
+
285
+ regexp.match('cat').captures
286
+ # => ["cat"]
287
+
288
+ regexp.match('dog').captures
289
+ # => ["dog"]
290
+ ```
291
+
292
+ This option changes groups created by grex; it does not force a group around
293
+ every pattern.
294
+
295
+ ### Verbose patterns
296
+
297
+ Set `verbose: true` to generate a multiline pattern. Use `puts` to display
298
+ the actual line breaks:
299
+
300
+ ```ruby
301
+ pattern = Grex.generate(%w[cat dog], verbose: true)
302
+ puts pattern
303
+ ```
304
+
305
+ Output:
306
+
307
+ ```text
308
+ (?x)
309
+ ^
310
+ (?:
311
+ cat
312
+ |
313
+ dog
314
+ )
315
+ $
316
+ ```
317
+
318
+ The inline `(?x)` flag enables extended mode: formatting whitespace is ignored
319
+ and unescaped `#` starts a comment. grex escapes literal spaces and hashes from
320
+ the input examples so that they still match:
321
+
322
+ ```ruby
323
+ pattern = Grex.generate(['a b#c'], verbose: true)
324
+ regexp = Regexp.new(pattern)
325
+
326
+ regexp.match?('a b#c')
327
+ # => true
328
+
329
+ regexp.match?('ab#c')
330
+ # => false
331
+ ```
332
+
333
+ ### Anchors in Ruby
334
+
335
+ By default, grex generates `^` and `$` anchors. In Ruby these match the beginning
336
+ and end of a line, so the pattern can match a complete line inside multiline text:
337
+
338
+ ```ruby
339
+ regexp = Regexp.new(Grex.generate(['cat']))
340
+
341
+ regexp.match?("dog\ncat\nbird")
342
+ # => true
343
+ ```
344
+
345
+ Use `anchors: false` to allow matches within a line as well:
346
+
347
+ ```ruby
348
+ pattern = Grex.generate(['cat'], anchors: false)
349
+ # => "cat"
350
+
351
+ Regexp.new(pattern).match?('a cat sleeps')
352
+ # => true
353
+ ```
354
+
355
+ Set `start_anchor: false` to remove only the initial `^`, retaining the final `$`:
356
+
357
+ ```ruby
358
+ pattern = Grex.generate(['cat'], start_anchor: false)
359
+ # => "cat$"
360
+
361
+ regexp = Regexp.new(pattern)
362
+ regexp.match?('a cat')
363
+ # => true
364
+
365
+ regexp.match?('cat sleeps')
366
+ # => false
367
+ ```
368
+
369
+ Set `end_anchor: false` to remove only the final `$`, retaining the initial `^`:
370
+
371
+ ```ruby
372
+ pattern = Grex.generate(['cat'], end_anchor: false)
373
+ # => "^cat"
374
+
375
+ regexp = Regexp.new(pattern)
376
+ regexp.match?('cat sleeps')
377
+ # => true
378
+
379
+ regexp.match?('a cat')
380
+ # => false
381
+
382
+ Grex.generate(['cat'], start_anchor: false, end_anchor: false)
383
+ # => "cat"
384
+ ```
385
+
386
+ Both `start_anchor` and `end_anchor` default to `true`. Setting `anchors: false`
387
+ removes both anchors even if the individual settings are explicitly `true`:
388
+
389
+ ```ruby
390
+ Grex.generate(['cat'], anchors: false, start_anchor: true, end_anchor: true)
391
+ # => "cat"
392
+ ```
393
+
394
+ Ruby's `\A` and `\z` anchor the entire string rather than individual lines.
395
+ Grex preserves grex's line anchors and does not automatically replace them with
396
+ `\A` and `\z`.
397
+
398
+ ### Invalid input
399
+
65
400
  An empty array raises `ArgumentError` with the message `samples must not be empty`.
66
401
  Passing a string instead of an array, or an array containing an integer, raises
67
402
  `TypeError`.
@@ -164,4 +499,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/sk8hig
164
499
 
165
500
  ## License
166
501
 
167
- GrexRuby is available under the [MIT License](LICENSE.txt).
502
+ The Ruby bindings in this project are available under the [MIT License](LICENSE.txt).
503
+ The underlying Rust library grex is licensed under
504
+ [Apache License 2.0](licenses/grex-APACHE-2.0.txt).
505
+ Dependencies retain their own licenses; see [third-party notices](THIRD_PARTY_NOTICES.md).
data/Rakefile CHANGED
@@ -1,18 +1,18 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require "rb_sys/extensiontask"
9
-
10
- task build: :compile
11
-
12
- GEMSPEC = Gem::Specification.load("grex_ruby.gemspec")
13
-
14
- RbSys::ExtensionTask.new("grex_ruby", GEMSPEC) do |ext|
15
- ext.lib_dir = "lib/grex_ruby"
16
- end
17
-
18
- task default: %i[compile spec]
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rb_sys/extensiontask"
9
+
10
+ task build: :compile
11
+
12
+ GEMSPEC = Gem::Specification.load("grex_ruby.gemspec")
13
+
14
+ RbSys::ExtensionTask.new("grex_ruby", GEMSPEC) do |ext|
15
+ ext.lib_dir = "lib/grex_ruby"
16
+ end
17
+
18
+ task default: %i[compile spec]
@@ -0,0 +1,24 @@
1
+ # Third-party notices
2
+
3
+ The Ruby bindings in this repository are licensed under MIT; see `LICENSE.txt`.
4
+ Dependencies retain their own licenses. The MIT license for the bindings does
5
+ not replace the licenses of those dependencies.
6
+
7
+ ## grex
8
+
9
+ - Version: 1.4.6 (see `Cargo.lock` for the resolved version).
10
+ - Project: https://github.com/pemistahl/grex
11
+ - License: Apache License, Version 2.0.
12
+ - Upstream copyright notice: Copyright © 2019-today Peter M. Stahl pemistahl@gmail.com
13
+ - License text: `licenses/grex-APACHE-2.0.txt`, copied unchanged from the grex 1.4.6 crate.
14
+
15
+ The bindings call grex through its public API. The source gem declares grex as
16
+ a Cargo dependency; Cargo downloads it when building the native extension.
17
+ The resulting native extension contains compiled dependency code.
18
+
19
+ No separate NOTICE file was present in the grex 1.4.6 crate inspected for this
20
+ release. This document records grex attribution and is not an upstream NOTICE file.
21
+
22
+ Other dependencies listed in `Cargo.lock` and the gemspec have their own license
23
+ terms. Before distributing precompiled gems, include the applicable license
24
+ texts and notices for all dependencies incorporated into those binaries.
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "grex_ruby"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  edition = "2021"
5
5
  authors = ["Nelly Simkova <sk8high3r@ya.ru>"]
6
6
  publish = false
@@ -1,6 +1,6 @@
1
- # frozen_string_literal: true
2
-
3
- require "mkmf"
4
- require "rb_sys/mkmf"
5
-
6
- create_rust_makefile("grex_ruby/grex_ruby")
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+ require "rb_sys/mkmf"
5
+
6
+ create_rust_makefile("grex_ruby/grex_ruby")
@@ -1,18 +1,46 @@
1
1
  use grex::RegExpBuilder;
2
- use magnus::{function, prelude::*, Error, Ruby};
3
-
4
- pub fn generate(
5
- ruby: &Ruby,
6
- samples: Vec<String>,
7
- convert_digits: bool,
8
- convert_repetitions: bool,
9
- ) -> Result<String, Error> {
2
+ use magnus::{function, prelude::*, Error, RHash, Ruby};
3
+
4
+ pub fn generate(ruby: &Ruby, samples: Vec<String>, options: RHash) -> Result<String, Error> {
5
+ let convert_digits: bool = options.fetch(ruby.to_symbol("convert_digits"))?;
6
+ let convert_repetitions: bool = options.fetch(ruby.to_symbol("convert_repetitions"))?;
7
+ let minimum_substring_length: u32 =
8
+ options.fetch(ruby.to_symbol("minimum_substring_length"))?;
9
+ let minimum_repetitions: u32 = options.fetch(ruby.to_symbol("minimum_repetitions"))?;
10
+ let anchors: bool = options.fetch(ruby.to_symbol("anchors"))?;
11
+ let ignore_case: bool = options.fetch(ruby.to_symbol("ignore_case"))?;
12
+ let capturing_groups: bool = options.fetch(ruby.to_symbol("capturing_groups"))?;
13
+ let verbose: bool = options.fetch(ruby.to_symbol("verbose"))?;
14
+ let convert_non_digits: bool = options.fetch(ruby.to_symbol("convert_non_digits"))?;
15
+ let convert_whitespace: bool = options.fetch(ruby.to_symbol("convert_whitespace"))?;
16
+ let convert_non_whitespace: bool = options.fetch(ruby.to_symbol("convert_non_whitespace"))?;
17
+ let convert_words: bool = options.fetch(ruby.to_symbol("convert_words"))?;
18
+ let convert_non_words: bool = options.fetch(ruby.to_symbol("convert_non_words"))?;
19
+ let start_anchor: bool = options.fetch(ruby.to_symbol("start_anchor"))?;
20
+ let end_anchor: bool = options.fetch(ruby.to_symbol("end_anchor"))?;
21
+ let escape_non_ascii: bool = options.fetch(ruby.to_symbol("escape_non_ascii"))?;
22
+
10
23
  if samples.is_empty() {
11
24
  return Err(Error::new(
12
25
  ruby.exception_arg_error(),
13
26
  "samples must not be empty",
14
27
  ));
15
28
  }
29
+
30
+ if minimum_substring_length == 0 {
31
+ return Err(Error::new(
32
+ ruby.exception_arg_error(),
33
+ "minimum_substring_length must be greater than zero",
34
+ ));
35
+ }
36
+
37
+ if minimum_repetitions == 0 {
38
+ return Err(Error::new(
39
+ ruby.exception_arg_error(),
40
+ "minimum_repetitions must be greater than zero",
41
+ ));
42
+ }
43
+
16
44
  let mut builder = RegExpBuilder::from(&samples);
17
45
 
18
46
  if convert_digits {
@@ -23,12 +51,63 @@ pub fn generate(
23
51
  builder.with_conversion_of_repetitions();
24
52
  }
25
53
 
54
+ if ignore_case {
55
+ builder.with_case_insensitive_matching();
56
+ }
57
+
58
+ builder.with_minimum_substring_length(minimum_substring_length);
59
+ builder.with_minimum_repetitions(minimum_repetitions);
60
+
61
+ if !anchors {
62
+ builder.without_anchors();
63
+ }
64
+
65
+ if capturing_groups {
66
+ builder.with_capturing_groups();
67
+ }
68
+
69
+ if verbose {
70
+ builder.with_verbose_mode();
71
+ }
72
+
73
+ if convert_non_digits {
74
+ builder.with_conversion_of_non_digits();
75
+ }
76
+
77
+ if convert_whitespace {
78
+ builder.with_conversion_of_whitespace();
79
+ }
80
+
81
+ if convert_non_whitespace {
82
+ builder.with_conversion_of_non_whitespace();
83
+ }
84
+
85
+ if convert_words {
86
+ builder.with_conversion_of_words();
87
+ }
88
+
89
+ if convert_non_words {
90
+ builder.with_conversion_of_non_words();
91
+ }
92
+
93
+ if !start_anchor {
94
+ builder.without_start_anchor();
95
+ }
96
+
97
+ if !end_anchor {
98
+ builder.without_end_anchor();
99
+ }
100
+
101
+ if escape_non_ascii {
102
+ builder.with_escaping_of_non_ascii_chars(false);
103
+ }
104
+
26
105
  Ok(builder.build())
27
106
  }
28
107
 
29
108
  #[magnus::init]
30
109
  fn init(ruby: &Ruby) -> Result<(), Error> {
31
- let module = ruby.define_module("GrexRuby")?;
32
- module.define_singleton_method("generate_native", function!(generate, 3))?;
110
+ let module = ruby.define_module("Grex")?;
111
+ module.define_singleton_method("generate_native", function!(generate, 2))?;
33
112
  Ok(())
34
113
  }
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module GrexRuby
4
- VERSION = "0.1.0"
3
+ module Grex
4
+ VERSION = "0.2.0"
5
5
  end
6
+
7
+ # Compatibility with the public module name used in version 0.1.0.
8
+ GrexRuby = Grex
data/lib/grex_ruby.rb CHANGED
@@ -1,37 +1,66 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'grex_ruby/version'
4
- require 'grex_ruby/grex_ruby'
5
-
6
- module GrexRuby
7
- class Error < StandardError; end
8
-
9
- def self.generate(samples, convert_digits: false, convert_repetitions: false)
10
- pattern = generate_native(samples, convert_digits, convert_repetitions)
11
- adapt_pattern(pattern)
12
- end
13
-
14
- def self.adapt_pattern(pattern)
15
- chars = pattern.chars
16
- result = +''
17
- index = 0
18
-
19
- while index < chars.length
20
- if chars[index] == '\\'
21
- result << if chars[index + 1] == 'd'
22
- '\p{Nd}'
23
- else
24
- chars[index, 2].join
25
- end
26
- index += 2
27
- else
28
- result << chars[index]
29
- index += 1
30
- end
31
- end
32
-
33
- result
34
- end
35
-
36
- private_class_method :generate_native, :adapt_pattern
37
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'grex_ruby/version'
4
+ require 'grex_ruby/grex_ruby'
5
+
6
+ module Grex
7
+ class Error < StandardError; end
8
+
9
+ def self.generate(samples,
10
+ convert_digits: false,
11
+ convert_repetitions: false,
12
+ minimum_substring_length: 1,
13
+ minimum_repetitions: 1,
14
+ anchors: true,
15
+ ignore_case: false,
16
+ capturing_groups: false,
17
+ verbose: false,
18
+ convert_non_digits: false,
19
+ convert_whitespace: false,
20
+ convert_non_whitespace: false,
21
+ convert_words: false,
22
+ convert_non_words: false,
23
+ start_anchor: true,
24
+ end_anchor: true,
25
+ escape_non_ascii: false)
26
+ pattern = generate_native(samples,
27
+ { convert_digits:, convert_repetitions:, minimum_substring_length:, minimum_repetitions:,
28
+ anchors:, ignore_case:, capturing_groups:, verbose:, convert_non_digits:, convert_whitespace:, convert_non_whitespace:, convert_words:, convert_non_words:, start_anchor:, end_anchor:, escape_non_ascii: })
29
+ adapt_pattern(pattern)
30
+ end
31
+
32
+ def self.adapt_pattern(pattern)
33
+ chars = pattern.chars
34
+ result = +''
35
+ index = 0
36
+
37
+ while index < chars.length
38
+ if chars[index] == '\\'
39
+ result << case chars[index + 1]
40
+ when 'd'
41
+ '\p{Nd}'
42
+ when 'D'
43
+ '\P{Nd}'
44
+ when 's'
45
+ '\p{Space}'
46
+ when 'S'
47
+ '\P{Space}'
48
+ when 'w'
49
+ '[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_Control}]'
50
+ when 'W'
51
+ '[^\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_Control}]'
52
+ else
53
+ chars[index, 2].join
54
+ end
55
+ index += 2
56
+ else
57
+ result << chars[index]
58
+ index += 1
59
+ end
60
+ end
61
+
62
+ result
63
+ end
64
+
65
+ private_class_method :generate_native, :adapt_pattern
66
+ end
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/sig/grex_ruby.rbs CHANGED
@@ -1,5 +1,25 @@
1
- module GrexRuby
1
+ module Grex
2
2
  VERSION: String
3
3
 
4
- def self.generate: (Array[String], ?convert_digits: bool, ?convert_repetitions: bool) -> String
4
+ def self.generate: (
5
+ Array[String],
6
+ ?convert_digits: bool,
7
+ ?convert_repetitions: bool,
8
+ ?minimum_substring_length: Integer,
9
+ ?minimum_repetitions: Integer,
10
+ ?anchors: bool,
11
+ ?ignore_case: bool,
12
+ ?capturing_groups: bool,
13
+ ?verbose: bool,
14
+ ?convert_non_digits: bool,
15
+ ?convert_whitespace: bool,
16
+ ?convert_non_whitespace: bool,
17
+ ?convert_words: bool,
18
+ ?convert_non_words: bool,
19
+ ?start_anchor: bool,
20
+ ?end_anchor: bool,
21
+ ?escape_non_ascii: bool,
22
+ ) -> String
5
23
  end
24
+
25
+ module GrexRuby = Grex
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grex_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nelly Simkova
@@ -37,17 +37,22 @@ files:
37
37
  - LICENSE.txt
38
38
  - README.md
39
39
  - Rakefile
40
+ - THIRD_PARTY_NOTICES.md
40
41
  - ext/grex_ruby/Cargo.toml
41
42
  - ext/grex_ruby/build.rs
42
43
  - ext/grex_ruby/extconf.rb
43
44
  - ext/grex_ruby/src/lib.rs
44
45
  - lib/grex_ruby.rb
45
46
  - lib/grex_ruby/version.rb
47
+ - licenses/grex-APACHE-2.0.txt
46
48
  - sig/grex_ruby.rbs
47
49
  homepage: https://github.com/sk8higher/grex_ruby
48
50
  licenses:
49
51
  - MIT
50
- metadata: {}
52
+ metadata:
53
+ homepage_uri: https://github.com/sk8higher/grex_ruby
54
+ source_code_uri: https://github.com/sk8higher/grex_ruby
55
+ changelog_uri: https://github.com/sk8higher/grex_ruby/blob/main/CHANGELOG.md
51
56
  rdoc_options: []
52
57
  require_paths:
53
58
  - lib