From f762cd5660d83b8e14ed2f9478c3710f5d19dd9c 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 49f3732e..7677ce6b 100644 --- a/top/top.c +++ b/top/top.c @@ -723,10 +723,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