Skip to content

Commit

Permalink
Make RuneWidth faster for code points below 0x0300
Browse files Browse the repository at this point in the history
  • Loading branch information
p-e-w committed Jun 13, 2020
1 parent 14e809f commit 3e1705c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion runewidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,19 @@ func NewCondition() *Condition {
// See http://www.unicode.org/reports/tr11/
func (c *Condition) RuneWidth(r rune) int {
switch {
case r < 0 || r > 0x10FFFF || inTables(r, nonprint, combining, notassigned):
case r < 0 || r > 0x10FFFF:
return 0
// Fast path
case r < 0x0300: // neither combining, non-assigned or double-width
switch {
case r <= 0x001F || (0x007F <= r && r <= 0x009F) || r == 0x00AD: // non-printable
return 0
case c.EastAsianWidth && IsAmbiguousWidth(r):
return 2
default:
return 1
}
case inTables(r, nonprint, combining, notassigned):
return 0
case (c.EastAsianWidth && IsAmbiguousWidth(r)) || inTables(r, doublewidth):
return 2
Expand Down

0 comments on commit 3e1705c

Please sign in to comment.