texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[5733] do less in regexp_search function


From: Gavin D. Smith
Subject: [5733] do less in regexp_search function
Date: Fri, 08 Aug 2014 09:24:56 +0000

Revision: 5733
          http://svn.sv.gnu.org/viewvc/?view=rev&root=texinfo&revision=5733
Author:   gavin
Date:     2014-08-08 09:24:54 +0000 (Fri, 08 Aug 2014)
Log Message:
-----------
do less in regexp_search function

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/info/info-utils.c
    trunk/info/search.c
    trunk/info/search.h
    trunk/info/session.c

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog     2014-08-07 15:08:46 UTC (rev 5732)
+++ trunk/ChangeLog     2014-08-08 09:24:54 UTC (rev 5733)
@@ -1,3 +1,16 @@
+2014-08-08  Gavin Smith  <address@hidden>
+
+       * info/search.c (regexp_expand_newlines_and_tabs): Split out from
+       regexp_search.
+       (regexp_search): Arguments changed.  Check if search was the same as
+       last time moved to calling code.
+       * info/session.c (match_in_match_list): Split out from regexp_search
+       in search.c.
+       (info_search_in_node_internal): Check if regexp_search needs to be
+       called and call match_in_match_list.  Search in node name disabled.
+       * info/info-utils.c (scan_node_contents): Loop through matches
+       returned from regexp_search.
+
 2014-08-07  Gavin Smith  <address@hidden>
 
        * info/window.h (WINDOW): New fields 'search_string',
@@ -45,8 +58,8 @@
        and "alternate" screens in an xterm when resizing.
 
        * info/display.c (display_update_one_window): Always display line
-       continuation character in rightmost column.  Don't display an extra 
character
-       if line wrapping is off.
+       continuation character in rightmost column.  Don't display an extra
+       character if line wrapping is off.
        * info/window.c (window_toggle_wrap): Don't free two arrays that were
        already freed.  Disable call to display_scroll_line_starts.
 

Modified: trunk/info/info-utils.c
===================================================================
--- trunk/info/info-utils.c     2014-08-07 15:08:46 UTC (rev 5732)
+++ trunk/info/info-utils.c     2014-08-08 09:24:54 UTC (rev 5733)
@@ -1594,6 +1594,7 @@
   char *search_string;
   long position;
   WINDOW save_search = {};
+  size_t i;
   int in_menu = 0;
 
   NODE *node = *node_ptr;
@@ -1653,8 +1654,8 @@
 
   parse_top_node_line (node);
 
-  search_string = INFO_MENU_REGEXP "|" INFO_XREF_REGEXP
-    "|" INFO_TAG_REGEXP;
+  search_string = INFO_MENU_REGEXP "|" INFO_MENU_ENTRY_REGEXP
+    "|" INFO_XREF_REGEXP "|" INFO_TAG_REGEXP;
 
   s.buffer = node->contents;
   s.start = inptr - node->contents;
@@ -1663,32 +1664,37 @@
 
   save_search.node = node;
 
