From b880397b7ea145f4112dcbe989e66ee78ac4616c Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 11 Dec 2016 14:00:15 -0500 Subject: [PATCH] Split up Cursor module into BufferIterator / BufferCursor --- src/core/Buffer.cc | 4 +- src/core/Buffer.h | 6 +- src/core/{Cursor.cc => BufferCursor.cc} | 94 +++++------------------- src/core/BufferCursor.h | 51 +++++++++++++ src/core/BufferIterator.cc | 56 +++++++++++++++ src/core/BufferIterator.h | 55 ++++++++++++++ src/core/Cursor.h | 96 ------------------------- src/gui/BufferPane.cc | 14 ++-- src/gui/BufferPane.h | 6 +- 9 files changed, 196 insertions(+), 186 deletions(-) rename src/core/{Cursor.cc => BufferCursor.cc} (61%) create mode 100644 src/core/BufferCursor.h create mode 100644 src/core/BufferIterator.cc create mode 100644 src/core/BufferIterator.h delete mode 100644 src/core/Cursor.h diff --git a/src/core/Buffer.cc b/src/core/Buffer.cc index ff50261..f69d852 100644 --- a/src/core/Buffer.cc +++ b/src/core/Buffer.cc @@ -104,12 +104,12 @@ bool Buffer::write_to_file(const char * filename) m_gap_buffer->compact(); - Cursor start_of_line(&*m_gap_buffer, m_encoding, 4u); + BufferCursor start_of_line(&*m_gap_buffer, m_encoding, 4u); size_t bytes_written = 0u; while (start_of_line.valid()) { - Cursor cursor = start_of_line; + BufferCursor cursor = start_of_line; cursor.go_end_of_line(true); size_t len = cursor.address() - start_of_line.address(); if (len > 0u) diff --git a/src/core/Buffer.h b/src/core/Buffer.h index 47b8de7..0744e5d 100644 --- a/src/core/Buffer.h +++ b/src/core/Buffer.h @@ -6,7 +6,7 @@ #include "LineEndings.h" #include "Encoding.h" #include "GapBuffer.h" -#include "Cursor.h" +#include "BufferCursor.h" class Buffer { @@ -16,9 +16,9 @@ public: Buffer(const uint8_t * data, size_t data_length); bool write_to_file(const char * filename); - std::shared_ptr add_cursor() + std::shared_ptr add_cursor() { - return std::make_shared(&*m_gap_buffer, m_encoding, 4u); + return std::make_shared(&*m_gap_buffer, m_encoding, 4u); } auto get_string() { return m_gap_buffer->get_string(); } diff --git a/src/core/Cursor.cc b/src/core/BufferCursor.cc similarity index 61% rename from src/core/Cursor.cc rename to src/core/BufferCursor.cc index 3807d5c..b481419 100644 --- a/src/core/Cursor.cc +++ b/src/core/BufferCursor.cc @@ -1,73 +1,17 @@ -#include "Cursor.h" +#include "BufferCursor.h" -void Iterator::go_forward() +bool BufferCursor::is_start_of_line() { - if (valid()) - { - m_offset += Encoding::num_bytes_in_code_point(m_encoding, address()); - } -} - -bool Iterator::check_go_forward() -{ - if (valid()) - { - Iterator i2 = *this; - i2.go_forward(); - if (i2.valid()) - { - m_offset = i2.m_offset; - return true; - } - } - return false; -} - -void Iterator::go_back() -{ - if (valid()) - { - if (m_offset == 0u) - { - m_offset = 0xFFFFFFFFu; - } - else - { - const uint8_t * a = m_gap_buffer->address(m_offset - 1u); - const uint8_t * beginning_of_code_point = Encoding::beginning_of_code_point(m_encoding, a); - m_offset -= ((a - beginning_of_code_point) + 1u); - } - } -} - -bool Iterator::check_go_back() -{ - if (valid()) - { - Iterator i2 = *this; - i2.go_back(); - if (i2.valid()) - { - m_offset = i2.m_offset; - return true; - } - } - return false; -} - - -bool Cursor::is_start_of_line() -{ - Iterator i2 = m_iterator; + BufferIterator i2 = m_iterator; if (!i2.check_go_back()) { - /* Iterator cannot go backwards so it is at beginning of buffer. */ + /* BufferIterator cannot go backwards so it is at beginning of buffer. */ return true; } return *i2 == '\n'; } -bool Cursor::is_end_of_line(bool allow_eol) +bool BufferCursor::is_end_of_line(bool allow_eol) { if (*m_iterator == '\n') { @@ -75,7 +19,7 @@ bool Cursor::is_end_of_line(bool allow_eol) } if (!allow_eol) { - Iterator i2 = m_iterator; + BufferIterator i2 = m_iterator; i2.go_forward(); if (*i2 == '\n') { @@ -85,10 +29,10 @@ bool Cursor::is_end_of_line(bool allow_eol) return false; } -bool Cursor::go_start_of_line() +bool BufferCursor::go_start_of_line() { bool moved = false; - Iterator i2 = m_iterator; + BufferIterator i2 = m_iterator; for (;;) { i2.go_back(); @@ -109,7 +53,7 @@ bool Cursor::go_start_of_line() return moved; } -bool Cursor::go_end_of_line(bool allow_eol) +bool BufferCursor::go_end_of_line(bool allow_eol) { bool moved = false; if (go_right(allow_eol)) @@ -123,7 +67,7 @@ bool Cursor::go_end_of_line(bool allow_eol) return moved; } -bool Cursor::go_left() +bool BufferCursor::go_left() { if (!is_start_of_line()) { @@ -145,7 +89,7 @@ bool Cursor::go_left() return false; } -bool Cursor::go_right(bool allow_eol) +bool BufferCursor::go_right(bool allow_eol) { if (!is_end_of_line(allow_eol)) { @@ -167,9 +111,9 @@ bool Cursor::go_right(bool allow_eol) return false; } -bool Cursor::go_up(size_t target_column) +bool BufferCursor::go_up(size_t target_column) { - Cursor c2 = *this; + BufferCursor c2 = *this; c2.go_start_of_line(); if (!c2.m_iterator.check_go_back()) { @@ -183,9 +127,9 @@ bool Cursor::go_up(size_t target_column) return true; } -bool Cursor::go_down(size_t target_column) +bool BufferCursor::go_down(size_t target_column) { - Cursor c2 = *this; + BufferCursor c2 = *this; c2.go_end_of_line(true); if (!c2.m_iterator.check_go_forward()) { @@ -198,7 +142,7 @@ bool Cursor::go_down(size_t target_column) return true; } -void Cursor::init_column() +void BufferCursor::init_column() { if (*m_iterator == '\t') { @@ -210,9 +154,9 @@ void Cursor::init_column() } } -void Cursor::calculate_column() +void BufferCursor::calculate_column() { - Cursor tmp_cursor = *this; + BufferCursor tmp_cursor = *this; tmp_cursor.go_start_of_line(); while (tmp_cursor < *this) { @@ -221,7 +165,7 @@ void Cursor::calculate_column() m_column = tmp_cursor.m_column; } -void Cursor::forward_to_column(size_t target_column) +void BufferCursor::forward_to_column(size_t target_column) { int32_t diff = abs((int32_t)(target_column - m_column)); if (diff == 0) diff --git a/src/core/BufferCursor.h b/src/core/BufferCursor.h new file mode 100644 index 0000000..9351f46 --- /dev/null +++ b/src/core/BufferCursor.h @@ -0,0 +1,51 @@ +#ifndef BufferCURSOR_H +#define BufferCURSOR_H + +#include +#include +#include "Encoding.h" +#include "GapBuffer.h" +#include "BufferIterator.h" + +class BufferCursor +{ +public: + BufferCursor(GapBuffer * gap_buffer, Encoding::Type encoding, uint8_t tabstop) + : m_iterator(gap_buffer, encoding) + { + m_line = 0u; + m_tabstop = tabstop; + init_column(); + } + bool is_start_of_line(); + bool is_end_of_line(bool allow_eol); + bool go_start_of_line(); + bool go_end_of_line(bool allow_eol); + bool go_left(); + bool go_right(bool allow_eol); + bool go_up(size_t target_column); + bool go_down(size_t target_column); + bool operator<(const BufferCursor & other) const + { + return m_iterator < other.m_iterator; + } + uint32_t operator*() const { return *m_iterator; } + uint8_t * address() const { return m_iterator.address(); } + bool valid() const { return m_iterator.valid(); } + size_t line() const { return m_line; } + size_t column() const { return m_column; } + BufferIterator & iterator() { return m_iterator; } + void set_line(size_t line) { m_line = line; } + void calculate_column(); + +protected: + BufferIterator m_iterator; + size_t m_line; + size_t m_column; + uint8_t m_tabstop; + + void init_column(); + void forward_to_column(size_t target_column); +}; + +#endif diff --git a/src/core/BufferIterator.cc b/src/core/BufferIterator.cc new file mode 100644 index 0000000..8a60901 --- /dev/null +++ b/src/core/BufferIterator.cc @@ -0,0 +1,56 @@ +#include "BufferIterator.h" + +void BufferIterator::go_forward() +{ + if (valid()) + { + m_offset += Encoding::num_bytes_in_code_point(m_encoding, address()); + } +} + +bool BufferIterator::check_go_forward() +{ + if (valid()) + { + BufferIterator i2 = *this; + i2.go_forward(); + if (i2.valid()) + { + m_offset = i2.m_offset; + return true; + } + } + return false; +} + +void BufferIterator::go_back() +{ + if (valid()) + { + if (m_offset == 0u) + { + m_offset = 0xFFFFFFFFu; + } + else + { + const uint8_t * a = m_gap_buffer->address(m_offset - 1u); + const uint8_t * beginning_of_code_point = Encoding::beginning_of_code_point(m_encoding, a); + m_offset -= ((a - beginning_of_code_point) + 1u); + } + } +} + +bool BufferIterator::check_go_back() +{ + if (valid()) + { + BufferIterator i2 = *this; + i2.go_back(); + if (i2.valid()) + { + m_offset = i2.m_offset; + return true; + } + } + return false; +} diff --git a/src/core/BufferIterator.h b/src/core/BufferIterator.h new file mode 100644 index 0000000..21a36ac --- /dev/null +++ b/src/core/BufferIterator.h @@ -0,0 +1,55 @@ +#ifndef BUFFERITERATOR_H +#define BUFFERITERATOR_H + +#include +#include +#include "Encoding.h" +#include "GapBuffer.h" + +class BufferIterator +{ +public: + BufferIterator(GapBuffer * gap_buffer, Encoding::Type encoding) + { + m_gap_buffer = gap_buffer; + m_offset = 0u; + m_encoding = encoding; + } + bool valid() const + { + return m_offset < m_gap_buffer->size(); + } + void go_forward(); + bool check_go_forward(); + void go_back(); + bool check_go_back(); + uint8_t * address() const + { + return m_gap_buffer->address(m_offset); + } + uint32_t operator*() const + { + if (valid()) + { + return Encoding::decode(m_encoding, address()); + } + else + { + return 0xFFFFFFFFu; + } + } + GapBuffer * gap_buffer() const { return m_gap_buffer; } + bool operator<(const BufferIterator & other) const + { + return m_offset < other.m_offset; + } + size_t offset() { return m_offset; } + void set_offset(size_t new_offset) { m_offset = new_offset; } + +protected: + GapBuffer * m_gap_buffer; + size_t m_offset; + Encoding::Type m_encoding; +}; + +#endif diff --git a/src/core/Cursor.h b/src/core/Cursor.h deleted file mode 100644 index f05934f..0000000 --- a/src/core/Cursor.h +++ /dev/null @@ -1,96 +0,0 @@ -#ifndef CURSOR_H -#define CURSOR_H - -#include -#include -#include "Encoding.h" -#include "GapBuffer.h" - -class Iterator -{ -public: - Iterator(GapBuffer * gap_buffer, Encoding::Type encoding) - { - m_gap_buffer = gap_buffer; - m_offset = 0u; - m_encoding = encoding; - } - bool valid() const - { - return m_offset < m_gap_buffer->size(); - } - void go_forward(); - bool check_go_forward(); - void go_back(); - bool check_go_back(); - uint8_t * address() const - { - return m_gap_buffer->address(m_offset); - } - uint32_t operator*() const - { - if (valid()) - { - return Encoding::decode(m_encoding, address()); - } - else - { - return 0xFFFFFFFFu; - } - } - GapBuffer * gap_buffer() const { return m_gap_buffer; } - bool operator<(const Iterator & other) const - { - return m_offset < other.m_offset; - } - size_t offset() { return m_offset; } - void set_offset(size_t new_offset) { m_offset = new_offset; } - -protected: - GapBuffer * m_gap_buffer; - size_t m_offset; - Encoding::Type m_encoding; -}; - -class Cursor -{ -public: - Cursor(GapBuffer * gap_buffer, Encoding::Type encoding, uint8_t tabstop) - : m_iterator(gap_buffer, encoding) - { - m_line = 0u; - m_tabstop = tabstop; - init_column(); - } - bool is_start_of_line(); - bool is_end_of_line(bool allow_eol); - bool go_start_of_line(); - bool go_end_of_line(bool allow_eol); - bool go_left(); - bool go_right(bool allow_eol); - bool go_up(size_t target_column); - bool go_down(size_t target_column); - bool operator<(const Cursor & other) const - { - return m_iterator < other.m_iterator; - } - uint32_t operator*() const { return *m_iterator; } - uint8_t * address() const { return m_iterator.address(); } - bool valid() const { return m_iterator.valid(); } - size_t line() const { return m_line; } - size_t column() const { return m_column; } - Iterator & iterator() { return m_iterator; } - void set_line(size_t line) { m_line = line; } - void calculate_column(); - -protected: - Iterator m_iterator; - size_t m_line; - size_t m_column; - uint8_t m_tabstop; - - void init_column(); - void forward_to_column(size_t target_column); -}; - -#endif diff --git a/src/gui/BufferPane.cc b/src/gui/BufferPane.cc index d828cb5..1af5c03 100644 --- a/src/gui/BufferPane.cc +++ b/src/gui/BufferPane.cc @@ -29,7 +29,7 @@ void BufferPane::update_cursor_row() int BufferPane::screen_rows_below_cursor(int stop_at) { - Cursor cursor = *m_cursor; + BufferCursor cursor = *m_cursor; size_t column = cursor.column(); cursor.go_end_of_line(false); int rows = (cursor.column() / m_columns) - (column / m_columns); @@ -43,11 +43,11 @@ int BufferPane::screen_rows_below_cursor(int stop_at) int BufferPane::screen_rows_above_cursor(int stop_at) { - Cursor cursor = *m_cursor; + BufferCursor cursor = *m_cursor; int rows = cursor.column() / m_columns; while ((rows < stop_at) && cursor.go_up(0u)) { - Cursor cursor2 = cursor; + BufferCursor cursor2 = cursor; cursor2.go_end_of_line(false); rows += (cursor2.column() / m_columns) + 1; } @@ -58,11 +58,11 @@ void BufferPane::draw() { update_cursor_row(); int screen_row = m_cursor_row - m_cursor->column() / m_columns; - Cursor iter_cursor = *m_cursor; + BufferCursor iter_cursor = *m_cursor; iter_cursor.go_start_of_line(); while ((screen_row > 0) && iter_cursor.go_up(0u)) { - Cursor cursor2 = iter_cursor; + BufferCursor cursor2 = iter_cursor; cursor2.go_end_of_line(false); screen_row -= (1 + cursor2.column() / m_columns); } @@ -78,9 +78,9 @@ void BufferPane::draw() } } -void BufferPane::draw_buffer_line(int screen_row, const Cursor & cursor) +void BufferPane::draw_buffer_line(int screen_row, const BufferCursor & cursor) { - Cursor iter_cursor = cursor; + BufferCursor iter_cursor = cursor; while (!iter_cursor.is_end_of_line(true)) { int draw_row = screen_row + (iter_cursor.column() / m_columns); diff --git a/src/gui/BufferPane.h b/src/gui/BufferPane.h index a118189..6229deb 100644 --- a/src/gui/BufferPane.h +++ b/src/gui/BufferPane.h @@ -15,7 +15,7 @@ public: std::shared_ptr gl); void resize(int width, int height) override; void draw(); - std::shared_ptr cursor() { return m_cursor; } + std::shared_ptr cursor() { return m_cursor; } protected: int effective_scroll_offset() @@ -25,7 +25,7 @@ protected: int screen_rows_below_cursor(int stop_at); int screen_rows_above_cursor(int stop_at); void update_cursor_row(); - void draw_buffer_line(int screen_row, const Cursor & cursor); + void draw_buffer_line(int screen_row, const BufferCursor & cursor); void draw_buffer_character(int screen_column, int screen_row, uint32_t character); void draw_cursor(int screen_column, int screen_row, bool insert_mode); void colrow_to_xy(int col, int row, int * x, int * y); @@ -37,7 +37,7 @@ protected: int m_columns; int m_scroll_offset; int m_cursor_row; - std::shared_ptr m_cursor; + std::shared_ptr m_cursor; }; #endif