aboutsummaryrefslogtreecommitdiffstats
path: root/to_string.hpp
diff options
context:
space:
mode:
authorclyne <clyne@bitgloo.com>2020-06-26 17:47:48 -0400
committerGitHub <noreply@github.com>2020-06-26 17:47:48 -0400
commit1d8ef0361c0636976f5fd835ef20101385f28de3 (patch)
treed1e7bcb9915e35dc2366763284ba8b561f2d43f3 /to_string.hpp
parent4cffc3ffbc96df9fbaabf67bf82652340b6af856 (diff)
Forgot to support to_string<0>
Diffstat (limited to 'to_string.hpp')
-rw-r--r--to_string.hpp18
1 files changed, 11 insertions, 7 deletions
diff --git a/to_string.hpp b/to_string.hpp
index 15df021..445c97f 100644
--- a/to_string.hpp
+++ b/to_string.hpp
@@ -22,7 +22,7 @@ struct to_string_t {
// The lambda calculates what the string length of N will be, so that `buf`
// fits to the number perfectly.
char buf[([] {
- unsigned int len = N >= 0 ? 1 : 2;
+ unsigned int len = N > 0 ? 1 : 2;
for (auto n = N < 0 ? -N : N; n; len++, n /= base);
return len;
}())] = {};
@@ -31,12 +31,16 @@ struct to_string_t {
* Constructs the object, filling `buf` with the string representation of N.
*/
constexpr to_string_t() noexcept {
- auto ptr = buf + sizeof(buf) / sizeof(buf[0]);
- *--ptr = '\0';
- for (auto n = N < 0 ? -N : N; n; n /= base)
- *--ptr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n % base];
- if (N < 0)
- *--ptr = '-';
+ if (N != 0) {
+ auto ptr = buf + sizeof(buf) / sizeof(buf[0]);
+ *--ptr = '\0';
+ for (auto n = N < 0 ? -N : N; n; n /= base)
+ *--ptr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n % base];
+ if (N < 0)
+ *--ptr = '-';
+ } else {
+ buf[0] = '0';
+ }
}
/**