Skip to content

Commit

Permalink
fix(wcwidth): non-breaking space has width 1
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 22, 2024
1 parent 261659d commit 46d9bb9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions wcwidth/wcwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ import (
"golang.org/x/text/width"
)

const nbsp = 0xA0

// RuneWidth returns fixed-width width of rune.
// https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms#In_Unicode
func RuneWidth(r rune) int {
// No width for categories Me (Mark, enclosing), Mn (Mark, non-spacing), and
// Cf (Other, format). We treat Control characters (class Cc) as zero width
// instead of -1.
if r == 0 || !unicode.IsPrint(r) || unicode.In(r, unicode.Me, unicode.Mn, unicode.Cf) {
if r == nbsp { // Special case: non-breaking space has width 1
return 1
}
return 0
}
k := width.LookupRune(r)
Expand Down
2 changes: 1 addition & 1 deletion wcwidth/wcwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var runewidthtests = []struct {
{'\u0488', 0}, // Combining Cyrillic Hundred Thousands Sign
{'\u00ad', 0}, // Soft hyphen
{0, 0}, // Special case, width of null rune is zero
{'\u00a0', 0},
{'\u00a0', 1}, // non-breaking space
}

func BenchmarkRuneWidth(b *testing.B) {
Expand Down

0 comments on commit 46d9bb9

Please sign in to comment.