Skip to content

Commit

Permalink
Fixes #669
Browse files Browse the repository at this point in the history
-Implemented a new "void pop_back()" method.
-Removes the last string from StringList.
-Returns false if deallocation fails
-Panics if StringList is empty.
-Clears when last string is poped.

Test cases covered:

-Size should be zero after removing last element from the list.
-front_ and back_ have to point to same string when initial list with two strings is poped.
-back_ should point to 'n-1' string after poping initial 'n' number of strings.
  • Loading branch information
jeshwanthreddy13 committed Jul 2, 2024
1 parent 3a19267 commit 007cd63
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/internal_modules/roc_core/string_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ bool StringList::push_back(const char* str_begin, const char* str_end) {
return true;
}

bool StringList::pop_back() {
if (size_ == 0) {
roc_panic("stringlist: list is empty");
}

const size_t blk_sz = back_->len;
if (!data_.resize(data_.size() - blk_sz)) {
return false;
}

size_--;
if (size_ > 0) {
back_ = (Header*)(data_.data() + data_.size() - blk_sz);
}

else {
clear();
}

return true;
}

const char* StringList::find(const char* str) {
if (str == NULL) {
roc_panic("stringlist: string is null");
Expand Down
5 changes: 5 additions & 0 deletions src/internal_modules/roc_core/string_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class StringList : public NonCopyable<> {
//! false if allocation failed.
ROC_ATTR_NODISCARD bool push_back(const char* str);

//! Remove string from end of the list.
//! @returns
//! false if deallocation failed.
ROC_ATTR_NODISCARD bool pop_back();

//! Append string from a range to the list.
//! @remarks
//! Reallocates memory if necessary.
Expand Down
23 changes: 23 additions & 0 deletions src/tests/roc_core/test_string_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ TEST(string_list, push_back) {
STRCMP_EQUAL("bar", sl.back());
}

TEST(string_list, pop_back) {
StringList sl(arena);

LONGS_EQUAL(0, sl.size());
CHECK(sl.push_back("foo"));
CHECK(sl.pop_back());
LONGS_EQUAL(0, sl.size());

CHECK(sl.push_back("foo"));
CHECK(sl.push_back("bar"));
CHECK(sl.pop_back());
LONGS_EQUAL(1, sl.size());
STRCMP_EQUAL("foo", sl.front());
STRCMP_EQUAL("foo", sl.back());

CHECK(sl.push_back("bar"));
CHECK(sl.push_back("baz"));
CHECK(sl.pop_back());
LONGS_EQUAL(2, sl.size());
STRCMP_EQUAL("foo", sl.front());
STRCMP_EQUAL("bar", sl.back());
}

TEST(string_list, push_back_range) {
StringList sl(arena);

Expand Down

0 comments on commit 007cd63

Please sign in to comment.