diff options
| author | bors <bors@rust-lang.org> | 2014-06-03 22:01:43 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-06-03 22:01:43 -0700 |
| commit | 5a6dc40a102cecdf73ddc9757afe7233e678cfeb (patch) | |
| tree | 7e4aac2afbd30862c16e69e4023627569cfa2ba7 | |
| parent | 3eeaa84a50bdbd6c3cc5e33c07a01110b59c7087 (diff) | |
| parent | 9d39178f2f60b6076e4bd00e263f2304084f34b4 (diff) | |
| download | rust-5a6dc40a102cecdf73ddc9757afe7233e678cfeb.tar.gz rust-5a6dc40a102cecdf73ddc9757afe7233e678cfeb.zip | |
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.
| -rw-r--r-- | src/libregex/parse/mod.rs | 3 | ||||
| -rw-r--r-- | src/libregex/test/tests.rs | 14 |
2 files changed, 17 insertions, 0 deletions
diff --git a/src/libregex/parse/mod.rs b/src/libregex/parse/mod.rs index bd2e454a9f8..14e34b805a3 100644 --- a/src/libregex/parse/mod.rs +++ b/src/libregex/parse/mod.rs @@ -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 { diff --git a/src/libregex/test/tests.rs b/src/libregex/test/tests.rs index 68d43156ae6..35cb7c3c5b0 100644 --- a/src/libregex/test/tests.rs +++ b/src/libregex/test/tests.rs @@ -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) => ( |
