Add GapBuffer::erase()
This commit is contained in:
parent
57d525ae44
commit
92b3e888b5
@ -33,6 +33,23 @@ void GapBuffer::insert(size_t position, const uint8_t * data, size_t length)
|
||||
m_size += length;
|
||||
}
|
||||
|
||||
void GapBuffer::erase(size_t position, size_t length)
|
||||
{
|
||||
if ((position < m_size) && ((position + length) <= m_size))
|
||||
{
|
||||
if ((position + length) == m_gap_position)
|
||||
{
|
||||
m_gap_position -= length;
|
||||
m_size -= length;
|
||||
}
|
||||
else
|
||||
{
|
||||
move_gap(position);
|
||||
m_size -= length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string GapBuffer::get_string()
|
||||
{
|
||||
compact();
|
||||
|
@ -46,6 +46,16 @@ public:
|
||||
*/
|
||||
void insert(size_t position, const uint8_t * data, size_t length);
|
||||
|
||||
/**
|
||||
* Erase data from the gap buffer.
|
||||
*
|
||||
* @param position
|
||||
* Position in the gap buffer to erase data from.
|
||||
* @param length
|
||||
* Length of the data to erase.
|
||||
*/
|
||||
void erase(size_t position, size_t length);
|
||||
|
||||
/**
|
||||
* Get the contents of the gap buffer as a string.
|
||||
*
|
||||
|
@ -43,6 +43,35 @@ TEST(GapBufferTest, allows_inserting)
|
||||
EXPECT_EQ(8u, gp.size());
|
||||
}
|
||||
|
||||
TEST(GapBufferTest, allows_deleting)
|
||||
{
|
||||
GapBuffer gp;
|
||||
|
||||
gp.insert(0u, (const uint8_t *)"test_123", 8u);
|
||||
EXPECT_EQ(8u, gp.size());
|
||||
EXPECT_EQ("test_123", gp.get_string());
|
||||
|
||||
gp.erase(6u, 2u);
|
||||
EXPECT_EQ(6u, gp.size());
|
||||
EXPECT_EQ("test_1", gp.get_string());
|
||||
|
||||
gp.insert(4u, (const uint8_t *)"!@#$", 4u);
|
||||
EXPECT_EQ(10u, gp.size());
|
||||
EXPECT_EQ("test!@#$_1", gp.get_string());
|
||||
|
||||
gp.erase(4u, 4u);
|
||||
EXPECT_EQ(6u, gp.size());
|
||||
EXPECT_EQ("test_1", gp.get_string());
|
||||
|
||||
gp.insert(4u, (const uint8_t *)"^&", 2u);
|
||||
EXPECT_EQ(8u, gp.size());
|
||||
EXPECT_EQ("test^&_1", gp.get_string());
|
||||
|
||||
gp.erase(2u, 5u);
|
||||
EXPECT_EQ(3u, gp.size());
|
||||
EXPECT_EQ("te1", gp.get_string());
|
||||
}
|
||||
|
||||
TEST(GapBufferTest, allocates_more_memory_and_copies_buffer_contents_when_needed)
|
||||
{
|
||||
GapBuffer gp;
|
||||
|
Loading…
x
Reference in New Issue
Block a user