]> granicus.if.org Git - procps-ng/commitdiff
top: make the 'utf8_proper_col' routine more efficient
authorJim Warner <james.warner@comcast.net>
Wed, 4 Oct 2017 05:00:00 +0000 (00:00 -0500)
committerCraig Small <csmall@enc.com.au>
Fri, 6 Oct 2017 22:07:17 +0000 (09:07 +1100)
This patch better exploits short-circuit evaluation in
two 'if' tests. In every case, the 1st of 2 conditions
in each 'if' test must take place but it always proves
true with each iteration for 1 of the 'if' statements.
Thus, the 2nd condition will have to be evaluated too.

By reordering 2 tests in each 'if', we can ensure that
the 2nd condition will then be tested much less often.

Signed-off-by: Jim Warner <james.warner@comcast.net>
top/top.c

index bd56ca74e128c929413ea30b7a4c4e5877e947d7..1554b25103ddfe4d7068d827e578c0a75d2c492b 100644 (file)
--- a/top/top.c
+++ b/top/top.c
@@ -562,10 +562,10 @@ static int utf8_proper_col (const char *str, int col, int tophysical) {
     while (*p) {
         // -1 represents a decoding error, don't encourage repositioning ...
         if (0 > (clen = UTF8_tab[*p])) return col;
-        if (tophysical && cnum + 1 > col) break;
+        if (cnum + 1 > col && tophysical) break;
         p += clen;
         tlen += clen;
-        if (!tophysical && tlen > col) break;
+        if (tlen > col && !tophysical) break;
         ++cnum;
     }
     return tophysical ? tlen : cnum;