aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-07-02 19:11:26 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-07-02 19:11:26 +0200
commita24e133b7b90afae64b3edf0d8dd29b6e98a250b (patch)
treebe34338a7abb8bac967783ff5b4086450f6e029a /libs
parentab8c37673ca247c190d60147dff66c958ec960de (diff)
downloadkernel-a24e133b7b90afae64b3edf0d8dd29b6e98a250b.tar.xz
kernel-a24e133b7b90afae64b3edf0d8dd29b6e98a250b.zip
kstd: implement resize for string
Diffstat (limited to 'libs')
-rw-r--r--libs/kstd/kstd/bits/basic_string.hpp30
-rw-r--r--libs/kstd/kstd/string.test.cpp169
2 files changed, 199 insertions, 0 deletions
diff --git a/libs/kstd/kstd/bits/basic_string.hpp b/libs/kstd/kstd/bits/basic_string.hpp
index 4d690ac3..c780a083 100644
--- a/libs/kstd/kstd/bits/basic_string.hpp
+++ b/libs/kstd/kstd/bits/basic_string.hpp
@@ -926,6 +926,36 @@ namespace kstd
return *this;
}
+ //! Resize this string to contain a given number of characters, filling with null-bytes if the size increases.
+ //!
+ //! @param count The new size of the string.
+ constexpr auto resize(size_type count) -> void
+ {
+ resize(count, CharacterType{});
+ }
+
+ //! Resize this string to contain a given number of characters, filling with the given character if the size
+ //! increases.
+ //!
+ //! @param count The new size of the string.
+ //! @param character The character to use to fill up empty space.
+ constexpr auto resize(size_type count, CharacterType character) -> void
+ {
+ if (count > size() && count < capacity())
+ {
+ traits_type::assign(m_data + size(), count - size(), character);
+ }
+ else if (count > capacity())
+ {
+ auto new_buffer = allocate_buffer(count);
+ traits_type::copy(new_buffer.data(), data(), size());
+ traits_type::assign(new_buffer.data() + size(), count - size(), character);
+ update_data(new_buffer);
+ }
+
+ update_length(count);
+ }
+
//! Swap the contents of this string with another one.
//!
//! @param other The string to swap contents with.
diff --git a/libs/kstd/kstd/string.test.cpp b/libs/kstd/kstd/string.test.cpp
index 27ca95b3..e11877e5 100644
--- a/libs/kstd/kstd/string.test.cpp
+++ b/libs/kstd/kstd/string.test.cpp
@@ -1954,6 +1954,112 @@ SCENARIO("String modifiers", "[string]")
REQUIRE(s.data() != old_data);
}
}
+
+ WHEN("resizing to a greater size within the capacity")
+ {
+ s.resize(10);
+
+ THEN("the capacity stays the same")
+ {
+ REQUIRE(s.capacity() == old_capacity);
+ }
+
+ THEN("the size increases")
+ {
+ CHECK(s.size() > old_size);
+ REQUIRE(s.size() == 10);
+ }
+
+ THEN("the string get filled with null characters")
+ {
+ REQUIRE(kstd::string::traits_type::compare(s.data(), "abcd\0\0\0\0\0\0", s.size() + 1) == 0);
+ }
+ }
+
+ WHEN("resizing to a greater size with a given character within the capacity")
+ {
+ s.resize(10, 'x');
+
+ THEN("the capacity stays the same")
+ {
+ REQUIRE(s.capacity() == old_capacity);
+ }
+
+ THEN("the size increases")
+ {
+ CHECK(s.size() > old_size);
+ REQUIRE(s.size() == 10);
+ }
+
+ THEN("the string get filled with the given character")
+ {
+ REQUIRE(kstd::string::traits_type::compare(s.data(), "abcdxxxxxx", s.size() + 1) == 0);
+ }
+ }
+
+ WHEN("resizing to a greater size outside the capacity")
+ {
+ s.resize(20);
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(s.capacity() > old_capacity);
+ }
+
+ THEN("the size increases")
+ {
+ CHECK(s.size() > old_size);
+ REQUIRE(s.size() == 20);
+ }
+
+ THEN("the string get filled with null characters")
+ {
+ REQUIRE(kstd::string::traits_type::compare(s.data(), "abcd\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", s.size() + 1) ==
+ 0);
+ }
+ }
+
+ WHEN("resizing to a greater size with a given character within the capacity")
+ {
+ s.resize(20, 'x');
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(s.capacity() > old_capacity);
+ }
+
+ THEN("the size increases")
+ {
+ CHECK(s.size() > old_size);
+ REQUIRE(s.size() == 20);
+ }
+
+ THEN("the string get filled with the given character")
+ {
+ REQUIRE(kstd::string::traits_type::compare(s.data(), "abcdxxxxxxxxxxxxxxxx", s.size() + 1) == 0);
+ }
+ }
+
+ WHEN("resizing to a lesser size")
+ {
+ s.resize(2);
+
+ THEN("the capacity stays the same")
+ {
+ REQUIRE(s.capacity() == old_capacity);
+ }
+
+ THEN("the size decreases")
+ {
+ CHECK(s.size() < old_size);
+ REQUIRE(s.size() == 2);
+ }
+
+ THEN("the string get filled with the given character")
+ {
+ REQUIRE(kstd::string::traits_type::compare(s.data(), "ab\0", s.size() + 1) == 0);
+ }
+ }
}
GIVEN("A long string")
@@ -2037,6 +2143,69 @@ SCENARIO("String modifiers", "[string]")
REQUIRE(s.data() != old_data);
}
}
+
+ WHEN("resizing to a greater size")
+ {
+ s.resize(22); // NOLINT
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(s.capacity() > old_capacity);
+ }
+
+ THEN("the size increases")
+ {
+ CHECK(s.size() > old_size);
+ REQUIRE(s.size() == 22);
+ }
+
+ THEN("the string get filled with null characters")
+ {
+ REQUIRE(kstd::string::traits_type::compare(s.data(), "abcdefghijABCDEFGHIJ\0\0", s.size() + 1) == 0);
+ }
+ }
+
+ WHEN("resizing to a greater size with a given character within the capacity")
+ {
+ s.resize(22, 'x'); // NOLINT
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(s.capacity() > old_capacity);
+ }
+
+ THEN("the size increases")
+ {
+ CHECK(s.size() > old_size);
+ REQUIRE(s.size() == 22);
+ }
+
+ THEN("the string get filled with the given character")
+ {
+ REQUIRE(kstd::string::traits_type::compare(s.data(), "abcdefghijABCDEFGHIJxx", s.size() + 1) == 0);
+ }
+ }
+
+ WHEN("resizing to a lesser size")
+ {
+ s.resize(2);
+
+ THEN("the capacity stays the same")
+ {
+ REQUIRE(s.capacity() == old_capacity);
+ }
+
+ THEN("the size decreases")
+ {
+ CHECK(s.size() < old_size);
+ REQUIRE(s.size() == 2);
+ }
+
+ THEN("the string get filled with the given character")
+ {
+ REQUIRE(kstd::string::traits_type::compare(s.data(), "ab\0", s.size() + 1) == 0);
+ }
+ }
}
GIVEN("Two strings")