fast_regexp 0.4.0-x86_64-linux

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: d198fcd2737203f54d11eb85aa27fa8984c80707b063656a9935213866bcf637
4
+ data.tar.gz: a41f3e686257d687ab331047b16e68154a4b083f1abb87c3a42f0eb4e8543405
5
+ SHA512:
6
+ metadata.gz: 1b6d6fae53917230d07d711a3d6b1c704c1fe56d5f41bebc8d27721cd3ccd13f06e2539362e58dcd369b04abb6bd249411d02608082518713d6352757cc82c20
7
+ data.tar.gz: a8766de6e78b32ca0d7dc6f032095f5773a24f2c083c005502f9a35d21d201b23be73195929b921feeec54306419af1cd27005d52ac1e147888e442b39651d5a
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,217 @@
1
+ # fast_regexp
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/fast_regexp.svg)](https://badge.fury.io/rb/fast_regexp)
4
+ [![Test](https://github.com/jetpks/fast_regexp/workflows/CI/badge.svg)](https://github.com/jetpks/fast_regexp/actions)
5
+
6
+ Ruby bindings for [rust/regex](https://docs.rs/regex/latest/regex/) library.
7
+
8
+ ## Installation
9
+
10
+ Install [Rust](https://www.rust-lang.org/) via [rustup](https://rustup.rs/) or in any other way.
11
+
12
+ Add as a dependency:
13
+
14
+ ```ruby
15
+ # In your Gemfile
16
+ gem "fast_regexp"
17
+
18
+ # Or without Bundler
19
+ gem install fast_regexp
20
+ ```
21
+
22
+ Include in your code:
23
+
24
+ ```ruby
25
+ require "fast_regexp"
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ Regular expressions should be pre-compiled before use:
31
+
32
+ ```ruby
33
+ re = Fast::Regexp.new('p.t{2}ern*')
34
+ # => #<Fast::Regexp:...>
35
+ ```
36
+
37
+ > [!TIP]
38
+ > Note the use of *single quotes* when passing the regular expression as
39
+ > a string to `rust/regex` so that the backslashes aren't interpreted as escapes.
40
+
41
+ You can also build from an existing Ruby `Regexp` — trailing flags (`/i`,
42
+ `/x`, `/m`) are translated to inline form for the rust engine:
43
+
44
+ ```ruby
45
+ Fast::Regexp.new(/foo/i).pattern # => "(?i)foo"
46
+ Fast::Regexp.new(/foo.bar/m).match?("foo\nbar") # => true (Ruby's /m = dotall)
47
+ ```
48
+
49
+ ### Matching
50
+
51
+ `#match` returns a `Fast::Regexp::MatchData` on a hit and `nil` on no match —
52
+ matching Ruby's `Regexp#match` shape:
53
+
54
+ ```ruby
55
+ m = Fast::Regexp.new('(\w+):(\d+)').match("ruby:123, rust:456")
56
+ m[0] # => "ruby:123" (whole match)
57
+ m[1] # => "ruby"
58
+ m[2] # => "123"
59
+ m.pre_match # => ""
60
+ m.post_match # => ", rust:456"
61
+ m.captures # => ["ruby", "123"]
62
+ m.to_a # => ["ruby:123", "ruby", "123"]
63
+ m.byteoffset(0) # => [0, 8]
64
+
65
+ Fast::Regexp.new('\d+').match("abc") # => nil
66
+ ```
67
+
68
+ Named captures use rust/regex's `(?P<name>...)` syntax:
69
+
70
+ ```ruby
71
+ m = Fast::Regexp.new('(?P<word>\w+):(?P<num>\d+)').match("ruby:123")
72
+ m[:word] # => "ruby"
73
+ m["num"] # => "123"
74
+ m.named_captures # => { "word" => "ruby", "num" => "123" }
75
+ ```
76
+
77
+ `#match?`, `#===`, and `#=~` are also available:
78
+
79
+ ```ruby
80
+ re = Fast::Regexp.new('\d+')
81
+ re.match?("123") # => true
82
+ re === "abc 42" # => true (works in case/when)
83
+ re =~ "abc 42" # => 4 (byte offset of first match)
84
+ ```
85
+
86
+ ### Scanning
87
+
88
+ ```ruby
89
+ Fast::Regexp.new('\w+:\d+').scan("ruby:123, rust:456")
90
+ # => ["ruby:123", "rust:456"]
91
+
92
+ Fast::Regexp.new('(\w+):(\d+)').scan("ruby:123, rust:456")
93
+ # => [["ruby", "123"], ["rust", "456"]]
94
+ ```
95
+
96
+ For per-match positions and pre/post-match access, use `#scan_matches`:
97
+
98
+ ```ruby
99
+ Fast::Regexp.new('(\w+):(\d+)').scan_matches("ruby:123, rust:456").map { |m| m.byteoffset(0) }
100
+ # => [[0, 8], [10, 18]]
101
+ ```
102
+
103
+ ### Substitution
104
+
105
+ `#sub` and `#gsub` use rust/regex's native replacement template — `$1`,
106
+ `${name}`, and `$$` for a literal `$`:
107
+
108
+ ```ruby
109
+ re = Fast::Regexp.new('(\w+):(\d+)')
110
+ re.sub("ruby:123 rust:456", '$2-$1') # => "123-ruby rust:456"
111
+ re.gsub("ruby:123 rust:456", '$2-$1') # => "123-ruby 456-rust"
112
+ ```
113
+
114
+ Block form receives a `MatchData`:
115
+
116
+ ```ruby
117
+ Fast::Regexp.new('\d+').gsub("a1 b22 c333") { |m| "<#{m[0].size}>" }
118
+ # => "a<1> b<2> c<3>"
119
+ ```
120
+
121
+ Pass `literal: true` to disable `$`-expansion entirely.
122
+
123
+ ### Other
124
+
125
+ ```ruby
126
+ Fast::Regexp.new('\w+:\d+').pattern # => "\\w+:\\d+"
127
+ Fast::Regexp.new('(?P<n>\w+)').names # => ["n"]
128
+ Fast::Regexp.new('(a)(b)').captures_count # => 2
129
+ ```
130
+
131
+ > [!WARNING]
132
+ > `rust/regex` regular expression syntax differs from Ruby's built-in
133
+ > [`Regexp`](https://docs.ruby-lang.org/en/3.4/Regexp.html) library, see the
134
+ > [official syntax page](https://docs.rs/regex/latest/regex/index.html#syntax) for more
135
+ > details.
136
+
137
+ ### Searching simultaneously
138
+
139
+ `Fast::Regexp::Set` represents a collection of
140
+ regular expressions that can be searched for simultaneously. Calling `Fast::Regexp::Set#match` will return an array containing the indices of all the patterns that matched.
141
+
142
+ ```ruby
143
+ set = Fast::Regexp::Set.new(["abc", "def", "ghi", "xyz"])
144
+
145
+ set.match("abcdefghi") # => [0, 1, 2]
146
+ set.match("ghidefabc") # => [0, 1, 2]
147
+ ```
148
+
149
+ > [!NOTE]
150
+ > Matches arrive in the order the constituent patterns were declared,
151
+ > not the order they appear in the haystack.
152
+
153
+ To check whether at least one pattern from the set matches the haystack:
154
+
155
+ ```ruby
156
+ Fast::Regexp::Set.new(["abc", "def"]).match?("abc")
157
+ # => true
158
+
159
+ Fast::Regexp::Set.new(["abc", "def"]).match?("123")
160
+ # => false
161
+ ```
162
+
163
+ Inspect original patterns:
164
+
165
+ ```ruby
166
+ Fast::Regexp::Set.new(["abc", "def"]).patterns
167
+ # => ["abc", "def"]
168
+ ```
169
+
170
+ ## Encoding
171
+
172
+ Currently, `fast_regexp` expects the haystack to be an UTF-8 string.
173
+
174
+ It also supports parsing of strings with invalid UTF-8 characters by default. It's achieved via using `regex::bytes` instead of plain `regex` under the hood, so any byte sequence can be matched. The output match is encoded as UTF-8 string.
175
+
176
+ In case unicode awarness of matchers should be disabled, both `Fast::Regexp` and `Fast::Regexp::Set` support `unicode: false` option:
177
+
178
+ ```ruby
179
+ Fast::Regexp.new('\w+').match('ю٤夏')
180
+ # => ["ю٤夏"]
181
+
182
+ Fast::Regexp.new('\w+', unicode: false).match('ю٤夏')
183
+ # => []
184
+
185
+ Fast::Regexp::Set.new(['\w', '\d', '\s']).match("ю٤\u2000")
186
+ # => [0, 1, 2]
187
+
188
+ Fast::Regexp::Set.new(['\w', '\d', '\s'], unicode: false).match("ю٤\u2000")
189
+ # => []
190
+ ```
191
+
192
+ ## Development
193
+
194
+ ```sh
195
+ bin/setup # install deps
196
+ bin/console # interactive prompt to play around
197
+ rake compile # (re)compile extension
198
+ rake spec # run tests
199
+ ```
200
+
201
+ ## Contributing
202
+
203
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jetpks/fast_regexp.
204
+
205
+ ## Credits
206
+
207
+ `fast_regexp` is a fork of
208
+ [`rust_regexp`](https://github.com/ocvit/rust_regexp) by Dmytro Horoshko —
209
+ huge thanks for the original bindings and the clean magnus integration that
210
+ made this work easy to extend. This fork rebrands the gem, reshapes the
211
+ public API (`Fast::Regexp`, real `MatchData`, `sub`/`gsub`, `===`/`=~`,
212
+ `Regexp`-constructor coercion), and releases the GVL around regex execution
213
+ for thread/fiber-friendly matching.
214
+
215
+ ## License
216
+
217
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Binary file
Binary file
Binary file
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fast
4
+ class Regexp
5
+ VERSION = "0.4.0"
6
+ end
7
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "fast_regexp/version"
4
+ require_relative "fast_regexp/fast_regexp"
5
+
6
+ # Ruby-side conveniences on top of the Rust extension. The native class only
7
+ # exposes raw primitives (`_native_new`, `_native_match`, `_native_sub`,
8
+ # `_native_gsub`); everything user-facing lives here so the API can grow
9
+ # without touching FFI.
10
+ module Fast
11
+ class Regexp
12
+ RUBY_FLAG_MAP = {
13
+ ::Regexp::IGNORECASE => "i",
14
+ ::Regexp::EXTENDED => "x",
15
+ ::Regexp::MULTILINE => "s" # Ruby's /m = dotall; in rust/regex that is (?s).
16
+ }.freeze
17
+
18
+ class << self
19
+ # Accept either a pattern string or an existing `::Regexp`. When given a
20
+ # Regexp the flags (`/i`, `/x`, `/m`) are translated into a leading
21
+ # inline group so the rust/regex engine sees them. Unsupported features
22
+ # (lookaround, backrefs) still raise from the engine with a clear
23
+ # message.
24
+ def new(pattern, **opts)
25
+ pattern = translate_regexp(pattern) if pattern.is_a?(::Regexp)
26
+ _native_new(pattern, **opts)
27
+ end
28
+
29
+ private
30
+
31
+ def translate_regexp(regexp)
32
+ flags = RUBY_FLAG_MAP.each_with_object(+"") do |(bit, char), acc|
33
+ acc << char if (regexp.options & bit) != 0
34
+ end
35
+ flags.empty? ? regexp.source : "(?#{flags})#{regexp.source}"
36
+ end
37
+ end
38
+
39
+ # Returns a `Fast::Regexp::MatchData` on hit, `nil` on miss — matching
40
+ # Ruby's `Regexp#match` shape so the old `[]`-truthy-but-empty trap is
41
+ # gone.
42
+ def match(haystack)
43
+ _native_match(coerce_string(haystack))
44
+ end
45
+
46
+ # Case-equality. Enables `case/when` and RSpec's
47
+ # `expect(str).to match(re)`.
48
+ def ===(other)
49
+ return false unless other.respond_to?(:to_str)
50
+ match?(other.to_str)
51
+ end
52
+
53
+ # Returns the byte offset of the first match, or nil. Matches the
54
+ # semantics of `Regexp#=~` except positions are in **bytes** (rust/regex
55
+ # is byte-based).
56
+ def =~(other)
57
+ return nil unless other.respond_to?(:to_str)
58
+ m = match(other.to_str)
59
+ m && m.byte_begin(0)
60
+ end
61
+
62
+ # `sub(haystack, replacement)` or `sub(haystack) { |m| ... }`.
63
+ #
64
+ # The string form uses rust/regex's native replacement template: `$1`,
65
+ # `${name}`, `$$` for a literal `$`. To pass a replacement string that
66
+ # contains `$` literally, pass `literal: true`.
67
+ def sub(haystack, replacement = nil, literal: false, &block)
68
+ haystack = coerce_string(haystack)
69
+ if block
70
+ raise ArgumentError, "wrong number of arguments (given 2, expected 1 with block)" if replacement
71
+ m = match(haystack)
72
+ return haystack.dup unless m
73
+ "#{m.pre_match}#{block.call(m)}#{m.post_match}"
74
+ else
75
+ raise ArgumentError, "wrong number of arguments (given 1, expected 2)" if replacement.nil?
76
+ _native_sub(haystack, coerce_string(replacement), literal)
77
+ end
78
+ end
79
+
80
+ # `gsub(haystack, replacement)` or `gsub(haystack) { |m| ... }`. See
81
+ # `#sub` for the template syntax.
82
+ def gsub(haystack, replacement = nil, literal: false, &block)
83
+ haystack = coerce_string(haystack)
84
+ if block
85
+ raise ArgumentError, "wrong number of arguments (given 2, expected 1 with block)" if replacement
86
+ gsub_with_block(haystack, &block)
87
+ else
88
+ raise ArgumentError, "wrong number of arguments (given 1, expected 2)" if replacement.nil?
89
+ _native_gsub(haystack, coerce_string(replacement), literal)
90
+ end
91
+ end
92
+
93
+ def inspect
94
+ "#<Fast::Regexp #{pattern.inspect}>"
95
+ end
96
+
97
+ alias_method :to_s, :pattern
98
+
99
+ private
100
+
101
+ def gsub_with_block(haystack)
102
+ matches = scan_matches(haystack)
103
+ return haystack.dup if matches.empty?
104
+
105
+ out = String.new(encoding: Encoding::UTF_8)
106
+ cursor = 0
107
+ matches.each do |m|
108
+ bs, be = m.byteoffset(0)
109
+ out << haystack.byteslice(cursor, bs - cursor) if bs > cursor
110
+ out << yield(m).to_s
111
+ cursor = be
112
+ end
113
+ out << haystack.byteslice(cursor, haystack.bytesize - cursor) if cursor < haystack.bytesize
114
+ out
115
+ end
116
+
117
+ def coerce_string(value)
118
+ return value if value.is_a?(String)
119
+ return value.to_str if value.respond_to?(:to_str)
120
+ raise TypeError, "no implicit conversion of #{value.class} into String"
121
+ end
122
+
123
+ class MatchData
124
+ include Enumerable
125
+
126
+ def each(&block)
127
+ to_a.each(&block)
128
+ end
129
+
130
+ def values_at(*indices)
131
+ indices.map { |i| self[i] }
132
+ end
133
+
134
+ def ==(other)
135
+ other.is_a?(MatchData) && to_a == other.to_a && string == other.string
136
+ end
137
+ alias_method :eql?, :==
138
+
139
+ def hash
140
+ [to_a, string].hash
141
+ end
142
+ end
143
+ end
144
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fast_regexp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: x86_64-linux
6
+ authors:
7
+ - Eric Jacobs
8
+ - Dmytro Horoshko
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2026-05-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: 'Ruby bindings to rust/regex with a Fast::Regexp API: MatchData, sub/gsub,
15
+ ===, =~, named captures, and GVL release for thread/fiber-friendly matching.'
16
+ email:
17
+ - eric@ebj.dev
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - LICENSE.txt
23
+ - README.md
24
+ - lib/fast_regexp.rb
25
+ - lib/fast_regexp/3.3/fast_regexp.so
26
+ - lib/fast_regexp/3.4/fast_regexp.so
27
+ - lib/fast_regexp/4.0/fast_regexp.so
28
+ - lib/fast_regexp/version.rb
29
+ homepage: https://github.com/jetpks/fast_regexp
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ bug_tracker_uri: https://github.com/jetpks/fast_regexp/issues
34
+ homepage_uri: https://github.com/jetpks/fast_regexp
35
+ source_code_uri: https://github.com/jetpks/fast_regexp
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '3.3'
45
+ - - "<"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.1.dev
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.5.23
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Fast regex for Ruby, backed by rust/regex
58
+ test_files: []