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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use crate::{Captures, EncodedChars, Error, Regex, RegexOptions, Region, SearchOptions};
use std::os::raw::c_int;
use std::ptr::null_mut;
/// Defines the search priority when multiple regexes could match at the same position
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegSetLead {
/// Return the match that occurs first in the text (position priority)
Position,
/// Same results as Position I think but slower
Regex,
/// Return the first regex in your regset that matches, regardless of position
PriorityToRegexOrder,
}
impl RegSetLead {
fn to_onig_lead(self) -> onig_sys::OnigRegSetLead {
match self {
RegSetLead::Position => onig_sys::OnigRegSetLead_ONIG_REGSET_POSITION_LEAD,
RegSetLead::Regex => onig_sys::OnigRegSetLead_ONIG_REGSET_REGEX_LEAD,
RegSetLead::PriorityToRegexOrder => {
onig_sys::OnigRegSetLead_ONIG_REGSET_PRIORITY_TO_REGEX_ORDER
}
}
}
}
/// A `RegSet` allows you to compile multiple regular expressions and search
/// for any of them in a single pass through the text. This is more efficient
/// than searching with each regex individually but `RegSet` has to own them.
#[derive(Debug)]
pub struct RegSet {
raw: *mut onig_sys::OnigRegSet,
options: RegexOptions,
}
unsafe impl Send for RegSet {}
unsafe impl Sync for RegSet {}
impl RegSet {
/// Create a new RegSet from a slice of pattern strings
///
/// All patterns will be compiled with default Regex options.
///
/// # Examples
///
/// ```rust
/// use onig::RegSet;
///
/// let set = RegSet::new(&[r"\d+", r"[a-z]+", r"[A-Z]+"]).unwrap();
/// ```
pub fn new(patterns: &[&str]) -> Result<RegSet, Error> {
Self::with_options(patterns, RegexOptions::REGEX_OPTION_NONE)
}
/// Create a new RegSet from a slice of pattern strings with specified options
///
/// All patterns will be compiled with the specified Regex options.
///
/// # Examples
///
/// ```rust
/// use onig::{RegSet, RegexOptions};
///
/// let set = RegSet::with_options(&[r"\d+", r"[a-z]+"], RegexOptions::REGEX_OPTION_CAPTURE_GROUP).unwrap();
/// ```
pub fn with_options(patterns: &[&str], options: RegexOptions) -> Result<RegSet, Error> {
let mut regset = Self::empty_with_options(options)?;
for pat in patterns {
regset.add_pattern(pat)?;
}
Ok(regset)
}
/// Create an empty RegSet
///
/// Creates a new empty RegSet that contains no regular expressions.
/// Patterns can be added later using the `add_pattern` method.
///
/// # Examples
///
/// ```rust
/// use onig::RegSet;
///
/// let empty_set = RegSet::empty().unwrap();
/// assert_eq!(empty_set.len(), 0);
/// assert!(empty_set.is_empty());
/// ```
pub fn empty() -> Result<RegSet, Error> {
Self::empty_with_options(RegexOptions::REGEX_OPTION_NONE)
}
/// Create an empty RegSet with specified options
///
/// Creates a new empty RegSet that contains no regular expressions.
/// Patterns added later will use the specified options.
///
/// # Examples
///
/// ```rust
/// use onig::{RegSet, RegexOptions};
///
/// let empty_set = RegSet::empty_with_options(RegexOptions::REGEX_OPTION_CAPTURE_GROUP).unwrap();
/// assert_eq!(empty_set.len(), 0);
/// assert!(empty_set.is_empty());
/// ```
pub fn empty_with_options(options: RegexOptions) -> Result<RegSet, Error> {
let mut raw_set: *mut onig_sys::OnigRegSet = null_mut();
let raw_set_ptr = &mut raw_set as *mut *mut onig_sys::OnigRegSet;
let err = unsafe { onig_sys::onig_regset_new(raw_set_ptr, 0, null_mut()) };
if err != onig_sys::ONIG_NORMAL as i32 {
return Err(Error::from_code(err));
}
if raw_set.is_null() {
return Err(Error::custom("Failed to create RegSet"));
}
Ok(RegSet {
raw: raw_set,
options,
})
}
/// Adds a new compiled regex pattern to the end of the RegSet.
///
/// # Examples
///
/// ```rust
/// use onig::RegSet;
///
/// let mut set = RegSet::empty().unwrap();
/// let idx = set.add_pattern(r"\d+").unwrap();
/// assert_eq!(idx, 0);
/// assert_eq!(set.len(), 1);
///
/// let idx2 = set.add_pattern(r"[a-z]+").unwrap();
/// assert_eq!(idx2, 1);
/// assert_eq!(set.len(), 2);
/// ```
pub fn add_pattern(&mut self, pattern: &str) -> Result<usize, Error> {
// Compile the new regex using stored options
let new_regex = Regex::with_options(pattern, self.options, crate::Syntax::default())?;
// Get the current length (this will be the index of the new pattern)
let new_index = self.len();
// Add the regex to the regset
let err = unsafe { onig_sys::onig_regset_add(self.raw, new_regex.as_raw()) };
if err != onig_sys::ONIG_NORMAL as i32 {
return Err(Error::from_code(err));
}
// Transfer ownership of the regex to the regset
std::mem::forget(new_regex);
Ok(new_index)
}
/// Replace a regex pattern at the specified index
///
/// # Examples
///
/// ```rust
/// use onig::RegSet;
///
/// let mut set = RegSet::new(&[r"\d+", r"[a-z]+"]).unwrap();
/// set.replace_pattern(0, r"[A-Z]+").unwrap();
///
/// assert!(set.find("123").is_none());
/// assert!(set.find("ABC").is_some());
/// ```
pub fn replace_pattern(&mut self, index: usize, pattern: &str) -> Result<(), Error> {
let regset_len = self.len();
if index >= regset_len {
return Err(Error::custom(format!(
"Index {} is out of bounds for RegSet with {} regexes",
index, regset_len
)));
}
let new_regex = Regex::with_options(pattern, self.options, crate::Syntax::default())?;
// Replace the regex in the regset
let err =
unsafe { onig_sys::onig_regset_replace(self.raw, index as c_int, new_regex.as_raw()) };
if err != onig_sys::ONIG_NORMAL as i32 {
return Err(Error::from_code(err));
}
// Transfer ownership of the regex to the regset
std::mem::forget(new_regex);
Ok(())
}
/// Returns the number of regexes in the set
pub fn len(&self) -> usize {
unsafe { onig_sys::onig_regset_number_of_regex(self.raw) as usize }
}
/// Returns true if the RegSet contains no regexes
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Find the first match of any regex in the set
///
/// Returns a tuple of `(regex_index, match_position)` if a match is found,
/// or `None` if no match is found.
/// # Examples
///
/// ```rust
/// use onig::RegSet;
///
/// let set = RegSet::new(&[r"\d+", r"[a-z]+"]).unwrap();
/// if let Some((regex_index, pos)) = set.find("hello123") {
/// println!("Regex {} matched at position {}", regex_index, pos);
/// }
/// ```
pub fn find(&self, text: &str) -> Option<(usize, usize)> {
self.find_with_options(
text,
RegSetLead::Position,
SearchOptions::SEARCH_OPTION_NONE,
)
}
/// Find the first match of any regex in the set with custom options
///
/// # Examples
///
/// ```rust
/// use onig::{RegSet, RegSetLead, SearchOptions};
///
/// let set = RegSet::new(&[r"\d+", r"[a-z]+"]).unwrap();
/// if let Some((regex_index, pos)) = set.find_with_options(
/// "hello123",
/// RegSetLead::Regex,
/// SearchOptions::SEARCH_OPTION_NONE
/// ) {
/// println!("Regex {} matched at position {}", regex_index, pos);
/// }
/// ```
pub fn find_with_options(
&self,
text: &str,
lead: RegSetLead,
options: SearchOptions,
) -> Option<(usize, usize)> {
self.search_with_encoding(text, 0, text.len(), lead, options)
}
/// Find the first match of any regex in the set with full capture group information
///
/// Returns a tuple of `(regex_index, captures)` if a match is found,
/// or `None` if no match is found.
///
/// # Examples
///
/// ```rust
/// use onig::RegSet;
///
/// let set = RegSet::new(&[r"(\d+)", r"([a-z]+)"]).unwrap();
/// if let Some((regex_index, captures)) = set.captures("hello123") {
/// println!("Regex {} matched", regex_index);
/// println!("Full match: {:?}", captures.at(0));
/// println!("First capture group: {:?}", captures.at(1));
/// }
/// ```
pub fn captures<'t>(&self, text: &'t str) -> Option<(usize, Captures<'t>)> {
self.captures_with_options(
text,
0,
text.len(),
RegSetLead::Position,
SearchOptions::SEARCH_OPTION_NONE,
)
}
/// Find the first match with full capture group information and encoding support
///
/// Returns a tuple of `(regex_index, captures)` if a match is found,
/// or `None` if no match is found.
///
/// # Examples
///
/// ```rust
/// use onig::{RegSet, RegSetLead, SearchOptions, EncodedBytes};
///
/// let set = RegSet::new(&[r"(\d+)", r"([a-z]+)"]).unwrap();
/// if let Some((regex_index, captures)) = set.captures_with_options(
/// "hello123",
/// 0,
/// 8,
/// RegSetLead::Position,
/// SearchOptions::SEARCH_OPTION_NONE
/// ) {
/// println!("Regex {} matched", regex_index);
/// println!("Full match: {:?}", captures.at(0));
/// println!("First capture group: {:?}", captures.at(1));
/// }
/// ```
pub fn captures_with_options<'t>(
&self,
text: &'t str,
from: usize,
to: usize,
lead: RegSetLead,
options: SearchOptions,
) -> Option<(usize, Captures<'t>)> {
if let Some((regex_index, match_pos)) =
self.do_search_with_encoding(&text, from, to, lead, options)
{
let region_ptr =
unsafe { onig_sys::onig_regset_get_region(self.raw, regex_index as c_int) };
if !region_ptr.is_null() {
// Pre-allocate region with reasonable capacity
// Most regexes have < 10 capture groups and it's not worth adding an option for that
// for RegSet
let mut region = Region::with_capacity(10);
unsafe {
onig_sys::onig_region_copy(&mut region.raw, region_ptr);
}
let captures = Captures::new(text, region, match_pos);
return Some((regex_index, captures));
}
}
None
}
fn do_search_with_encoding<T>(
&self,
chars: &T,
from: usize,
to: usize,
lead: RegSetLead,
options: SearchOptions,
) -> Option<(usize, usize)>
where
T: EncodedChars,
{
if from > chars.len() || to > chars.len() || from > to {
return None;
}
let mut rmatch_pos: c_int = 0;
let rmatch_pos_ptr = &mut rmatch_pos as *mut c_int;
let (beg, end) = (chars.start_ptr(), chars.limit_ptr());
let result = unsafe {
let start = beg.add(from);
let range = beg.add(to);
onig_sys::onig_regset_search(
self.raw,
beg,
end,
start,
range,
lead.to_onig_lead(),
options.bits(),
rmatch_pos_ptr,
)
};
if result >= 0 {
Some((result as usize, rmatch_pos as usize))
} else {
None
}
}
fn search_with_encoding<T>(
&self,
chars: T,
from: usize,
to: usize,
lead: RegSetLead,
options: SearchOptions,
) -> Option<(usize, usize)>
where
T: EncodedChars,
{
self.do_search_with_encoding(&chars, from, to, lead, options)
}
}
impl Drop for RegSet {
fn drop(&mut self) {
unsafe {
onig_sys::onig_regset_free(self.raw);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_regset_empty_patterns() {
let set = RegSet::new(&[]).unwrap();
assert_eq!(set.len(), 0);
assert!(set.is_empty());
}
#[test]
fn test_regset_new() {
let set = RegSet::new(&[r"\d+"]).unwrap();
assert_eq!(set.len(), 1);
assert!(!set.is_empty());
}
#[test]
fn test_regset_find_with_options() {
let set = RegSet::new(&[r"\d+", r"[a-z]+"]).unwrap();
let result = set.find_with_options(
"hello123",
RegSetLead::Position,
SearchOptions::SEARCH_OPTION_NONE,
);
assert!(result.is_some());
let result = set.find_with_options(
"hello123",
RegSetLead::Regex,
SearchOptions::SEARCH_OPTION_NONE,
);
assert!(result.is_some());
let result = set.find_with_options(
"!@#$%",
RegSetLead::Regex,
SearchOptions::SEARCH_OPTION_NONE,
);
assert!(result.is_none());
}
#[test]
fn test_regset_captures() {
let set = RegSet::new(&[r"(\d+)-(\d+)", r"([a-z]+)"]).unwrap();
if let Some((regex_index, captures)) = set.captures("hello123") {
assert_eq!(regex_index, 1); // "[a-z]+" matches first by position
assert_eq!(captures.at(0), Some("hello"));
assert_eq!(captures.pos(0), Some((0, 5)));
} else {
panic!("Expected to find a match");
}
if let Some((regex_index, captures)) = set.captures("123-456") {
assert_eq!(regex_index, 0); // First pattern with groups
assert_eq!(captures.len(), 3); // Full match + 2 groups
assert_eq!(captures.at(0), Some("123-456"));
assert_eq!(captures.at(1), Some("123"));
assert_eq!(captures.at(2), Some("456"));
} else {
panic!("Expected to find a match");
}
assert!(set.captures("!@#$%").is_none());
}
#[test]
fn test_regset_replace_pattern() {
let mut set = RegSet::new(&[r"\d+", r"[a-z]+"]).unwrap();
assert!(set.find("123").is_some());
set.replace_pattern(0, r"[A-Z]+").unwrap();
assert!(set.replace_pattern(100, r"[A-Z]+").is_err());
assert!(set.find("123").is_none());
assert!(set.find("ABC").is_some());
assert!(set.find("hello").is_some());
assert_eq!(set.len(), 2);
}
#[test]
fn test_regset_add_pattern() {
let mut set = RegSet::empty().unwrap();
let idx1 = set.add_pattern(r"\d+").unwrap();
assert_eq!(idx1, 0);
assert_eq!(set.len(), 1);
assert_eq!(set.find("hello123"), Some((0, 5)));
let idx2 = set.add_pattern(r"[a-z]+").unwrap();
assert_eq!(idx2, 1);
assert_eq!(set.len(), 2);
assert_eq!(set.find("hello123"), Some((1, 0)));
}
#[test]
fn test_regset_add_pattern_captures() {
let mut set = RegSet::empty().unwrap();
set.add_pattern(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
let (idx, caps) = set.captures("2023-12-25").unwrap();
assert_eq!(idx, 0);
assert_eq!(caps.at(1), Some("2023"));
assert_eq!(caps.at(2), Some("12"));
assert_eq!(caps.at(3), Some("25"));
}
#[test]
fn test_regset_add_pattern_errors() {
let mut set = RegSet::empty().unwrap();
assert!(set.add_pattern(r"[").is_err());
assert_eq!(set.len(), 0);
assert!(set.replace_pattern(0, r"\d+").is_err());
set.add_pattern(r"\d+").unwrap();
assert_eq!(set.len(), 1);
}
#[test]
fn test_regset_captures_with_options() {
let set = RegSet::new(&[r"(\d+)", r"([a-z]+)"]).unwrap();
if let Some((regex_index, captures)) = set.captures_with_options(
"hello123",
0,
8,
RegSetLead::Position,
SearchOptions::SEARCH_OPTION_NONE,
) {
assert_eq!(regex_index, 1); // "[a-z]+" matches first by position
assert_eq!(captures.at(0), Some("hello"));
assert_eq!(captures.at(1), Some("hello"));
} else {
panic!("Expected to find a match");
}
}
}