17 releases

0.2.0 Sep 19, 2024
0.1.13 Jun 4, 2024
0.1.11 Sep 19, 2023
0.1.10 Sep 13, 2022
0.1.2 Jul 9, 2015

#20 in Text processing

Download history 1636192/week @ 2024-07-18 1648312/week @ 2024-07-25 1649471/week @ 2024-08-01 1719450/week @ 2024-08-08 1631798/week @ 2024-08-15 1676945/week @ 2024-08-22 1607533/week @ 2024-08-29 1754426/week @ 2024-09-05 1675216/week @ 2024-09-12 1763393/week @ 2024-09-19 1887921/week @ 2024-09-26 1972902/week @ 2024-10-03 1909337/week @ 2024-10-10 2032987/week @ 2024-10-17 2024151/week @ 2024-10-24 1939914/week @ 2024-10-31

8,295,510 downloads per month
Used in 24,602 crates (677 directly)

MIT/Apache

1.5MB
23K SLoC

unicode-width

Build status crates.io version Docs status

Determine displayed width of char and str types according to Unicode Standard Annex #11 and other portions of the Unicode standard.

This crate is #![no_std].

use unicode_width::UnicodeWidthStr;

fn main() {
    let teststr = "Hello, world!";
    let width = teststr.width();
    println!("{}", teststr);
    println!("The above string is {} columns wide.", width);
    let width = teststr.width_cjk();
    println!("The above string is {} columns wide (CJK).", width);
}

NOTE: The computed width values may not match the actual rendered column width. For example, many Brahmic scripts like Devanagari have complex rendering rules which this crate does not currently handle (and will never fully handle, because the exact rendering depends on the font):

extern crate unicode_width;
use unicode_width::UnicodeWidthStr;

fn main() {
    assert_eq!("".width(), 1); // Devanagari letter Ka
    assert_eq!("".width(), 1); // Devanagari letter Ssa
    assert_eq!("क्ष".width(), 2); // Ka + Virama + Ssa
}

Additionally, defective combining character sequences and nonstandard Korean jamo sequences may be rendered with a different width than what this crate says. (This is not an exhaustive list.) For a list of what this crate does handle, see docs.rs.

crates.io

You can use this package in your project by adding the following to your Cargo.toml:

[dependencies]
unicode-width = "0.1.11"

Changelog

0.2.0

  • Treat \n as width 1 (#60)
  • Treat ambiguous Modifier_Letters as narrow (#63)
  • Support Grapheme_Cluster_Break=Prepend (#62)
  • Support lots of ligatures (#53)

Note: If you are using unicode-width for linebreaking, the change treating \n as width 1 may cause behavior changes. It is recommended that in such cases you feed already-line segmented text to unicode-width. In other words, please apply higher level control character based line breaking protocols before feeding text to unicode-width. Relying on any character producing a stable width in this crate is likely the sign of a bug.

Dependencies