LineAndColumns: always pass by value

This commit is contained in:
Maxime Coste 2013-07-26 00:44:00 +02:00
parent 5d681f9efe
commit 3862b5cbb8
17 changed files with 76 additions and 77 deletions

View File

@ -86,7 +86,7 @@ bool Buffer::set_name(String name)
return false;
}
BufferIterator Buffer::iterator_at(const BufferCoord& coord) const
BufferIterator Buffer::iterator_at(BufferCoord coord) const
{
return is_end(coord) ? end() : BufferIterator(*this, clamp(coord));
}
@ -126,7 +126,7 @@ LineCount Buffer::line_count() const
return LineCount(m_lines.size());
}
String Buffer::string(const BufferCoord& begin, const BufferCoord& end) const
String Buffer::string(BufferCoord begin, BufferCoord end) const
{
String res;
for (auto line = begin.line; line <= end.line and line < line_count(); ++line)
@ -187,7 +187,7 @@ class UndoGroupOptimizer
return coord;
}
static ByteCount count_byte_to(BufferCoord pos, const BufferCoord& endpos, const String& str)
static ByteCount count_byte_to(BufferCoord pos, BufferCoord endpos, const String& str)
{
ByteCount count = 0;
for (auto it = str.begin(); it != str.end() and pos != endpos; ++it)
@ -417,7 +417,7 @@ void Buffer::check_invariant() const
#endif
}
BufferCoord Buffer::do_insert(const BufferCoord& pos, const String& content)
BufferCoord Buffer::do_insert(BufferCoord pos, const String& content)
{
kak_assert(is_valid(pos));
@ -497,7 +497,7 @@ BufferCoord Buffer::do_insert(const BufferCoord& pos, const String& content)
return begin;
}
BufferCoord Buffer::do_erase(const BufferCoord& begin, const BufferCoord& end)
BufferCoord Buffer::do_erase(BufferCoord begin, BufferCoord end)
{
kak_assert(is_valid(begin));
kak_assert(is_valid(end));
@ -531,7 +531,7 @@ BufferCoord Buffer::do_erase(const BufferCoord& begin, const BufferCoord& end)
void Buffer::apply_modification(const Modification& modification)
{
const String& content = modification.content;
const BufferCoord& coord = modification.coord;
BufferCoord coord = modification.coord;
kak_assert(is_valid(coord));
// in modifications, end coords should be {line_count(), 0}
@ -678,31 +678,31 @@ BufferCoord Buffer::char_prev(BufferCoord coord) const
return coord;
}
ByteCount Buffer::distance(const BufferCoord& begin, const BufferCoord& end) const
ByteCount Buffer::distance(BufferCoord begin, BufferCoord end) const
{
return offset(end) - offset(begin);
}
ByteCount Buffer::offset(const BufferCoord& c) const
ByteCount Buffer::offset(BufferCoord c) const
{
if (c.line == line_count())
return m_lines.back().start + m_lines.back().length();
return m_lines[c.line].start + c.column;
}
bool Buffer::is_valid(const BufferCoord& c) const
bool Buffer::is_valid(BufferCoord c) const
{
return (c.line < line_count() and c.column < m_lines[c.line].length()) or
(c.line == line_count() - 1 and c.column == m_lines.back().length()) or
(c.line == line_count() and c.column == 0);
}
bool Buffer::is_end(const BufferCoord& c) const
bool Buffer::is_end(BufferCoord c) const
{
return c >= BufferCoord{line_count() - 1, m_lines.back().length()};
}
char Buffer::byte_at(const BufferCoord& c) const
char Buffer::byte_at(BufferCoord c) const
{
kak_assert(c.line < line_count() and c.column < m_lines[c.line].length());
return m_lines[c.line].content[c.column];

View File

@ -69,8 +69,8 @@ private:
class BufferChangeListener
{
public:
virtual void on_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end) = 0;
virtual void on_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end) = 0;
virtual void on_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end) = 0;
virtual void on_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end) = 0;
};
// A Buffer is a in-memory representation of a file
@ -109,11 +109,11 @@ public:
bool undo();
bool redo();
String string(const BufferCoord& begin, const BufferCoord& end) const;
String string(BufferCoord begin, BufferCoord end) const;
char byte_at(const BufferCoord& c) const;
ByteCount offset(const BufferCoord& c) const;
ByteCount distance(const BufferCoord& begin, const BufferCoord& end) const;
char byte_at(BufferCoord c) const;
ByteCount offset(BufferCoord c) const;
ByteCount distance(BufferCoord begin, BufferCoord end) const;
BufferCoord advance(BufferCoord coord, ByteCount count) const;
BufferCoord next(BufferCoord coord) const;
BufferCoord prev(BufferCoord coord) const;
@ -124,8 +124,8 @@ public:
BufferCoord back_coord() const { return { line_count() - 1, m_lines.back().length() - 1 }; }
BufferCoord end_coord() const { return { line_count() - 1, m_lines.back().length() }; }
bool is_valid(const BufferCoord& c) const;
bool is_end(const BufferCoord& c) const;
bool is_valid(BufferCoord c) const;
bool is_end(BufferCoord c) const;
BufferIterator begin() const;
BufferIterator end() const;
@ -136,7 +136,7 @@ public:
{ return m_lines[line].content; }
// returns an iterator at given coordinates. clamp line_and_column
BufferIterator iterator_at(const BufferCoord& coord) const;
BufferIterator iterator_at(BufferCoord coord) const;
// returns nearest valid coordinates from given ones
BufferCoord clamp(BufferCoord coord) const;
@ -177,8 +177,8 @@ private:
};
LineList m_lines;
BufferCoord do_insert(const BufferCoord& pos, const String& content);
BufferCoord do_erase(const BufferCoord& begin, const BufferCoord& end);
BufferCoord do_insert(BufferCoord pos, const String& content);
BufferCoord do_erase(BufferCoord begin, BufferCoord end);
String m_name;
Flags m_flags;

