This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Use anchors to prune failing and global searches


This is a small heuristic that is in PCRE as well.

Paolo

2002-12-17 Paolo Bonzini <bonzini@gnu.org>

       * posix/regexec.c (re_search_internal): Limit
       search to the beginning of the buffer if the
       initial states are empty for contexts that do
       not include CONTEXT_BEGBUF or, if
       !preg->newline_anchor, that do not include any one
       of CONTEXT_BEGBUF and CONTEXT_NEWLINE.

--- regexec.c.sav	2003-12-17 12:01:21.000000000 +0100
+++ regexec.c	2003-12-17 14:30:10.000000000 +0100
@@ -599,6 +599,24 @@
 	  || dfa->init_state_begbuf == NULL, 0))
     return REG_NOMATCH;
 
+#ifdef DEBUG
+  /* We assume front-end functions already check them.  */
+  assert (start + range >= 0 && start + range <= length);
+#endif
+
+  /* If initial states with non-begbuf contexts have no elements,
+     the regex must be anchored.  If preg->newline_anchor is set,
+     we'll never use init_state_nl, so do not check it.  */
+  if (dfa->init_state->nodes.nelem == 0
+      && dfa->init_state_word->nodes.nelem == 0
+      && (dfa->init_state_nl->nodes.nelem == 0
+	  || !preg->newline_anchor))
+    {
+      if (start != 0 && start + range != 0)
+        return REG_NOMATCH;
+      start = range = 0;
+    }
+
   re_node_set_init_empty (&empty_set);
   memset (&mctx, '\0', sizeof (re_match_context_t));
 
@@ -632,11 +650,6 @@
   else
     mctx.state_log = NULL;
 
-#ifdef DEBUG
-  /* We assume front-end functions already check them.  */
-  assert (start + range >= 0 && start + range <= length);
-#endif
-
   match_first = start;
   input.tip_context = ((eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
 		       : CONTEXT_NEWLINE | CONTEXT_BEGBUF);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]