Commit 641c699
committed
feat(re): Enhance CFFI re module and add tests
This commit includes several enhancements to the CFFI-based `re` module:
- Introduced `re.error` for regex-specific exceptions.
- Implemented `MatchObject` for `search()` results, providing access to captured groups via `group()` and `groups()` methods.
- Enhanced `findall()` to correctly return:
- `list[str]` of full matches if no capture groups.
- `list[str]` of captured strings if one capture group.
- `list[tuple[str,...]]` if multiple capture groups.
- Ensured `sub()` supports standard ECMAScript group references (e.g., `$1`, `$&`).
- Added comprehensive tests to `tests/test_re.py` to cover:
- Basic matching, searching, character classes, quantifiers, anchors.
- Detailed behavior of `search()` with `MatchObject` and groups.
- Detailed behavior of `findall()` with 0, 1, and multiple groups.
- `sub()` with various group backreferences.
- Error handling for invalid regex patterns.
The C++ backend in `native/src/regex_wrapper.cpp` was updated to support these features, including functions to get mark counts and return structured group information for `search`. The CFFI interface in `src/stdlib/re.py` and the stub file `native/src/regex_wrapper.pyi` were updated accordingly.1 parent 067d0a0 commit 641c699
File tree
3 files changed
+372
-50
lines changed- native/src
- src/stdlib
- tests
3 files changed
+372
-50
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
66 | 70 | | |
67 | 71 | | |
68 | 72 | | |
69 | | - | |
| 73 | + | |
70 | 74 | | |
71 | 75 | | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
76 | 109 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 110 | | |
82 | 111 | | |
83 | | - | |
84 | | - | |
| 112 | + | |
| 113 | + | |
85 | 114 | | |
86 | 115 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
91 | 119 | | |
92 | 120 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
102 | 126 | | |
103 | 127 | | |
104 | 128 | | |
| 129 | + | |
105 | 130 | | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 131 | + | |
110 | 132 | | |
111 | 133 | | |
112 | | - | |
| 134 | + | |
113 | 135 | | |
114 | 136 | | |
115 | 137 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
73 | 82 | | |
74 | 83 | | |
75 | 84 | | |
| |||
0 commit comments