View File

@ -36,12 +36,12 @@ void DynamicSelectionList::check_invariant() const
#endif
}
void DynamicSelectionList::on_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
void DynamicSelectionList::on_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end)
{
update_insert(buffer, begin, end);
}
void DynamicSelectionList::on_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
void DynamicSelectionList::on_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end)
{
update_erase(buffer, begin, end);
}

View File

@ -19,8 +19,8 @@ public:
void check_invariant() const;
private:
void on_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end) override;
void on_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end) override;
void on_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end) override;
void on_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end) override;
};
}

View File

@ -168,7 +168,7 @@ void sort_and_merge_overlapping(SelectionList& selections, size_t& main_selectio
merge_overlapping(selections, main_selection, overlaps);
}
BufferCoord Editor::offset_coord(const BufferCoord& coord, CharCount offset)
BufferCoord Editor::offset_coord(BufferCoord coord, CharCount offset)
{
auto& line = buffer()[coord.line];
auto character = std::max(0_char, std::min(line.char_count_to(coord.column) + offset,
@ -189,7 +189,7 @@ void Editor::move_selections(CharCount offset, SelectMode mode)
sort_and_merge_overlapping(m_selections, m_main_sel);
}
BufferCoord Editor::offset_coord(const BufferCoord& coord, LineCount offset)
BufferCoord Editor::offset_coord(BufferCoord coord, LineCount offset)
{
auto character = (*m_buffer)[coord.line].char_count_to(coord.column);
auto line = clamp(coord.line + offset, 0_line, m_buffer->line_count()-1);
@ -364,21 +364,21 @@ public:
ModifiedRangesListener(Buffer& buffer)
: BufferChangeListener_AutoRegister(buffer) {}
void on_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
void on_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end)
{
m_ranges.update_insert(buffer, begin, end);
auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), begin,
[](const BufferCoord& c, const Selection& sel)
[](BufferCoord c, const Selection& sel)
{ return c < sel.min(); });
m_ranges.emplace(it, begin, buffer.char_prev(end));
}
void on_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
void on_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end)
{
m_ranges.update_erase(buffer, begin, end);
auto pos = std::min(begin, buffer.back_coord());
auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), pos,
[](const BufferCoord& c, const Selection& sel)
[](BufferCoord c, const Selection& sel)
{ return c < sel.min(); });
m_ranges.emplace(it, pos, pos);
}

