diff options
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/kstd/string | 17 | ||||
| -rw-r--r-- | libs/kstd/kstd/string.test.cpp | 18 |
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"}); + } + } + } +} |
