aboutsummaryrefslogtreecommitdiffstats
path: root/buffer.c
diff options
context:
space:
mode:
authornytpu <alex@nytpu.com>2021-06-04 16:09:11 -0600
committernytpu <alex@nytpu.com>2021-06-04 16:09:11 -0600
commit7aff99458d98dd75ef6ee0d56dbde9ce3c177ae5 (patch)
tree7f4815bfb18d7febc80670ab3f9a16c92944e299 /buffer.c
parentadd framework for commands (diff)
downloaded-7aff99458d98dd75ef6ee0d56dbde9ce3c177ae5.tar.bz2
ed-7aff99458d98dd75ef6ee0d56dbde9ce3c177ae5.zip
use one-indexing interally unless actually indexing rows array
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/buffer.c b/buffer.c
index 7005651..b863a16 100644
--- a/buffer.c
+++ b/buffer.c
@@ -10,10 +10,19 @@
#include <stdlib.h>
#include <string.h>
+bool
+in_bounds(RowNum at, RowNum top)
+{
+ if (at < 1 || at > top) return false;
+ return true;
+}
+
void
insert_row(RowNum at, char *r, size_t len)
{
- if (at > E.numrows) return;
+ if (!in_bounds(at, E.numrows+1)) return;
+ --at;
+
E.rows = realloc(E.rows, (E.numrows+1) * sizeof(struct Row));
if (at != E.numrows) {
memmove(&E.rows[at+1], &E.rows[at], (E.numrows-at) * sizeof(struct Row));
@@ -31,7 +40,9 @@ insert_row(RowNum at, char *r, size_t len)
void
delete_row(RowNum at)
{
- if (at >= E.numrows) return;
+ if (!in_bounds(at, E.numrows)) return;
+ --at;
+
free(E.rows[at].chars);
memmove(&E.rows[at], &E.rows[at+1], (E.numrows-at-1) * sizeof(struct Row));
for (RowNum i = at; i < E.numrows; ++i) --E.rows[i].idx;
@@ -42,6 +53,7 @@ delete_row(RowNum at)
void
delete_rows(RowNum from, RowNum to)
{
- if (from < 0 || to >= E.numrows) return;
- for (; from != to; ++from) delete_row(from);
+ if (!in_bounds(from, E.numrows)) return;
+ if (!in_bounds(to, E.numrows)) return;
+ for (; from <= to; ++from) delete_row(from);
}