[PATCH] vi: fix backward search with GNU regex (original) (raw)

Denys Vlasenko vda.linux at googlemail.com
Mon Jun 21 13:38:35 UTC 2021


Applied, thank you

On Mon, Jun 21, 2021 at 12:31 PM Ron Yorston <rmy at pobox.com> wrote:

With FEATUREVIREGEXSEARCH enabled backward searches don't work. This is problematic on distros that enable regexes, such as Tiny Core Linux and Fedora. When calling GNU research() with a negative range parameter (indicating a backward search) the start offset must be set to the end of the area being searched. The return value of research() is the offset of the matched pattern from the start of the area being searched. For a successful search (positive return value) charsearch() can return the pointer to the start of the area plus the offset. FEATUREVIREGEXSEARCH isn't enabled by default but when it is: function old new delta charsearch 256 247 -9 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-9) Total: -9 bytes Signed-off-by: Andrey Dobrovolsky <andrey.dobrovolsky.odessa at gmail.com> Signed-off-by: Ron Yorston <rmy at pobox.com> --- editors/vi.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/editors/vi.c b/editors/vi.c index d85cdd98d..87d6a628e 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -2371,9 +2371,7 @@ static char *charsearch(char *p, const char *pat, int dirandrange) struct repatternbuffer preg; const char *err; char *q; - int i; - int size; - int range; + int i, size, range, start; resyntaxoptions = RESYNTAXPOSIXEXTENDED; if (ignorecase) @@ -2398,31 +2396,26 @@ static char *charsearch(char *p, const char *pat, int dirandrange) // RANGE could be negative if we are searching backwards range = q - p; - q = p; - size = range; if (range < 0) {_ _- size = -size;_ _- q = p - size;_ _- if (q < text)_ _- q = text;_ _+ size = -range;_ _+ start = size;_ _+ } else {_ _+ size = range;_ _+ start = 0;_ _}_ _+ q = p - start;_ _+ if (q < text)_ _+ q = text;_ _// search for the compiled pattern, preg, in p[]_ _- // range < 0: search backward_ _- // range > 0: search forward - // 0 < start < size_ _+ // range < 0, start == size: search backward_ _+ // range > 0, start == 0: search forward // research() < 0: not found or error_ _// research() >= 0: index of found pattern // struct pattern char int int int struct reg // research(*patternbuffer, *string, size, start, range, *regs) - i = research(&preg, q, size, /start:/ 0, range, /struct reregisters:*/ NULL); + i = research(&preg, q, size, start, range, /struct reregisters:*/ NULL); regfree(&preg); - if (i < 0)_ _- return NULL;_ _- if (dirandrange > 0) // FORWARD? - p = p + i; - else - p = p - i; - return p; + return i < 0 ? NULL : q + i; } # else # if ENABLEFEATUREVISETOPTS -- 2.31.1


busybox mailing list busybox at busybox.net http://lists.busybox.net/mailman/listinfo/busybox



More information about the busybox mailing list