From a74f60444186befeb574f23e260e92fe2ae3436e Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Wed, 4 Oct 2017 00:00:00 -0500 Subject: [PATCH] top: make the 'utf8_proper_col' routine more efficient 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 --- top/top.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/top/top.c b/top/top.c index bd56ca74..1554b251 100644 --- 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; -- 2.40.0