]> code.bitgloo.com Git - clyne/constexpr-to-string.git/commitdiff
Remove redundant length calculation; expand base support to 36
authorclyne <clyne@bitgloo.com>
Fri, 26 Jun 2020 17:12:14 +0000 (13:12 -0400)
committerGitHub <noreply@github.com>
Fri, 26 Jun 2020 17:12:14 +0000 (13:12 -0400)
to_string.hpp

index 9565b64a586d19add0c165e35fadec8016d1bbaf..116cdd8f8c498d31b57e6d9fbb1a7626e34c3999 100644 (file)
  * @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 = '-';
     }