Skip to content

Commit 0a947bf

Browse files
Add support for regress rust crate
This adds support for the new regress regex engine.
1 parent f237ff0 commit 0a947bf

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The following regex engines are supported and covered by the tool:
3131
- [Tre](https://github.com/laurikari/tre)
3232
- [PCRE2](http://www.pcre.org)
3333
- [Rust regex crate](https://doc.rust-lang.org/regex/regex/index.html)
34+
- [Regress regex crate](https://docs.rs/regress/)
3435

3536
The engines are built from their sources. In the case an installed engine should be used,
3637
the corresponding cmake variable `INCLUDE_<name>` has to be set to `system`. The configuration script

src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static struct engines engines [] = {
3333
{.name = "hscan", .find_all = hs_find_all},
3434
#endif
3535
{.name = "rust_regex", .find_all = rust_find_all},
36+
{.name = "rust_regrs", .find_all = regress_find_all},
3637
};
3738

3839
static char * regex [] = {

src/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ int onig_find_all(char* pattern, char* subject, int subject_len, int repeat, str
4040
int hs_find_all(char * pattern, char * subject, int subject_len, int repeat, struct result * res);
4141
#endif
4242
int rust_find_all(char * pattern, char * subject, int subject_len, int repeat, struct result * res);
43+
int regress_find_all(char * pattern, char * subject, int subject_len, int repeat, struct result * res);
4344

4445
#ifdef __cplusplus
4546
}

src/rust.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,37 @@ int rust_find_all(char * pattern, char * subject, int subject_len, int repeat, s
3535

3636
return 0;
3737
}
38+
39+
int regress_find_all(char *pattern, char *subject, int subject_len, int repeat, struct result *res)
40+
{
41+
TIME_TYPE start, end;
42+
int found = 0;
43+
44+
struct Regress const *regex_hdl = regress_new(pattern);
45+
if (regex_hdl == NULL)
46+
{
47+
fprintf(stderr, "ERROR: Unable to compile pattern \"%s\"\n", pattern);
48+
return -1;
49+
}
50+
51+
double *times = calloc(repeat, sizeof(double));
52+
int const times_len = repeat;
53+
54+
do
55+
{
56+
GET_TIME(start);
57+
found = regress_matches(regex_hdl, (uint8_t *)subject, subject_len);
58+
GET_TIME(end);
59+
60+
times[repeat - 1] = TIME_DIFF_IN_MS(start, end);
61+
62+
} while (--repeat > 0);
63+
64+
res->matches = found;
65+
get_mean_and_derivation(times, times_len, res);
66+
67+
regress_free(regex_hdl);
68+
free(times);
69+
70+
return 0;
71+
}

src/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ crate-type = ["staticlib"]
99

1010
[dependencies]
1111
regex = "1.3.7"
12+
regress = "0.1"
1213
libc = "0.2.19"
1314

1415
[features]

src/rust/include/rregex.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ struct Regex;
55
extern struct Regex const * regex_new(char * const regex);
66
extern uint64_t regex_matches(struct Regex const * const exp, uint8_t * const str, uint64_t str_len);
77
extern void regex_free(struct Regex const * const exp);
8+
9+
struct Regress;
10+
11+
extern struct Regress const *regress_new(char * const regress);
12+
extern uint64_t regress_matches(struct Regress const * const exp, uint8_t * const str, uint64_t str_len);
13+
extern void regress_free(struct Regress const * const exp);

src/rust/src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
extern crate regex;
2+
extern crate regress;
23
extern crate libc;
34

45
use regex::bytes::Regex;
6+
use regress::Regex as Regress;
57
use libc::c_char;
68
use std::boxed::Box;
79
use std::ffi::CStr;
@@ -34,3 +36,40 @@ pub extern fn regex_matches(raw_exp: *mut Regex, p: *const u8, len: u64) -> u64
3436
pub extern fn regex_free(raw_exp: *mut Regex) {
3537
unsafe { Box::from_raw(raw_exp) };
3638
}
39+
40+
#[no_mangle]
41+
pub extern fn regress_new(c_buf: *const c_char) -> *const Regress {
42+
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };
43+
44+
// Pretend regress supports the "(?i)" syntax.
45+
let mut pat = c_str.to_str().unwrap();
46+
let mut flags = regress::Flags::default();
47+
if pat.starts_with("(?i)") {
48+
flags.icase = true;
49+
pat = &pat["(?i)".len()..]
50+
}
51+
let exp = match regress::Regex::newf(pat, flags) {
52+
Ok(val) => Box::into_raw(Box::new(val)),
53+
Err(_) => ptr::null(),
54+
};
55+
56+
exp as *const Regress
57+
}
58+
59+
#[no_mangle]
60+
pub extern fn regress_matches(raw_exp: *mut Regress, p: *const u8, len: u64) -> u64 {
61+
let exp = unsafe { Box::from_raw(raw_exp) };
62+
let s = unsafe {
63+
let sl = slice::from_raw_parts(p, len as usize);
64+
std::str::from_utf8_unchecked(sl)
65+
};
66+
67+
let findings = exp.find_iter_ascii(s).count();
68+
Box::into_raw(exp);
69+
findings as u64
70+
}
71+
72+
#[no_mangle]
73+
pub extern fn regress_free(raw_exp: *mut Regress) {
74+
unsafe { Box::from_raw(raw_exp) };
75+
}

0 commit comments

Comments
 (0)