51#include <system_error>
98 std::size_t index = 0;
99 const auto digit = [](Char unit)
noexcept {
100 return unit >=
static_cast<Char
>(
'0') && unit <=
static_cast<Char
>(
'9');
102 if (index < count && data[index] ==
static_cast<Char
>(
':'))
106 if (index < count && data[index] ==
static_cast<Char
>(
'0')) {
110 if (index < count && digit(data[index])) {
113 while (index < count && digit(data[index])) {
114 const unsigned next =
static_cast<unsigned>(data[index] -
static_cast<Char
>(
'0'));
115 if (width > (100000u - next) / 10u)
117 width = width * 10u + next;
122 if (index < count && data[index] ==
static_cast<Char
>(
'.')) {
126 while (index < count && digit(data[index])) {
127 precision = precision * 10 +
static_cast<int>(data[index] -
static_cast<Char
>(
'0'));
135 const Char type = data[index++];
136 if (type !=
static_cast<Char
>(
'd') && type !=
static_cast<Char
>(
'f') && type !=
static_cast<Char
>(
'g') && type !=
static_cast<Char
>(
'G') && type !=
static_cast<Char
>(
'x') && type !=
static_cast<Char
>(
'X'))
138 spec.
type =
static_cast<char>(type);
156 std::size_t found = 0;
157 for (std::size_t index = 0; index < text.size() && found < capacity; ++index) {
158 if (text[index] !=
static_cast<Char
>(
'{'))
160 const std::size_t close = text.find(
static_cast<Char
>(
'}'), index + 1);
161 if (close == std::basic_string_view<Char>::npos)
163 const auto inside = text.substr(index + 1, close - index - 1);
164 if (inside.find(
static_cast<Char
>(
'{')) != std::basic_string_view<Char>::npos)
177 inline void AppendHexDigits(std::string& out, std::uintmax_t value,
bool upper) {
182 char digits[
sizeof(std::uintmax_t) * 2];
184 const char* alphabet = upper ?
"0123456789ABCDEF" :
"0123456789abcdef";
186 digits[count++] = alphabet[value & 0xF];
190 out.push_back(digits[--count]);
194 if (!spec.width_set || text.size() >= spec.width)
196 const std::size_t pad = spec.width - text.size();
198 const std::size_t at = (!text.empty() && (text[0] ==
'-' || text[0] ==
'+')) ? 1 : 0;
199 text.insert(at, pad,
'0');
201 text.insert(0, pad,
' ');
205 void AssignAscii(std::basic_string<Char>& out, std::string_view ascii) {
206 out.assign(ascii.begin(), ascii.end());
212 if (spec.type ==
'x' || spec.type ==
'X') {
213 std::uintmax_t bits = 0;
214 if constexpr (Type::Signed<Int>)
215 bits =
static_cast<std::make_unsigned_t<Int>
>(value);
217 bits =
static_cast<std::uintmax_t
>(value);
218 AppendHexDigits(ascii, bits, spec.type ==
'X');
221 const std::to_chars_result result = std::to_chars(buffer, buffer +
sizeof(buffer), value);
222 ascii.assign(buffer, result.ptr);
224 PadAscii(ascii, spec);
228 template<
class Float>
230 const int precision = spec.precision_set ? spec.precision : 6;
231 std::chars_format format = std::chars_format::general;
232 if (spec.type ==
'f')
233 format = std::chars_format::fixed;
235 const std::to_chars_result result = std::to_chars(buffer, buffer +
sizeof(buffer), value, format, precision);
236 std::string ascii(buffer, result.ptr == buffer ? buffer : result.ptr);
237 if (result.ec != std::errc{})
238 ascii = std::to_string(
static_cast<double>(value));
239 if (spec.type ==
'G') {
240 for (
char& unit : ascii) {
241 if (unit >=
'a' && unit <=
'z')
242 unit =
static_cast<char>(unit -
'a' +
'A');
245 PadAscii(ascii, spec);
257 template<
class Char,
class T>
278 template<
class Char,
class T>
280 using Bare = std::remove_cvref_t<T>;
282 AssignAscii(out, value ? std::string_view(
"true") : std::string_view(
"false"));
285 if (value ==
nullptr)
288 out.assign(std::basic_string_view<Char>(value));
291 const void* address =
nullptr;
293 address =
reinterpret_cast<const void*
>(value);
295 address =
static_cast<const void*
>(value.get());
297 std::string ascii =
"0x";
298 AppendHexDigits(ascii,
reinterpret_cast<std::uintptr_t
>(address),
false);
299 AssignAscii(out, ascii);
301 AssignAscii(out, FormatIntegerAscii(
reinterpret_cast<std::uintptr_t
>(address), spec));
305 out.assign(1,
static_cast<char>(value));
307 AssignAscii(out, FormatIntegerAscii(
static_cast<int>(
static_cast<unsigned char>(value)), spec));
311 out.assign(1,
static_cast<wchar_t>(value));
313 AssignAscii(out, FormatIntegerAscii(
static_cast<std::uint32_t
>(value), spec));
316 out.assign(std::basic_string_view<Char>(std::forward<T>(value)));
321 out = std::to_wstring(value);
323 out = std::to_string(value);
324 }
else if (spec.
type ==
'x' || spec.
type ==
'X' || spec.
type ==
'd')
325 AssignAscii(out, FormatIntegerAscii(
static_cast<long long>(value), spec));
327 AssignAscii(out, FormatFloatAscii(value, spec));
332 out = std::to_wstring(value);
334 out = std::to_string(value);
335 }
else if (spec.
type ==
'f' || spec.
type ==
'g' || spec.
type ==
'G')
336 AssignAscii(out, FormatFloatAscii(
static_cast<double>(value), spec));
338 AssignAscii(out, FormatIntegerAscii(value, spec));
341 const auto bits =
static_cast<std::uint64_t
>(std::forward<T>(value));
344 if (spec.
type ==
'f' || spec.
type ==
'g' || spec.
type ==
'G')
345 AssignAscii(out, FormatFloatAscii(
static_cast<double>(bits), spec));
347 AssignAscii(out, FormatIntegerAscii(bits, spec));
352 AssignAscii(out, FormatIntegerAscii(
static_cast<std::uint64_t
>(std::forward<T>(value)), spec));
True when FormatReplaceToken always writes T and never asks for a string conversion.
Definition replace_token.hxx:258
From is implicitly convertible to To (std::is_convertible).
Definition conversions.hxx:74
From may be explicitly converted to To with static_cast.
Definition conversions.hxx:86
Floating-point type (float, double, long double).
Definition categories.hxx:197
Integral type (bool, char, int, long, …), including cv.
Definition categories.hxx:185
Host integer or suite count type.
Definition categories.hxx:257
Raw (possibly cv-qualified) pointer type.
Definition categories.hxx:131
Same type after stripping cv and references from both sides.
Definition relations.hxx:139
Smart pointer-like type: dereferenceable and exposes get().
Definition categories.hxx:147
Owned values that cross a DLL without the caller's CRT.
bool FormatReplaceToken(std::basic_string< Char > &out, T &&value, const ReplaceTokenSpec &spec)
Render one argument when it does not need an owned-string conversion.
Definition replace_token.hxx:279
std::size_t FindReplaceTokens(std::basic_string_view< Char > text, ReplaceTokenPlace< Char > *places, std::size_t capacity) noexcept
Collect up to capacity fields, in order.
Definition replace_token.hxx:155
constexpr bool ParseReplaceTokenSpec(const Char *data, std::size_t count, ReplaceTokenSpec &out) noexcept
Parse the inside of one {...}.
Definition replace_token.hxx:96
Where a field sits in the pattern, and how to render its argument.
Definition replace_token.hxx:81
ReplaceTokenSpec spec
Definition replace_token.hxx:84
std::size_t offset
Definition replace_token.hxx:82
std::size_t length
Definition replace_token.hxx:83
One ReplaceToken field.
Definition replace_token.hxx:63
unsigned width
Definition replace_token.hxx:66
constexpr bool IsDefault() const noexcept
Definition replace_token.hxx:71
bool zero
Definition replace_token.hxx:64
int precision
Definition replace_token.hxx:68
bool width_set
Definition replace_token.hxx:65
bool precision_set
Definition replace_token.hxx:67
char type
Definition replace_token.hxx:69