summaryrefslogtreecommitdiff
path: root/page.c
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-20 02:51:10 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-20 03:01:05 -0300
commit09062c839b561249730ebb2b67cdf47f36f4c542 (patch)
treee0e5b6a84b820aaf8a06d12e643e322337948f95 /page.c
parent852c6d1f1a6ab8b8c3fd40922a83af70a70fdf2d (diff)
downloadjet-09062c839b561249730ebb2b67cdf47f36f4c542.tar.gz
jet-09062c839b561249730ebb2b67cdf47f36f4c542.zip
Rework gap movement
Diffstat (limited to 'page.c')
-rw-r--r--page.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/page.c b/page.c
index ee4861c..a0e2577 100644
--- a/page.c
+++ b/page.c
@@ -83,19 +83,24 @@ void free_page(struct page **page) {
free(tmp);
}
-void move_gap(struct page *page, int target) {
- while(target) {
- if (target > 0) {
- page->buffer[page->gap_start] = page->buffer[page->gap_end];
- page->gap_start++;
- page->gap_end++;
- target--;
- } else {
- page->gap_end--;
- page->gap_start--;
- page->buffer[page->gap_end] = page->buffer[page->gap_start];
- target++;
- }
+void move_gap_forward(struct page *page) {
+ page->buffer[page->gap_start] = page->buffer[page->gap_end];
+ page->gap_start++;
+ page->gap_end++;
+}
+
+void move_gap_backward(struct page *page) {
+ page->gap_end--;
+ page->gap_start--;
+ page->buffer[page->gap_end] = page->buffer[page->gap_start];
+}
+
+void move_gap(struct page *page, uint16_t offset) {
+ while (page->gap_start < offset) {
+ move_gap_forward(page);
+ }
+ while (page->gap_start > offset) {
+ move_gap_backward(page);
}
}
@@ -107,7 +112,7 @@ void insert_into_page(struct page *page, uint8_t c) {
}
void delete_from_page(struct page *page) {
- if (page->gap_start != page->buffer) {
+ if (page->gap_start != 0) {
page->gap_start--;
}
}