[prev in list] [next in list] [prev in thread] [next in thread] 

List:       nano-devel
Subject:    [Nano-devel] [PATCH] search: make a regex with a beginning-of-word anchor work correctly
From:       Benno Schulenberg <bensberg () justemail ! net>
Date:       2017-01-01 19:27:27
Message-ID: 20170101192727.19755-1-bensberg () justemail ! net
[Download RAW message or body]

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


_______________________________________________
Nano-devel mailing list
Nano-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/nano-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic