A port of ucwidth C library to Elixir, with Emoji (basis, variations and zwj-combined-sequences) supported.
When developing a terminal based Elixir application, I found lack of some library handling character width correctly. Turns out there's a need to port ucwidth to Elixir. So I made this library.
I'm not professional at Unicode, just read the specifications and some references. So please report any issues you meet, thanks in advance.
This library provides a function Ucwidth.width/2
for detecting the display width of a Unicode character.
For example:
iex> Ucwidth.width("行")
2
iex> Ucwidth.width("h")
1
iex> Ucwidth.width("\x0C")
0
iex> Ucwidth.width("Hello, <<👪>>, family lovers!")
29
Note: according to Unicode standard, not every character has fixed width. There are some characters with variable width, which is called ambiguous width characters
. Their width vary depends on the context. This library by default treats ambiguous charaters as narrow ones, and also provides a way to pass be context, e.g:
iex> Ucwidth.width("\u00a1")
1
iex> Ucwidth.width("\u00a1", :wide)
2
Add ucwidth
to your list of dependencies in mix.exs
:
def deps do
[
{:ucwidth, "~> 0.2"}
]
end
Attention to performance was paid during development. You can check the performance by running:
mix run bench/benchmark.exs
This library is open-sourced under an MIT license.