1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:14:45 -07:00
go/internal/span/utf16.go
Ian Cottrell 48d47c461c internal/span: fix utf16 column when span is line based
A span with column 0 is intended to mean the start of the line, which in utf16
mode must be the 1st character

Change-Id: I4b98fe86528b889bbfe4b5ed3ae79c4da81017b1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/167459
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-03-13 22:14:19 +00:00

75 lines
2.2 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package span
import (
"unicode/utf16"
"unicode/utf8"
)
// ToUTF16Column calculates the utf16 column expressed by the point given the
// supplied file contents.
// This is used to convert from the native (always in bytes) column
// representation and the utf16 counts used by some editors.
func ToUTF16Column(offsets Offsets, p Point, content []byte) int {
if content == nil || p.Column < 0 {
return -1
}
if p.Column == 0 {
return 1
}
// make sure we have a valid offset
p.updateOffset(offsets)
lineOffset := p.Offset - (p.Column - 1)
if lineOffset < 0 || p.Offset > len(content) {
return -1
}
// use the offset to pick out the line start
start := content[lineOffset:]
// now truncate down to the supplied column
start = start[:p.Column]
// and count the number of utf16 characters
// in theory we could do this by hand more efficiently...
return len(utf16.Encode([]rune(string(start))))
}
// FromUTF16Column calculates the byte column expressed by the utf16 character
// offset given the supplied file contents.
// This is used to convert from the utf16 counts used by some editors to the
// native (always in bytes) column representation.
func FromUTF16Column(offsets Offsets, line, chr int, content []byte) Point {
// first build a point for the start of the line the normal way
p := Point{Line: line, Column: 1, Offset: 0}
// now use that to work out the byte offset of the start of the line
p.updateOffset(offsets)
if chr <= 1 {
return p
}
// use that to pick the line out of the file content
remains := content[p.Offset:]
// and now scan forward the specified number of characters
for count := 1; count < chr; count++ {
if len(remains) <= 0 {
return Point{Offset: -1}
}
r, w := utf8.DecodeRune(remains)
if r == '\n' {
return Point{Offset: -1}
}
remains = remains[w:]
if r >= 0x10000 {
// a two point rune
count++
// if we finished in a two point rune, do not advance past the first
if count >= chr {
break
}
}
p.Column += w
p.Offset += w
}
return p
}