View File

@ -61,7 +61,7 @@ public:
void flip_selections();
void keep_selection(int index);
void remove_selection(int index);
void select(const BufferCoord& c, SelectMode mode = SelectMode::Replace)
void select(BufferCoord c, SelectMode mode = SelectMode::Replace)
{ select(Selection{ buffer().clamp(c) }, mode); }
void select(const Selection& sel,
SelectMode mode = SelectMode::Replace);
@ -91,8 +91,8 @@ private:
void begin_edition();
void end_edition();
virtual BufferCoord offset_coord(const BufferCoord& coord, LineCount move);
virtual BufferCoord offset_coord(const BufferCoord& coord, CharCount move);
virtual BufferCoord offset_coord(BufferCoord coord, LineCount move);
virtual BufferCoord offset_coord(BufferCoord coord, CharCount move);
int m_edition_level;

View File

@ -386,7 +386,7 @@ private:
m_option->get<std::vector<LineAndFlag>>();
}
void on_insert(const Buffer&, const BufferCoord& begin, const BufferCoord& end) override
void on_insert(const Buffer&, BufferCoord begin, BufferCoord end) override
{
LineCount new_lines = end.line - begin.line;
if (new_lines == 0)
@ -401,7 +401,7 @@ private:
m_option->set(lines);
}
void on_erase(const Buffer&, const BufferCoord& begin, const BufferCoord& end) override
void on_erase(const Buffer&, BufferCoord begin, BufferCoord end) override
{
LineCount removed_lines = end.line - begin.line;
if (removed_lines == 0)

View File

@ -497,7 +497,7 @@ struct BufferCompletion
};
static BufferCompletion complete_word(const Buffer& buffer,
const BufferCoord& cursor_pos,
BufferCoord cursor_pos,
bool other_buffers)
{
auto pos = buffer.iterator_at(cursor_pos);
@ -545,7 +545,7 @@ static BufferCompletion complete_word(const Buffer& buffer,
}
static BufferCompletion complete_opt(const Buffer& buffer,
const BufferCoord& cursor_pos,
BufferCoord cursor_pos,
OptionManager& options)
{
using StringList = std::vector<String>;

View File

@ -13,60 +13,60 @@ struct LineAndColumn
constexpr LineAndColumn(LineType line = 0, ColumnType column = 0)
: line(line), column(column) {}
constexpr EffectiveType operator+(const EffectiveType& other) const
constexpr EffectiveType operator+(EffectiveType other) const
{
return EffectiveType(line + other.line, column + other.column);
}
EffectiveType& operator+=(const EffectiveType& other)
EffectiveType& operator+=(EffectiveType other)
{
line += other.line;
column += other.column;
return *static_cast<EffectiveType*>(this);
}
constexpr EffectiveType operator-(const EffectiveType& other) const
constexpr EffectiveType operator-(EffectiveType other) const
{
return EffectiveType(line - other.line, column - other.column);
}
EffectiveType& operator-=(const EffectiveType& other)
EffectiveType& operator-=(EffectiveType other)
{
line -= other.line;
column -= other.column;
return *static_cast<EffectiveType*>(this);
}
constexpr bool operator< (const EffectiveType& other) const
constexpr bool operator< (EffectiveType other) const
{
return (line != other.line) ? line < other.line
: column < other.column;
}
constexpr bool operator<= (const EffectiveType& other) const
constexpr bool operator<= (EffectiveType other) const
{
return (line != other.line) ? line < other.line
: column <= other.column;
}
constexpr bool operator> (const EffectiveType& other) const
constexpr bool operator> (EffectiveType other) const
{
return (line != other.line) ? line > other.line
: column > other.column;
}
constexpr bool operator>= (const EffectiveType& other) const
constexpr bool operator>= (EffectiveType other) const
{
return (line != other.line) ? line > other.line
: column >= other.column;
}
constexpr bool operator== (const EffectiveType& other) const
constexpr bool operator== (EffectiveType other) const
{
return line == other.line and column == other.column;
}
constexpr bool operator!= (const EffectiveType& other) const
constexpr bool operator!= (EffectiveType other) const
{
return line != other.line or column != other.column;
}

View File

@ -510,8 +510,8 @@ static DisplayCoord compute_needed_size(const String& str)
return res;
}
static DisplayCoord compute_pos(const DisplayCoord& anchor,
const DisplayCoord& size,
static DisplayCoord compute_pos(DisplayCoord anchor,
DisplayCoord size,
WINDOW* opt_window_to_avoid = nullptr)
{
DisplayCoord scrsize = window_size(stdscr);

View File

@ -723,7 +723,7 @@ void align(Context& context)
{
auto& selections = context.editor().selections();
auto& buffer = context.buffer();
auto get_column = [&buffer](const BufferCoord& coord)
auto get_column = [&buffer](BufferCoord coord)
{ return buffer[coord.line].char_count_to(coord.column); };
CharCount max_col = 0;

View File

@ -19,10 +19,10 @@ namespace
template<template <bool, bool> class UpdateFunc>
void on_buffer_change(const Buffer& buffer, SelectionList& sels,
const BufferCoord& begin, const BufferCoord& end, LineCount end_line)
BufferCoord begin, BufferCoord end, LineCount end_line)
{
auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin,
[](const Selection& s, const BufferCoord& c) { return std::max(s.first(), s.last()) < c; });
[](const Selection& s, BufferCoord c) { return std::max(s.first(), s.last()) < c; });
auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line,
[](LineCount l, const Selection& s) { return l < std::min(s.first(), s.last()).line; });
@ -51,7 +51,7 @@ template<bool assume_different_line, bool assume_greater_than_begin>
struct UpdateInsert
{
void operator()(const Buffer& buffer, BufferCoord& coord,
const BufferCoord& begin, const BufferCoord& end) const
BufferCoord begin, BufferCoord end) const
{
if (assume_different_line)
kak_assert(begin.line < coord.line);
@ -68,7 +68,7 @@ template<bool assume_different_line, bool assume_greater_than_begin>
struct UpdateErase
{
void operator()(const Buffer& buffer, BufferCoord& coord,
const BufferCoord& begin, const BufferCoord& end) const
BufferCoord begin, BufferCoord end) const
{
if (not assume_greater_than_begin and coord < begin)
return;
@ -91,12 +91,12 @@ struct UpdateErase
}
void SelectionList::update_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
void SelectionList::update_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end)
{
on_buffer_change<UpdateInsert>(buffer, *this, begin, end, begin.line);
}
void SelectionList::update_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
void SelectionList::update_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end)
{
on_buffer_change<UpdateErase>(buffer, *this, begin, end, end.line);
}

