Skip to content

Commit d680c02

Browse files
authored
Merge pull request #1 from EnilPajic/patch-1-negative-minimum-value
Fixed potential bug with -MINIMUM_VALUE
2 parents 6ae484c + 6e79390 commit d680c02

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

to_string.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
* @tparam N Number to convert
1616
* @tparam base Desired base, can be from 2 to 36
1717
*/
18-
template<auto N, unsigned int base, typename char_type,
18+
template<auto N, int base, typename char_type,
1919
std::enable_if_t<std::is_integral_v<decltype(N)>, int> = 0,
2020
std::enable_if_t<(base > 1 && base < 37), int> = 0>
2121
class to_string_t {
2222
// The lambda calculates what the string length of N will be, so that `buf`
2323
// fits to the number perfectly.
2424
char_type buf[([]() constexpr noexcept {
2525
unsigned int len = N > 0 ? 1 : 2;
26-
for (auto n = N < 0 ? -N : N; n; len++, n /= base);
26+
for (auto n = N; n; len++, n /= base);
2727
return len;
2828
}())] = {};
2929

@@ -35,8 +35,8 @@ class to_string_t {
3535
auto ptr = end();
3636
*--ptr = '\0';
3737
if (N != 0) {
38-
for (auto n = N < 0 ? -N : N; n; n /= base)
39-
*--ptr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n % base];
38+
for (auto n = N; n; n /= base)
39+
*--ptr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[(N < 0 ? -1 : 1) * (n % base)];
4040
if (N < 0)
4141
*--ptr = '-';
4242
} else {

0 commit comments

Comments
 (0)