Crate safe_regex[−][src]
A safe regular expression library.
Features
forbid(unsafe_code)
- Good test coverage (??%) - TODO(mleonhard) Update.
- Checks input in a single pass.
Runtime and memory usage are both
O(n * r * 2^g)
wheren
is the length of the data to checkr
is the length of the regexg
is the number of capturing groups in the regex TODO(mleonhard) Confirm this with a benchmark.
- Rust compiler checks and optimizes the matcher
- Supports basic regular expression syntax:
- Any byte:
.
- Sequences:
abc
- Classes:
[-ab0-9]
,[^ab]
- Repetition:
a?
,a*
,a+
,a{1}
,a{1,}
,a{,1}
,a{1,2}
,a{,}
- Alternates:
a|b|c
- Capturing groups:
a(b*)?
- Any byte:
Limitations
- Only works on byte slices, not strings.
- Allocates. Uses
std::collections::HashSet
during matching.
Alternatives
regex
- Mature & Popular
- Maintained by the core Rust language developers
- Contains
unsafe
code.
pcre2
- Uses PCRE library which is written in unsafe C.
regular-expression
- No documentation
rec
Cargo Geiger Safety Report
Examples
use safe_regex::{regex, Matcher}; let re: Matcher<_> = regex!(br"(ab)?c"); assert_eq!(None, re.match_all(b"")); assert_eq!(None, re.match_all(b"abcX")); let groups1 = re.match_all(b"abc").unwrap(); assert_eq!(b"ab", groups1.group(0).unwrap()); assert_eq!(0..2, groups1.group_range(0).unwrap()); let groups2 = re.match_all(b"c").unwrap(); assert_eq!(None, groups2.group(0)); assert_eq!(None, groups2.group_range(0)); // groups2.group(1); // panics
Changelog
- v0.1.0 - First published version
TO DO
- DONE - Read about regular expressions
- DONE - Read about NFAs, https://swtch.com/~rsc/regexp/
- DONE - Design API
- DONE - Implement
- DONE - Add integration tests
- DONE - Add macro,
regex!(r"[a-z][0-9]")
- Add fuzzing tests
- Add common character classes: whitespace, letters, punctuation, etc.
- Match strings
TO DO
- Add a memory-limited
match_all
fn, for use on untrusted data. Make it the default. - Once const generics
are stable, use the feature to simplify
Repeat
and other types.
Release Process
- Edit
Cargo.toml
and bump version number. - Run
./release.sh
Modules
internal |
Macros
regex | Compiles a regular expression into a Rust type. |
Structs
Groups | Groups captured by a regular expression. |
Matcher | A compiled regular expression. |