]> code.bitgloo.com Git - clyne/constexpr-to-string.git/commitdiff
Forgot to support to_string<0>
authorclyne <clyne@bitgloo.com>
Fri, 26 Jun 2020 21:47:48 +0000 (17:47 -0400)
committerGitHub <noreply@github.com>
Fri, 26 Jun 2020 21:47:48 +0000 (17:47 -0400)
to_string.hpp

index 15df021c1fb27ba31b4a208cb960f1005be7da84..445c97f6cc421dd0ceec42e9166a3fd6e2408c00 100644 (file)
@@ -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';
+        }
     }
 
     /**