From 3e1705c5c059ecad3c90b785f4d364c8ecfd45c1 Mon Sep 17 00:00:00 2001 From: Philipp Emanuel Weidmann Date: Sat, 13 Jun 2020 08:32:00 +0530 Subject: [PATCH] Make RuneWidth faster for code points below 0x0300 --- runewidth.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/runewidth.go b/runewidth.go index 19f8e04..dc020f5 100644 --- a/runewidth.go +++ b/runewidth.go @@ -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