texinfo-commits
[Top][All Lists]
Advanced

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

[5749] literal searches


From: Gavin D. Smith
Subject: [5749] literal searches
Date: Wed, 13 Aug 2014 10:05:07 +0000

Revision: 5749
          http://svn.sv.gnu.org/viewvc/?view=rev&root=texinfo&revision=5749
Author:   gavin
Date:     2014-08-13 10:05:05 +0000 (Wed, 13 Aug 2014)
Log Message:
-----------
literal searches

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

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog     2014-08-12 21:45:32 UTC (rev 5748)
+++ trunk/ChangeLog     2014-08-13 10:05:05 UTC (rev 5749)
@@ -1,3 +1,13 @@
+2014-08-13  Gavin Smith  <address@hidden>
+
+       * info/search.c (regexp_escape_string): New function.
+       (regexp_search): New argument controlling whether to search for string
+       literally.  Callers updated.
+       * info/session.c (info_search_in_node_internal): Call regexp_search
+       function for literal searches.
+       * info/footnotes.c (make_footnotes_node): Use strstr instead of
+       info_search_in_node_internal.
+
 2014-08-12  Gavin Smith  <address@hidden>
 
        * info/session.c (info_set_node_of_window): Clear search matches here

Modified: trunk/info/footnotes.c
===================================================================
--- trunk/info/footnotes.c      2014-08-12 21:45:32 UTC (rev 5748)
+++ trunk/info/footnotes.c      2014-08-13 10:05:05 UTC (rev 5749)
@@ -54,17 +54,19 @@
 {
   NODE *fn_node, *footnotes_node = NULL, *result = NULL;
   long fn_start = -1;
-  enum search_result ret;
+  char *fnptr;
 
   /* Make the initial assumption that the footnotes appear as simple
      text within this windows node. */
   fn_node = node;
 
   /* See if this node contains the magic footnote label. */
-  ret = info_search_in_node_internal (FOOTNOTE_LABEL, node, 0,
-                                      NULL, 1, 0, 0, 0, &fn_start);
+  fnptr = strstr (node->contents, FOOTNOTE_LABEL);
+  if (fnptr)
+    fn_start = fnptr - node->contents;
+
   /* If it doesn't, check to see if it has an associated footnotes node. */
-  if (ret != search_success)
+  if (!fnptr)
     {
       REFERENCE **refs;
 

Modified: trunk/info/info-utils.c
===================================================================
--- trunk/info/info-utils.c     2014-08-12 21:45:32 UTC (rev 5748)
+++ trunk/info/info-utils.c     2014-08-13 10:05:05 UTC (rev 5749)
@@ -1657,7 +1657,7 @@
   search_string = INFO_MENU_REGEXP "|" INFO_MENU_ENTRY_REGEXP
     "|" INFO_XREF_REGEXP "|" INFO_TAG_REGEXP;
 
-  if (regexp_search (search_string, 1, node->contents, node->nodelen,
+  if (regexp_search (search_string, 0, 1, node->contents, node->nodelen,
                      &matches, &match_count)
       == search_success)
   for (i = 0; i < match_count; i++)

Modified: trunk/info/search.c
===================================================================
--- trunk/info/search.c 2014-08-12 21:45:32 UTC (rev 5748)
+++ trunk/info/search.c 2014-08-13 10:05:05 UTC (rev 5749)
@@ -119,9 +119,34 @@
   return unescaped_regexp;
 }
 
+/* Escape any special characters in SEARCH_STRING. */
+static char *
+regexp_escape_string (char *search_string)
+{
+  char *special_chars = "\\[]^$.*(){}|+?";
+  char *p, *q;
+
+  char *escaped_string = xmalloc (strlen (search_string) * 2 + 1);
+
+  for (p = search_string, q = escaped_string; *p != '\0'; )
+    {
+      if (strchr (special_chars, *p))
+        {
+          *q++ = '\\';
+        }
+      *q++ = *p++;
+    }
+
+  *q = '\0';
+
+  return escaped_string;
+}
+
+
 /* Search BUFFER for REGEXP.  Pass back the list of matches in MATCHES. */
 enum search_result
-regexp_search (char *regexp, int is_insensitive, char *buffer, size_t buflen,
+regexp_search (char *regexp, int is_literal, int is_insensitive,
+               char *buffer, size_t buflen,
                regmatch_t **matches_out, size_t *match_count_out)
 {
   regmatch_t *matches = 0; /* List of found matches. */
@@ -130,16 +155,19 @@
 
   regex_t preg; /* Compiled pattern buffer for regexp. */
   int result;
-  char *unescaped_regexp;
+  char *regexp_str;
   char saved_char;
   regoff_t offset = 0;
 
-  unescaped_regexp = regexp_expand_newlines_and_tabs (regexp);
+  if (!is_literal)
+    regexp_str = regexp_expand_newlines_and_tabs (regexp);
+  else
+    regexp_str = regexp_escape_string (regexp);
 
-  result = regcomp (&preg, unescaped_regexp,
+  result = regcomp (&preg, regexp_str,
                     REG_EXTENDED | REG_NEWLINE
                     | (is_insensitive ? REG_ICASE : 0));
-  free (unescaped_regexp);
+  free (regexp_str);
 
   if (result != 0)
     {

Modified: trunk/info/search.h
===================================================================
--- trunk/info/search.h 2014-08-12 21:45:32 UTC (rev 5748)
+++ trunk/info/search.h 2014-08-13 10:05:05 UTC (rev 5749)
@@ -60,10 +60,10 @@
                                           long *poff);
 extern enum search_result search (char *string, SEARCH_BINDING *binding,
                                  long *poff);
-enum search_result regexp_search (char *regexp, int is_insensitive,
-                                  char *buffer, size_t buflen,
-                                  regmatch_t **matches_out,
-                                  size_t *match_count_out);
+enum search_result regexp_search (char *regexp,
+               int is_literal, int is_insensitive,
+               char *buffer, size_t buflen,
+               regmatch_t **matches_out, size_t *match_count_out);
 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-12 21:45:32 UTC (rev 5748)
+++ trunk/info/session.c        2014-08-13 10:05:05 UTC (rev 5749)
@@ -3576,32 +3576,28 @@
       else if (dir > 0 && binding.start < node->body_start)
        binding.start = node->body_start;
       
-      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))
         {
-          /* 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))
-            {
-              window->search_string = xstrdup (string);
-              window->search_is_case_sensitive = !(binding.flags & S_FoldCase);
-              result = regexp_search (string, binding.flags & S_FoldCase,
-                                      node->contents, node->nodelen,
-                                      &matches, &match_count);
-            }
-          else
-            result = search_success;
+          window->search_string = xstrdup (string);
+          window->search_is_case_sensitive = !(binding.flags & S_FoldCase);
+          result = regexp_search (string, !match_regexp,
+                                  binding.flags & S_FoldCase,
+                                  node->contents, node->nodelen,
+                                  &matches, &match_count);
+        }
+      else
+        result = search_success;
 
-          if (result != search_failure)
-            {
-              result = match_in_match_list (matches, match_count,
-                                            &binding, &match_index);
-              if (result == search_success)
-                *poff = matches[match_index].rm_so;
-            }
+      if (result != search_failure)
+        {
+          result = match_in_match_list (matches, match_count,
+                                        &binding, &match_index);
+          if (result == search_success)
+            *poff = matches[match_index].rm_so;
         }
     }
   




reply via email to

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