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: Binary search for rare characters (O(log n))
This approach is 3-4x 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(log n) for rare characters (fallback)
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 RuneWidth for strings containing Unicode characters.
Special handling:
- 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. |