Skip to content

A port of ucwidth from C to Elixir, for determining the width (no-space, full-width or half-width) of an Unicode character.

Notifications You must be signed in to change notification settings

qhwa/elixir-ucwidth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ucwidth

A port of ucwidth C library to Elixir, with Emoji (basis, variations and zwj-combined-sequences) supported.

Online documentation

Motivation

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.

Usage

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

Installation

Add ucwidth to your list of dependencies in mix.exs:

def deps do
  [
    {:ucwidth, "~> 0.2"}
  ]
end

Performance

Attention to performance was paid during development. You can check the performance by running:

mix run bench/benchmark.exs

License

This library is open-sourced under an MIT license.

About

A port of ucwidth from C to Elixir, for determining the width (no-space, full-width or half-width) of an Unicode character.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages