mirror of https://github.com/golang/go.git
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:
parent
46e69bf3b2
commit
0886cdd1dc
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue