]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
util: handle utf-8 thousend separators correctly
authorDaniel Wagner <dwagner@suse.de>
Mon, 25 Sep 2023 12:32:23 +0000 (14:32 +0200)
committerDaniel Wagner <wagi@monom.org>
Mon, 25 Sep 2023 12:43:25 +0000 (14:43 +0200)
The separator char can be an utf-8 encoded symbol thus the length
of the string is necessarly one.

The check if we have to issue an separator needs to a fixed modulo of 3
or 4 (without or with l10n enabled) to place the symbol at the right
position in the string.

The copy loop needs to copy the all bytes from the back of the separator
byte sequence to the front, the same order we print the number.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
util/types.c

index 044391d4a4d09c7ee3d4a72f94a2df046748e914..376c73435e122aa00d472639c5c6ff3a7f7a8374 100644 (file)
@@ -67,20 +67,21 @@ static char *__uint128_t_to_string(nvme_uint128_t val, bool l10n)
        int idx = 60;
        __u64 div, rem;
        char *sep = NULL;
-       int i, len = 0;
+       int i, len = 0, cl = 0;
 
        if (l10n) {
                sep = localeconv()->thousands_sep;
                len = strlen(sep);
+               cl = 1;
        }
 
        /* terminate at the end, and build up from the ones */
        str[--idx] = '\0';
 
        do {
-               if (len && !((sizeof(str) - idx) % (3 + len))) {
+               if (len && !((sizeof(str) - idx) % (3 + cl))) {
                        for (i = 0; i < len; i++)
-                               str[--idx] = sep[i];
+                               str[--idx] = sep[len - i - 1];
                }
 
                rem = val.words[0];