Skip to content

Commit 3e1705c

Browse files
committed
Make RuneWidth faster for code points below 0x0300
1 parent 14e809f commit 3e1705c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

runewidth.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,19 @@ func NewCondition() *Condition {
101101
// See http://www.unicode.org/reports/tr11/
102102
func (c *Condition) RuneWidth(r rune) int {
103103
switch {
104-
case r < 0 || r > 0x10FFFF || inTables(r, nonprint, combining, notassigned):
104+
case r < 0 || r > 0x10FFFF:
105+
return 0
106+
// Fast path
107+
case r < 0x0300: // neither combining, non-assigned or double-width
108+
switch {
109+
case r <= 0x001F || (0x007F <= r && r <= 0x009F) || r == 0x00AD: // non-printable
110+
return 0
111+
case c.EastAsianWidth && IsAmbiguousWidth(r):
112+
return 2
113+
default:
114+
return 1
115+
}
116+
case inTables(r, nonprint, combining, notassigned):
105117
return 0
106118
case (c.EastAsianWidth && IsAmbiguousWidth(r)) || inTables(r, doublewidth):
107119
return 2

0 commit comments

Comments
 (0)