From 4e2a1ba9b194f359471f80bd4d00e591ba740ce4 Mon Sep 17 00:00:00 2001
From: Jim Warner <james.warner@comcast.net>
Date: Sun, 8 Oct 2017 00:00:00 -0500
Subject: [PATCH] top: stop neglecting potential utf8 field descriptions

And I thought those strange characters I saw with only
certain translations in Fields Management descriptions
were resulting from my terminal emulator deficiencies.

Turns out that ol' top wasn't addressing possibilities
of such descriptions ending with multi-byte sequences.

Signed-off-by: Jim Warner <james.warner@comcast.net>
---
 top/top.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/top/top.c b/top/top.c
index e322808b..e90935c4 100644
--- a/top/top.c
+++ b/top/top.c
@@ -2197,16 +2197,17 @@ static void display_fields (int focus, int extend) {
  #define mkERR { putp("\n"); putp(N_txt(XTRA_winsize_txt)); return; }
  #define mxCOL ( (Screen_cols / 11) > 0 ? (Screen_cols / 11) : 1 )
  #define yRSVD  4
- #define xSUFX  22
+ #define xEQUS  2                      // length of suffix beginning '= '
+ #define xSUFX  22                     // total suffix length, incl xEQUS
  #define xPRFX (10 + xadd)
  #define xTOTL (xPRFX + xSUFX)
+   static int col_sav, row_sav;
    WIN_t *w = Curwin;                  // avoid gcc bloat with a local copy
    int i;                              // utility int (a row, tot cols, ix)
    int smax;                           // printable width of xSUFX
    int xadd = 0;                       // spacing between data columns
    int cmax = Screen_cols;             // total data column width
    int rmax = Screen_rows - yRSVD;     // total useable rows
-   static int col_sav, row_sav;
 
    i = (EU_MAXPFLGS % mxCOL) ? 1 : 0;
    if (rmax < i + (EU_MAXPFLGS / mxCOL)) mkERR;
@@ -2230,15 +2231,15 @@ static void display_fields (int focus, int extend) {
       int b = FLDviz(w, i), x = (i / rmax) * cmax, y = (i % rmax) + yRSVD;
       const char *e = (i == focus && extend) ? w->capclr_hdr : "";
       FLG_t f = FLDget(w, i);
-      char sbuf[xSUFX+1];
+      char sbuf[xSUFX*4];                        // 4 = max multi-byte
       int xcol, xfld;
 
+      // prep sacrificial suffix (allowing for beginning '= ') ...
+      snprintf(sbuf, sizeof(sbuf), "= %.*s", utf8_embody(N_fld(f), smax - xEQUS), N_fld(f));
+
       // obtain translated deltas (if any) ...
       xcol = utf8_delta(fmtmk("%.*s", utf8_embody(N_col(f), 7), N_col(f)));
-      xfld = utf8_delta(fmtmk("%.*s", utf8_embody(N_fld(f), smax), N_fld(f)));
-
-      // prep sacrificial suffix ...
-      snprintf(sbuf, sizeof(sbuf), "= %s", N_fld(f));
+      xfld = utf8_delta(sbuf + xEQUS);           // ignore beginning '= '
 
       PUTT("%s%c%s%s %s%-*.*s%s%s%s %-*.*s%s"
          , tg2(x, y)
@@ -2260,6 +2261,7 @@ static void display_fields (int focus, int extend) {
  #undef mkERR
  #undef mxCOL
  #undef yRSVD
+ #undef xEQUS
  #undef xSUFX
  #undef xPRFX
  #undef xTOTL
-- 
2.40.0