From 9b6b21a1d005285d04d0e68d8f85cc0bc6556dff Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 11 Dec 2016 15:17:15 -0500 Subject: [PATCH] Move Iterator and Cursor classes into Buffer class. --- .../{BufferCursor.cc => Buffer-Cursor.cc} | 38 ++++---- .../{BufferIterator.cc => Buffer-Iterator.cc} | 16 ++-- src/core/Buffer.cc | 4 +- src/core/Buffer.h | 94 ++++++++++++++++++- src/core/BufferCursor.h | 51 ---------- src/core/BufferIterator.h | 55 ----------- src/gui/BufferPane.cc | 14 +-- src/gui/BufferPane.h | 6 +- 8 files changed, 130 insertions(+), 148 deletions(-) rename src/core/{BufferCursor.cc => Buffer-Cursor.cc} (77%) rename src/core/{BufferIterator.cc => Buffer-Iterator.cc} (71%) delete mode 100644 src/core/BufferCursor.h delete mode 100644 src/core/BufferIterator.h diff --git a/src/core/BufferCursor.cc b/src/core/Buffer-Cursor.cc similarity index 77% rename from src/core/BufferCursor.cc rename to src/core/Buffer-Cursor.cc index b481419..d7220fe 100644 --- a/src/core/BufferCursor.cc +++ b/src/core/Buffer-Cursor.cc @@ -1,17 +1,17 @@ -#include "BufferCursor.h" +#include "Buffer.h" -bool BufferCursor::is_start_of_line() +bool Buffer::Cursor::is_start_of_line() { - BufferIterator i2 = m_iterator; + Iterator i2 = m_iterator; if (!i2.check_go_back()) { - /* BufferIterator cannot go backwards so it is at beginning of buffer. */ + /* Iterator cannot go backwards so it is at beginning of buffer. */ return true; } return *i2 == '\n'; } -bool BufferCursor::is_end_of_line(bool allow_eol) +bool Buffer::Cursor::is_end_of_line(bool allow_eol) { if (*m_iterator == '\n') { @@ -19,7 +19,7 @@ bool BufferCursor::is_end_of_line(bool allow_eol) } if (!allow_eol) { - BufferIterator i2 = m_iterator; + Iterator i2 = m_iterator; i2.go_forward(); if (*i2 == '\n') { @@ -29,10 +29,10 @@ bool BufferCursor::is_end_of_line(bool allow_eol) return false; } -bool BufferCursor::go_start_of_line() +bool Buffer::Cursor::go_start_of_line() { bool moved = false; - BufferIterator i2 = m_iterator; + Iterator i2 = m_iterator; for (;;) { i2.go_back(); @@ -53,7 +53,7 @@ bool BufferCursor::go_start_of_line() return moved; } -bool BufferCursor::go_end_of_line(bool allow_eol) +bool Buffer::Cursor::go_end_of_line(bool allow_eol) { bool moved = false; if (go_right(allow_eol)) @@ -67,7 +67,7 @@ bool BufferCursor::go_end_of_line(bool allow_eol) return moved; } -bool BufferCursor::go_left() +bool Buffer::Cursor::go_left() { if (!is_start_of_line()) { @@ -89,7 +89,7 @@ bool BufferCursor::go_left() return false; } -bool BufferCursor::go_right(bool allow_eol) +bool Buffer::Cursor::go_right(bool allow_eol) { if (!is_end_of_line(allow_eol)) { @@ -111,9 +111,9 @@ bool BufferCursor::go_right(bool allow_eol) return false; } -bool BufferCursor::go_up(size_t target_column) +bool Buffer::Cursor::go_up(size_t target_column) { - BufferCursor c2 = *this; + Buffer::Cursor c2 = *this; c2.go_start_of_line(); if (!c2.m_iterator.check_go_back()) { @@ -127,9 +127,9 @@ bool BufferCursor::go_up(size_t target_column) return true; } -bool BufferCursor::go_down(size_t target_column) +bool Buffer::Cursor::go_down(size_t target_column) { - BufferCursor c2 = *this; + Buffer::Cursor c2 = *this; c2.go_end_of_line(true); if (!c2.m_iterator.check_go_forward()) { @@ -142,7 +142,7 @@ bool BufferCursor::go_down(size_t target_column) return true; } -void BufferCursor::init_column() +void Buffer::Cursor::init_column() { if (*m_iterator == '\t') { @@ -154,9 +154,9 @@ void BufferCursor::init_column() } } -void BufferCursor::calculate_column() +void Buffer::Cursor::calculate_column() { - BufferCursor tmp_cursor = *this; + Buffer::Cursor tmp_cursor = *this; tmp_cursor.go_start_of_line(); while (tmp_cursor < *this) { @@ -165,7 +165,7 @@ void BufferCursor::calculate_column() m_column = tmp_cursor.m_column; } -void BufferCursor::forward_to_column(size_t target_column) +void Buffer::Cursor::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/BufferIterator.cc b/src/core/Buffer-Iterator.cc similarity index 71% rename from src/core/BufferIterator.cc rename to src/core/Buffer-Iterator.cc index 8a60901..585bf6b 100644 --- a/src/core/BufferIterator.cc +++ b/src/core/Buffer-Iterator.cc @@ -1,6 +1,6 @@ -#include "BufferIterator.h" +#include "Buffer.h" -void BufferIterator::go_forward() +void Buffer::Iterator::go_forward() { if (valid()) { @@ -8,11 +8,11 @@ void BufferIterator::go_forward() } } -bool BufferIterator::check_go_forward() +bool Buffer::Iterator::check_go_forward() { if (valid()) { - BufferIterator i2 = *this; + Buffer::Iterator i2 = *this; i2.go_forward(); if (i2.valid()) { @@ -23,7 +23,7 @@ bool BufferIterator::check_go_forward() return false; } -void BufferIterator::go_back() +void Buffer::Iterator::go_back() { if (valid()) { @@ -33,18 +33,18 @@ void BufferIterator::go_back() } else { - const uint8_t * a = m_gap_buffer->address(m_offset - 1u); + const uint8_t * a = m_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() +bool Buffer::Iterator::check_go_back() { if (valid()) { - BufferIterator i2 = *this; + Buffer::Iterator i2 = *this; i2.go_back(); if (i2.valid()) { diff --git a/src/core/Buffer.cc b/src/core/Buffer.cc index f69d852..09d9a56 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(); - BufferCursor start_of_line(&*m_gap_buffer, m_encoding, 4u); + Cursor start_of_line(this, m_encoding, 4u); size_t bytes_written = 0u; while (start_of_line.valid()) { - BufferCursor cursor = start_of_line; + Cursor 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 0744e5d..c46e70b 100644 --- a/src/core/Buffer.h +++ b/src/core/Buffer.h @@ -6,21 +6,109 @@ #include "LineEndings.h" #include "Encoding.h" #include "GapBuffer.h" -#include "BufferCursor.h" class Buffer { public: + class Iterator + { + public: + Iterator(Buffer * buffer, Encoding::Type encoding) + { + m_buffer = buffer; + m_offset = 0u; + m_encoding = encoding; + } + bool valid() const + { + return m_offset < m_buffer->size(); + } + void go_forward(); + bool check_go_forward(); + void go_back(); + bool check_go_back(); + uint8_t * address() const + { + return m_buffer->address(m_offset); + } + uint32_t operator*() const + { + if (valid()) + { + return Encoding::decode(m_encoding, address()); + } + else + { + return 0xFFFFFFFFu; + } + } + Buffer * buffer() const { return m_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: + Buffer * m_buffer; + size_t m_offset; + Encoding::Type m_encoding; + }; + + class Cursor + { + public: + Cursor(Buffer * buffer, Encoding::Type encoding, uint8_t tabstop) + : m_iterator(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); + }; + Buffer(); Buffer(const char * filename); 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(this, m_encoding, 4u); } auto get_string() { return m_gap_buffer->get_string(); } + size_t size() const { return m_gap_buffer->size(); } + uint8_t * address(size_t offset) const { return m_gap_buffer->address(offset); } protected: bool m_eol_at_eof; diff --git a/src/core/BufferCursor.h b/src/core/BufferCursor.h deleted file mode 100644 index 9351f46..0000000 --- a/src/core/BufferCursor.h +++ /dev/null @@ -1,51 +0,0 @@ -#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.h b/src/core/BufferIterator.h deleted file mode 100644 index 21a36ac..0000000 --- a/src/core/BufferIterator.h +++ /dev/null @@ -1,55 +0,0 @@ -#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/gui/BufferPane.cc b/src/gui/BufferPane.cc index 1af5c03..7e545cb 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) { - BufferCursor cursor = *m_cursor; + Buffer::Cursor 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) { - BufferCursor cursor = *m_cursor; + Buffer::Cursor cursor = *m_cursor; int rows = cursor.column() / m_columns; while ((rows < stop_at) && cursor.go_up(0u)) { - BufferCursor cursor2 = cursor; + Buffer::Cursor 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; - BufferCursor iter_cursor = *m_cursor; + Buffer::Cursor iter_cursor = *m_cursor; iter_cursor.go_start_of_line(); while ((screen_row > 0) && iter_cursor.go_up(0u)) { - BufferCursor cursor2 = iter_cursor; + Buffer::Cursor 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 BufferCursor & cursor) +void BufferPane::draw_buffer_line(int screen_row, const Buffer::Cursor & cursor) { - BufferCursor iter_cursor = cursor; + Buffer::Cursor 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 6229deb..2d2d926 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 BufferCursor & cursor); + void draw_buffer_line(int screen_row, const Buffer::Cursor & 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