diff options
| author | Ivan Delalande <colona@ycc.fr> | 2014-06-27 09:15:56 +0200 |
|---|---|---|
| committer | Roberto E. Vargas Caballero <k0ga@shike2.com> | 2014-07-08 09:21:48 +0200 |
| commit | 955923b38b1f08f6cb25a706cea2af5966339187 (patch) | |
| tree | 45e518b217dfa8d64d0ebea0635c9700492e1db7 | |
| parent | 0015e198bfceae50a55403ad1339345340f4dcc0 (diff) | |
| download | st-955923b38b1f08f6cb25a706cea2af5966339187.tar.gz st-955923b38b1f08f6cb25a706cea2af5966339187.zip | |
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>
| -rw-r--r-- | st.c | 42 |
1 files changed, 16 insertions, 26 deletions
| @@ -375,6 +375,7 @@ static void tdeletechar(int); | |||
| 375 | static void tdeleteline(int); | 375 | static void tdeleteline(int); |
| 376 | static void tinsertblank(int); | 376 | static void tinsertblank(int); |
| 377 | static void tinsertblankline(int); | 377 | static void tinsertblankline(int); |
| 378 | static int tlinelen(int); | ||
| 378 | static void tmoveto(int, int); | 379 | static void tmoveto(int, int); |
| 379 | static void tmoveato(int, int); | 380 | static void tmoveato(int, int); |
| 380 | static void tnew(int, int); | 381 | static void tnew(int, int); |
| @@ -920,7 +921,7 @@ bpress(XEvent *e) { | |||
| 920 | char * | 921 | char * |
| 921 | getsel(void) { | 922 | getsel(void) { |
| 922 | char *str, *ptr; | 923 | char *str, *ptr; |
| 923 | int x, y, bufsize, size, ex; | 924 | int y, bufsize, size, lastx, linelen; |
| 924 | Glyph *gp, *last; | 925 | Glyph *gp, *last; |
| 925 | 926 | ||
| 926 | if(sel.ob.x == -1) | 927 | if(sel.ob.x == -1) |
| @@ -931,16 +932,19 @@ getsel(void) { | |||
| 931 | 932 | ||
| 932 | /* append every set & selected glyph to the selection */ | 933 | /* append every set & selected glyph to the selection */ |
| 933 | for(y = sel.nb.y; y < sel.ne.y + 1; y++) { | 934 | for(y = sel.nb.y; y < sel.ne.y + 1; y++) { |
| 934 | gp = &term.line[y][0]; | 935 | linelen = tlinelen(y); |
| 935 | last = &gp[term.col-1]; | ||
| 936 | 936 | ||
| 937 | while(last >= gp && !(selected(last - gp, y) && | 937 | if(sel.type == SEL_RECTANGULAR) { |
| 938 | strcmp(last->c, " ") != 0)) { | 938 | gp = &term.line[y][sel.nb.x]; |
| 939 | --last; | 939 | lastx = sel.ne.x; |
| 940 | } else { | ||
| 941 | gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0]; | ||
| 942 | lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1; | ||
| 940 | } | 943 | } |
| 944 | last = &term.line[y][MIN(lastx, linelen-1)]; | ||
| 941 | 945 | ||
| 942 | for(x = 0; gp <= last; x++, ++gp) { | 946 | for( ; gp <= last; ++gp) { |
| 943 | if(!selected(x, y) || (gp->mode & ATTR_WDUMMY)) | 947 | if(gp->mode & ATTR_WDUMMY) |
| 944 | continue; | 948 | continue; |
| 945 | 949 | ||
| 946 | size = utf8len(gp->c); | 950 | size = utf8len(gp->c); |
| @@ -957,20 +961,8 @@ getsel(void) { | |||
| 957 | * st. | 961 | * st. |
| 958 | * FIXME: Fix the computer world. | 962 | * FIXME: Fix the computer world. |
| 959 | */ | 963 | */ |
| 960 | if(y < sel.ne.y && !(x > 0 && (gp-1)->mode & ATTR_WRAP)) | 964 | if(sel.ne.y > y || lastx >= linelen) |
| 961 | *ptr++ = '\n'; | 965 | *ptr++ = '\n'; |
| 962 | |||
| 963 | /* | ||
| 964 | * If the last selected line expands in the selection | ||
| 965 | * after the visible text '\n' is appended. | ||
| 966 | */ | ||
| 967 | if(y == sel.ne.y) { | ||
| 968 | ex = sel.ne.x; | ||
| 969 | if(sel.nb.y == sel.ne.y && sel.ne.x < sel.nb.x) | ||
| 970 | ex = sel.nb.x; | ||
| 971 | if(tlinelen(y) < ex) | ||
| 972 | *ptr++ = '\n'; | ||
| 973 | } | ||
| 974 | } | 966 | } |
| 975 | *ptr = 0; | 967 | *ptr = 0; |
| 976 | return str; | 968 | return str; |
| @@ -2287,12 +2279,10 @@ tdumpline(int n) { | |||
| 2287 | Glyph *bp, *end; | 2279 | Glyph *bp, *end; |
| 2288 | 2280 | ||
| 2289 | bp = &term.line[n][0]; | 2281 | bp = &term.line[n][0]; |
| 2290 | end = &bp[term.col-1]; | 2282 | end = &bp[MIN(tlinelen(n), term.col) - 1]; |
| 2291 | while(end > bp && !strcmp(" ", end->c)) | 2283 | if(bp != end || bp->c[0] != ' ') { |
| 2292 | --end; | ||
| 2293 | if(bp != end || strcmp(bp->c, " ")) { | ||
| 2294 | for( ;bp <= end; ++bp) | 2284 | for( ;bp <= end; ++bp) |
| 2295 | tprinter(bp->c, strlen(bp->c)); | 2285 | tprinter(bp->c, utf8len(bp->c)); |
| 2296 | } | 2286 | } |
| 2297 | tprinter("\n", 1); | 2287 | tprinter("\n", 1); |
| 2298 | } | 2288 | } |
