Skip to content

Commit 9d39178

Browse files
committed
Fixes #13843.
An empty regex is a valid regex that always matches. This behavior is consistent with at least Go and Python. A couple regression tests are included.
1 parent f5ead0d commit 9d39178

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/libregex/parse/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ pub fn parse(s: &str) -> Result<Ast, Error> {
201201

202202
impl<'a> Parser<'a> {
203203
fn parse(&mut self) -> Result<Ast, Error> {
204+
if self.chars.len() == 0 {
205+
return Ok(Nothing);
206+
}
204207
loop {
205208
let c = self.cur();
206209
match c {

src/libregex/test/tests.rs

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ fn split() {
2828
assert_eq!(subs, vec!("cauchy", "plato", "tyler", "binx"));
2929
}
3030

31+
#[test]
32+
fn empty_regex_empty_match() {
33+
let re = regex!("");
34+
let ms = re.find_iter("").collect::<Vec<(uint, uint)>>();
35+
assert_eq!(ms, vec![(0, 0)]);
36+
}
37+
38+
#[test]
39+
fn empty_regex_nonempty_match() {
40+
let re = regex!("");
41+
let ms = re.find_iter("abc").collect::<Vec<(uint, uint)>>();
42+
assert_eq!(ms, vec![(0, 0), (1, 1), (2, 2), (3, 3)]);
43+
}
44+
3145
macro_rules! replace(
3246
($name:ident, $which:ident, $re:expr,
3347
$search:expr, $replace:expr, $result:expr) => (

0 commit comments

Comments
 (0)