Bounds check delete regions in rope buffer
This commit is contained in:
@@ -114,7 +114,17 @@ func (b *RopeBuffer) Insert(line, col int, value []byte) {
|
||||
// Remove deletes any characters between startLine, startCol, and endLine,
|
||||
// endCol, inclusive bounds.
|
||||
func (b *RopeBuffer) Remove(startLine, startCol, endLine, endCol int) {
|
||||
(*rope.Node)(b).Remove(b.pos(startLine, startCol), b.pos(endLine, endCol)+1)
|
||||
start := b.pos(startLine, startCol)
|
||||
end := b.pos(endLine, endCol) + 1
|
||||
|
||||
if len := (*rope.Node)(b).Len(); end >= len {
|
||||
end = len
|
||||
if start > end {
|
||||
start = end
|
||||
}
|
||||
}
|
||||
|
||||
(*rope.Node)(b).Remove(start, end)
|
||||
}
|
||||
|
||||
// Returns the number of occurrences of 'sequence' in the buffer, within the range
|
||||
|
@@ -4,16 +4,29 @@ import "testing"
|
||||
|
||||
func TestRopeInserting(t *testing.T) {
|
||||
var buf Buffer = NewRopeBuffer([]byte("some"))
|
||||
buf.Insert(0, 4, []byte(" text")) // Insert " text" after "some"
|
||||
buf.Insert(0, 4, []byte(" text\n")) // Insert " text" after "some"
|
||||
buf.Insert(0, 0, []byte("with\n\t"))
|
||||
// "with\n\tsome text"
|
||||
//with
|
||||
// some text
|
||||
//
|
||||
|
||||
buf.Remove(0, 4, 1, 5) // Delete from line 0, col 4, to line 1, col 6 "\n\tsome "
|
||||
|
||||
if str := string(buf.Bytes()); str != "withtext" {
|
||||
if str := string(buf.Bytes()); str != "withtext\n" {
|
||||
t.Errorf("string does not match \"withtext\", got %#v", str)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
//withtext
|
||||
//
|
||||
|
||||
// Note the inclusive bounds and pointing to last line, first column.
|
||||
buf.Remove(0, 0, 1, 0) // Delete all of the buffer
|
||||
|
||||
if str := string(buf.Bytes()); str != "" {
|
||||
t.Errorf("string does not math \"\", got %#v", str)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRopeBounds(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user