support c++14
This commit is contained in:
parent
3234772a90
commit
bd157e503a
51
README.md
51
README.md
@ -1,10 +1,12 @@
|
||||
# constexpr-to-string
|
||||
|
||||
Requires C++14 or later.
|
||||
|
||||
**Features:**
|
||||
|
||||
* Convert any integral type to a string at compile-time
|
||||
* Supports converting to any base between 2 and 36 inclusive
|
||||
* No external dependencies, only includes `type_traits` for template parameter checking
|
||||
* Supports converting to bases 2 through 36
|
||||
* No external dependencies
|
||||
* Supports custom character types, e.g. `to_string<123, 10, wchar_t>`
|
||||
* C++20: Supports floating-point-to-string conversion with `f_to_string`
|
||||
|
||||
@ -32,48 +34,11 @@ puts(f_to_string<{3.1415926, 7}>); // Specify precision: "3.1415926"
|
||||
|
||||
# How it works
|
||||
|
||||
The basic structure of `to_string` is shown below:
|
||||
C++14 greatly expanded the capabilities of compile-time code execution through `constexpr`. In particular, it allows for non-trivial constructors to be `constexpr`.
|
||||
|
||||
```cpp
|
||||
template<auto N, unsigned int base, typename char_type, /* N type-check and base bounds-check */>
|
||||
struct to_string_t {
|
||||
char_type buf[]; // Array size determination explained later.
|
||||
constexpr to_string_t() {} // Converts the integer to a string stored in buf.
|
||||
constexpr operator char_type *() {} // These allow for the object to be implicitly converted
|
||||
constexpr operator const char_type *() {} // to a character pointer.
|
||||
|
||||
// begin() and end() are supported too.
|
||||
};
|
||||
`to_string` takes advantage of this by providing an object that converts a template-parameter integer to a string using a basic `itoa` implementation in the constructor. Through an additional `constexpr` member function, we can calculate the length of the resulting string; this can be used to size the object's string buffer for a perfect fit.
|
||||
|
||||
template<auto N, unsigned int base = 10, typename char_type = char>
|
||||
constexpr to_string_t<N, base, char_type> to_string; // Simplifies usage, e.g. to_string_t<367>() becomes to_string<367>.
|
||||
```
|
||||
Beyond this, `to_string` simply provides familiar member functions that allow for iteration and data access. The expansion of the capabilities of `auto` in C++14 help make these definitions concise.
|
||||
|
||||
Since the number and base are template parameters, each differing `to_string` use will get its own character buffer.
|
||||
The floating-point implementation `f_to_string` takes a similar approach, but requires C++20 as it needs a `double_wrapper` object to capture the `double` value. `double` and `float` cannot directly be template parameters as of C++20, and a non-type template parameter like the `double_wrapper` structure was not allowed before C++20.
|
||||
|
||||
The integer/string conversion is done using a simple method I learned over the years, where the string is built in reverse using `n % base` to calculate the value of the lowest digit:
|
||||
|
||||
(*Note: The below examples of code are not up-to-date, though they still give a general idea of how `to_string` works.*)
|
||||
|
||||
```cpp
|
||||
constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
constexpr to_string_t() {
|
||||
auto ptr = buf + sizeof(buf) / sizeof(buf[0]);
|
||||
*--ptr = '\0';
|
||||
for (auto n = N < 0 ? -N : N; n; n /= base)
|
||||
*--ptr = digits[n % base];
|
||||
if (N < 0)
|
||||
*--ptr = '-';
|
||||
}
|
||||
```
|
||||
|
||||
As you may have noticed, `buf` needs to be given a size for all this to work; in fact, the above code relies on the buffer having a size equal to the generated string (or else `buf[0]` would still be uninitialized). This is actually the case: a lambda is used within `buf`'s declaration to count how many characters long the string will ultimately be. This counting is done in a manner similar to conversion loop shown above:
|
||||
|
||||
```cpp
|
||||
char buf[([] {
|
||||
unsigned int len = N >= 0 ? 1 : 2; // Need one byte for '\0', two if there'll be a minus
|
||||
for (auto n = N < 0 ? -N : N; n; len++, n /= base);
|
||||
return len;
|
||||
}())];
|
||||
```
|
||||
|
@ -7,11 +7,12 @@
|
||||
#ifndef TCSULLIVAN_TO_STRING_HPP_
|
||||
#define TCSULLIVAN_TO_STRING_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
namespace constexpr_to_string {
|
||||
|
||||
inline constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
/**
|
||||
* @struct to_string_t
|
||||
@ -19,17 +20,17 @@ inline constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
* @tparam N Number to convert
|
||||
* @tparam base Desired base, can be from 2 to 36
|
||||
*/
|
||||
template<auto N, int base, typename char_type,
|
||||
std::enable_if_t<std::is_integral_v<decltype(N)>, int> = 0,
|
||||
template<std::intmax_t N, int base, typename char_type,
|
||||
std::enable_if_t<(base > 1 && base < sizeof(digits)), int> = 0>
|
||||
class to_string_t {
|
||||
// The lambda calculates what the string length of N will be, so that `buf`
|
||||
// fits to the number perfectly.
|
||||
char_type buf[([]() constexpr noexcept {
|
||||
unsigned int len = N > 0 ? 1 : 2;
|
||||
for (auto n = N; n; len++, n /= base);
|
||||
return len;
|
||||
}())] = {};
|
||||
|
||||
constexpr static auto buflen() noexcept {
|
||||
unsigned int len = N > 0 ? 1 : 2;
|
||||
for (auto n = N; n; len++, n /= base);
|
||||
return len;
|
||||
}
|
||||
|
||||
char_type buf[buflen()] = {};
|
||||
|
||||
public:
|
||||
/**
|
||||
@ -38,6 +39,7 @@ class to_string_t {
|
||||
constexpr to_string_t() noexcept {
|
||||
auto ptr = end();
|
||||
*--ptr = '\0';
|
||||
|
||||
if (N != 0) {
|
||||
for (auto n = N; n; n /= base)
|
||||
*--ptr = digits[(N < 0 ? -1 : 1) * (n % base)];
|
||||
@ -53,6 +55,7 @@ class to_string_t {
|
||||
constexpr operator const char_type *() const noexcept { return buf; }
|
||||
|
||||
constexpr auto size() const noexcept { return sizeof(buf) / sizeof(buf[0]); }
|
||||
|
||||
// Element access
|
||||
constexpr auto data() noexcept { return buf; }
|
||||
constexpr const auto data() const noexcept { return buf; }
|
||||
@ -62,6 +65,7 @@ class to_string_t {
|
||||
constexpr const auto& front() const noexcept { return buf[0]; }
|
||||
constexpr auto& back() noexcept { return buf[size() - 1]; }
|
||||
constexpr const auto& back() const noexcept { return buf[size() - 1]; }
|
||||
|
||||
// Iterators
|
||||
constexpr auto begin() noexcept { return buf; }
|
||||
constexpr const auto begin() const noexcept { return buf; }
|
||||
@ -74,7 +78,8 @@ class to_string_t {
|
||||
/**
|
||||
* Simplifies use of `to_string_t` from `to_string_t<N>()` to `to_string<N>`.
|
||||
*/
|
||||
template<auto N, int base = 10, typename char_type = char>
|
||||
template<std::intmax_t N, int base = 10, typename char_type = char>
|
||||
constexpr constexpr_to_string::to_string_t<N, base, char_type> to_string;
|
||||
|
||||
#endif // TCSULLIVAN_TO_STRING_HPP_
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user