- Notifications
You must be signed in to change notification settings - Fork32
Displayed width of Unicode characters and strings according to UAX#11 rules.
License
Apache-2.0, MIT licenses found
Licenses found
unicode-rs/unicode-width
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Determine displayed width ofchar
andstr
types according toUnicode Standard Annex #11and other portions of the Unicode standard.
This crate is#![no_std]
.
use unicode_width::UnicodeWidthStr;fnmain(){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 columnwidth. For example, many Brahmic scripts like Devanagari have complex rendering ruleswhich this crate does not currently handle (and will never fully handle, becausethe exact rendering depends on the font):
externcrate unicode_width;use unicode_width::UnicodeWidthStr;fnmain(){assert_eq!("क".width(),1);// Devanagari letter Kaassert_eq!("ष".width(),1);// Devanagari letter Ssaassert_eq!("क्ष".width(),2);// Ka + Virama + Ssa}
Additionally,defective combining character sequencesand nonstandardKorean jamo sequences maybe rendered with a different width than what this crate says. (This is not anexhaustive list.) For a list of what this cratedoes handle, seedocs.rs.
You can use this package in your project by adding the followingto yourCargo.toml
:
[dependencies]unicode-width ="0.2"
- Treat
\n
as width 1 (#60) - Treat ambiguous
Modifier_Letter
s as narrow (#63) - Support
Grapheme_Cluster_Break=Prepend
(#62) - Support lots of ligatures (#53)
Note: If you are usingunicode-width
for linebreaking, the change treating\n
as width 1may cause behavior changes. It is recommended that in such cases you feed already-line segmented text tounicode-width
. In other words, please apply higher level control character based line breaking protocols before feeding text tounicode-width
. Relying on any character producing a stable width in this crate is likely the sign of a bug.
About
Displayed width of Unicode characters and strings according to UAX#11 rules.
Resources
License
Apache-2.0, MIT licenses found
Licenses found
Uh oh!
There was an error while loading.Please reload this page.