Skip to content

Commit 3dbc35c

Browse files
authored
Merge pull request #52 from sirosen/expose-flags
Add support for passing flags as strings
2 parents 3819d34 + df9b07b commit 3dbc35c

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

regress.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ from __future__ import annotations
22

33
from typing import Iterable
44

5+
class RegressError(Exception): ...
6+
57
class Regex:
6-
def __init__(self, pattern: str): ...
8+
def __init__(self, pattern: str, flags: str | None = None): ...
79
def find(self, text: str) -> Match | None: ...
810
def find_iter(self, text: str) -> Iterable[Match] | None: ...
911

src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ struct RegexPy {
1212
#[pymethods]
1313
impl RegexPy {
1414
#[new]
15-
fn init(value: &str) -> PyResult<Self> {
16-
match Regex::new(value) {
17-
Ok(inner) => Ok(RegexPy { inner }),
18-
Err(e) => Err(RegressError::new_err(e.to_string())),
15+
#[pyo3(signature = (value, flags=None))]
16+
fn init(value: &str, flags: Option<&str>) -> PyResult<Self> {
17+
match flags {
18+
Some(f) => match Regex::with_flags(value, f) {
19+
Ok(inner) => Ok(RegexPy { inner }),
20+
Err(e) => Err(RegressError::new_err(e.to_string())),
21+
},
22+
None => match Regex::new(value) {
23+
Ok(inner) => Ok(RegexPy { inner }),
24+
Err(e) => Err(RegressError::new_err(e.to_string())),
25+
},
1926
}
2027
}
2128

tests/test_regress.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,16 @@ def test_error_handling():
6060
pass
6161
else:
6262
pytest.fail("error not reached")
63+
64+
65+
def test_with_flags():
66+
# "L" for letters, "Z" for spaces, "N" for numerics
67+
pattern = r"^\p{L}\p{Z}\p{N}$"
68+
69+
regex = regress.Regex(pattern, flags="u")
70+
flagless_regex = regress.Regex(pattern)
71+
72+
match = regex.find("a 0")
73+
flagless_match = flagless_regex.find("a 0")
74+
assert match is not None
75+
assert flagless_match is None

0 commit comments

Comments
 (0)