aboutsummaryrefslogtreecommitdiff
path: root/editor.c
diff options
context:
space:
mode:
Diffstat (limited to 'editor.c')
-rw-r--r--editor.c23
1 files changed, 5 insertions, 18 deletions
diff --git a/editor.c b/editor.c
index ed2a870..0962089 100644
--- a/editor.c
+++ b/editor.c
@@ -358,7 +358,8 @@ static void piece_init(Piece *p, Piece *prev, Piece *next, char *content, size_t
p->len = len;
}
-/* returns the piece holding the text at byte offset pos */
+/* returns the piece holding the text at byte offset pos.
+ * if pos is zero, then the begin sentinel piece is returned. */
static Location piece_get(Editor *ed, size_t pos) {
Location loc = {};
// TODO: handle position at end of file: pos+1
@@ -375,22 +376,6 @@ static Location piece_get(Editor *ed, size_t pos) {
return loc;
}
-static Location piece_get_public(Editor *ed, size_t pos) {
- Location loc = {};
- // TODO: handle position at end of file: pos+1
- size_t cur = 0;
- for (Piece *p = ed->begin.next; p->next; p = p->next) {
- if (cur <= pos && pos <= cur + p->len) {
- loc.piece = p;
- loc.off = pos - cur;
- return loc;
- }
- cur += p->len;
- }
-
- return loc;
-}
-
/* allocate a new change, associate it with current action or a newly
* allocated one if none exists. */
static Change *change_alloc(Editor *ed) {
@@ -770,8 +755,10 @@ bool editor_modified(Editor *ed) {
}
Iterator editor_iterator_get(Editor *ed, size_t pos) {
- Location loc = piece_get_public(ed, pos);
+ Location loc = piece_get(ed, pos);
Piece *p = loc.piece;
+ if (p == &ed->begin)
+ p = p->next;
return (Iterator){
.piece = p,
.text = p ? p->content + loc.off : NULL,