From a4210b7d7704e7c35fa1a560a5089e1707f4ffb7 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 2 Apr 2018 22:42:24 -0400 Subject: [PATCH] reworked findstr function --- inc/edit.h | 2 +- lib/buf.c | 41 +++++++++++++++++++---------------------- lib/view.c | 6 +----- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index 722fdb7..f055231 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -80,7 +80,7 @@ void buf_getblock(Buf* buf, Rune beg, Rune end, Sel* sel); size_t buf_byrune(Buf* buf, size_t pos, int count); size_t buf_byword(Buf* buf, size_t pos, int count); size_t buf_byline(Buf* buf, size_t pos, int count); -void buf_findstr(Buf* buf, int dir, char* str, size_t* beg, size_t* end); +bool buf_findstr(Buf* buf, Sel* sel, int dir, char* str); void buf_setln(Buf* buf, Sel* sel, size_t line); void buf_getcol(Buf* buf, Sel* p_sel); diff --git a/lib/buf.c b/lib/buf.c index 70aba7c..62afe92 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -14,7 +14,7 @@ static void log_delete(Buf* buf, Log** list, size_t off, char* r, size_t len); static void syncgap(Buf* buf, size_t off); static void delete(Buf* buf, size_t off); static size_t insert(Buf* buf, size_t off, Rune rune); -static int rune_match(Buf* buf, size_t mbeg, size_t mend, Rune* runes); +static int bytes_match(Buf* buf, size_t mbeg, size_t mend, char* str); static void swaplog(Buf* buf, Log** from, Log** to, Sel* sel); static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)); @@ -381,25 +381,30 @@ size_t buf_byline(Buf* buf, size_t pos, int count) { return pos; } -void buf_findstr(Buf* buf, int dir, char* str, size_t* beg, size_t* end) { - if (!str) return; - Rune* runes = charstorunes(str); - size_t rlen = rstrlen(runes); - size_t start = *beg, mbeg = start+dir, mend = mbeg + rlen; +bool buf_findstr(Buf* buf, Sel* sel, int dir, char* str) { + size_t len = strlen(str); + size_t start = sel->beg, mbeg = (start + dir), mend = (mbeg + len); while (mbeg != start) { - if ((buf_getrat(buf, mbeg) == runes[0]) && - (buf_getrat(buf, mend-1) == runes[rlen-1]) && - (0 == rune_match(buf, mbeg, mend, runes))) + if ((getb(buf, mbeg) == str[0]) && + (getb(buf, mend-1) == str[len-1]) && + (0 == bytes_match(buf, mbeg, mend, str))) { - *beg = mbeg; - *end = mend; - break; + sel->beg = mbeg, sel->end = mend; + return true; } mbeg += dir, mend += dir; if (mend > buf_end(buf)) - mbeg = (dir < 0 ? buf_end(buf)-rlen : 0), mend = mbeg+rlen; + mbeg = (dir < 0 ? buf_end(buf)-len : 0), mend = mbeg + len; + } + return false; +} + +static int bytes_match(Buf* buf, size_t mbeg, size_t mend, char* str) { + for (; *str; str++, mbeg++) { + int cmp = *str - getb(buf, mbeg); + if (cmp != 0) return cmp; } - free(runes); + return 0; } void buf_setln(Buf* buf, Sel* sel, size_t line) { @@ -434,14 +439,6 @@ void buf_setcol(Buf* buf, Sel* sel) { } } -static int rune_match(Buf* buf, size_t mbeg, size_t mend, Rune* runes) { - for (; *runes; runes++, mbeg++) { - int cmp = *runes - buf_getrat(buf, mbeg); - if (cmp != 0) return cmp; - } - return 0; -} - static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)) { bool ret = false; size_t end = buf_end(buf); diff --git a/lib/view.c b/lib/view.c index d43019e..92e0373 100644 --- a/lib/view.c +++ b/lib/view.c @@ -159,11 +159,7 @@ char* view_fetch(View* view, size_t row, size_t col, bool (*isword)(Rune)) { } bool view_findstr(View* view, int dir, char* str) { - Sel sel = view->selection; - size_t prev = sel.end; - buf_findstr(&(view->buffer), dir, str, &sel.beg, &sel.end); - bool found = (0 != memcmp(&sel, &(view->selection), sizeof(Sel))); - view->selection = sel; + bool found = buf_findstr(&(view->buffer), &(view->selection), dir, str); view->sync_needed = true; view->sync_center = true; return found; -- 2.54.0