aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-01 17:18:52 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-01 17:18:52 +0000
commit482f56b4a2f9a30a57b78188cd3faa5e21bbb588 (patch)
treefdc9456107b5afc90d45c412c0ac665a729c7b90
parentcsi: DECSTBM: move cursor to top left corner of scrolling region (diff)
grid: implement a memmove-sort-of function
grid_memmove() moves cell data from one index to another, taking the grid offset into account.
-rw-r--r--grid.c33
-rw-r--r--grid.h1
2 files changed, 34 insertions, 0 deletions
diff --git a/grid.c b/grid.c
index bb8d46eb..a21e0c88 100644
--- a/grid.c
+++ b/grid.c
@@ -40,3 +40,36 @@ grid_memset(struct grid *grid, size_t start, int c, size_t length)
start += count;
}
}
+
+void
+grid_memmove(struct grid *grid, size_t dst, size_t src, size_t length)
+{
+ size_t left = length;
+ size_t copy_idx = 0;
+ struct cell copy[left];
+
+ while (left > 0) {
+ size_t count = left;
+ struct cell *src_cells = grid_get_range(grid, src, &count);
+
+ memcpy(&copy[copy_idx], src_cells, count * sizeof(copy[0]));
+
+ left -= count;
+ src += count;
+ copy_idx += count;
+ }
+
+ left = length;
+ copy_idx = 0;
+
+ while (left > 0) {
+ size_t count = left;
+ struct cell *dst_cells = grid_get_range(grid, dst, &count);
+
+ memcpy(dst_cells, &copy[copy_idx], count * sizeof(copy[0]));
+
+ left -= count;
+ dst += count;
+ copy_idx += count;
+ }
+}
diff --git a/grid.h b/grid.h
index 3a5f3f4b..275d81bc 100644
--- a/grid.h
+++ b/grid.h
@@ -5,3 +5,4 @@
struct cell *grid_get_range(struct grid *grid, size_t start, size_t *length);
void grid_memset(struct grid *grid, size_t start, int c, size_t length);
+void grid_memmove(struct grid *grid, size_t dst, size_t src, size_t length);