st

fork of suckless's simple terminal
Index Commits Files Refs README LICENSE
commit 955923b38b1f08f6cb25a706cea2af5966339187
parent 0015e198bfceae50a55403ad1339345340f4dcc0
Author: Ivan Delalande <colona@ycc.fr>
Date:   Fri, 27 Jun 2014 09:15:56 +0200

Remove all strcmp and strlen calls on Glyph.c[]

There were a few occurrences of strcmp and strlen being called on Glyph.c[],
which is not always null-terminated (this actually depends on the last values in
the buffer s in ttyread()). This patch replace all the calls to strcmp with a
test on c[0] directly or a call to tlinelen, and the one to strlen with utf8len.
I also took the opportunity to refactor getsel and tdumpline.

Signed-off-by: Roberto E. Vargas Caballero <k0ga@shike2.com>

Diffstat:
Mst.c | 42++++++++++++++++--------------------------
1 file changed, 16 insertions(+), 26 deletions(-)
diff --git a/st.c b/st.c
@@ -375,6 +375,7 @@ static void tdeletechar(int);
 static void tdeleteline(int);
 static void tinsertblank(int);
 static void tinsertblankline(int);
+static int tlinelen(int);
 static void tmoveto(int, int);
 static void tmoveato(int, int);
 static void tnew(int, int);
@@ -920,7 +921,7 @@ bpress(XEvent *e) {
 char *
 getsel(void) {
     char *str, *ptr;
-    int x, y, bufsize, size, ex;
+    int y, bufsize, size, lastx, linelen;
     Glyph *gp, *last;
 
     if(sel.ob.x == -1)
@@ -931,16 +932,19 @@ getsel(void) {
 
     /* append every set & selected glyph to the selection */
     for(y = sel.nb.y; y < sel.ne.y + 1; y++) {
-        gp = &term.line[y][0];
-        last = &gp[term.col-1];
+        linelen = tlinelen(y);
 
-        while(last >= gp && !(selected(last - gp, y) &&
-                      strcmp(last->c, " ") != 0)) {
-            --last;
+        if(sel.type == SEL_RECTANGULAR) {
+            gp = &term.line[y][sel.nb.x];
+            lastx = sel.ne.x;
+        } else {
+            gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
+            lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
         }
+        last = &term.line[y][MIN(lastx, linelen-1)];
 
-        for(x = 0; gp <= last; x++, ++gp) {
-            if(!selected(x, y) || (gp->mode & ATTR_WDUMMY))
+        for( ; gp <= last; ++gp) {
+            if(gp->mode & ATTR_WDUMMY)
                 continue;
 
             size = utf8len(gp->c);
@@ -957,20 +961,8 @@ getsel(void) {
          * st.
          * FIXME: Fix the computer world.
          */
-        if(y < sel.ne.y && !(x > 0 && (gp-1)->mode & ATTR_WRAP))
+        if(sel.ne.y > y || lastx >= linelen)
             *ptr++ = '\n';
-
-        /*
-         * If the last selected line expands in the selection
-         * after the visible text '\n' is appended.
-         */
-        if(y == sel.ne.y) {
-            ex = sel.ne.x;
-            if(sel.nb.y == sel.ne.y && sel.ne.x < sel.nb.x)
-                ex = sel.nb.x;
-            if(tlinelen(y) < ex)
-                *ptr++ = '\n';
-        }
     }
     *ptr = 0;
     return str;
@@ -2287,12 +2279,10 @@ tdumpline(int n) {
     Glyph *bp, *end;
 
     bp = &term.line[n][0];
-    end = &bp[term.col-1];
-    while(end > bp && !strcmp(" ", end->c))
-        --end;
-    if(bp != end || strcmp(bp->c, " ")) {
+    end = &bp[MIN(tlinelen(n), term.col) - 1];
+    if(bp != end || bp->c[0] != ' ') {
         for( ;bp <= end; ++bp)
-            tprinter(bp->c, strlen(bp->c));
+            tprinter(bp->c, utf8len(bp->c));
     }
     tprinter("\n", 1);
 }