Skip to content

Commit

Permalink
refactor(wcwidth): simplify RuneWidth and remove IsComb
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 22, 2024
1 parent c527e24 commit f9d4e91
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions wcwidth/wcwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// Unlike go-runewidth, wcwidth treats East Asian ambiguous characters as
// single-width characters. This is consistent with the behavior of wcwidth(3).
//
// See https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c

package wcwidth

Expand All @@ -13,15 +15,13 @@ import (
"golang.org/x/text/width"
)

// IsComb returns true if r is a Unicode combining character. Alias of:
//
// unicode.Is(unicode.Mn, r)
func IsComb(r rune) bool { return unicode.Is(unicode.Mn, r) }

// RuneWidth returns fixed-width width of rune.
// https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms#In_Unicode
func RuneWidth(r rune) int {
if r == 0 || !unicode.IsPrint(r) || IsComb(r) {
// 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) {
return 0
}
k := width.LookupRune(r)
Expand All @@ -41,5 +41,5 @@ func StringWidth(s string) (n int) {
for _, r := range s {
n += RuneWidth(r)
}
return n
return
}

0 comments on commit f9d4e91

Please sign in to comment.