From e780e41e0222517caa9c69030b5955ab2b458a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Sat, 2 Feb 2019 04:05:01 +0100 Subject: Faster TitleToNumber (#343) * TestTitleToNumber: more test cases * TitleToNumber: drop use of math.Pow() Compute using pure integers * TitleToNumber: simplify Remove unecessary casts to int --- lib.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'lib.go') diff --git a/lib.go b/lib.go index 99c513e..30a20e0 100644 --- a/lib.go +++ b/lib.go @@ -14,7 +14,6 @@ import ( "bytes" "io" "log" - "math" "strconv" "strings" "unicode" @@ -91,15 +90,15 @@ func ToAlphaString(value int) string { // excelize.TitleToNumber("ak") // func TitleToNumber(s string) int { - weight := 0.0 + weight := 1 sum := 0 for i := len(s) - 1; i >= 0; i-- { - ch := int(s[i]) - if int(s[i]) >= int('a') && int(s[i]) <= int('z') { - ch = int(s[i]) - 32 + ch := s[i] + if ch >= 'a' && ch <= 'z' { + ch -= 32 } - sum = sum + (ch-int('A')+1)*int(math.Pow(26, weight)) - weight++ + sum += int(ch-'A'+1) * weight + weight *= 26 } return sum - 1 } -- cgit v1.2.1