| 1 | # displaywidth |
| 2 | |
| 3 | A high-performance Go package for measuring the monospace display width of strings, UTF-8 bytes, and runes. |
| 4 | |
| 5 | [](https://pkg.go.dev/github.com/clipperhouse/displaywidth) |
| 6 | [](https://github.com/clipperhouse/displaywidth/actions/workflows/gotest.yml) |
| 7 | [](https://github.com/clipperhouse/displaywidth/actions/workflows/gofuzz.yml) |
| 8 | |
| 9 | ## Install |
| 10 | ```bash |
| 11 | go get github.com/clipperhouse/displaywidth |
| 12 | ``` |
| 13 | |
| 14 | ## Usage |
| 15 | |
| 16 | ```go |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "github.com/clipperhouse/displaywidth" |
| 22 | ) |
| 23 | |
| 24 | func main() { |
| 25 | width := displaywidth.String("Hello, δΈη!") |
| 26 | fmt.Println(width) |
| 27 | |
| 28 | width = displaywidth.Bytes([]byte("π")) |
| 29 | fmt.Println(width) |
| 30 | |
| 31 | width = displaywidth.Rune('π') |
| 32 | fmt.Println(width) |
| 33 | } |
| 34 | ``` |
| 35 | |
| 36 | For most purposes, you should use the `String` or `Bytes` methods. They sum |
| 37 | the widths of grapheme clusters in the string or byte slice. |
| 38 | |
| 39 | > Note: in your application, iterating over runes to measure width is likely incorrect; |
| 40 | the smallest unit of display is a grapheme, not a rune. |
| 41 | |
| 42 | ### Iterating over graphemes |
| 43 | |
| 44 | If you need the individual graphemes: |
| 45 | |
| 46 | ```go |
| 47 | import ( |
| 48 | "fmt" |
| 49 | "github.com/clipperhouse/displaywidth" |
| 50 | ) |
| 51 | |
| 52 | func main() { |
| 53 | g := displaywidth.StringGraphemes("Hello, δΈη!") |
| 54 | for g.Next() { |
| 55 | width := g.Width() |
| 56 | value := g.Value() |
| 57 | // do something with the width or value |
| 58 | } |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ### Options |
| 63 | |
| 64 | Create the options you need, and then use methods on the options struct. |
| 65 | |
| 66 | ```go |
| 67 | var myOptions = displaywidth.Options{ |
| 68 | EastAsianWidth: true, |
| 69 | ControlSequences: true, |
| 70 | } |
| 71 | |
| 72 | width := myOptions.String("Hello, δΈη!") |
| 73 | ``` |
| 74 | |
| 75 | #### ControlSequences |
| 76 | |
| 77 | `ControlSequences` specifies whether to ignore ECMA-48 escape sequences |
| 78 | when calculating the display width. When `false` (default), ANSI escape |
| 79 | sequences are treated as just a series of characters. When `true`, they are |
| 80 | treated as a single zero-width unit. |
| 81 | |
| 82 | #### ControlSequences8Bit |
| 83 | |
| 84 | `ControlSequences8Bit` specifies whether to ignore 8-bit ECMA-48 escape sequences |
| 85 | when calculating the display width. When `false` (default), these are treated |
| 86 | as just a series of characters. When `true`, they are treated as a single |
| 87 | zero-width unit. |
| 88 | |
| 89 | Note: this option is ignored by the `Truncate` methods, as the concatenation |
| 90 | can lead to unintended UTF-8 semantics. |
| 91 | |
| 92 | #### EastAsianWidth |
| 93 | |
| 94 | `EastAsianWidth` defines how |
| 95 | [East Asian Ambiguous characters](https://www.unicode.org/reports/tr11/#Ambiguous) |
| 96 | are treated. |
| 97 | |
| 98 | When `false` (default), East Asian Ambiguous characters are treated as width 1. |
| 99 | When `true`, they are treated as width 2. |
| 100 | |
| 101 | You may wish to configure this based on environment variables or locale. |
| 102 | `go-runewidth`, for example, does so |
| 103 | [during package initialization](https://github.com/mattn/go-runewidth/blob/master/runewidth.go#L26C1-L45C2). `displaywidth` does not do this automatically, we prefer to leave it to you. |
| 104 | |
| 105 | |
| 106 | ## Technical standards and compatibility |
| 107 | |
| 108 | This package implements the Unicode East Asian Width standard |
| 109 | ([UAX #11](https://www.unicode.org/reports/tr11/tr11-43.html)), and handles |
| 110 | [version selectors](https://en.wikipedia.org/wiki/Variation_Selectors_(Unicode_block)), |
| 111 | and [regional indicator pairs](https://en.wikipedia.org/wiki/Regional_indicator_symbol) |
| 112 | (flags). We implement [Unicode TR51](https://www.unicode.org/reports/tr51/tr51-27.html) |
| 113 | for emojis. We are keeping an eye on |
| 114 | [emerging standards](https://www.jeffquast.com/post/state-of-terminal-emulation-2025/). |
| 115 | |
| 116 | For control sequences, we implement the [ECMA-48](https://ecma-international.org/publications-and-standards/standards/ecma-48/) standard for 7-bit and 8-bit control sequences. |
| 117 | |
| 118 | `clipperhouse/displaywidth`, `mattn/go-runewidth`, and `rivo/uniseg` will |
| 119 | give the same outputs for most real-world text. Extensive details are in the |
| 120 | [compatibility analysis](comparison/COMPATIBILITY_ANALYSIS.md). |
| 121 | |
| 122 | ## Invalid UTF-8 |
| 123 | |
| 124 | This package does not validate UTF-8. If you pass invalid UTF-8, the results |
| 125 | are undefined. We fuzz against invalid UTF-8 to ensure we don't panic or |
| 126 | loop indefinitely. |
| 127 | |
| 128 | The `ControlSequences8Bit` option means that we will segment valid 8-bit |
| 129 | control sequences, which are typically _not_ valid UTF-8. 8-bit control bytes |
| 130 | happen to also be UTF-8 continuation bytes. Use with caution. |
| 131 | |
| 132 | ## Prior Art |
| 133 | |
| 134 | [mattn/go-runewidth](https://github.com/mattn/go-runewidth) |
| 135 | |
| 136 | [rivo/uniseg](https://github.com/rivo/uniseg) |
| 137 | |
| 138 | [x/text/width](https://pkg.go.dev/golang.org/x/text/width) |
| 139 | |
| 140 | [x/text/internal/triegen](https://pkg.go.dev/golang.org/x/text/internal/triegen) |
| 141 | |
| 142 | ## Benchmarks |
| 143 | |
| 144 | ```bash |
| 145 | cd comparison |
| 146 | go test -bench=. -benchmem |
| 147 | ``` |
| 148 | |
| 149 | ``` |
| 150 | goos: darwin |
| 151 | goarch: arm64 |
| 152 | pkg: github.com/clipperhouse/displaywidth/comparison |
| 153 | cpu: Apple M2 |
| 154 | |
| 155 | BenchmarkString_Mixed/clipperhouse/displaywidth-8 5784 ns/op 291.69 MB/s 0 B/op 0 allocs/op |
| 156 | BenchmarkString_Mixed/mattn/go-runewidth-8 14751 ns/op 114.36 MB/s 0 B/op 0 allocs/op |
| 157 | BenchmarkString_Mixed/rivo/uniseg-8 19360 ns/op 87.14 MB/s 0 B/op 0 allocs/op |
| 158 | |
| 159 | BenchmarkString_ASCII/clipperhouse/displaywidth-8 54.60 ns/op 2344.32 MB/s 0 B/op 0 allocs/op |
| 160 | BenchmarkString_ASCII/mattn/go-runewidth-8 1195 ns/op 107.08 MB/s 0 B/op 0 allocs/op |
| 161 | BenchmarkString_ASCII/rivo/uniseg-8 1578 ns/op 81.13 MB/s 0 B/op 0 allocs/op |
| 162 | |
| 163 | BenchmarkString_EastAsian/clipperhouse/displaywidth-8 5837 ns/op 289.01 MB/s 0 B/op 0 allocs/op |
| 164 | BenchmarkString_EastAsian/mattn/go-runewidth-8 24418 ns/op 69.09 MB/s 0 B/op 0 allocs/op |
| 165 | BenchmarkString_EastAsian/rivo/uniseg-8 19339 ns/op 87.23 MB/s 0 B/op 0 allocs/op |
| 166 | |
| 167 | BenchmarkString_Emoji/clipperhouse/displaywidth-8 3225 ns/op 224.51 MB/s 0 B/op 0 allocs/op |
| 168 | BenchmarkString_Emoji/mattn/go-runewidth-8 4851 ns/op 149.25 MB/s 0 B/op 0 allocs/op |
| 169 | BenchmarkString_Emoji/rivo/uniseg-8 6591 ns/op 109.85 MB/s 0 B/op 0 allocs/op |
| 170 | |
| 171 | BenchmarkRune_Mixed/clipperhouse/displaywidth-8 3385 ns/op 498.34 MB/s 0 B/op 0 allocs/op |
| 172 | BenchmarkRune_Mixed/mattn/go-runewidth-8 5354 ns/op 315.07 MB/s 0 B/op 0 allocs/op |
| 173 | |
| 174 | BenchmarkRune_EastAsian/clipperhouse/displaywidth-8 3397 ns/op 496.56 MB/s 0 B/op 0 allocs/op |
| 175 | BenchmarkRune_EastAsian/mattn/go-runewidth-8 15673 ns/op 107.64 MB/s 0 B/op 0 allocs/op |
| 176 | |
| 177 | BenchmarkRune_ASCII/clipperhouse/displaywidth-8 255.7 ns/op 500.53 MB/s 0 B/op 0 allocs/op |
| 178 | BenchmarkRune_ASCII/mattn/go-runewidth-8 261.5 ns/op 489.55 MB/s 0 B/op 0 allocs/op |
| 179 | |
| 180 | BenchmarkRune_Emoji/clipperhouse/displaywidth-8 1371 ns/op 528.22 MB/s 0 B/op 0 allocs/op |
| 181 | BenchmarkRune_Emoji/mattn/go-runewidth-8 2267 ns/op 319.43 MB/s 0 B/op 0 allocs/op |
| 182 | |
| 183 | BenchmarkTruncateWithTail/clipperhouse/displaywidth-8 3229 ns/op 54.82 MB/s 192 B/op 14 allocs/op |
| 184 | BenchmarkTruncateWithTail/mattn/go-runewidth-8 8408 ns/op 21.05 MB/s 192 B/op 14 allocs/op |
| 185 | |
| 186 | BenchmarkTruncateWithoutTail/clipperhouse/displaywidth-8 3554 ns/op 64.43 MB/s 0 B/op 0 allocs/op |
| 187 | BenchmarkTruncateWithoutTail/mattn/go-runewidth-8 11189 ns/op 20.47 MB/s 0 B/op 0 allocs/op |
| 188 | ``` |
| 189 | |
| 190 | Here are some notes on [how to make Unicode things fast](https://clipperhouse.com/go-unicode/). |