Skip to content

Commit 33f0ed2

Browse files
committed
add python 3.15 re compat: prefixmatch
1 parent a896491 commit 33f0ed2

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ numbers = pattern.findall(b"line 1\nline 22")
8181
```
8282

8383
`pcre` mirrors the core helpers from Python’s standard library `re` module
84-
`match`, `search`, `fullmatch`, `finditer`, `findall`, and `compile` while
84+
`prefixmatch`, `match`, `search`, `fullmatch`, `finditer`, `findall`, and `compile` while
8585
exposing PCRE2’s extended flag set through the Pythonic `Flag` enum
8686
(`Flag.CASELESS`, `Flag.MULTILINE`, `Flag.UTF`, ...).
8787

@@ -90,6 +90,9 @@ exposing PCRE2’s extended flag set through the Pythonic `Flag` enum
9090
- Module-level helpers and the `Pattern` class follow the same call shapes as
9191
the standard library `re` module, including `pos`, `endpos`, and `flags`
9292
behaviour.
93+
- Python 3.15's `prefixmatch()` alias is available at both the module level
94+
and on compiled `Pattern` objects, and `re.NOFLAG` is re-exported as the
95+
zero-value compatibility alias.
9396
- `Pattern` mirrors `re.Pattern` attributes like `.pattern`, `.groupindex`,
9497
and `.groups`, while `Match` objects surface the familiar `.re`, `.string`,
9598
`.pos`, `.endpos`, `.lastindex`, `.lastgroup`, `.regs`, and `.expand()` API.

pcre/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
fullmatch,
3434
match,
3535
parallel_map,
36+
prefixmatch,
3637
search,
3738
split,
3839
sub,
@@ -84,6 +85,7 @@ def escape(pattern: Any) -> Any:
8485
# continue referencing familiar names. Prefer `pcre.Flag` for new code.
8586
_FLAG_ZERO = Flag(0)
8687
_FLAG_COMPAT_ALIASES = {
88+
"NOFLAG": _FLAG_ZERO,
8789
"IGNORECASE": Flag.CASELESS,
8890
"I": Flag.CASELESS,
8991
"MULTILINE": Flag.MULTILINE,
@@ -118,6 +120,7 @@ def escape(pattern: Any) -> Any:
118120
"set_cache_limit",
119121
"get_cache_limit",
120122
"compile",
123+
"prefixmatch",
121124
"match",
122125
"search",
123126
"fullmatch",

pcre/pcre.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ def match(
252252
raw, resolved_end = _call_with_optional_end(self._pattern.match, subject, pos, endpos, options)
253253
return self._wrap_match(raw, subject, pos, resolved_end)
254254

255+
prefixmatch = match
256+
255257
def search(
256258
self,
257259
subject: Any,
@@ -569,10 +571,13 @@ def compile(pattern: Any, flags: FlagInput = 0) -> Pattern:
569571
return compiled
570572

571573

572-
def match(pattern: Any, string: Any, flags: FlagInput = 0) -> Match | None:
574+
def prefixmatch(pattern: Any, string: Any, flags: FlagInput = 0) -> Match | None:
573575
return compile(pattern, flags=flags).match(string)
574576

575577

578+
match = prefixmatch
579+
580+
576581
def search(pattern: Any, string: Any, flags: FlagInput = 0) -> Match | None:
577582
return compile(pattern, flags=flags).search(string)
578583

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ build-backend = "setuptools.build_meta"
99

1010
[project]
1111
name = "PyPcre"
12-
version = "0.2.14"
12+
version = "0.2.15"
1313
description = "Modern, GIL-friendly, Fast Python bindings for PCRE2 with auto caching and JIT of compiled patterns."
1414
readme = "README.md"
1515
requires-python = ">=3.9"

tests/test_api_parity.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ def test_purge_aliases_clear_cache():
2020
assert pcre.purge is pcre.clear_cache
2121

2222

23+
def test_prefixmatch_aliases_match():
24+
assert pcre.match is pcre.prefixmatch
25+
assert "prefixmatch" in pcre.__all__
26+
27+
module_match = pcre.prefixmatch(r"ab", "abcd")
28+
assert module_match is not None
29+
assert module_match.span() == re.match(r"ab", "abcd").span()
30+
31+
compiled = pcre.compile(r"ab")
32+
pattern_match = compiled.prefixmatch("zab", pos=1)
33+
assert pattern_match is not None
34+
assert pattern_match.span() == re.compile(r"ab").match("zab", 1).span()
35+
36+
2337
def test_error_aliases_and_escape():
2438
assert pcre.error is pcre.PcreError
2539
assert pcre.PatternError is pcre.PcreError
@@ -38,6 +52,7 @@ def test_error_aliases_and_escape():
3852

3953

4054
def test_stdlib_style_flag_aliases():
55+
assert pcre.NOFLAG == 0
4156
assert pcre.IGNORECASE == pcre.Flag.CASELESS
4257
assert pcre.I == pcre.Flag.CASELESS
4358
assert pcre.MULTILINE == pcre.Flag.MULTILINE

0 commit comments

Comments
 (0)