nano-devel
[Top][All Lists]
Advanced

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

[Nano-devel] [PATCH] search: make a regex with a beginning-of-word ancho


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH] search: make a regex with a beginning-of-word anchor work correctly
Date: Sun, 1 Jan 2017 20:27:27 +0100

This fixes https://savannah.gnu.org/bugs/?45630.
---
 src/search.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/search.c b/src/search.c
index 7343aebc..3ecc6bc2 100644
--- a/src/search.c
+++ b/src/search.c
@@ -38,6 +38,8 @@ static bool history_changed = FALSE;
 #ifdef HAVE_REGEX_H
 static bool regexp_compiled = FALSE;
        /* Have we compiled any regular expressions? */
+static bool bow_anchored = FALSE;
+       /* Whether a regex starts with a beginning-of-word anchor. */
 
 /* Compile the regular expression regexp to see if it's valid.  Return
  * TRUE if it is, or FALSE otherwise. */
@@ -63,6 +65,9 @@ bool regexp_init(const char *regexp)
 
     regexp_compiled = TRUE;
 
+    bow_anchored = (strncmp(regexp, "\\<", 2) == 0 ||
+                       strncmp(regexp, "\\b", 2) == 0);
+
     return TRUE;
 }
 
@@ -295,6 +300,22 @@ int findnextstr(const char *needle, bool whole_word_only, 
size_t *match_len,
        /* Search for the needle in the current line. */
        found = strstrwrapper(fileptr->data, needle, rev_start);
 
+       /* If the regex starts with a beginning-of-word anchor, check that the
+        * found match actually is the start of a word.  If not, continue. */
+       if (bow_anchored && found != NULL && found != fileptr->data) {
+           size_t before = move_mbleft(fileptr->data, found - fileptr->data);
+
+           /* If there is a word char before the match, skip this match. */
+           if (is_word_mbchar(fileptr->data + before, FALSE)) {
+               if (ISSET(BACKWARDS_SEARCH))
+                   rev_start = fileptr->data + before;
+               else
+                   rev_start = fileptr->data + move_mbright(fileptr->data,
+                                               found - fileptr->data);
+               continue;
+           }
+       }
+
        if (found != NULL) {
            /* Remember the length of the potential match. */
            found_len =
-- 
2.11.0




reply via email to

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