Skip to content

Commit 5a6dc40

Browse files
committed
auto merge of #14634 : BurntSushi/rust/fix-13843, r=alexcrichton
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. I'd just assume that an empty regex is an invalid regex and that an error should be returned (I can't think of any reason to use an empty regex?), but it's probably better to be consistent with other regex libraries.
2 parents 3eeaa84 + 9d39178 commit 5a6dc40

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/libregex/parse/mod.rs

Lines changed: 3 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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)