Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Oct 2020 21:18:50 +0200
From:      Matthias Apitz <guru@unixarea.de>
To:        freebsd-questions@freebsd.org
Subject:   printf(1) and UTF-8 multi-byte chars
Message-ID:  <20201017191850.GA11909@c720-r342378>

Next in thread | Raw E-Mail | Index | Archive | Help

If you look at the two examples:

$ printf "[%-10s]\n" "xxxx?xxx"
[xxxx?xxx  ]

$ printf "[%-10s]\n" "xxxx¿xxx"
[xxxx¿xxx ]

you see that in the first two blanks are used to fill the '%-10s'
pattern, while in the second only one blank is used. For sure, the
problem/bug has todo with being '¿' a multi-byte UTF-8 char:

$ echo '¿' | od -tx1
0000000    c2  bf  0a

i.e. with "xxxx¿xxx" 8 chars plus one blank are printed to give %-10s, with 
"xxxx?xxx" 8 chars plus two blanks are printed. This means the output of
printf(1) is byte oriented and not character oriented.

Is there a way to print it like this:

[xxxx¿xxx  ]
[xxxx?xxx  ]

Thanks

	matthias
-- 
Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045
Public GnuPG key: http://www.unixarea.de/key.pub
Без книги нет знания, без знания нет коммунизма (Влaдимир Ильич Ленин)
Without books no knowledge - without knowledge no communism (Vladimir Ilyich Lenin) 
Sin libros no hay saber - sin saber no hay comunismo. (Vladimir Ilich Lenin)



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?20201017191850.GA11909>