Add Buffer::Iterator functions to move forward/backward on/up to characters in a line
This commit is contained in:
parent
32b272b75c
commit
3c6b3bc49e
@ -202,6 +202,80 @@ void Buffer::Iterator::warp_to_offset(size_t offset)
|
||||
}
|
||||
}
|
||||
|
||||
bool Buffer::Iterator::go_forward_in_line_up_to_char(uint32_t c)
|
||||
{
|
||||
size_t last_offset = m_offset;
|
||||
Buffer::Iterator it = *this;
|
||||
while (it.go_right_in_line(false))
|
||||
{
|
||||
if (*it == c)
|
||||
{
|
||||
if (last_offset != m_offset)
|
||||
{
|
||||
m_offset = last_offset;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
last_offset = it.m_offset;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Buffer::Iterator::go_forward_in_line_on_to_char(uint32_t c)
|
||||
{
|
||||
Buffer::Iterator it = *this;
|
||||
while (it.go_right_in_line(false))
|
||||
{
|
||||
if (*it == c)
|
||||
{
|
||||
m_offset = it.m_offset;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Buffer::Iterator::go_backward_in_line_up_to_char(uint32_t c)
|
||||
{
|
||||
size_t last_offset = m_offset;
|
||||
Buffer::Iterator it = *this;
|
||||
while (it.go_left_in_line())
|
||||
{
|
||||
if (*it == c)
|
||||
{
|
||||
if (last_offset != m_offset)
|
||||
{
|
||||
m_offset = last_offset;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
last_offset = it.m_offset;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Buffer::Iterator::go_backward_in_line_on_to_char(uint32_t c)
|
||||
{
|
||||
Buffer::Iterator it = *this;
|
||||
while (it.go_left_in_line())
|
||||
{
|
||||
if (*it == c)
|
||||
{
|
||||
m_offset = it.m_offset;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Buffer::Iterator Buffer::Iterator::prev()
|
||||
{
|
||||
Iterator i = *this;
|
||||
|
@ -49,6 +49,10 @@ public:
|
||||
bool go_right_in_line(bool allow_eol);
|
||||
bool go_previous_line();
|
||||
bool go_next_line();
|
||||
bool go_forward_in_line_up_to_char(uint32_t c);
|
||||
bool go_forward_in_line_on_to_char(uint32_t c);
|
||||
bool go_backward_in_line_up_to_char(uint32_t c);
|
||||
bool go_backward_in_line_on_to_char(uint32_t c);
|
||||
Iterator prev();
|
||||
Iterator next();
|
||||
void warp(ssize_t offset_offset, ssize_t line_offset)
|
||||
|
@ -109,6 +109,41 @@ TEST(BufferTest, allows_navigating_using_iterators2)
|
||||
EXPECT_EQ(C('a'), **iterator);
|
||||
}
|
||||
|
||||
TEST(BufferTest, allows_navigating_using_iterators3)
|
||||
{
|
||||
std::string s("abcabc\n");
|
||||
Buffer b((const uint8_t *)&s[0], s.size());
|
||||
auto it = b.add_cursor();
|
||||
|
||||
EXPECT_FALSE(it->go_forward_in_line_up_to_char(C('d')));
|
||||
EXPECT_EQ(0u, it->offset());
|
||||
EXPECT_FALSE(it->go_forward_in_line_up_to_char(C('b')));
|
||||
EXPECT_EQ(0u, it->offset());
|
||||
EXPECT_TRUE(it->go_forward_in_line_up_to_char(C('c')));
|
||||
EXPECT_EQ(1u, it->offset());
|
||||
|
||||
EXPECT_TRUE(it->go_forward_in_line_on_to_char(C('c')));
|
||||
EXPECT_EQ(2u, it->offset());
|
||||
EXPECT_TRUE(it->go_forward_in_line_on_to_char(C('c')));
|
||||
EXPECT_EQ(5u, it->offset());
|
||||
EXPECT_FALSE(it->go_forward_in_line_on_to_char(C('c')));
|
||||
EXPECT_EQ(5u, it->offset());
|
||||
|
||||
EXPECT_FALSE(it->go_backward_in_line_up_to_char(C('d')));
|
||||
EXPECT_EQ(5u, it->offset());
|
||||
EXPECT_FALSE(it->go_backward_in_line_up_to_char(C('b')));
|
||||
EXPECT_EQ(5u, it->offset());
|
||||
EXPECT_TRUE(it->go_backward_in_line_up_to_char(C('a')));
|
||||
EXPECT_EQ(4u, it->offset());
|
||||
|
||||
EXPECT_FALSE(it->go_backward_in_line_on_to_char(C('d')));
|
||||
EXPECT_EQ(4u, it->offset());
|
||||
EXPECT_TRUE(it->go_backward_in_line_on_to_char(C('a')));
|
||||
EXPECT_EQ(3u, it->offset());
|
||||
EXPECT_TRUE(it->go_backward_in_line_on_to_char(C('a')));
|
||||
EXPECT_EQ(0u, it->offset());
|
||||
}
|
||||
|
||||
TEST(BufferTest, allows_inserting_and_erasing_characters)
|
||||
{
|
||||
std::string s("abc\ndef\nghi\n");
|
||||
|
Loading…
x
Reference in New Issue
Block a user