How to determine the visual length of a character?

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?

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

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

Thanks, I've have just found it too.

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.

2 Likes