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>
dev
Ivan Delalande 2014-06-27 09:15:56 +02:00 committed by Roberto E. Vargas Caballero
parent 0015e198bf
commit 955923b38b
1 changed files with 16 additions and 26 deletions

42
st.c
View File

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