internal/span: fix off-by-one in ToUTF16Column
Change-Id: I8c8344046b13f2606d7257ef8632e0426e15a85b Reviewed-on: https://go-review.googlesource.com/c/tools/+/173498 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
parent
e58f34171d
commit
4bf14f7f06
|
@ -40,8 +40,8 @@ func ToUTF16Column(p Point, content []byte) (int, error) {
|
|||
start := content[lineOffset:]
|
||||
|
||||
// Now, truncate down to the supplied column.
|
||||
if col >= len(start) {
|
||||
return -1, fmt.Errorf("ToUTF16Column: line (%v) is shorter than column (%v)", len(start), col)
|
||||
if col > len(start) { // col is 1-indexed
|
||||
return -1, fmt.Errorf("ToUTF16Column: length of line (%v) is less than column (%v)", len(start), col)
|
||||
}
|
||||
start = start[:col]
|
||||
// and count the number of utf16 characters
|
||||
|
|
|
@ -27,7 +27,7 @@ func TestUTF16(t *testing.T) {
|
|||
c := span.NewContentConverter("test", input)
|
||||
for line := 1; line <= 9; line++ {
|
||||
runeColumn, runeChr := 0, 0
|
||||
for chr := 1; chr <= 9; chr++ {
|
||||
for chr := 1; chr <= 10; chr++ {
|
||||
switch {
|
||||
case chr <= line:
|
||||
runeChr = chr
|
||||
|
@ -67,3 +67,28 @@ func TestUTF16(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUTF16Errors(t *testing.T) {
|
||||
var input = []byte(`
|
||||
hello
|
||||
world
|
||||
`)[1:]
|
||||
for _, test := range []struct {
|
||||
line, col, offset int
|
||||
want string
|
||||
}{
|
||||
{
|
||||
1, 6, 12,
|
||||
"ToUTF16Column: length of line (5) is less than column (6)",
|
||||
},
|
||||
{
|
||||
1, 6, 13,
|
||||
"ToUTF16Column: offsets 8-13 outside file contents (12)",
|
||||
},
|
||||
} {
|
||||
p := span.NewPoint(test.line, test.col, test.offset)
|
||||
if _, err := span.ToUTF16Column(p, input); err == nil || err.Error() != test.want {
|
||||
t.Errorf("expected %v, got %v", test.want, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue