Skip to content

Commit

Permalink
Merge pull request #84 from G-Research/83-find_matches_as_indexes-ret…
Browse files Browse the repository at this point in the history
…urns-bogus-values

Handle empty patterns more reasonably
  • Loading branch information
itamarst authored Sep 21, 2023
2 parents 7bcfe13 + f86ec64 commit 10411e3
Show file tree
Hide file tree
Showing 87 changed files with 96 additions and 32 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added .hypothesis/unicode_data/13.0.0/charmap.json.gz
Binary file not shown.
Binary file added .hypothesis/unicode_data/13.0.0/codec-utf-8.json.gz
Binary file not shown.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Changelog

## 0.19.0

* If an empty string is passed in as a pattern, `AhoCorasick()` will now raise a `ValueError`.
Previously using empty patterns could result in garbage results or exceptions when matching.
* Upgraded to `aho-corasick` v1.1.1.

## 0.18.0

* Upgrade to `aho-corasick` v1.1.0, which can run faster on ARM machines like newer Macs.
* Upgraded to `aho-corasick` v1.1.0, which can run faster on ARM machines like newer Macs.

## 0.17.0

Expand Down
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ahocorasick_rs"
version = "0.18.0"
version = "0.19.0"
edition = "2021"
authors = ["G-Research <[email protected]>", "Itamar Turner-Trauring <[email protected]>"]
description = "Search a string for multiple substrings at once"
Expand Down
5 changes: 4 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ install-dev-dependencies:

setup: venv install-dev-dependencies

test:
lint:
flake8 tests/
black --check tests/
mypy --strict tests

test:
pytest tests/

# Prepare for benchmarking; will only work on Linux:
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.72"
channel = "1.72.1"
components = ["rustfmt", "clippy"]
62 changes: 39 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::sync::{Arc, Mutex};
use std::{sync::{
Arc, Mutex,
}, cell::Cell};

use aho_corasick::{
AhoCorasick, AhoCorasickBuilder, AhoCorasickKind, Match, MatchError, MatchKind,
Expand All @@ -14,7 +16,9 @@ use pyo3::{
///
/// Takes three arguments:
///
/// * ``patterns``: A list of strings, the patterns to match against.
/// * ``patterns``: A list of strings, the patterns to match against. Empty
/// patterns are not supported and will result in a ``ValueError`` exception
/// being raised.
/// * ``matchkind``: Defaults to ``"MATCHKING_STANDARD"``.
/// * ``store_patterns``: If ``True``, keep a reference to the patterns, which
/// will speed up ``find_matches_as_strings()`` but will use more memory. If
Expand Down Expand Up @@ -177,27 +181,39 @@ impl PyAhoCorasick {
None
};

let result = Ok(Self {
ac_impl: AhoCorasickBuilder::new()
.kind(implementation.map(|i| i.into()))
.match_kind(matchkind.into())
.build(
first_few_patterns
.into_iter()
.chain(patterns_iter)
.chunks(10 * 1024)
.into_iter()
.flat_map(|chunk| {
let result = chunk.filter_map(|s| s.extract::<String>().ok());
// Release the GIL in case some other thread wants to do work:
py.allow_threads(|| ());
result
}),
)
// TODO make sure this error is menaingful to Python users
.map_err(|e| PyValueError::new_err(e.to_string()))?,
patterns,
});
let has_empty_patterns = Cell::new(false);
let ac_impl = AhoCorasickBuilder::new()
.kind(implementation.map(|i| i.into()))
.match_kind(matchkind.into())
.build(
first_few_patterns
.into_iter()
.chain(patterns_iter)
.chunks(10 * 1024)
.into_iter()
.flat_map(|chunk| {
let result =
chunk
.filter_map(|s| s.extract::<String>().ok())
.inspect(|s| {
if s.is_empty() {
has_empty_patterns.set(true);
}
});
// Release the GIL in case some other thread wants to do work:
py.allow_threads(|| ());
result
}),
) // TODO make sure this error is menaingful to Python users
.map_err(|e| PyValueError::new_err(e.to_string()))?;

if has_empty_patterns.get() {
return Err(PyValueError::new_err(
"You passed in an empty string as a pattern",
));
}

let result = Ok(Self { ac_impl, patterns });
if let Ok(mut guard) = patterns_error.lock() {
if let Some(err) = guard.take() {
return Err(err);
Expand Down
39 changes: 39 additions & 0 deletions tests/test_ac.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,45 @@ def test_unicode_extensive(
assert ac.find_matches_as_strings(haystack) == [pattern]


@pytest.mark.parametrize("bad_patterns", [[""], ["", "xx"], ["xx", ""]])
@pytest.mark.parametrize("store_patterns", [True, False])
def test_empty_patterns_are_not_legal(
bad_patterns: list[str], store_patterns: bool
) -> None:
"""
Passing in an empty pattern suggests a bug in user code, and the outputs
are bad when you do have that, so raise an error.
"""
with pytest.raises(ValueError) as e:
AhoCorasick(bad_patterns, store_patterns=store_patterns)
assert "You passed in an empty string as a pattern" in str(e.value)


@given(st.text(min_size=1), st.text(), st.sampled_from([True, False, None]))
def test_unicode_totally_random(
pattern: str, haystack: str, store_patterns: Optional[bool]
) -> None:
"""
Catch more edge cases of patterns and haystacks.
"""
if store_patterns is None:
ac = AhoCorasick([pattern])
else:
ac = AhoCorasick([pattern], store_patterns=store_patterns)

index_matches = ac.find_matches_as_indexes(haystack)
string_matches = ac.find_matches_as_strings(haystack)

expected_index = haystack.find(pattern)
if expected_index == -1:
assert index_matches == []
assert string_matches == []
else:
assert index_matches[0][1] == expected_index
assert [haystack[s:e] for (_, s, e) in index_matches][0] == pattern
assert string_matches[0] == pattern


def test_matchkind() -> None:
"""
Different matchkinds give different results.
Expand Down

0 comments on commit 10411e3

Please sign in to comment.