vi: fix FEATURE_VI_REGEX_SEARCH to respect LIMITED (one-line) search
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 29 Nov 2018 13:19:57 +0000 (14:19 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 29 Nov 2018 13:19:57 +0000 (14:19 +0100)
If busybox is compiled with FEATURE_VI_REGEX_SEARCH enabled, command
":s/x/y/" searches not only in the current line, but continues search
after it. This makes range searches (":1,3s/x/y/") work incorrect. For
example file "./test":
1
2
3

$ vi ./test
:1,2s/3/e/
gives us:
1
2
e

function                                             old     new   delta
char_search                                          213     241     +28

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/vi.c

index 2aa0ad9dd375b41d157dc46c7019ee112ffe05a3..bff47250d6faf216a61e2dc4ccb4c30ef218e199 100644 (file)
@@ -255,8 +255,8 @@ enum {
        YANKDEL = TRUE,
        FORWARD = 1,    // code depends on "1"  for array index
        BACK = -1,      // code depends on "-1" for array index
-       LIMITED = 0,    // how much of text[] in char_search
-       FULL = 1,       // how much of text[] in char_search
+       LIMITED = 0,    // char_search() only current line
+       FULL = 1,       // char_search() to the end/beginning of entire text
 
        S_BEFORE_WS = 1,        // used in skip_thing() for moving "dot"
        S_TO_WS = 2,            // used in skip_thing() for moving "dot"
@@ -1914,10 +1914,15 @@ static char *char_search(char *p, const char *pat, int dir, int range)
                return p;
        }
 
-       // assume a LIMITED forward search
-       q = end - 1;
-       if (dir == BACK)
+       q = end - 1; // if FULL
+       if (range == LIMITED)
+               q = next_line(p);
+       if (dir == BACK) {
                q = text;
+               if (range == LIMITED)
+                       q = prev_line(p);
+       }
+
        // RANGE could be negative if we are searching backwards
        range = q - p;
        q = p;