diff options
author | clyne <clyne@bitgloo.com> | 2020-06-26 13:12:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-26 13:12:14 -0400 |
commit | 796eb3e7ee4a700e497eac94c6f76c6409162aea (patch) | |
tree | 5253c102f2b7fe41ddb8a021e582654a27d2b155 | |
parent | aa85b760a89452c7a07830860b9ab3aa6e51ff09 (diff) |
Remove redundant length calculation; expand base support to 36
-rw-r--r-- | to_string.hpp | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/to_string.hpp b/to_string.hpp index 9565b64..116cdd8 100644 --- a/to_string.hpp +++ b/to_string.hpp @@ -13,9 +13,11 @@ * @struct to_string_t * @brief Provides the ability to convert any integral to a string at compile-time. * @tparam N Number to convert - * @tparam base Desired base, can be from 2 to 16 (though no checking is done) + * @tparam base Desired base, can be from 2 to 36 */ -template<auto N, unsigned int base = 10, std::enable_if_t<std::is_integral_v<decltype(N)>, int> = 0> +template<auto N, unsigned int base, + std::enable_if_t<std::is_integral_v<decltype(N)>, int> = 0, + std::enable_if_t<(base > 1 && base < 37), int> = 0> struct to_string_t { // The lambda calculates what the string length of N will be, so that `buf` // fits to the number perfectly. @@ -29,13 +31,10 @@ struct to_string_t { * Constructs the object, filling `buf` with the string representation of N. */ constexpr to_string_t() { - unsigned int len = N >= 0 ? 1 : 2; - for (auto n = N < 0 ? -N : N; n; len++, n /= base); - - auto ptr = buf + len; + auto ptr = buf + sizeof(buf) / sizeof(buf[0]); *--ptr = '\0'; for (auto n = N < 0 ? -N : N; n; n /= base) - *--ptr = "0123456789ABCDEF"[n % base]; + *--ptr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n % base]; if (N < 0) *--ptr = '-'; } |