Bounds check delete regions in rope buffer

This commit is contained in:
Luke I. Wilson
2021-03-24 11:40:18 -05:00
parent 296fa8b4ac
commit 9133ab55d0
3 changed files with 28 additions and 4 deletions

View File

@@ -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