| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_FORMAT_SIMPLE_INTEGER_HPP |
| 4 |
#define MPT_FORMAT_SIMPLE_INTEGER_HPP |
| 5 |
|
| 6 |
|
| 7 |
#include "mpt/base/detect.hpp" |
| 8 |
|
| 9 |
#if !defined(MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT) |
| 10 |
#define MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 1 |
| 11 |
#else // MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT |
| 12 |
#define MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 0 |
| 13 |
#endif // !MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT |
| 14 |
|
| 15 |
#include "mpt/base/algorithm.hpp" |
| 16 |
#include "mpt/base/namespace.hpp" |
| 17 |
#include "mpt/format/helpers.hpp" |
| 18 |
#include "mpt/format/simple_spec.hpp" |
| 19 |
#include "mpt/string/types.hpp" |
| 20 |
|
| 21 |
#if !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 22 |
#include <algorithm> |
| 23 |
#endif // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 24 |
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 25 |
#include <charconv> |
| 26 |
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 27 |
#if !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 28 |
#include <ios> |
| 29 |
#include <locale> |
| 30 |
#endif // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 31 |
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 32 |
#include <string> |
| 33 |
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 34 |
#if !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 35 |
#include <sstream> |
| 36 |
#endif // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 37 |
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 38 |
#include <system_error> |
| 39 |
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 40 |
#include <type_traits> |
| 41 |
|
| 42 |
#include <cstddef> |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
namespace mpt { |
| 47 |
inline namespace MPT_INLINE_NS { |
| 48 |
|
| 49 |
|
| 50 |
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 51 |
|
| 52 |
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true> |
| 53 |
inline Tstring format_simple_integer_to_chars(const T & x, int base) { |
| 54 |
std::string str(1, '\0'); |
| 55 |
bool done = false; |
| 56 |
while (!done) { |
| 57 |
if constexpr (std::is_same<T, bool>::value) { |
| 58 |
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), static_cast<int>(x), base); |
| 59 |
if (result.ec != std::errc{}) { |
| 60 |
str.resize(mpt::exponential_grow(str.size()), '\0'); |
| 61 |
} else { |
| 62 |
str.resize(result.ptr - str.data()); |
| 63 |
done = true; |
| 64 |
} |
| 65 |
} else { |
| 66 |
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x, base); |
| 67 |
if (result.ec != std::errc{}) { |
| 68 |
str.resize(mpt::exponential_grow(str.size()), '\0'); |
| 69 |
} else { |
| 70 |
str.resize(result.ptr - str.data()); |
| 71 |
done = true; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
return mpt::convert_formatted_simple<Tstring>(str); |
| 76 |
} |
| 77 |
|
| 78 |
#else // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 79 |
|
| 80 |
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true> |
| 81 |
inline Tstring format_simple_integer_to_stream(const T & x, int base) { |
| 82 |
using stream_char_type = typename mpt::select_format_char_type<typename Tstring::value_type>::type; |
| 83 |
if ((base == 10) || ((base == 16) && std::is_unsigned<T>::value) || ((base == 8) && std::is_unsigned<T>::value)) { |
| 84 |
// fast path |
| 85 |
std::basic_ostringstream<stream_char_type> s; |
| 86 |
s.imbue(std::locale::classic()); |
| 87 |
if (base == 16) { |
| 88 |
s << std::hex; |
| 89 |
} else if (base == 8) { |
| 90 |
s << std::oct; |
| 91 |
} |
| 92 |
if constexpr (std::is_same<T, bool>::value) { |
| 93 |
s << static_cast<int>(x); |
| 94 |
} else if constexpr (mpt::is_character<T>::value) { |
| 95 |
s << (x + 0); // force integral promotion |
| 96 |
} else { |
| 97 |
s << x; |
| 98 |
} |
| 99 |
return mpt::convert_formatted_simple<Tstring>(s.str()); |
| 100 |
} else { |
| 101 |
if constexpr (std::is_same<T, bool>::value) { |
| 102 |
return x ? Tstring(1, mpt::char_constants<typename Tstring::value_type>::number1) : Tstring(1, mpt::char_constants<typename Tstring::value_type>::number0); |
| 103 |
} else if constexpr (std::is_unsigned<T>::value) { |
| 104 |
Tstring result; |
| 105 |
T val = x; |
| 106 |
if (val == 0) { |
| 107 |
result += Tstring(1, mpt::char_constants<typename Tstring::value_type>::number0); |
| 108 |
} else { |
| 109 |
using Tunsigned = typename std::make_unsigned<T>::type; |
| 110 |
while (val > 0) { |
| 111 |
Tunsigned digit = static_cast<Tunsigned>(val % static_cast<unsigned int>(base)); |
| 112 |
val = static_cast<Tunsigned>(val / static_cast<unsigned int>(base)); |
| 113 |
if (digit >= 10) { |
| 114 |
result += Tstring(1, static_cast<typename Tstring::value_type>(mpt::char_constants<typename Tstring::value_type>::a - 10 + digit)); |
| 115 |
} else { |
| 116 |
result += Tstring(1, static_cast<typename Tstring::value_type>(mpt::char_constants<typename Tstring::value_type>::number0 + digit)); |
| 117 |
} |
| 118 |
} |
| 119 |
std::reverse(result.begin(), result.end()); |
| 120 |
} |
| 121 |
return result; |
| 122 |
} else { |
| 123 |
Tstring result; |
| 124 |
if (x == 0) { |
| 125 |
result += Tstring(1, mpt::char_constants<typename Tstring::value_type>::number0); |
| 126 |
} else { |
| 127 |
using Tunsigned = typename std::make_unsigned<T>::type; |
| 128 |
Tunsigned val = (x != -x) ? ((x >= 0) ? x : -x) : (static_cast<Tunsigned>(-(x + 1)) + 1); |
| 129 |
while (val > 0) { |
| 130 |
Tunsigned digit = static_cast<Tunsigned>(val % static_cast<unsigned int>(base)); |
| 131 |
val = static_cast<Tunsigned>(val / static_cast<unsigned int>(base)); |
| 132 |
if (digit >= 10) { |
| 133 |
result += Tstring(1, static_cast<typename Tstring::value_type>(mpt::char_constants<typename Tstring::value_type>::a - 10 + digit)); |
| 134 |
} else { |
| 135 |
result += Tstring(1, static_cast<typename Tstring::value_type>(mpt::char_constants<typename Tstring::value_type>::number0 + digit)); |
| 136 |
} |
| 137 |
} |
| 138 |
if (x < 0) { |
| 139 |
result += Tstring(1, mpt::char_constants<typename Tstring::value_type>::minus); |
| 140 |
} |
| 141 |
std::reverse(result.begin(), result.end()); |
| 142 |
} |
| 143 |
return result; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 149 |
|
| 150 |
|
| 151 |
template <typename Tstring> |
| 152 |
inline Tstring format_simple_integer_postprocess_case(Tstring str, const format_simple_spec<Tstring> & format) { |
| 153 |
format_simple_flags f = format.GetFlags(); |
| 154 |
if (f & format_simple_base::CaseUpp) { |
| 155 |
for (auto & c : str) { |
| 156 |
if (mpt::char_constants<typename Tstring::value_type>::a <= c && c <= mpt::char_constants<typename Tstring::value_type>::z) { |
| 157 |
c -= mpt::char_constants<typename Tstring::value_type>::a - mpt::char_constants<typename Tstring::value_type>::A; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
return str; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
template <typename Tstring> |
| 166 |
inline Tstring format_simple_integer_postprocess_digits(Tstring str, const format_simple_spec<Tstring> & format) { |
| 167 |
format_simple_flags f = format.GetFlags(); |
| 168 |
std::size_t width = format.GetWidth(); |
| 169 |
if (f & format_simple_base::FillNul) { |
| 170 |
auto pos = str.begin(); |
| 171 |
if (str.length() > 0) { |
| 172 |
if (str[0] == mpt::char_constants<typename Tstring::value_type>::plus) { |
| 173 |
pos++; |
| 174 |
width++; |
| 175 |
} else if (str[0] == mpt::char_constants<typename Tstring::value_type>::minus) { |
| 176 |
pos++; |
| 177 |
width++; |
| 178 |
} |
| 179 |
} |
| 180 |
if (str.length() < width) { |
| 181 |
str.insert(pos, width - str.length(), mpt::char_constants<typename Tstring::value_type>::number0); |
| 182 |
} |
| 183 |
} |
| 184 |
return str; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
#if MPT_COMPILER_MSVC |
| 189 |
#pragma warning(push) |
| 190 |
#pragma warning(disable : 4723) // potential divide by 0 |
| 191 |
#endif // MPT_COMPILER_MSVC |
| 192 |
template <typename Tstring> |
| 193 |
inline Tstring format_simple_integer_postprocess_group(Tstring str, const format_simple_spec<Tstring> & format) { |
| 194 |
if (format.GetGroup() > 0) { |
| 195 |
const unsigned int groupSize = format.GetGroup(); |
| 196 |
const Tstring groupSep = format.GetGroupSep(); |
| 197 |
std::size_t len = str.length(); |
| 198 |
for (std::size_t n = 0; n < len; ++n) { |
| 199 |
if (n > 0 && (n % groupSize) == 0) { |
| 200 |
if (!(n == (len - 1) && (str[0] == mpt::char_constants<typename Tstring::value_type>::plus || str[0] == mpt::char_constants<typename Tstring::value_type>::minus))) { |
| 201 |
str.insert(len - n, groupSep); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
return str; |
| 207 |
} |
| 208 |
#if MPT_COMPILER_MSVC |
| 209 |
#pragma warning(pop) |
| 210 |
#endif // MPT_COMPILER_MSVC |
| 211 |
|
| 212 |
|
| 213 |
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 214 |
|
| 215 |
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true> |
| 216 |
inline Tstring format_simple(const T & x, const format_simple_spec<Tstring> & format) { |
| 217 |
int base = 10; |
| 218 |
if (format.GetFlags() & format_simple_base::BaseDec) { |
| 219 |
base = 10; |
| 220 |
} |
| 221 |
if (format.GetFlags() & format_simple_base::BaseHex) { |
| 222 |
base = 16; |
| 223 |
} |
| 224 |
using format_string_type = typename mpt::select_format_string_type<Tstring>::type; |
| 225 |
const format_simple_spec<format_string_type> f = mpt::transcode_format_simple_spec<format_string_type>(format); |
| 226 |
return mpt::transcode<Tstring>(mpt::format_simple_integer_postprocess_group(mpt::format_simple_integer_postprocess_digits(mpt::format_simple_integer_postprocess_case(mpt::format_simple_integer_to_chars<format_string_type>(x, base), f), f), f)); |
| 227 |
} |
| 228 |
|
| 229 |
#else // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 230 |
|
| 231 |
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true> |
| 232 |
inline Tstring format_simple(const T & x, const format_simple_spec<Tstring> & format) { |
| 233 |
int base = 10; |
| 234 |
if (format.GetFlags() & format_simple_base::BaseDec) { |
| 235 |
base = 10; |
| 236 |
} |
| 237 |
if (format.GetFlags() & format_simple_base::BaseHex) { |
| 238 |
base = 16; |
| 239 |
} |
| 240 |
using format_string_type = typename mpt::select_format_string_type<Tstring>::type; |
| 241 |
const format_simple_spec<format_string_type> f = mpt::transcode_format_simple_spec<format_string_type>(format); |
| 242 |
return mpt::transcode<Tstring>(mpt::format_simple_integer_postprocess_group(mpt::format_simple_integer_postprocess_digits(mpt::format_simple_integer_postprocess_case(mpt::format_simple_integer_to_stream<format_string_type>(x, base), f), f), f)); |
| 243 |
} |
| 244 |
|
| 245 |
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 |
| 246 |
|
| 247 |
|
| 248 |
} // namespace MPT_INLINE_NS |
| 249 |
} // namespace mpt |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
#endif // MPT_FORMAT_SIMPLE_INTEGER_HPP |