internal/span: compute utf16 length directly

computing the length by hand avoids an allocation.

Change-Id: Idc15f9347628b3b68cb3722d03c2c706a4f60a68
Reviewed-on: https://go-review.googlesource.com/c/tools/+/321469
Run-TryBot: Peter Weinberger <pjw@google.com>
Trust: Peter Weinberger <pjw@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
pjw 2021-05-20 07:15:38 -04:00 committed by Peter Weinberger
parent 46e69bf3b2
commit 0886cdd1dc
1 changed files with 8 additions and 4 deletions

View File

@ -6,7 +6,6 @@ package span
import (
"fmt"
"unicode/utf16"
"unicode/utf8"
)
@ -41,9 +40,14 @@ func ToUTF16Column(p Point, content []byte) (int, error) {
// Now, truncate down to the supplied column.
start = start[:colZero]
// and count the number of utf16 characters
// in theory we could do this by hand more efficiently...
return len(utf16.Encode([]rune(string(start)))) + 1, nil
cnt := 0
for _, r := range string(start) {
cnt++
if r > 0xffff {
cnt++
}
}
return cnt + 1, nil // the +1 is for 1-based columns
}
// FromUTF16Column advances the point by the utf16 character offset given the