-  while (regexp_search (search_string, &s, &position, 0, &save_search)
-         == search_success)
+  if (regexp_search (search_string, 0, &save_search) == search_success)
+  for (i = 0; i < save_search.match_count; i++)
     {
       int in_parentheses = 0;
       REFERENCE *entry;
-      char *match = s.buffer + position;
+      char *match;
+      position = save_search.matches[i].rm_so;
 
+      match = node->contents + position;
+
+      if (match < inptr)
+        continue;
+
       /* Write out up to match */
       copy_input_to_output (match - inptr); 
 
       /* Was "* Menu:" seen?  If so, search for menu entries hereafter. */
-      if (!in_menu && match[0] == '\n')
+      if (!in_menu && !strncmp (match, INFO_MENU_LABEL,
+                                strlen (INFO_MENU_LABEL)))
         {
           in_menu = 1;
           skip_input (strlen ("\n* Menu:"));
           if (*inptr == '\n')
             skip_input (strspn (inptr, "\n") - 1); /* Keep one newline. */
 
-          search_string = INFO_MENU_ENTRY_REGEXP "|" INFO_XREF_REGEXP
-            "|" INFO_TAG_REGEXP;
         }
       else if (match[0] == ' ') /* Info tag */
         {
           scan_info_tag (node, &in_index, fb);
         }
-      else
+      else if (match[0] != '\n' || in_menu)
         {
           /* Create REFERENCE entity. */
           entry = info_new_reference (0, 0);

Modified: trunk/info/search.c
===================================================================
--- trunk/info/search.c 2014-08-07 15:08:46 UTC (rev 5732)
+++ trunk/info/search.c 2014-08-08 09:24:54 UTC (rev 5733)
@@ -84,74 +84,61 @@
   return result;
 }
 
-/* Search forwards or backwards for anything matching the regexp in the text
-   delimited by BINDING. The search is forwards if BINDING->start is greater
-   than BINDING->end.
+/* Expand \n and \t in regexp to newlines and tabs */
+static char *
+regexp_expand_newlines_and_tabs (char *regexp)
+{
+  char *unescaped_regexp = xmalloc (1 + strlen (regexp));
+  char *p, *q;
 
-   If PEND is specified, it receives a copy of BINDING at the end of a
-   succeded search.  Its START and END fields contain bounds of the found
-   string instance.
+  for (p = regexp, q = unescaped_regexp; *p != '\0'; p++, q++)
+    {
+      if (*p == '\\')
+        switch(*++p)
+          {
+          case 'n':
+            *q = '\n';
+            break;
+          case 't':
+            *q = '\t';
+            break;
+          case '\0':
+            *q = '\\';
+            p--;
+            break;
+          default:
+            *q++ = '\\';
+            *q = *p;
+            break;
+          }
+      else
+        *q = *p;
+    }
+  *q = '\0';
 
-   If WINDOW is specified, pass back the list of matches in WINDOW->matches.
-*/
+  return unescaped_regexp;
+}
+
+/* Pass back the list of matches in WINDOW->matches. */
 enum search_result
-regexp_search (char *regexp, SEARCH_BINDING *binding, 
-              long *poff, SEARCH_BINDING *pend, WINDOW *window)
+regexp_search (char *regexp, int is_insensitive, WINDOW *window)
 {
-  int is_insensitive = 0;
-
-  regoff_t start = 0, end;
-
   char *buffer; /* Buffer to search in. */
 
-  regmatch_t *matches; /* List of matches. */
-  size_t match_count;
-
-  regmatch_t *new_matches = 0; /* List of matches if they have to be
-                                  recalculated. */
+  regmatch_t *new_matches = 0; /* List of found matches. */
   size_t match_alloc = 0;
   size_t new_match_count = 0;
 
-  /* Check if we need to calculate new results. */
-  if (!window->matches || strcmp (window->search_string, regexp)
-      || window->search_is_case_sensitive != binding->flags & S_FoldCase)
+  /* Calculate new results. */
     {
       regex_t preg; /* Compiled pattern buffer for regexp. */
       int result;
       char *unescaped_regexp;
-      char *p, *q;
       char saved_char;
       regoff_t offset = 0;
 
-      is_insensitive = binding->flags & S_FoldCase;
+      unescaped_regexp = regexp_expand_newlines_and_tabs (regexp);
 
-      /* expand the \n and \t in regexp */
-      unescaped_regexp = xmalloc (1 + strlen (regexp));
-      for (p = regexp, q = unescaped_regexp; *p != '\0'; p++, q++)
-        {
-          if (*p == '\\')
-            switch(*++p)
-              {
-              case 'n':
-                *q = '\n';
-                break;
-              case 't':
-                *q = '\t';
-                break;
-              case '\0':
-                *q = '\\';
-                p--;
-                break;
-              default:
-                *q++ = '\\';
-                *q = *p;
-                break;
-              }
-          else
-            *q = *p;
-        }
-      *q = '\0';
-
       result = regcomp (&preg, unescaped_regexp,
                        REG_EXTENDED|
                        REG_NEWLINE|
@@ -207,82 +194,12 @@
     }
 
   /* Pass back the full list of results. */
-  if (window && new_matches)
-    {
-      window->search_string = xstrdup (regexp);
-      window->search_is_case_sensitive =  binding->flags & S_FoldCase;
-      window->matches = new_matches;
-      window->match_count = new_match_count;
-    }
+  window->search_string = xstrdup (regexp);
+  window->search_is_case_sensitive = !is_insensitive;
+  window->matches = new_matches;
+  window->match_count = new_match_count;
 
-  /* Extract the matches we are looking for. */
-
-  matches = window->matches;
-  match_count = window->match_count;
-
-  if (binding->start < binding->end)
-    {
-      start = binding->start;
-      end = binding->end;
-    }
-  else
-    {
-      start = binding->end;
-      end = binding->start;
-    }
-  
-  if (binding->start > binding->end)
-    {
-      /* searching backward */
-      int i;
-      for (i = match_count - 1; i >= 0; i--)
-        {
-          if (matches[i].rm_so < start)
-            break; /* No matches found in search area. */
-
-          if (matches[i].rm_so < end)
-           {
-             if (pend)
-               {
-                 pend->buffer = buffer;
-                 pend->flags = binding->flags;
-                 pend->start = matches[i].rm_so;
-                 pend->end = matches[i].rm_eo;
-               }
-             *poff = matches[i].rm_so;
-             return search_success;
-           }
-        }
-    }
-  else
-    {
-      /* searching forward */
-      int i;
-      for (i = 0; i < match_count; i++)
-        {
-          if (matches[i].rm_so >= end)
-            break; /* No matches found in search area. */
-
-          if (matches[i].rm_so >= start)
-            {
-             if (pend)
-               {
-                 pend->buffer = buffer;
-                 pend->flags = binding->flags;
-                 pend->start = matches[i].rm_so;
-                 pend->end = matches[i].rm_eo;
-               }
-              if (binding->flags & S_SkipDest)
-                *poff = matches[i].rm_eo;
-              else
-                *poff = matches[i].rm_so;
-             return search_success;
-            }
-        }
-    }
-
-  /* not found */
-  return search_not_found;
+  return search_success;
 }
 
 /* Search forwards for STRING through the text delimited in BINDING. */

Modified: trunk/info/search.h
===================================================================
--- trunk/info/search.h 2014-08-07 15:08:46 UTC (rev 5732)
+++ trunk/info/search.h 2014-08-08 09:24:54 UTC (rev 5733)
@@ -60,10 +60,8 @@
                                           long *poff);
 extern enum search_result search (char *string, SEARCH_BINDING *binding,
                                  long *poff);
-extern enum search_result regexp_search (char *regexp,
-                                        SEARCH_BINDING *binding,
-                                        long *poff,
-                                        SEARCH_BINDING *pret, WINDOW *window);
+extern enum search_result regexp_search (char *regexp, int is_sensitive,
+                                        WINDOW *window);
 extern int looking_at (char *string, SEARCH_BINDING *binding);
 
 /* Note that STRING_IN_LINE () always returns the offset of the 1st character

Modified: trunk/info/session.c
===================================================================
--- trunk/info/session.c        2014-08-07 15:08:46 UTC (rev 5732)
+++ trunk/info/session.c        2014-08-08 09:24:54 UTC (rev 5733)
@@ -3442,6 +3442,91 @@
   return NULL;
 }
 
+/* Search forwards or backwards for anything matching the regexp in the text
+   delimited by BINDING. The search is forwards if BINDING->start is greater
+   than BINDING->end.
+
+   If PEND is specified, it receives a copy of BINDING at the end of a
+   succeded search.  Its START and END fields contain bounds of the found
+   string instance. */
+static enum search_result
+match_in_match_list (WINDOW *window, SEARCH_BINDING *binding,
+                     long *poff, SEARCH_BINDING *pend)
+{
+  regoff_t start = 0, end;
+  regmatch_t *matches; /* List of matches. */
+  size_t match_count;
+
+  /* Extract the matches we are looking for. */
+
+  matches = window->matches;
+  match_count = window->match_count;
+
+  if (binding->start < binding->end)
+    {
+      start = binding->start;
+      end = binding->end;
+    }
+  else
+    {
+      start = binding->end;
+      end = binding->start;
+    }
+  
+  if (binding->start > binding->end)
+    {
+      /* searching backward */
+      int i;
+      for (i = match_count - 1; i >= 0; i--)
+        {
+          if (matches[i].rm_so < start)
+            break; /* No matches found in search area. */
+
+          if (matches[i].rm_so < end)
+           {
+             if (pend)
+               {
+                 pend->buffer = binding->buffer;
+                 pend->flags = binding->flags;
+                 pend->start = matches[i].rm_so;
+                 pend->end = matches[i].rm_eo;
+               }
+             *poff = matches[i].rm_so;
+             return search_success;
+           }
+        }
+    }
+  else
+    {
+      /* searching forward */
+      int i;
+      for (i = 0; i < match_count; i++)
+        {
+          if (matches[i].rm_so >= end)
+            break; /* No matches found in search area. */
+
+          if (matches[i].rm_so >= start)
+            {
+             if (pend)
+               {
+                 pend->buffer = binding->buffer;
+                 pend->flags = binding->flags;
+                 pend->start = matches[i].rm_so;
+                 pend->end = matches[i].rm_eo;
+               }
+              if (binding->flags & S_SkipDest)
+                *poff = matches[i].rm_eo;
+              else
+                *poff = matches[i].rm_so;
+             return search_success;
+            }
+        }
+    }
+
+  /* not found */
+  return search_not_found;
+}
+
 /* Search for STRING in NODE starting at START.  If the string was found,
    return its location in POFF.  If WINDOW is passed as non-null, set the
    window's node to be NODE, its point to be the found string, and readjust
@@ -3469,6 +3554,7 @@
   if (isearch_is_active)
     binding.flags |= S_SkipDest;
   
+#if 0
   if (match_nodename)
     {
       /* Match_nodename is set when we have changed the node and are
@@ -3494,6 +3580,7 @@
             *poff += start_off;
        }
     }
+#endif
 
   if (result != search_success)
     {
@@ -3522,9 +3609,24 @@
       else if (binding.start < node->body_start)
        binding.start = node->body_start;
       
-      result = (match_regexp ? 
-               regexp_search (string, &binding, poff, resbnd, window):
-               search (string, &binding, poff));
+      if (!match_regexp)
+        result = search (string, &binding, poff);
+      else
+        {
+          /* Check if we need to calculate new results. */
+          if (!window->matches
+              || strcmp (window->search_string, string)
+              || !!window->search_is_case_sensitive
+                 != !!(binding.flags & S_FoldCase))
+            result = regexp_search (string, binding.flags & S_FoldCase,
+                                    window);
+          else
+            result = search_success;
+
+          if (result != search_failure)
+            result = match_in_match_list (window, &binding, poff, resbnd);
+        }
+
       if (saved_node)
         window->node = saved_node;
     }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]