How to determine the visual length of a character?


#1

How to determine the visual length of a character?

+----------------------+
|                      |
|       中文处理       |
|       12345678       |
|                      |
+----------------------+

I always had an assumption that alphanumeric characters only
occupies 1 space, but the case for chinese characters, most occupies 2 space units (at least in monospace editors, vim, some source code editor).

Are there any characters which occupies more than 2 space units?


#2

I’ve been using https://github.com/unicode-rs/unicode-width.

$ cargo script -D unicode-width -e 'unicode_width::UnicodeWidthStr::width("中文处理")'
8

#3

Thanks, I’ve have just found it too.


#4

Keep in mind that the width can vary hugely based on what program is displaying it. Some terminals will respect unicode width, while others (such as the win32 console) blatantly disregard it.