#[derive(Debug)]
enum Atom {
Literal(u8),
Dot,
Class {
negated: bool,
members: Vec<Vec<u8>>,
},
}
fn utf8_len(byte: u8) -> usize {
match byte {
0x00..=0x7F => 1,
0xC0..=0xDF => 2,
0xE0..=0xEF => 3,
0xF0..=0xF7 => 4,
_ => 1,
}
}
pub fn char_width(text: &[u8]) -> usize {
if text.is_empty() {
0
} else {
utf8_len(text[0]).min(text.len())
}
}
#[derive(Debug)]
enum Element {
One(Atom),
Star(Atom),
Caret,
Dollar,
GroupStart(usize),
GroupEnd(usize),
BackRef(usize),
}
const NGROUPS: usize = 9;
#[derive(Debug, Clone, Copy)]
struct Caps {
start: [usize; NGROUPS],
end: [usize; NGROUPS],
valid: [bool; NGROUPS],
}
impl Caps {
fn new() -> Self {
Caps {
start: [0; NGROUPS],
end: [0; NGROUPS],
valid: [false; NGROUPS],
}
}
}
#[derive(Debug)]
pub struct Regex {
elements: Vec<Element>,
}
#[derive(Debug)]
pub struct Match {
pub start: usize,
pub end: usize,
caps: Caps,
}
impl Match {
pub fn group(&self, n: usize) -> Option<(usize, usize)> {
if (1..=NGROUPS).contains(&n) && self.caps.valid[n - 1] {
Some((self.caps.start[n - 1], self.caps.end[n - 1]))
} else {
None
}
}
}
impl Regex {
pub fn compile(pattern: &[u8]) -> Regex {
let mut elements = Vec::new();
let mut i = 0;
let mut group_num: usize = 0;
let mut group_stack: Vec<usize> = Vec::new();
if i < pattern.len() && pattern[i] == b'^' {
elements.push(Element::Caret);
i += 1;
}
while i < pattern.len() {
if pattern[i] == b'$' && i + 1 == pattern.len() {
elements.push(Element::Dollar);
i += 1;
continue;
}
if pattern[i] == b'\\' && i + 1 < pattern.len() {
if pattern[i + 1] == b'0'
&& i + 4 <= pattern.len()
&& (b'0'..=b'7').contains(&pattern[i + 2])
&& (b'0'..=b'7').contains(&pattern[i + 3])
{
let byte = (pattern[i + 2] - b'0') * 8 + (pattern[i + 3] - b'0');
let atom = Atom::Literal(byte);
i += 4;
if i < pattern.len() && pattern[i] == b'*' {
elements.push(Element::Star(atom));
i += 1;
} else {
elements.push(Element::One(atom));
}
continue;
}
match pattern[i + 1] {
b'(' => {
group_num += 1;
group_stack.push(group_num);
elements.push(Element::GroupStart(group_num));
i += 2;
continue;
}
b')' => {
if let Some(n) = group_stack.pop() {
elements.push(Element::GroupEnd(n));
}
i += 2;
continue;
}
c @ b'1'..=b'9' => {
let n = (c - b'0') as usize;
elements.push(Element::BackRef(n));
i += 2;
continue;
}
c => {
let atom = Atom::Literal(c);
i += 2;
if i < pattern.len() && pattern[i] == b'*' {
elements.push(Element::Star(atom));
i += 1;
} else {
elements.push(Element::One(atom));
}
continue;
}
}
}
let (atom, next) = parse_atom(pattern, i);
i = next;
if i < pattern.len() && pattern[i] == b'*' {
elements.push(Element::Star(atom));
i += 1;
} else {
elements.push(Element::One(atom));
}
}
Regex { elements }
}
pub fn find(&self, text: &[u8]) -> Option<Match> {
self.find_at(text, 0)
}
pub fn find_at(&self, text: &[u8], start: usize) -> Option<Match> {
let elements = &self.elements;
let anchored = !elements.is_empty() && matches!(elements[0], Element::Caret);
let elems = if anchored { &elements[1..] } else { elements };
if anchored && start > 0 {
return None;
}
let mut offset = start;
loop {
let caps = Caps::new();
if let Some((len, caps)) = self.match_here(elems, &text[offset..], offset, caps, text) {
return Some(Match {
start: offset,
end: offset + len,
caps,
});
}
if anchored || offset >= text.len() {
return None;
}
offset += utf8_len(text[offset]).min(text.len() - offset);
}
}
fn match_here(
&self,
elements: &[Element],
text: &[u8],
pos: usize,
caps: Caps,
full_text: &[u8],
) -> Option<(usize, Caps)> {
if elements.is_empty() {
return Some((0, caps));
}
match &elements[0] {
Element::Dollar => {
if text.is_empty() {
Some((0, caps))
} else {
None
}
}
Element::Caret => None,
Element::GroupStart(n) => {
let mut caps = caps;
if *n <= NGROUPS {
caps.start[n - 1] = pos;
}
self.match_here(&elements[1..], text, pos, caps, full_text)
}
Element::GroupEnd(n) => {
let mut caps = caps;
if *n <= NGROUPS {
caps.end[n - 1] = pos;
caps.valid[n - 1] = true;
}
self.match_here(&elements[1..], text, pos, caps, full_text)
}
Element::BackRef(n) => {
if !caps.valid[n - 1] {
return None;
}
let captured = &full_text[caps.start[n - 1]..caps.end[n - 1]];
if text.starts_with(captured) {
let clen = captured.len();
self.match_here(&elements[1..], &text[clen..], pos + clen, caps, full_text)
.map(|(len, caps)| (len + clen, caps))
} else {
None
}
}
Element::Star(atom) => {
self.match_star(atom, &elements[1..], text, pos, caps, full_text)
}
Element::One(atom) => match atom_width(atom, text) {
Some(w) => self
.match_here(&elements[1..], &text[w..], pos + w, caps, full_text)
.map(|(len, caps)| (len + w, caps)),
None => None,
},
}
}
fn match_star(
&self,
atom: &Atom,
elements: &[Element],
text: &[u8],
pos: usize,
caps: Caps,
full_text: &[u8],
) -> Option<(usize, Caps)> {
let mut offsets = vec![0];
let mut total = 0;
while let Some(w) = atom_width(atom, &text[total..]) {
total += w;
offsets.push(total);
}
for &max in offsets.iter().rev() {
if let Some((len, caps)) =
self.match_here(elements, &text[max..], pos + max, caps, full_text)
{
return Some((max + len, caps));
}
}
None
}
}
fn parse_atom(pattern: &[u8], i: usize) -> (Atom, usize) {
match pattern[i] {
b'.' => (Atom::Dot, i + 1),
b'[' => parse_bracket(pattern, i),
c => (Atom::Literal(c), i + 1),
}
}
fn parse_bracket(pattern: &[u8], start: usize) -> (Atom, usize) {
let mut i = start + 1;
let negated = if i < pattern.len() && pattern[i] == b'^' {
i += 1;
true
} else {
false
};
let mut members: Vec<Vec<u8>> = Vec::new();
if i < pattern.len() && pattern[i] == b']' {
members.push(vec![b']']);
i += 1;
}
while i < pattern.len() && pattern[i] != b']' {
if utf8_len(pattern[i]) == 1
&& i + 2 < pattern.len()
&& pattern[i + 1] == b'-'
&& pattern[i + 2] != b']'
&& utf8_len(pattern[i + 2]) == 1
{
let lo = pattern[i];
let hi = pattern[i + 2];
for c in lo..=hi {
members.push(vec![c]);
}
i += 3;
} else {
let w = utf8_len(pattern[i]).min(pattern.len() - i);
members.push(pattern[i..i + w].to_vec());
i += w;
}
}
if i < pattern.len() {
i += 1;
}
(Atom::Class { negated, members }, i)
}
fn atom_width(atom: &Atom, text: &[u8]) -> Option<usize> {
if text.is_empty() {
return None;
}
match atom {
Atom::Literal(c) => (text[0] == *c).then_some(1),
Atom::Dot => Some(utf8_len(text[0]).min(text.len())),
Atom::Class { negated, members } => {
let matched = members
.iter()
.find(|m| text.len() >= m.len() && &text[..m.len()] == m.as_slice())
.map(Vec::len);
match (*negated, matched) {
(false, found) => found,
(true, Some(_)) => None,
(true, None) => Some(utf8_len(text[0]).min(text.len())),
}
}
}
}
pub fn expand_replacement(replacement: &[u8], m: &Match, text: &[u8]) -> Vec<u8> {
let mut result = Vec::new();
let mut i = 0;
while i < replacement.len() {
match replacement[i] {
b'&' => {
result.extend_from_slice(&text[m.start..m.end]);
i += 1;
}
b'\\' if i + 1 < replacement.len() => {
if replacement[i + 1] == b'0'
&& i + 4 <= replacement.len()
&& (b'0'..=b'7').contains(&replacement[i + 2])
&& (b'0'..=b'7').contains(&replacement[i + 3])
{
let byte = (replacement[i + 2] - b'0') * 8 + (replacement[i + 3] - b'0');
result.push(byte);
i += 4;
continue;
}
match replacement[i + 1] {
b'&' => {
result.push(b'&');
i += 2;
}
b'\\' => {
result.push(b'\\');
i += 2;
}
c @ b'1'..=b'9' => {
let n = (c - b'0') as usize;
if let Some((s, e)) = m.group(n) {
result.extend_from_slice(&text[s..e]);
}
i += 2;
}
c => {
result.push(c);
i += 2;
}
}
}
c => {
result.push(c);
i += 1;
}
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
fn has_match(pattern: &[u8], text: &[u8]) -> bool {
Regex::compile(pattern).find(text).is_some()
}
#[test]
fn literal_exact_match() {
assert!(has_match(b"abc", b"abc"));
}
#[test]
fn literal_match_in_middle() {
assert!(has_match(b"abc", b"xabcy"));
}
#[test]
fn literal_no_match() {
assert!(!has_match(b"abc", b"xyz"));
}
#[test]
fn literal_pattern_longer_than_text() {
assert!(!has_match(b"abc", b"ab"));
}
#[test]
fn empty_pattern_always_matches() {
assert!(has_match(b"", b"hello"));
}
#[test]
fn dot_matches_letter() {
assert!(has_match(b"a.c", b"abc"));
}
#[test]
fn dot_matches_underscore() {
assert!(has_match(b"a.c", b"a_c"));
}
#[test]
fn dot_requires_a_character() {
assert!(!has_match(b"a.c", b"ac"));
}
#[test]
fn dot_matches_whole_multibyte_character() {
assert!(has_match(b"caf.", b"caf\xc3\xa9")); let m = Regex::compile(b"caf.").find(b"caf\xc3\xa9").unwrap();
assert_eq!(m.end - m.start, 5); }
#[test]
fn four_dots_span_cafe_but_five_do_not() {
assert!(has_match(b"^....$", b"caf\xc3\xa9"));
assert!(!has_match(b"^.....$", b"caf\xc3\xa9"));
}
#[test]
fn dot_global_style_repetition_over_cafe_yields_four_matches() {
let re = Regex::compile(b".");
let text = "café".as_bytes();
let mut pos = 0;
let mut count = 0;
while let Some(m) = re.find(&text[pos..]) {
count += 1;
pos += m.end.max(1);
}
assert_eq!(count, 4);
}
#[test]
fn failed_match_at_a_character_does_not_search_inside_it() {
let text = "\u{20ac}yq"; assert!(!has_match(b"\\(..\\)y", text.as_bytes()));
}
#[test]
fn search_skips_a_whole_failed_character_in_one_hop() {
let text = "\u{20ac}zzy"; let m = Regex::compile(b"..y").find(text.as_bytes()).unwrap();
assert_eq!((m.start, m.end), (3, 6)); }
#[test]
fn star_zero_matches() {
assert!(has_match(b"a*b", b"b"));
}
#[test]
fn star_many_matches() {
assert!(has_match(b"a*b", b"aaab"));
}
#[test]
fn star_combined_with_slide() {
assert!(has_match(b"a*b", b"xb"));
}
#[test]
fn dot_star_matches_anything() {
assert!(has_match(b".*", b"anything"));
}
#[test]
fn caret_anchors_at_start() {
assert!(has_match(b"^abc", b"abc"));
}
#[test]
fn caret_refuses_to_slide() {
assert!(!has_match(b"^abc", b"xabc"));
}
#[test]
fn dollar_anchors_at_end() {
assert!(has_match(b"abc$", b"abc"));
}
#[test]
fn dollar_refuses_tail() {
assert!(!has_match(b"abc$", b"abcx"));
}
#[test]
fn both_anchors() {
assert!(has_match(b"^abc$", b"abc"));
}
#[test]
fn empty_anchored_matches_empty() {
assert!(has_match(b"^$", b""));
}
#[test]
fn empty_anchored_rejects_nonempty() {
assert!(!has_match(b"^$", b"x"));
}
#[test]
fn dollar_alone_finds_end() {
assert!(has_match(b"$", b"anything"));
}
#[test]
fn anchored_star_on_empty() {
assert!(has_match(b"^a*$", b""));
}
#[test]
fn anchored_star_many() {
assert!(has_match(b"^a*$", b"aaa"));
}
#[test]
fn anchored_star_rejects_wrong_char() {
assert!(!has_match(b"^a*$", b"aab"));
}
#[test]
fn class_matches_member() {
assert!(has_match(b"[abc]", b"b"));
}
#[test]
fn class_rejects_nonmember() {
assert!(!has_match(b"[abc]", b"d"));
}
#[test]
fn class_in_context() {
assert!(has_match(b"x[abc]y", b"xby"));
}
#[test]
fn range_matches_inside() {
assert!(has_match(b"[a-z]", b"m"));
}
#[test]
fn range_rejects_outside() {
assert!(!has_match(b"[a-z]", b"M"));
}
#[test]
fn multiple_ranges() {
assert!(has_match(b"[a-zA-Z]", b"M"));
}
#[test]
fn negated_class_matches_nonmember() {
assert!(has_match(b"[^abc]", b"d"));
}
#[test]
fn negated_class_rejects_member() {
assert!(!has_match(b"^[^abc]$", b"a"));
}
#[test]
fn negated_range_matches_outside() {
assert!(has_match(b"[^0-9]", b"x"));
}
#[test]
fn negated_range_rejects_inside() {
assert!(!has_match(b"^[^0-9]$", b"5"));
}
#[test]
fn literal_close_bracket_first() {
assert!(has_match(b"[]abc]", b"]"));
}
#[test]
fn literal_dash_at_end() {
assert!(has_match(b"[abc-]", b"-"));
}
#[test]
fn literal_dash_at_start() {
assert!(has_match(b"[-abc]", b"-"));
}
#[test]
fn class_matches_multibyte_member() {
assert!(has_match("[é]".as_bytes(), "café".as_bytes()));
}
#[test]
fn class_member_match_is_whole_sequence_not_shared_lead_byte() {
assert!(!has_match("[é]".as_bytes(), "È".as_bytes()));
}
#[test]
fn negated_class_over_multibyte_text_consumes_whole_character() {
let m = Regex::compile(b"[^x]").find("é".as_bytes()).unwrap();
assert_eq!((m.start, m.end), (0, 2));
}
#[test]
fn range_endpoint_that_is_multibyte_falls_back_to_literal_members() {
assert!(has_match("[é-z]".as_bytes(), "é".as_bytes()));
assert!(has_match(b"[\xc3\xa9-z]", b"-"));
assert!(has_match("[é-z]".as_bytes(), b"z"));
assert!(!has_match("[é-z]".as_bytes(), b"m"));
}
#[test]
fn class_star_zero() {
assert!(has_match(b"^[a-z]*$", b""));
}
#[test]
fn class_star_many() {
assert!(has_match(b"^[a-z]*$", b"hello"));
}
#[test]
fn class_star_rejects() {
assert!(!has_match(b"^[a-z]*$", b"hello!"));
}
#[test]
fn one_or_more_digits() {
assert!(has_match(b"[0-9][0-9]*", b"abc123def"));
}
#[test]
fn position_at_start() {
let m = Regex::compile(b"abc").find(b"abcdef").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 3);
}
#[test]
fn position_in_middle() {
let m = Regex::compile(b"abc").find(b"xabcy").unwrap();
assert_eq!(m.start, 1);
assert_eq!(m.end, 4);
}
#[test]
fn position_dot_star() {
let m = Regex::compile(b".*").find(b"hello").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 5);
}
#[test]
fn position_anchored_dot_star() {
let m = Regex::compile(b"^.*$").find(b"hello").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 5);
}
#[test]
fn position_digits() {
let m = Regex::compile(b"[0-9][0-9]*").find(b"abc123def").unwrap();
assert_eq!(m.start, 3);
assert_eq!(m.end, 6);
}
#[test]
fn star_is_greedy_unanchored() {
let m = Regex::compile(b"a*").find(b"aaa").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 3);
}
#[test]
fn star_backs_off_for_trailing_atom() {
let m = Regex::compile(b".*c").find(b"abcabc").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 6);
}
#[test]
fn star_greedy_with_class() {
let m = Regex::compile(b"[0-9]*").find(b"123abc").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 3);
}
#[test]
fn star_followed_by_literal_backs_off() {
let m = Regex::compile(b"a*ab").find(b"aaab").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 4);
}
#[test]
fn position_no_match() {
assert!(Regex::compile(b"xyz").find(b"hello").is_none());
}
#[test]
fn position_empty_match() {
let m = Regex::compile(b"").find(b"hello").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 0);
}
#[test]
fn position_dollar() {
let m = Regex::compile(b"abc$").find(b"xabc").unwrap();
assert_eq!(m.start, 1);
assert_eq!(m.end, 4);
}
#[test]
fn group_simple() {
let m = Regex::compile(b"^\\(abc\\)$").find(b"abc").unwrap();
assert_eq!(m.start, 0);
assert_eq!(m.end, 3);
assert_eq!(m.group(1), Some((0, 3)));
}
#[test]
fn group_in_context() {
let m = Regex::compile(b"^x\\(abc\\)y$").find(b"xabcy").unwrap();
assert_eq!(m.group(1), Some((1, 4)));
}
#[test]
fn group_two_groups() {
let m = Regex::compile(b"^\\(ab\\)\\(cd\\)$").find(b"abcd").unwrap();
assert_eq!(m.group(1), Some((0, 2)));
assert_eq!(m.group(2), Some((2, 4)));
}
#[test]
fn group_nested() {
let m = Regex::compile(b"^\\(\\(ab\\)cd\\)$").find(b"abcd").unwrap();
assert_eq!(m.group(1), Some((0, 4)));
assert_eq!(m.group(2), Some((0, 2)));
}
#[test]
fn group_with_dot() {
let m = Regex::compile(b"^\\(.\\)$").find(b"x").unwrap();
assert_eq!(m.group(1), Some((0, 1)));
}
#[test]
fn group_with_class() {
let m = Regex::compile(b"^\\([a-z][a-z]*\\)$")
.find(b"hello")
.unwrap();
assert_eq!(m.group(1), Some((0, 5)));
}
#[test]
fn group_no_match_returns_none() {
assert!(Regex::compile(b"\\(abc\\)").find(b"xyz").is_none());
}
#[test]
fn group_uncaptured_returns_none() {
let m = Regex::compile(b"^\\(abc\\)$").find(b"abc").unwrap();
assert_eq!(m.group(2), None);
}
#[test]
fn group_zero_returns_none() {
let m = Regex::compile(b"^\\(abc\\)$").find(b"abc").unwrap();
assert_eq!(m.group(0), None);
}
#[test]
fn group_ten_returns_none() {
let m = Regex::compile(b"^\\(abc\\)$").find(b"abc").unwrap();
assert_eq!(m.group(10), None);
}
#[test]
fn escaped_dot_is_literal() {
assert!(has_match(b"a\\.c", b"a.c"));
assert!(!has_match(b"a\\.c", b"abc"));
}
#[test]
fn escaped_star_is_literal() {
assert!(has_match(b"a\\*", b"a*"));
assert!(!has_match(b"a\\*", b"aaa"));
}
#[test]
fn escaped_backslash_is_literal() {
assert!(has_match(b"a\\\\b", b"a\\b"));
}
#[test]
fn backref_simple() {
assert!(has_match(b"^\\(abc\\)\\1$", b"abcabc"));
}
#[test]
fn backref_rejects_mismatch() {
assert!(!has_match(b"^\\(abc\\)\\1$", b"abcdef"));
}
#[test]
fn backref_with_dot() {
assert!(has_match(b"^\\(.\\)\\1$", b"aa"));
assert!(!has_match(b"^\\(.\\)\\1$", b"ab"));
}
#[test]
fn backref_two_groups() {
let m = Regex::compile(b"^\\(a\\)\\(b\\)\\2\\1$")
.find(b"abba")
.unwrap();
assert_eq!(m.group(1), Some((0, 1)));
assert_eq!(m.group(2), Some((1, 2)));
}
#[test]
fn backref_to_uncaptured_fails() {
assert!(!has_match(b"^\\1$", b"anything"));
}
#[test]
fn backref_multi_char() {
assert!(has_match(b"^\\(hello\\).*\\1$", b"hello world hello"));
}
#[test]
fn backref_multi_char_rejects() {
assert!(!has_match(b"^\\(hello\\).*\\1$", b"hello world help"));
}
#[test]
fn backref_with_class() {
assert!(has_match(b"\\([a-z]\\)\\1", b"xaabbx"));
assert!(!has_match(b"^\\([a-z]\\)\\1$", b"ab"));
}
#[test]
fn replace_literal() {
let text = b"hello world";
let m = Regex::compile(b"world").find(text).unwrap();
assert_eq!(expand_replacement(b"earth", &m, text), b"earth");
}
#[test]
fn replace_ampersand_whole_match() {
let text = b"hello world";
let m = Regex::compile(b"world").find(text).unwrap();
assert_eq!(expand_replacement(b"[&]", &m, text), b"[world]");
}
#[test]
fn replace_escaped_ampersand() {
let text = b"hello";
let m = Regex::compile(b"hello").find(text).unwrap();
assert_eq!(expand_replacement(b"\\&", &m, text), b"&");
}
#[test]
fn replace_backref_group() {
let text = b"abcd";
let m = Regex::compile(b"^\\(ab\\)\\(cd\\)$").find(text).unwrap();
assert_eq!(expand_replacement(b"\\2-\\1", &m, text), b"cd-ab");
}
#[test]
fn replace_backref_uncaptured_vanishes() {
let text = b"abc";
let m = Regex::compile(b"^\\(abc\\)$").find(text).unwrap();
assert_eq!(expand_replacement(b"\\1-\\2", &m, text), b"abc-");
}
#[test]
fn replace_escaped_backslash() {
let text = b"hello";
let m = Regex::compile(b"hello").find(text).unwrap();
assert_eq!(expand_replacement(b"a\\\\b", &m, text), b"a\\b");
}
#[test]
fn replace_ampersand_with_groups() {
let text = b"xabcy";
let m = Regex::compile(b"x\\(abc\\)y").find(text).unwrap();
assert_eq!(expand_replacement(b"&-\\1", &m, text), b"xabcy-abc");
}
#[test]
fn octal_escape_matches_us_separator() {
assert!(has_match(b"\\037", b"a\x1fb"));
assert!(!has_match(b"\\037", b"abc"));
}
#[test]
fn octal_escape_matches_tab() {
assert!(has_match(b"a\\011b", b"a\tb"));
}
#[test]
fn octal_escape_with_star() {
assert!(has_match(b"a\\040*b", b"a b"));
assert!(has_match(b"a\\040*b", b"ab"));
}
#[test]
fn octal_escape_zero_byte() {
assert!(has_match(b"\\000", b"x\0y"));
}
#[test]
fn octal_escape_malformed_still_literal_zero() {
assert!(has_match(b"\\0x", b"0x"));
}
#[test]
fn backref_still_works_after_octal_added() {
assert!(has_match(b"^\\(ab\\)\\1$", b"abab"));
}
#[test]
fn replace_octal_escape_inserts_byte() {
let text = b"foo bar";
let m = Regex::compile(b" ").find(text).unwrap();
assert_eq!(expand_replacement(b"\\037", &m, text), b"\x1f");
}
#[test]
fn replace_octal_among_other_specials() {
let text = b"foo bar";
let m = Regex::compile(b"foo \\(bar\\)").find(text).unwrap();
assert_eq!(
expand_replacement(b"&\\037\\1", &m, text),
b"foo bar\x1fbar"
);
}
#[test]
fn tenth_group_delimits_without_being_recorded() {
let text = b"abcdefghij";
let pat = b"\\(a\\)\\(b\\)\\(c\\)\\(d\\)\\(e\\)\\(f\\)\\(g\\)\\(h\\)\\(i\\)\\(j\\)";
let m = Regex::compile(pat)
.find(text)
.expect("pattern should match");
assert_eq!((m.start, m.end), (0, 10));
assert_eq!(m.group(9), Some((8, 9)));
assert_eq!(m.group(10), None);
}
#[test]
fn many_groups_do_not_panic() {
let text = b"aaaaaaaaaaaaaaa";
let pat = b"\\(a\\)".repeat(15);
assert!(Regex::compile(&pat).find(text).is_some());
}
}