]> git.mdlowis.com Git - projs/tide.git/commitdiff
reworked findstr function
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 3 Apr 2018 02:42:24 +0000 (22:42 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 3 Apr 2018 02:42:24 +0000 (22:42 -0400)
inc/edit.h
lib/buf.c
lib/view.c

index 722fdb7e25a9fbe81178f843f83baf18d010e8d2..f055231529318a2acfb55e8416abc4ded9221ed5 100644 (file)
@@ -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);
index 70aba7c85c5656ee4dc3da5bc64aab665f42af05..62afe92ac2ac2b1462432eebc26590bf8d246090 100644 (file)
--- 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);
index d43019ece557afe6379308f0cd65f14440213eff..92e0373837473454a412c4050f542d87fb2c6d2f 100644 (file)
@@ -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;