From 007cd6397feae626e053e9abaadededdbc4b6ebe Mon Sep 17 00:00:00 2001 From: J Jeshwanth Reddy <60390772+jeshwanthreddy13@users.noreply.github.com> Date: Tue, 2 Jul 2024 22:19:13 +0530 Subject: [PATCH] Fixes #669 -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. --- src/internal_modules/roc_core/string_list.cpp | 22 ++++++++++++++++++ src/internal_modules/roc_core/string_list.h | 5 ++++ src/tests/roc_core/test_string_list.cpp | 23 +++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/internal_modules/roc_core/string_list.cpp b/src/internal_modules/roc_core/string_list.cpp index 7c8a56cab..6a1af5ad7 100644 --- a/src/internal_modules/roc_core/string_list.cpp +++ b/src/internal_modules/roc_core/string_list.cpp @@ -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"); diff --git a/src/internal_modules/roc_core/string_list.h b/src/internal_modules/roc_core/string_list.h index a8ac620e8..986767526 100644 --- a/src/internal_modules/roc_core/string_list.h +++ b/src/internal_modules/roc_core/string_list.h @@ -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. diff --git a/src/tests/roc_core/test_string_list.cpp b/src/tests/roc_core/test_string_list.cpp index 6a65b812a..c55813b43 100644 --- a/src/tests/roc_core/test_string_list.cpp +++ b/src/tests/roc_core/test_string_list.cpp @@ -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);