From 74f957ef54a912b9396232fb3b0bb8c8c47bf5f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Wed, 3 Jun 2020 22:18:26 -0300 Subject: Separate pop into byte and element variants --- buffer.cpp | 6 +--- jet.cpp | 1 + point.cpp | 103 ++++++++++++++++++++++++++++++++++++------------------------- 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/buffer.cpp b/buffer.cpp index 027ffca..9313380 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -7,11 +7,7 @@ struct Buffer { void prev_line(int window_width) { cursor--; - cursor--; - cursor.rseek('\n', window_width - 2); - if (cursor.element() == '\n') { - cursor++; - } + cursor.rseek('\n', window_width - 1); } void next_line(int window_width) { diff --git a/jet.cpp b/jet.cpp index ff1da36..f5a901f 100644 --- a/jet.cpp +++ b/jet.cpp @@ -50,6 +50,7 @@ int main(int argc, char *argv[]) { printw("%lc", window_end.element()); window_end++; } + printw("%d", buffer.cursor.index); if (x > -1 && y > -1) { move(y, x); } diff --git a/point.cpp b/point.cpp index 0561fcd..235d93c 100644 --- a/point.cpp +++ b/point.cpp @@ -2,7 +2,27 @@ #include #include -class Point { +size_t byte_type(uint8_t byte) { + if (byte & 1 << 7) { + if (byte & 1 << 6) { + if (byte & 1 << 5) { + if (byte & 1 << 4) { + return 4; + } else { + return 3; + } + } else { + return 2; + } + } else { + return 0; + } + } else { + return 1; + } +} + +struct Point { Page *page; uint16_t index; @@ -14,7 +34,19 @@ class Point { } } - uint8_t byte() { + uint8_t prev_byte() { + if (index == 0) { + if (page->prev) { + return page->prev->elements[Point(page->prev, page->prev->element_count - 1).index_to_offset()]; + } else { + return 0; + } + } else { + return page->elements[Point(page, index - 1).index_to_offset()]; + } + } + + uint8_t next_byte() { if (index == page->element_count) { if (page->next) { return page->next->elements[Point(page->next, 0).index_to_offset()]; @@ -44,23 +76,24 @@ class Point { } } - size_t rune_type() { - if (byte() & 1 << 7) { - if (byte() & 1 << 6) { - if (byte() & 1 << 5) { - if (byte() & 1 << 4) { - return 4; - } else { - return 3; - } - } else { - return 2; - } - } else { - return 0; + void pop_byte() { + if (!at_start()) { + align_gap(); + page->pop(); + move_backward(); + if (index == 0) { + move_backward(); + } + } + if (page->is_empty()) { + if (page->prev) { + move_backward(); + delete page->next; + } else if (page->next) { + page->next->copy_to(page); + delete page->next; + index = 0; } - } else { - return 1; } } @@ -90,31 +123,31 @@ class Point { do { if (index == page->element_count) move_forward(); move_forward(); - } while (!rune_type()); + } while (!byte_type(next_byte())); } void operator--(int) { do { move_backward(); if (index == 0) move_backward(); - } while (!rune_type()); + } while (!byte_type(next_byte())); } wchar_t element() { - size_t type = rune_type(); - wchar_t rune = byte() & (0xff >> type); + size_t type = byte_type(next_byte()); + wchar_t rune = next_byte() & (0xff >> type); Point iter(*this); for (size_t i = 1; i < type; i++) { rune <<= 6; iter.move_forward(); - rune |= (iter.byte() & 0x3f); + rune |= (iter.next_byte() & 0x3f); } return rune; } uint64_t seek(uint8_t c, uint64_t limit) { uint64_t travel_distance = 0; - while (!at_end() && byte() != c && travel_distance < limit) { + while (!at_end() && next_byte() != c && travel_distance < limit) { move_forward(); travel_distance++; } @@ -123,7 +156,7 @@ class Point { uint64_t rseek(uint8_t c, uint64_t limit) { uint64_t travel_distance = 0; - while (!at_start() && byte() != c && travel_distance < limit) { + while (!at_start() && prev_byte() != c && travel_distance < limit) { move_backward(); travel_distance++; } @@ -153,24 +186,10 @@ class Point { } void pop() { - if (!at_start()) { - align_gap(); - page->pop(); - move_backward(); - if (index == 0) { - move_backward(); - } - } - if (page->is_empty()) { - if (page->prev) { - move_backward(); - delete page->next; - } else if (page->next) { - page->next->copy_to(page); - delete page->next; - index = 0; - } + while (!byte_type(prev_byte())) { + pop_byte(); } + pop_byte(); } }; -- cgit v1.2.3