posts: gap-buffer: add deletion

This commit is contained in:
Bruno BELANYI 2024-07-06 23:36:46 +01:00
parent dbbcd528c3
commit 11138dafd1

View file

@ -140,3 +140,22 @@ def insert(self, val: str) -> None:
self._buf[self._gap_start : self._gap_start + len(val)] = val
self._gap_start += len(val)
```
### Deletion
Removing text from the buffer simply expands the gap in the corresponding
direction, shortening the string's prefix/suffix. This makes it very cheap.
The methods are named after the `backspace` and `delete` keys on the keyboard.
```python
def backspace(self, dist: int = 1) -> None:
assert dist <= self.prefix_length
# Extend gap to the left
self._gap_start -= dist
def delete(self, dist: int = 1) -> None:
assert dist <= self.suffix_length
# Extend gap to the right
self._gap_end += dist
```