1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright © 2019-today Peter M. Stahl pemistahl@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::builder::{
RegExpBuilder, MINIMUM_REPETITIONS_MESSAGE, MINIMUM_SUBSTRING_LENGTH_MESSAGE,
MISSING_TEST_CASES_MESSAGE,
};
use crate::config::RegExpConfig;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyType;
use regex::{Captures, Regex};
use std::sync::LazyLock;
#[pymodule]
fn grex(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<RegExpBuilder>()?;
Ok(())
}
#[pymethods]
impl RegExpBuilder {
#[new]
fn new(test_cases: Vec<String>) -> PyResult<Self> {
if test_cases.is_empty() {
Err(PyValueError::new_err(MISSING_TEST_CASES_MESSAGE))
} else {
Ok(Self {
test_cases,
config: RegExpConfig::new(),
})
}
}
/// Specify the test cases to build the regular expression from.
///
/// The test cases need not be sorted because `RegExpBuilder` sorts them internally.
///
/// Args:
/// test_cases (list[str]): The list of test cases
///
/// Raises:
/// ValueError: if `test_cases` is empty
#[classmethod]
fn from_test_cases(_cls: &Bound<PyType>, test_cases: Vec<String>) -> PyResult<Self> {
Self::new(test_cases)
}
/// Convert any Unicode decimal digit to character class `\d`.
///
/// This method takes precedence over `with_conversion_of_words` if both are set.
/// Decimal digits are converted to `\d`, the remaining word characters to `\w`.
///
/// This method takes precedence over `with_conversion_of_non_whitespace` if both are set.
/// Decimal digits are converted to `\d`, the remaining non-whitespace characters to `\S`.
#[pyo3(name = "with_conversion_of_digits")]
fn py_with_conversion_of_digits(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_digit_converted = true;
self_
}
/// Convert any character which is not a Unicode decimal digit to character class `\D`.
///
/// This method takes precedence over `with_conversion_of_non_words` if both are set.
/// Non-digits which are also non-word characters are converted to `\D`.
///
/// This method takes precedence over `with_conversion_of_non_whitespace` if both are set.
/// Non-digits which are also non-space characters are converted to `\D`.
#[pyo3(name = "with_conversion_of_non_digits")]
fn py_with_conversion_of_non_digits(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_non_digit_converted = true;
self_
}
/// Convert any Unicode whitespace character to character class `\s`.
///
/// This method takes precedence over `with_conversion_of_non_digits` if both are set.
/// Whitespace characters are converted to `\s`, the remaining non-digit characters to `\D`.
///
/// This method takes precedence over `with_conversion_of_non_words` if both are set.
/// Whitespace characters are converted to `\s`, the remaining non-word characters to `\W`.
#[pyo3(name = "with_conversion_of_whitespace")]
fn py_with_conversion_of_whitespace(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_space_converted = true;
self_
}
/// Convert any character which is not a Unicode whitespace character to character class `\S`.
#[pyo3(name = "with_conversion_of_non_whitespace")]
fn py_with_conversion_of_non_whitespace(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_non_space_converted = true;
self_
}
/// Convert any Unicode word character to character class `\w`.
///
/// This method takes precedence over `with_conversion_of_non_digits` if both are set.
/// Word characters are converted to `\w`, the remaining non-digit characters to `\D`.
///
/// This method takes precedence over `with_conversion_of_non_whitespace` if both are set.
/// Word characters are converted to `\w`, the remaining non-space characters to `\S`.
#[pyo3(name = "with_conversion_of_words")]
fn py_with_conversion_of_words(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_word_converted = true;
self_
}
/// Convert any character which is not a Unicode word character to character class `\W`.
///
/// This method takes precedence over `with_conversion_of_non_whitespace` if both are set.
/// Non-words which are also non-space characters are converted to `\W`.
#[pyo3(name = "with_conversion_of_non_words")]
fn py_with_conversion_of_non_words(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_non_word_converted = true;
self_
}
/// Detect repeated non-overlapping substrings and convert them to `{min,max}` quantifier notation.
#[pyo3(name = "with_conversion_of_repetitions")]
fn py_with_conversion_of_repetitions(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_repetition_converted = true;
self_
}
/// Enable case-insensitive matching of test cases so that letters match both upper and lower case.
#[pyo3(name = "with_case_insensitive_matching")]
fn py_with_case_insensitive_matching(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_case_insensitive_matching = true;
self_
}
/// Replace non-capturing groups by capturing ones.
#[pyo3(name = "with_capturing_groups")]
fn py_with_capturing_groups(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_capturing_group_enabled = true;
self_
}
/// Specify the minimum quantity of substring repetitions to be converted if `with_conversion_of_repetitions` is set.
///
/// If the quantity is not explicitly set with this method, a default value of 1 will be used.
///
/// Args:
/// quantity (int): The minimum quantity of substring repetitions
///
/// Raises:
/// ValueError: if `quantity` is zero
#[pyo3(name = "with_minimum_repetitions")]
fn py_with_minimum_repetitions(
mut self_: PyRefMut<Self>,
quantity: i32,
) -> PyResult<PyRefMut<Self>> {
if quantity <= 0 {
Err(PyValueError::new_err(MINIMUM_REPETITIONS_MESSAGE))
} else {
self_.config.minimum_repetitions = quantity as u32;
Ok(self_)
}
}
/// Specify the minimum length a repeated substring must have in order to be converted if `with_conversion_of_repetitions` is set.
///
/// If the length is not explicitly set with this method, a default value of 1 will be used.
///
/// Args:
/// length (int): The minimum substring length
///
/// Raises:
/// ValueError: if `length` is zero
#[pyo3(name = "with_minimum_substring_length")]
fn py_with_minimum_substring_length(
mut self_: PyRefMut<Self>,
length: i32,
) -> PyResult<PyRefMut<Self>> {
if length <= 0 {
Err(PyValueError::new_err(MINIMUM_SUBSTRING_LENGTH_MESSAGE))
} else {
self_.config.minimum_substring_length = length as u32;
Ok(self_)
}
}
/// Convert non-ASCII characters to unicode escape sequences.
///
/// The parameter `use_surrogate_pairs` specifies whether to convert astral code planes
/// (range `U+010000` to `U+10FFFF`) to surrogate pairs.
///
/// Args:
/// use_surrogate_pairs (bool): Whether to convert astral code planes to surrogate pairs
#[pyo3(name = "with_escaping_of_non_ascii_chars")]
fn py_with_escaping_of_non_ascii_chars(
mut self_: PyRefMut<Self>,
use_surrogate_pairs: bool,
) -> PyRefMut<Self> {
self_.config.is_non_ascii_char_escaped = true;
self_.config.is_astral_code_point_converted_to_surrogate = use_surrogate_pairs;
self_
}
/// Produce a nicer looking regular expression in verbose mode.
#[pyo3(name = "with_verbose_mode")]
fn py_with_verbose_mode(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_verbose_mode_enabled = true;
self_
}
/// Remove the caret anchor '^' from the resulting regular expression, thereby allowing to
/// match the test cases also when they do not occur at the start of a string.
#[pyo3(name = "without_start_anchor")]
fn py_without_start_anchor(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_start_anchor_disabled = true;
self_
}
/// Remove the dollar sign anchor '$' from the resulting regular expression, thereby allowing
/// to match the test cases also when they do not occur at the end of a string.
#[pyo3(name = "without_end_anchor")]
fn py_without_end_anchor(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_end_anchor_disabled = true;
self_
}
/// Remove the caret and dollar sign anchors from the resulting regular expression, thereby
/// allowing to match the test cases also when they occur within a larger string that contains
/// other content as well.
#[pyo3(name = "without_anchors")]
fn py_without_anchors(mut self_: PyRefMut<Self>) -> PyRefMut<Self> {
self_.config.is_start_anchor_disabled = true;
self_.config.is_end_anchor_disabled = true;
self_
}
/// Build the actual regular expression using the previously given settings.
#[pyo3(name = "build")]
fn py_build(&mut self) -> String {
let regexp = self.build();
if self.config.is_non_ascii_char_escaped {
replace_unicode_escape_sequences(regexp)
} else {
regexp
}
}
}
/// Replaces Rust Unicode escape sequences with Python Unicode escape sequences.
fn replace_unicode_escape_sequences(regexp: String) -> String {
static FOUR_CHARS_ESCAPE_SEQUENCE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\\u\{([0-9a-f]{4})\}").unwrap());
static FIVE_CHARS_ESCAPE_SEQUENCE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\\u\{([0-9a-f]{5})\}").unwrap());
let mut replacement = FOUR_CHARS_ESCAPE_SEQUENCE
.replace_all(®exp, |caps: &Captures| format!("\\u{}", &caps[1]))
.to_string();
replacement = FIVE_CHARS_ESCAPE_SEQUENCE
.replace_all(&replacement, |caps: &Captures| {
format!("\\U000{}", &caps[1])
})
.to_string();
replacement
}