From 6c8b068c15e28e91117f84cb8d5789f5fe6fcbd0 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 1 Jun 2026 20:31:57 +0200 Subject: kstd/string: simplify comparisons --- libs/kstd/kstd/string | 105 ++++++++++++++++++----------------------- libs/kstd/kstd/string.test.cpp | 44 ++++++++--------- 2 files changed, 69 insertions(+), 80 deletions(-) (limited to 'libs') diff --git a/libs/kstd/kstd/string b/libs/kstd/kstd/string index e228a04..9343b42 100644 --- a/libs/kstd/kstd/string +++ b/libs/kstd/kstd/string @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -52,7 +53,7 @@ namespace kstd * @brief Constructs a string from a string view by copying the characters into owned storage. * @param view The string view to copy the characters from. */ - string(std::string_view view) + explicit string(std::string_view view) : string() { append(view); @@ -101,6 +102,26 @@ namespace kstd */ constexpr auto operator=(string const & other) -> string & = default; + constexpr auto operator=(std::string_view const & other) -> string & + { + clear(); + append(other); + return *this; + } + + constexpr auto operator=(char const * other) -> string & + { + clear(); + append(std::string_view{other}); + return *this; + } + + //! Create a string view from this string. + constexpr operator std::string_view() const noexcept + { + return {data(), size()}; + } + /** * @brief Returns the number of characters in this string, not including the null terminator. */ @@ -224,6 +245,7 @@ namespace kstd { if (!view.empty()) { + m_storage.reserve(size() + view.size() + 1); std::ranges::for_each(view, [this](auto const ch) { push_back(ch); }); } @@ -237,7 +259,7 @@ namespace kstd */ constexpr auto append(string const & other) -> string & { - return append(other.view()); + return append(static_cast(other)); } /** @@ -261,12 +283,29 @@ namespace kstd return *this; } - /** - * @brief Returns a string view of this string, which is a non-owning view into the characters of this string. - */ - [[nodiscard]] constexpr auto view() const noexcept -> std::string_view + //! Compare this string lexicographically to another string. + //! + //! @param other The string to compare to this one. + [[nodiscard]] constexpr auto operator<=>(string const & other) const noexcept -> std::strong_ordering = default; + + [[nodiscard]] constexpr auto operator==(string const & other) const noexcept -> bool = default; + + //! Compare this string lexicographically to a C-style string. + //! + //! @param other The C-style string to compare to this one. + //! @return The result of the comparison. + [[nodiscard]] constexpr auto operator<=>(char const * other) const noexcept + { + return static_cast(*this) <=> other; + } + + //! Check if this string compares equal to a C-style string. + //! + //! @param other The C-style string to compare to this one. + //! @return @p true iff. this string compares equal to @p other, @p false otherwise. + [[nodiscard]] constexpr auto operator==(char const * other) const noexcept -> bool { - return std::string_view{data(), size()}; + return static_cast(*this) == other; } private: @@ -316,62 +355,12 @@ namespace kstd return result; } - [[nodiscard]] constexpr auto inline operator==(string const & lhs, string const & rhs) -> bool - { - return lhs.view() == rhs.view(); - } - - [[nodiscard]] constexpr auto inline operator!=(string const & lhs, string const & rhs) -> bool - { - return !(lhs == rhs); - } - - [[nodiscard]] constexpr auto inline operator==(string const & lhs, std::string_view rhs) -> bool - { - return lhs.view() == rhs; - } - - [[nodiscard]] constexpr auto inline operator!=(string const & lhs, std::string_view rhs) -> bool - { - return !(lhs == rhs); - } - - [[nodiscard]] constexpr auto inline operator==(std::string_view lhs, string const & rhs) -> bool - { - return lhs == rhs.view(); - } - - [[nodiscard]] constexpr auto inline operator!=(std::string_view lhs, string const & rhs) -> bool - { - return !(lhs == rhs); - } - - [[nodiscard]] constexpr auto inline operator==(string const & lhs, char const * rhs) -> bool - { - return lhs.view() == std::string_view{rhs}; - } - - [[nodiscard]] constexpr auto inline operator!=(string const & lhs, char const * rhs) -> bool - { - return !(lhs == rhs); - } - - [[nodiscard]] constexpr auto inline operator==(char const * lhs, string const & rhs) -> bool - { - return std::string_view{lhs} == rhs.view(); - } - - [[nodiscard]] constexpr auto inline operator!=(char const * lhs, string const & rhs) -> bool - { - return !(lhs == rhs); - } - template<> struct formatter : formatter { auto format(string const & str, format_context & context) const -> void { - formatter::format(str.view(), context); + formatter::format(static_cast(str), context); } }; diff --git a/libs/kstd/kstd/string.test.cpp b/libs/kstd/kstd/string.test.cpp index 9755676..b81cd3a 100644 --- a/libs/kstd/kstd/string.test.cpp +++ b/libs/kstd/kstd/string.test.cpp @@ -22,7 +22,7 @@ SCENARIO("String initialization and construction", "[string]") THEN("the string is empty") { - REQUIRE(str.view() == std::string_view{}); + REQUIRE(str == std::string_view{}); } } } @@ -43,7 +43,7 @@ SCENARIO("String initialization and construction", "[string]") THEN("the string contains the same characters as the view") { - REQUIRE(str.view() == view); + REQUIRE(str == view); } } } @@ -64,7 +64,7 @@ SCENARIO("String initialization and construction", "[string]") THEN("the string contains the same characters as the C-style string") { - REQUIRE(str.view() == c_str); + REQUIRE(str == c_str); } } } @@ -85,7 +85,7 @@ SCENARIO("String initialization and construction", "[string]") THEN("the string contains the same character as the given character") { - REQUIRE(str.view() == std::string_view{&ch, 1}); + REQUIRE(str == std::string_view{&ch, 1}); } } } @@ -100,7 +100,7 @@ SCENARIO("String initialization and construction", "[string]") THEN("the new string contains the same characters as the original") { - REQUIRE(str.view() == other.view()); + REQUIRE(static_cast(str) == static_cast(other)); } } @@ -113,7 +113,7 @@ SCENARIO("String initialization and construction", "[string]") THEN("the string contains the same characters as the assigned string") { - REQUIRE(str.view() == other.view()); + REQUIRE(static_cast(str) == static_cast(other)); } } } @@ -129,7 +129,7 @@ SCENARIO("String initialization and construction", "[string]") THEN("the string contains the same characters as the assigned view") { - REQUIRE(str.view() == view); + REQUIRE(str == view); } } } @@ -145,7 +145,7 @@ SCENARIO("String initialization and construction", "[string]") THEN("the string contains the same characters as the assigned C-style string") { - REQUIRE(str.view() == c_str); + REQUIRE(str == c_str); } } } @@ -164,7 +164,7 @@ SCENARIO("String concatenation", "[string]") THEN("the first string contains the characters of both strings concatenated") { - REQUIRE(str1.view() == "Blub Blub"); + REQUIRE(str1 == "Blub Blub"); } THEN("the size of the first string is the sum of the sizes of both strings") @@ -179,7 +179,7 @@ SCENARIO("String concatenation", "[string]") THEN("the first string contains the characters of both strings concatenated") { - REQUIRE(str1.view() == "Blub Blub"); + REQUIRE(str1 == "Blub Blub"); } THEN("the size of the first string is the sum of the sizes of both strings") @@ -194,7 +194,7 @@ SCENARIO("String concatenation", "[string]") THEN("the new string contains the characters of both strings concatenated") { - REQUIRE(str3.view() == "Blub Blub"); + REQUIRE(str3 == "Blub Blub"); } THEN("the size of the new string is the sum of the sizes of both strings") @@ -215,7 +215,7 @@ SCENARIO("String concatenation", "[string]") THEN("the string contains the characters of both the original string and the appended view concatenated") { - REQUIRE(str.view() == "Blub Blub"); + REQUIRE(str == "Blub Blub"); } THEN("the size of the string is the sum of the sizes of the original string and the appended view") @@ -236,7 +236,7 @@ SCENARIO("String concatenation", "[string]") THEN("the string contains the original characters followed by the appended character") { - REQUIRE(str.view() == "Blub!"); + REQUIRE(str == "Blub!"); } THEN("the size of the string is one more than the original size") @@ -251,7 +251,7 @@ SCENARIO("String concatenation", "[string]") THEN("the string contains the original characters followed by the appended character") { - REQUIRE(str.view() == "Blub!"); + REQUIRE(str == "Blub!"); } THEN("the size of the string is one more than the original size") @@ -276,8 +276,8 @@ SCENARIO("String conversion and comparison", "[string]") THEN("the string contains the decimal representation of the unsigned integer") { - REQUIRE(str1.view() == "12345"); - REQUIRE(str2.view() == "0"); + REQUIRE(str1 == "12345"); + REQUIRE(str2 == "0"); } } } @@ -335,7 +335,7 @@ SCENARIO("String clearing", "[string]") THEN("the string contains no characters") { - REQUIRE(str.view() == std::string_view{}); + REQUIRE(str == std::string_view{}); } } } @@ -351,7 +351,7 @@ SCENARIO("String iteration", "[string]") { kstd::string result; - for (auto ch : str.view()) + for (auto ch : static_cast(str)) { result.push_back(ch); } @@ -396,14 +396,14 @@ SCENARIO("String iteration", "[string]") { kstd::string result; - for (auto ch : str.view()) + for (auto ch : static_cast(str)) { result.push_back(ch); } THEN("the iterated characters are the same as the characters in the string") { - REQUIRE(result == str.view()); + REQUIRE(result == static_cast(str)); } } @@ -429,7 +429,7 @@ SCENARIO("String iteration", "[string]") { kstd::string result; - for (auto ch : str.view()) + for (auto ch : static_cast(str)) { result.push_back(ch); } @@ -438,7 +438,7 @@ SCENARIO("String iteration", "[string]") { REQUIRE(result.empty()); REQUIRE(result.size() == 0); - REQUIRE(result.view() == std::string_view{}); + REQUIRE(static_cast(result) == std::string_view{}); } } } -- cgit v1.2.3 From 772861fc5fae1c126fcc63a8809b0a9c729bd152 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 2 Jun 2026 13:43:49 +0200 Subject: kernel/vfs: add type registry tests --- libs/kstd/kstd/flat_map | 16 ++++++++++++++++ libs/kstd/kstd/flat_map.test.cpp | 10 ++++++++++ 2 files changed, 26 insertions(+) (limited to 'libs') diff --git a/libs/kstd/kstd/flat_map b/libs/kstd/kstd/flat_map index f12b1b5..943e9fc 100644 --- a/libs/kstd/kstd/flat_map +++ b/libs/kstd/kstd/flat_map @@ -356,6 +356,22 @@ namespace kstd return find(key) != cend(); } + //! Get a reference to the keys container. + //! + //! @return a reference to the keys container. + [[nodiscard]] constexpr auto keys() const noexcept -> key_container_type const & + { + return m_containers.keys; + } + + //! Get a reference to the values container. + //! + //! @return a reference to the values container. + [[nodiscard]] constexpr auto values() const noexcept -> mapped_container_type const & + { + return m_containers.values; + } + private: containers m_containers; key_compare m_comparator; diff --git a/libs/kstd/kstd/flat_map.test.cpp b/libs/kstd/kstd/flat_map.test.cpp index 2e5a47c..6c57173 100644 --- a/libs/kstd/kstd/flat_map.test.cpp +++ b/libs/kstd/kstd/flat_map.test.cpp @@ -20,6 +20,16 @@ SCENARIO("Flat Map initialization and construction", "[flat_map]") { REQUIRE_FALSE(map.contains(1)); } + + THEN("the keys container is empty") + { + REQUIRE(map.keys().empty()); + } + + THEN("the values container is empty") + { + REQUIRE(map.values().empty()); + } } } } -- cgit v1.2.3 From 46d3f8978e9f4235064daf5f19de5bf3054e7c24 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 2 Jun 2026 16:38:30 +0200 Subject: acpi: fix two lint issues --- libs/acpi/acpi/common/basic_table.hpp | 5 +++++ libs/acpi/acpi/common/vla_table.hpp | 5 +++++ 2 files changed, 10 insertions(+) (limited to 'libs') diff --git a/libs/acpi/acpi/common/basic_table.hpp b/libs/acpi/acpi/common/basic_table.hpp index 33f23d5..f5b5b27 100644 --- a/libs/acpi/acpi/common/basic_table.hpp +++ b/libs/acpi/acpi/common/basic_table.hpp @@ -28,6 +28,11 @@ namespace acpi { return signature() == table_signature_v && validate_checksum(); } + + private: + friend TableType; + + constexpr basic_table() noexcept = default; }; } // namespace acpi diff --git a/libs/acpi/acpi/common/vla_table.hpp b/libs/acpi/acpi/common/vla_table.hpp index a65a28e..d3f33a7 100644 --- a/libs/acpi/acpi/common/vla_table.hpp +++ b/libs/acpi/acpi/common/vla_table.hpp @@ -109,6 +109,11 @@ namespace acpi { return end(); } + + private: + friend TableType; + + constexpr vla_table() noexcept = default; }; } // namespace acpi -- cgit v1.2.3