diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-06-24 13:10:40 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-06-24 13:10:40 +0200 |
| commit | b41581ed656dde74b7a7b961b8a5c08bc61fb7d4 (patch) | |
| tree | 52b963a5c3c5475e248a345392695025e8b2b828 /libs/kstd | |
| parent | 08bfe2af099f6acc78772e7b315a6cf922626b76 (diff) | |
| download | kernel-b41581ed656dde74b7a7b961b8a5c08bc61fb7d4.tar.xz kernel-b41581ed656dde74b7a7b961b8a5c08bc61fb7d4.zip | |
kstd: simplify kstd::to_string
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/kstd/string | 30 |
1 files changed, 7 insertions, 23 deletions
diff --git a/libs/kstd/kstd/string b/libs/kstd/kstd/string index de254a1d..682b8404 100644 --- a/libs/kstd/kstd/string +++ b/libs/kstd/kstd/string @@ -5,11 +5,12 @@ #include <kstd/bits/format/context.hpp> #include <kstd/bits/format/formatter.hpp> #include <kstd/bits/format/formatter/cstring.hpp> +#include <kstd/bits/format/formatter/integral.hpp> +#include <kstd/bits/format/vformat.hpp> #include <kstd/cstring> #include <kstd/os/error.hpp> #include <kstd/vector> -#include <algorithm> #include <concepts> namespace kstd @@ -17,32 +18,15 @@ namespace kstd using string = basic_string<char>; /** - * @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. + * @brief Converts an integer to a string as if by kstd::format("{}", value). + * @tparam IntegralType 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 + template<std::integral IntegralType> + [[nodiscard]] constexpr auto inline to_string(IntegralType 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; + return kstd::format("{}", value); } template<> |
