Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement StringList pop_back() #745

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 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,32 @@ 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;
const Footer* prev_footer = NULL;
if (size_ > 1) {
prev_footer = (const Footer*)((const char*)back_ - sizeof(Footer));
}

if (!data_.resize(data_.size() - blk_sz)) {
return false;
}

size_--;
if (size_) {
back_ = (Header*)(data_.data() + data_.size() - prev_footer->len);
} else {
front_ = NULL;
back_ = NULL;
}

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 @@ -87,6 +87,11 @@ class StringList : public NonCopyable<> {
//! false if allocation failed.
ROC_ATTR_NODISCARD bool push_back(const char* str_begin, const char* str_end);

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

//! Find string in the list.
//! @returns
//! the string in the list or NULL if it is not found.
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("barbaz"));
CHECK(sl.pop_back());
LONGS_EQUAL(1, sl.size());
STRCMP_EQUAL("foo", sl.front());
STRCMP_EQUAL("foo", sl.back());

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

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

Expand Down
Loading