aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-07-01 10:20:12 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-07-01 10:20:12 +0200
commitab8c37673ca247c190d60147dff66c958ec960de (patch)
tree7bb83f4adbd7117a199e0e8fe10263e273ae317d
parent278256683f3be86d21e008670ab1bc1b76cdc3b1 (diff)
downloadkernel-ab8c37673ca247c190d60147dff66c958ec960de.tar.xz
kernel-ab8c37673ca247c190d60147dff66c958ec960de.zip
kstd: implement _s UDL operator for string.
-rw-r--r--libs/kstd/kstd/string17
-rw-r--r--libs/kstd/kstd/string.test.cpp18
2 files changed, 35 insertions, 0 deletions
diff --git a/libs/kstd/kstd/string b/libs/kstd/kstd/string
index 682b8404..4a67a3a4 100644
--- a/libs/kstd/kstd/string
+++ b/libs/kstd/kstd/string
@@ -12,6 +12,7 @@
#include <kstd/vector>
#include <concepts>
+#include <cstddef>
namespace kstd
{
@@ -38,6 +39,22 @@ namespace kstd
}
};
+ inline namespace literals
+ {
+ inline namespace string_literals
+ {
+ //! Create a string new string from a literal C-style string.
+ //!
+ //! @param string The C-style string to use as a source for the new string.
+ //! @param length The length of the supplied C-style string.
+ //! @return a new string object, containing the same value as the provided C-style string.
+ constexpr auto operator""_s(char const * string, std::size_t length) -> kstd::string
+ {
+ return {string, length};
+ }
+ } // namespace string_literals
+ } // namespace literals
+
} // namespace kstd
#endif \ No newline at end of file
diff --git a/libs/kstd/kstd/string.test.cpp b/libs/kstd/kstd/string.test.cpp
index 2de8b513..27ca95b3 100644
--- a/libs/kstd/kstd/string.test.cpp
+++ b/libs/kstd/kstd/string.test.cpp
@@ -2977,3 +2977,21 @@ SCENARIO("String STL integration", "[string]")
}
}
}
+
+SCENARIO("String user-defined literals")
+{
+ GIVEN("the user defined literals are in scope")
+ {
+ using namespace kstd::string_literals;
+
+ WHEN("creating a string using the _s literal suffix")
+ {
+ auto s = "abcd"_s;
+
+ THEN("the result is equal to the string created with the same literal")
+ {
+ REQUIRE(s == kstd::string{"abcd"});
+ }
+ }
+ }
+}