View File

@ -10,7 +10,7 @@ namespace Kakoune
struct Range
{
public:
Range(const BufferCoord& first, const BufferCoord& last)
Range(BufferCoord first, BufferCoord last)
: m_first{first}, m_last{last} {}
void merge_with(const Range& range);
@ -56,8 +56,8 @@ using CaptureList = std::vector<String>;
// A selection is a Range, associated with a CaptureList
struct Selection : public Range
{
explicit Selection(const BufferCoord& pos) : Range(pos,pos) {}
Selection(const BufferCoord& first, const BufferCoord& last,
explicit Selection(BufferCoord pos) : Range(pos,pos) {}
Selection(BufferCoord first, BufferCoord last,
CaptureList captures = {})
: Range(first, last), m_captures(std::move(captures)) {}
@ -76,8 +76,8 @@ struct SelectionList : std::vector<Selection>
SelectionList() = default;
SelectionList(Selection s) : std::vector<Selection>{s} {}
void update_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end);
void update_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end);
void update_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end);
void update_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end);
void check_invariant() const;
};

View File

@ -228,8 +228,8 @@ Selection select_matching(const Buffer& buffer, const Selection& selection)
// c++14 will add std::optional, so we use boost::optional until then
using boost::optional;
static optional<Range> find_surrounding(const Buffer& buffer,
const BufferCoord& coord,
const CodepointPair& matching,
BufferCoord coord,
CodepointPair matching,
ObjectFlags flags)
{
const bool to_begin = flags & ObjectFlags::ToBegin;
@ -290,7 +290,7 @@ static optional<Range> find_surrounding(const Buffer& buffer,
}
Selection select_surrounding(const Buffer& buffer, const Selection& selection,
const CodepointPair& matching,
CodepointPair matching,
ObjectFlags flags)
{
auto res = find_surrounding(buffer, selection.last(), matching, flags);

View File

@ -68,8 +68,7 @@ SelectionList split_selection(const Buffer& buffer, const Selection& selection,
using CodepointPair = std::pair<Codepoint, Codepoint>;
Selection select_surrounding(const Buffer& buffer, const Selection& selection,
const CodepointPair& matching,
ObjectFlags flags);
CodepointPair matching, ObjectFlags flags);
}

View File

@ -89,13 +89,13 @@ void Window::update_display_buffer()
m_timestamp = buffer().timestamp();
}
void Window::set_position(const DisplayCoord& position)
void Window::set_position(DisplayCoord position)
{
m_position.line = std::max(0_line, position.line);
m_position.column = std::max(0_char, position.column);
}
void Window::set_dimensions(const DisplayCoord& dimensions)
void Window::set_dimensions(DisplayCoord dimensions)
{
m_dimensions = dimensions;
}
@ -174,7 +174,7 @@ void Window::scroll_to_keep_cursor_visible_ifn()
namespace
{
CharCount find_display_column(const DisplayLine& line, const Buffer& buffer,
const BufferCoord& coord)
BufferCoord coord)
{
CharCount column = 0;
for (auto& atom : line)
@ -212,7 +212,7 @@ BufferCoord find_buffer_coord(const DisplayLine& line, const Buffer& buffer,
}
}
DisplayCoord Window::display_position(const BufferCoord& coord)
DisplayCoord Window::display_position(BufferCoord coord)
{
LineCount l = 0;
for (auto& line : m_display_buffer.lines())
@ -225,7 +225,7 @@ DisplayCoord Window::display_position(const BufferCoord& coord)
return { 0, 0 };
}
BufferCoord Window::offset_coord(const BufferCoord& coord, LineCount offset)
BufferCoord Window::offset_coord(BufferCoord coord, LineCount offset)
{
auto line = clamp(coord.line + offset, 0_line, buffer().line_count()-1);
DisplayBuffer display_buffer;

View File

@ -25,10 +25,10 @@ public:
~Window();
const DisplayCoord& position() const { return m_position; }
void set_position(const DisplayCoord& position);
void set_position(DisplayCoord position);
const DisplayCoord& dimensions() const { return m_dimensions; }
void set_dimensions(const DisplayCoord& dimensions);
void set_dimensions(DisplayCoord dimensions);
const DisplayBuffer& display_buffer() const { return m_display_buffer; }
@ -38,7 +38,7 @@ public:
void scroll(CharCount offset);
void update_display_buffer();
DisplayCoord display_position(const BufferCoord& coord);
DisplayCoord display_position(BufferCoord coord);
HighlighterGroup& highlighters() { return m_highlighters; }
@ -57,7 +57,7 @@ private:
void scroll_to_keep_cursor_visible_ifn();
BufferCoord offset_coord(const BufferCoord& coord, LineCount move) override;
BufferCoord offset_coord(BufferCoord coord, LineCount move) override;
DisplayCoord m_position;
DisplayCoord m_dimensions;