diff --git a/wcwidth/wcwidth.go b/wcwidth/wcwidth.go index 81dd272e..d9f77158 100644 --- a/wcwidth/wcwidth.go +++ b/wcwidth/wcwidth.go @@ -15,6 +15,8 @@ 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 { @@ -22,6 +24,9 @@ func RuneWidth(r rune) int { // 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) diff --git a/wcwidth/wcwidth_test.go b/wcwidth/wcwidth_test.go index 598c2a11..ebd759fc 100644 --- a/wcwidth/wcwidth_test.go +++ b/wcwidth/wcwidth_test.go @@ -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) {