diff options
| -rw-r--r-- | libs/kstd/include/kstd/string | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/libs/kstd/include/kstd/string b/libs/kstd/include/kstd/string index 62126a9..f9583d5 100644 --- a/libs/kstd/include/kstd/string +++ b/libs/kstd/include/kstd/string @@ -6,6 +6,7 @@ #include <kstd/vector> #include <algorithm> +#include <concepts> #include <cstddef> #include <string_view> @@ -55,6 +56,29 @@ namespace kstd } /** + * @brief Constructs a string from a null-terminated C-style string by copying the characters into owned storage. + * @param c_str The null-terminated C-style string to copy. + */ + string(char const * c_str) + : string() + { + if (c_str != nullptr) + { + append(std::string_view{c_str}); + } + } + + /** + * @brief Constructs a string containing a single character. + * @param c The character to copy. + */ + string(value_type c) + : string() + { + push_back(c); + } + + /** * @brief Constructs a string by copying another string. * @param other The string to copy. */ @@ -250,13 +274,42 @@ namespace kstd /** * @brief Concatenates a strings and a character and returns the result as a new string. * @param lhs The string to concatenate. - * @param c The character to concatenate. - * @return A new string that is the result of concatenating @p lhs and @p c. + * @param rhs The string to concatenate. + * @return A new string that is the result of concatenating @p lhs and @p rhs. */ - [[nodiscard]] constexpr auto inline operator+(string const & lhs, char const c) -> string + [[nodiscard]] constexpr auto inline operator+(string const & lhs, string const & rhs) -> string { string result{lhs}; - result += c; + result += rhs; + return result; + } + + /** + * @brief Converts an unsigned integer to a string by converting each digit to the corresponding character and + * concatenating them. + * @tparam N The type of the unsigned integer to convert. + * @param value The unsigned integer to convert. + * @return A string representation of the given unsigned integer. + */ + template<typename N> + requires std::unsigned_integral<N> + [[nodiscard]] constexpr auto inline to_string(N value) -> string + { + if (value == 0) + { + return "0"; + } + + string result; + + while (value > 0) + { + char const digit = '0' + (value % 10); + result.push_back(digit); + value /= 10; + } + + std::reverse(result.begin(), result.end()); return result; } } // namespace kstd |
