Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require "rust_regexp"
Regular expressions should be pre-compiled before use:

```ruby
re = RustRegexp.new('(\w+):(\d+)')
re = RustRegexp.new('p.t{2}ern*')
# => #<RustRegexp:...>
```

Expand All @@ -41,31 +41,37 @@ re = RustRegexp.new('(\w+):(\d+)')
To find a single match in the haystack:

```ruby
re.match("ruby:123, rust:456")
RustRegexp.new('\w+:\d+').match("ruby:123, rust:456")
# => ["ruby:123"]

RustRegexp.new('(\w+):(\d+)').match("ruby:123, rust:456")
# => ["ruby", "123"]
```

To find all matches in the haystack:

```ruby
re.scan("ruby:123, rust:456")
RustRegexp.new('\w+:\d+').scan("ruby:123, rust:456")
# => ["ruby:123", "rust:456"]

RustRegexp.new('(\w+):(\d+)').scan("ruby:123, rust:456")
# => [["ruby", "123"], ["rust", "456"]]
```

To check whether there is at least one match in the haystack:

```ruby
re.match?("ruby:123")
RustRegexp.new('\w+:\d+').match?("ruby:123")
# => true

re.match?("ruby")
RustRegexp.new('\w+:\d+').match?("ruby")
# => false
```

Inspect original pattern:

```ruby
re.pattern
RustRegexp.new('\w+:\d+').pattern
# => "(\\w+):(\\d+)"
```

Expand Down Expand Up @@ -94,18 +100,18 @@ set.match("ghidefabc") # => [0, 1, 2]
To check whether at least one pattern from the set matches the haystack:

```ruby
set.match?("abc")
RustRegexp::Set.new(["abc", "def"]).match?("abc")
# => true

set.match?("123")
RustRegexp::Set.new(["abc", "def"]).match?("123")
# => false
```

Inspect original patterns:

```ruby
set.patterns
# => ["abc", "def", "ghi", "xyz"]
RustRegexp::Set.new(["abc", "def"]).patterns
# => ["abc", "def"]
```

## Development
Expand Down
10 changes: 3 additions & 7 deletions ext/rust_regexp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl RustRegexp {
let regex = &self.0;
let haystack = unsafe { haystack.as_slice() };

// no capture groups defined except the default one
if regex.captures_len() == 1 {
// speed optimization, `.find` is faster than `.captures`
if let Some(capture) = regex.find(haystack) {
Expand Down Expand Up @@ -65,17 +66,12 @@ impl RustRegexp {
let regex = &self.0;
let haystack = unsafe { haystack.as_slice() };

// no capture groups defined except the default one
if regex.captures_len() == 1 {
// speed optimization, `.find_iter` is faster than `.captures_iter`
for capture in regex.find_iter(haystack) {
let group = RArray::with_capacity(1);

group
.push(Self::capture_to_ruby_string(&capture))
.expect("Non-frozen array");

result
.push(group)
.push(Self::capture_to_ruby_string(&capture))
.expect("Non-frozen array");
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion spec/rust_regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

describe "#scan" do
examples = [
['\w+:\d+', "ruby:123, rust:456", [["ruby:123"], ["rust:456"]]],
['\w+:\d+', "ruby:123, rust:456", ["ruby:123", "rust:456"]],
['(\w+):(\d+)', 'ruby:123, rust:456', [["ruby", "123"], ["rust", "456"]]],
['(\w+):(\d+)', '123', []],
]
Expand Down
Loading