]> git.lizzy.rs Git - rust.git/commitdiff
Fixes #13843.
authorAndrew Gallant <jamslam@gmail.com>
Wed, 4 Jun 2014 03:04:59 +0000 (23:04 -0400)
committerAndrew Gallant <jamslam@gmail.com>
Wed, 4 Jun 2014 03:04:59 +0000 (23:04 -0400)
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.

src/libregex/parse/mod.rs
src/libregex/test/tests.rs

index bd2e454a9f811b78d634a364a9e032e8b1584283..14e34b805a3e332f47a1504d45cb6e817192f2e9 100644 (file)
@@ -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 {
index 68d43156ae63194734b2886564ff2c74c366fbf0..35cb7c3c5b09c78185e3e8225d6e8f0897dd1152 100644 (file)
@@ -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) => (