Move Iterator and Cursor classes into Buffer class.

This commit is contained in:
Josh Holtrop 2016-12-11 15:17:15 -05:00
parent b880397b7e
commit 9b6b21a1d0
8 changed files with 130 additions and 148 deletions

View File

@ -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)

View File

@ -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())
{

View File

@ -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)

View File

@ -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<BufferCursor> add_cursor()
std::shared_ptr<Cursor> add_cursor()
{
return std::make_shared<BufferCursor>(&*m_gap_buffer, m_encoding, 4u);
return std::make_shared<Cursor>(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;

View File

@ -1,51 +0,0 @@
#ifndef BufferCURSOR_H
#define BufferCURSOR_H
#include <stdint.h>
#include <stdlib.h>
#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

View File

@ -1,55 +0,0 @@
#ifndef BUFFERITERATOR_H
#define BUFFERITERATOR_H
#include <stdint.h>
#include <stdlib.h>
#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

View File

@ -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);

View File

@ -15,7 +15,7 @@ public:
std::shared_ptr<GL> gl);
void resize(int width, int height) override;
void draw();
std::shared_ptr<BufferCursor> cursor() { return m_cursor; }
std::shared_ptr<Buffer::Cursor> 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<BufferCursor> m_cursor;
std::shared_ptr<Buffer::Cursor> m_cursor;
};
#endif