Expand description
§East Asian Width
A Rust library for determining the display width of Unicode characters in East Asian contexts. This is particularly useful for terminal applications, text editors, and other software that needs to properly align text containing CJK (Chinese, Japanese, Korean) characters.
§Features
- Fast lookups: Pre-generated lookup tables for O(1) character width determination
- Unicode compliant: Based on the official Unicode East Asian Width property
- Flexible API: Support for both simple and configurable width calculations
- Zero dependencies: No runtime dependencies for the core library
§Usage
§Basic Usage
use east_asian_width::{east_asian_width, east_asian_width_type, DisplayWidth};
// Get the display width of a character
assert_eq!(east_asian_width('A' as u32), DisplayWidth::Narrow); // Narrow/Neutral = 1
assert_eq!(east_asian_width('字' as u32), DisplayWidth::Wide); // Wide = 2
// Convert to numeric values when needed
assert_eq!(east_asian_width('A' as u32).as_u8(), 1);
assert_eq!(east_asian_width('字' as u32).as_u8(), 2);
// Get the East Asian Width category
assert_eq!(east_asian_width_type('A' as u32), "narrow");
assert_eq!(east_asian_width_type('字' as u32), "wide");§Handling Ambiguous Characters
Some characters are classified as “ambiguous” and can be displayed as either narrow or wide depending on the context. You can control this behavior:
use east_asian_width::{east_asian_width, DisplayWidth};
let ambiguous_char = 0x00A1; // ¡ (inverted exclamation mark)
// Default: ambiguous characters are treated as narrow (width 1)
assert_eq!(east_asian_width(ambiguous_char), DisplayWidth::Narrow);
// Treat ambiguous characters as wide (width 2)
assert_eq!(east_asian_width((ambiguous_char, true)), DisplayWidth::Wide);
// Convert to numeric values
assert_eq!(east_asian_width(ambiguous_char).as_u8(), 1);
assert_eq!(east_asian_width((ambiguous_char, true)).as_u8(), 2);§Character Categories
The library recognizes six East Asian Width categories:
- Narrow (Na): Characters that are always narrow (width 1)
- Neutral (N): Characters that don’t have East Asian context (width 1)
- Halfwidth (H): Characters that are narrow in East Asian context (width 1)
- Ambiguous (A): Characters that can be narrow or wide depending on context
- Wide (W): Characters that are always wide (width 2)
- Fullwidth (F): Characters that are wide in East Asian context (width 2)
Re-exports§
pub use error::EastAsianWidthError;pub use error::validate_code_point;pub use lookup::is_ambiguous;pub use lookup::is_full_width;pub use lookup::is_wide;
Modules§
- error
- Error types and validation functions for East Asian Width operations.
- lookup
- Contains the generated Unicode lookup tables and functions.
Enums§
- Display
Width - Represents the display width of a character in East Asian contexts
Traits§
- East
Asian Width Input - Trait to allow different parameter types for the
east_asian_widthfunction.
Functions§
- east_
asian_ width - Returns the display width of a Unicode code point in East Asian contexts
- east_
asian_ width_ type - Returns the East Asian Width category for a Unicode code point
- try_
east_ asian_ width - Fallible version of
east_asian_width - try_
east_ asian_ width_ type - Fallible version of
east_asian_width_type