Skip to content

Commit

Permalink
Fixes #13843.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BurntSushi committed Jun 4, 2014
1 parent f5ead0d commit 9d39178
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/libregex/parse/mod.rs
Expand Up @@ -201,6 +201,9 @@ pub fn parse(s: &str) -> Result<Ast, Error> {

impl<'a> Parser<'a> {
fn parse(&mut self) -> Result<Ast, Error> {
if self.chars.len() == 0 {
return Ok(Nothing);
}
loop {
let c = self.cur();
match c {
Expand Down
14 changes: 14 additions & 0 deletions src/libregex/test/tests.rs
Expand Up @@ -28,6 +28,20 @@ fn split() {
assert_eq!(subs, vec!("cauchy", "plato", "tyler", "binx"));
}

#[test]
fn empty_regex_empty_match() {
let re = regex!("");
let ms = re.find_iter("").collect::<Vec<(uint, uint)>>();
assert_eq!(ms, vec![(0, 0)]);
}

#[test]
fn empty_regex_nonempty_match() {
let re = regex!("");
let ms = re.find_iter("abc").collect::<Vec<(uint, uint)>>();
assert_eq!(ms, vec![(0, 0), (1, 1), (2, 2), (3, 3)]);
}

macro_rules! replace(
($name:ident, $which:ident, $re:expr,
$search:expr, $replace:expr, $result:expr) => (
Expand Down

5 comments on commit 9d39178

@bors
Copy link
Contributor

@bors bors commented on 9d39178 Jun 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at BurntSushi@9d39178

@bors
Copy link
Contributor

@bors bors commented on 9d39178 Jun 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging BurntSushi/rust/fix-13843 = 9d39178 into auto

@bors
Copy link
Contributor

@bors bors commented on 9d39178 Jun 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BurntSushi/rust/fix-13843 = 9d39178 merged ok, testing candidate = 5a6dc40

@bors
Copy link
Contributor

@bors bors commented on 9d39178 Jun 4, 2014

@bors
Copy link
Contributor

@bors bors commented on 9d39178 Jun 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 5a6dc40

Please sign in to comment.