Documentation
¶
Overview ¶
Package uniwidth provides modern Unicode width calculation for Go 1.25+.
uniwidth uses a tiered lookup strategy for optimal performance:
- Tier 1: ASCII (O(1), ~95% of typical content)
- Tier 2: Common CJK & Emoji (O(1), ~90% of non-ASCII)
- Tier 3: Common Emoji (O(1))
- Tier 4: 3-stage table lookup for all other characters (O(1))
All tiers are O(1) with zero allocations for single-rune lookups. This approach is 3-46x faster than traditional binary-search-only methods like go-runewidth, while maintaining full Unicode 16.0 compliance.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RuneWidth ¶
RuneWidth returns the visual width of a rune in monospace terminals.
Returns:
- 0 for control characters, zero-width joiners, combining marks
- 1 for most characters (ASCII, Latin, Cyrillic, etc.)
- 2 for wide characters (CJK, Emoji, etc.)
This function uses a tiered lookup strategy:
- O(1) for ASCII (most common case)
- O(1) for common CJK and emoji (hot paths)
- O(1) for all other characters (3-stage table lookup)
func RuneWidthWithOptions ¶
RuneWidthWithOptions returns the visual width of a rune with custom options.
This function applies the same tiered lookup strategy as RuneWidth, but allows customization of ambiguous character handling and emoji presentation.
Example:
// East Asian locale (ambiguous characters are wide)
width := uniwidth.RuneWidthWithOptions('±', uniwidth.WithEastAsianAmbiguous(uniwidth.EAWide))
// width = 2
// Neutral locale (ambiguous characters are narrow)
width := uniwidth.RuneWidthWithOptions('±', uniwidth.WithEastAsianAmbiguous(uniwidth.EANarrow))
// width = 1
func StringWidth ¶
StringWidth calculates the visual width of a string in monospace terminals.
This function provides a fast path for ASCII-only strings, and uses a state machine for correct handling of multi-rune sequences.
Special handling:
- ZWJ emoji sequences (👨👩👧👦) are treated as width 2, not the sum of parts
- Emoji modifier sequences (👍🏽) are treated as width 2
- Variation selectors (U+FE0E/U+FE0F) modify the width of the preceding character
- Regional indicator pairs (flags) are counted as width 2, not 4
func StringWidthWithOptions ¶
StringWidthWithOptions calculates the visual width of a string with custom options.
This function applies the same fast paths as StringWidth, but allows customization of ambiguous character handling and emoji presentation.
Example:
// East Asian locale (ambiguous characters are wide)
opts := []uniwidth.Option{
uniwidth.WithEastAsianAmbiguous(uniwidth.EAWide),
}
width := uniwidth.StringWidthWithOptions("Hello ±½", opts...)
// width = 10 (Hello=5, space=1, ±=2, ½=2)
// Neutral locale (ambiguous characters are narrow)
opts := []uniwidth.Option{
uniwidth.WithEastAsianAmbiguous(uniwidth.EANarrow),
}
width := uniwidth.StringWidthWithOptions("Hello ±½", opts...)
// width = 8 (Hello=5, space=1, ±=1, ½=1)
Types ¶
type Option ¶
type Option func(*Options)
Option is a functional option for configuring Unicode width calculation.
func WithEastAsianAmbiguous ¶
WithEastAsianAmbiguous sets the width for East Asian Ambiguous characters.
Example:
// Treat ambiguous characters as wide (East Asian locale)
width := uniwidth.StringWidthWithOptions("±½", uniwidth.WithEastAsianAmbiguous(uniwidth.EAWide))
// width = 4 (each character is 2 columns wide)
// Treat ambiguous characters as narrow (neutral locale)
width := uniwidth.StringWidthWithOptions("±½", uniwidth.WithEastAsianAmbiguous(uniwidth.EANarrow))
// width = 2 (each character is 1 column wide)
func WithEmojiPresentation ¶
WithEmojiPresentation sets whether emoji should be rendered as emoji (wide) or text (narrow).
Example:
// Emoji as emoji (wide, width 2) - default
width := uniwidth.StringWidthWithOptions("😀", uniwidth.WithEmojiPresentation(true))
// width = 2
// Emoji as text (narrow, width 1)
width := uniwidth.StringWidthWithOptions("😀", uniwidth.WithEmojiPresentation(false))
// width = 1
Note: This primarily affects emoji that have both text and emoji presentation variants. Most emoji are always rendered as wide regardless of this setting.
type Options ¶
type Options struct {
// EastAsianAmbiguous specifies how to handle ambiguous-width characters.
// Default: EANarrow (width 1)
EastAsianAmbiguous EAWidth
// EmojiPresentation specifies whether emoji should be rendered as emoji (width 2)
// or text (width 1). When true, emoji are treated as width 2.
// Default: true (emoji presentation)
EmojiPresentation bool
}
Options configures Unicode width calculation behavior.
Use the functional options pattern to create customized configurations:
opts := []uniwidth.Option{
uniwidth.WithEastAsianAmbiguous(uniwidth.EAWide),
uniwidth.WithEmojiPresentation(true),
}
width := uniwidth.StringWidthWithOptions("Hello 世界", opts...)
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
generate-tables
command
generate-tables generates Unicode width tables from official Unicode 16.0 data.
|
generate-tables generates Unicode width tables from official Unicode 16.0 data. |