2016-08-03 22:51:05 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "types.h"
|
2018-09-05 18:57:52 +02:00
|
|
|
#include "asm.h"
|
2016-08-14 02:22:19 +02:00
|
|
|
#include <climits>
|
2016-08-03 22:51:05 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-08-05 18:49:45 +02:00
|
|
|
#include <algorithm>
|
2016-08-03 22:51:05 +02:00
|
|
|
|
2019-05-22 05:47:47 +02:00
|
|
|
static const size_t size_dropped = std::numeric_limits<size_t>::max();
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
/*
|
|
|
|
C-style format parser. Appends formatted string to `out`, returns number of characters written.
|
2016-08-05 18:49:45 +02:00
|
|
|
`out`: mutable reference to std::string, std::vector<char> or other compatible container
|
|
|
|
`fmt`: null-terminated string of `Char` type (char or constructible from char)
|
|
|
|
`src`: rvalue reference to argument provider.
|
2016-08-03 22:51:05 +02:00
|
|
|
*/
|
2016-08-05 18:49:45 +02:00
|
|
|
template<typename Dst, typename Char, typename Src>
|
|
|
|
std::size_t cfmt_append(Dst& out, const Char* fmt, Src&& src)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
const std::size_t start_pos = out.size();
|
2016-08-03 22:51:05 +02:00
|
|
|
|
|
|
|
struct cfmt_context
|
|
|
|
{
|
|
|
|
std::size_t size; // Size of current format sequence
|
|
|
|
|
|
|
|
u8 args; // Number of extra args used
|
|
|
|
u8 type; // Integral type bytesize
|
|
|
|
bool dot; // Precision enabled
|
|
|
|
bool left;
|
|
|
|
bool sign;
|
|
|
|
bool space;
|
|
|
|
bool alter;
|
|
|
|
bool zeros;
|
|
|
|
|
|
|
|
uint width;
|
|
|
|
uint prec;
|
|
|
|
};
|
|
|
|
|
|
|
|
cfmt_context ctx{0};
|
|
|
|
|
|
|
|
// Error handling: print untouched sequence, stop further formatting
|
|
|
|
const auto drop_sequence = [&]
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
out.insert(out.end(), fmt - ctx.size, fmt);
|
2019-05-22 05:47:47 +02:00
|
|
|
ctx.size = size_dropped;
|
2016-08-03 22:51:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto read_decimal = [&](uint result) -> uint
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
while (fmt[0] >= '0' && fmt[0] <= '9' && result <= (UINT_MAX / 10))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
result = result * 10 + (fmt[0] - '0');
|
|
|
|
fmt++, ctx.size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
const auto write_octal = [&](u64 value, u64 min_num)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2018-09-05 18:57:52 +02:00
|
|
|
out.resize(out.size() + std::max<u64>(min_num, 66 / 3 - (utils::cntlz64(value | 1, true) + 2) / 3), '0');
|
2016-08-03 22:51:05 +02:00
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
// Write in reversed order
|
|
|
|
for (auto i = out.rbegin(); value; i++, value /= 8)
|
|
|
|
{
|
|
|
|
*i = value % 8 + '0';
|
|
|
|
}
|
|
|
|
};
|
2016-08-03 22:51:05 +02:00
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
const auto write_hex = [&](u64 value, bool upper, u64 min_num)
|
|
|
|
{
|
2018-09-05 18:57:52 +02:00
|
|
|
out.resize(out.size() + std::max<u64>(min_num, 64 / 4 - utils::cntlz64(value | 1, true) / 4), '0');
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
// Write in reversed order
|
|
|
|
for (auto i = out.rbegin(); value; i++, value /= 16)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
*i = (upper ? "0123456789ABCDEF" : "0123456789abcdef")[value % 16];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto write_decimal = [&](u64 value, s64 min_size)
|
|
|
|
{
|
|
|
|
const std::size_t start = out.size();
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
out.push_back(value % 10 + '0');
|
|
|
|
value /= 10;
|
|
|
|
}
|
|
|
|
while (0 < --min_size || value);
|
|
|
|
|
|
|
|
// Revert written characters
|
|
|
|
for (std::size_t i = start, j = out.size() - 1; i < j; i++, j--)
|
|
|
|
{
|
|
|
|
std::swap(out[i], out[j]);
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Single pass over fmt string (null-terminated), TODO: check correct order
|
2016-08-05 18:49:45 +02:00
|
|
|
while (const Char ch = *fmt++) if (ctx.size == 0)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
if (ch == '%')
|
|
|
|
{
|
|
|
|
ctx.size = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
out.push_back(ch);
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ctx.size == 1 && ch == '%')
|
|
|
|
{
|
|
|
|
ctx = {0};
|
2016-08-05 18:49:45 +02:00
|
|
|
out.push_back(ch);
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
2019-05-22 05:47:47 +02:00
|
|
|
else if (ctx.size == size_dropped)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
out.push_back(ch);
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
else switch (ctx.size++, ch)
|
|
|
|
{
|
|
|
|
case '-': ctx.left = true; break;
|
|
|
|
case '+': ctx.sign = true; break;
|
|
|
|
case ' ': ctx.space = true; break;
|
|
|
|
case '#': ctx.alter = true; break;
|
|
|
|
case '0': ctx.zeros = true; break;
|
|
|
|
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.width))
|
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx.width = read_decimal(ch - '0');
|
|
|
|
}
|
2018-09-05 18:57:52 +02:00
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.width || !src.test(ctx.args)))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
2016-08-05 18:49:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const int warg = src.template get<int>(ctx.args++);
|
|
|
|
ctx.width = std::abs(warg);
|
|
|
|
ctx.left |= warg < 0;
|
|
|
|
}
|
2018-09-05 18:57:52 +02:00
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.dot || ctx.prec))
|
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
|
|
|
else if (*fmt >= '0' && *fmt <= '9') // TODO: does it allow '0'?
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
ctx.prec = read_decimal(0);
|
|
|
|
ctx.dot = true;
|
|
|
|
}
|
|
|
|
else if (*fmt == '*')
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(!src.test(ctx.args)))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
2016-08-05 18:49:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt++, ctx.size++;
|
|
|
|
const int parg = src.template get<int>(ctx.args++);
|
|
|
|
ctx.prec = std::max(parg, 0);
|
|
|
|
ctx.dot = parg >= 0;
|
|
|
|
}
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx.prec = 0;
|
|
|
|
ctx.dot = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.type))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
|
|
|
else if (fmt[0] == 'h')
|
|
|
|
{
|
|
|
|
fmt++, ctx.size++;
|
2017-05-16 13:29:07 +02:00
|
|
|
ctx.type = src.size_char;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-16 13:29:07 +02:00
|
|
|
ctx.type = src.size_short;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.type))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
|
|
|
else if (fmt[0] == 'l')
|
|
|
|
{
|
|
|
|
fmt++, ctx.size++;
|
2017-05-16 13:29:07 +02:00
|
|
|
ctx.type = src.size_llong;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-16 13:29:07 +02:00
|
|
|
ctx.type = src.size_long;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'z':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.type))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-16 13:29:07 +02:00
|
|
|
ctx.type = src.size_size;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'j':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.type))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-16 13:29:07 +02:00
|
|
|
ctx.type = src.size_max;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.type))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-16 13:29:07 +02:00
|
|
|
ctx.type = src.size_diff;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.type || !src.test(ctx.args)))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t start = out.size();
|
2016-08-05 18:49:45 +02:00
|
|
|
out.push_back(src.template get<Char>(ctx.args));
|
2016-08-03 22:51:05 +02:00
|
|
|
|
|
|
|
if (1 < ctx.width)
|
|
|
|
{
|
|
|
|
// Add spaces if necessary
|
2016-08-05 18:49:45 +02:00
|
|
|
out.insert(out.begin() + start + ctx.left, ctx.width - 1, ' ');
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src.skip(ctx.args);
|
|
|
|
ctx = {0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(ctx.type || !src.test(ctx.args)))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t start = out.size();
|
2016-08-05 18:49:45 +02:00
|
|
|
const std::size_t size1 = src.fmt_string(out, ctx.args);
|
2018-09-05 18:57:52 +02:00
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
if (ctx.dot && size1 > ctx.prec)
|
|
|
|
{
|
|
|
|
// Shrink if necessary
|
|
|
|
out.resize(start + ctx.prec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t size2 = out.size() - start;
|
|
|
|
|
|
|
|
if (size2 < ctx.width)
|
|
|
|
{
|
|
|
|
// Add spaces if necessary
|
2016-08-05 18:49:45 +02:00
|
|
|
out.insert(ctx.left ? out.end() : out.begin() + start, ctx.width - size2, ' ');
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src.skip(ctx.args);
|
|
|
|
ctx = {0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(!src.test(ctx.args)))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx.type)
|
|
|
|
{
|
2019-11-28 19:18:37 +01:00
|
|
|
ctx.type = static_cast<u8>(src.type(ctx.args));
|
2017-05-16 13:29:07 +02:00
|
|
|
|
|
|
|
if (!ctx.type)
|
|
|
|
{
|
|
|
|
ctx.type = src.size_int;
|
|
|
|
}
|
2016-08-05 18:49:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 12:25:05 +02:00
|
|
|
// Sign-extended argument expected
|
2016-08-05 18:49:45 +02:00
|
|
|
const u64 val = src.template get<u64>(ctx.args);
|
2016-08-06 12:25:05 +02:00
|
|
|
const bool negative = ctx.type && static_cast<s64>(val) < 0;
|
2016-08-05 18:49:45 +02:00
|
|
|
|
|
|
|
const std::size_t start = out.size();
|
|
|
|
|
|
|
|
if (!ctx.dot || ctx.prec)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
if (negative)
|
2016-08-05 18:49:45 +02:00
|
|
|
{
|
|
|
|
out.push_back('-');
|
|
|
|
}
|
|
|
|
else if (ctx.sign)
|
|
|
|
{
|
|
|
|
out.push_back('+');
|
|
|
|
}
|
|
|
|
else if (ctx.space)
|
|
|
|
{
|
|
|
|
out.push_back(' ');
|
|
|
|
}
|
|
|
|
|
2016-08-06 12:25:05 +02:00
|
|
|
write_decimal(negative ? 0 - val : val, ctx.prec);
|
2016-08-05 18:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t size2 = out.size() - start;
|
|
|
|
|
|
|
|
if (size2 < ctx.width)
|
|
|
|
{
|
|
|
|
// Add padding if necessary
|
|
|
|
if (ctx.zeros && !ctx.left && !ctx.dot)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
out.insert(out.begin() + start + (negative || ctx.sign || ctx.space), ctx.width - size2, '0');
|
2016-08-05 18:49:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out.insert(ctx.left ? out.end() : out.begin() + start, ctx.width - size2, ' ');
|
|
|
|
}
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src.skip(ctx.args);
|
|
|
|
ctx = {0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(!src.test(ctx.args)))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx.type)
|
|
|
|
{
|
2019-11-28 19:18:37 +01:00
|
|
|
ctx.type = static_cast<u8>(src.type(ctx.args));
|
2017-05-16 13:29:07 +02:00
|
|
|
|
|
|
|
if (!ctx.type)
|
|
|
|
{
|
|
|
|
ctx.type = src.size_int;
|
|
|
|
}
|
2016-08-05 18:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const u64 mask =
|
2019-07-26 18:00:56 +02:00
|
|
|
ctx.type == 1 ? 0xff :
|
|
|
|
ctx.type == 2 ? 0xffff :
|
|
|
|
ctx.type == 4 ? 0xffff'ffffu :
|
|
|
|
0xffff'ffff'ffff'ffffu;
|
2016-08-05 18:49:45 +02:00
|
|
|
|
2016-08-06 12:25:05 +02:00
|
|
|
// Trunc sign-extended signed types
|
2016-08-05 18:49:45 +02:00
|
|
|
const u64 val = src.template get<u64>(ctx.args) & mask;
|
|
|
|
|
|
|
|
const std::size_t start = out.size();
|
|
|
|
|
|
|
|
if (ctx.alter)
|
|
|
|
{
|
|
|
|
out.push_back('0');
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
write_octal(val, ctx.prec ? ctx.prec - 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!ctx.dot || ctx.prec)
|
|
|
|
{
|
|
|
|
write_octal(val, ctx.prec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t size2 = out.size() - start;
|
|
|
|
|
|
|
|
if (size2 < ctx.width)
|
|
|
|
{
|
|
|
|
// Add padding if necessary
|
|
|
|
out.insert(ctx.left ? out.end() : out.begin() + start, ctx.width - size2, ctx.zeros && !ctx.left && !ctx.dot ? '0' : ' ');
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src.skip(ctx.args);
|
|
|
|
ctx = {0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(!src.test(ctx.args)))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx.type)
|
|
|
|
{
|
2019-11-28 19:18:37 +01:00
|
|
|
ctx.type = static_cast<u8>(src.type(ctx.args));
|
2017-05-16 13:29:07 +02:00
|
|
|
|
|
|
|
if (!ctx.type)
|
|
|
|
{
|
|
|
|
ctx.type = src.size_int;
|
|
|
|
}
|
2016-08-05 18:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const u64 mask =
|
2019-07-26 18:00:56 +02:00
|
|
|
ctx.type == 1 ? 0xff :
|
|
|
|
ctx.type == 2 ? 0xffff :
|
|
|
|
ctx.type == 4 ? 0xffff'ffffu :
|
|
|
|
0xffff'ffff'ffff'ffffu;
|
2016-08-05 18:49:45 +02:00
|
|
|
|
2016-08-06 12:25:05 +02:00
|
|
|
// Trunc sign-extended signed types
|
2016-08-05 18:49:45 +02:00
|
|
|
const u64 val = src.template get<u64>(ctx.args) & mask;
|
|
|
|
|
|
|
|
const std::size_t start = out.size();
|
|
|
|
|
|
|
|
if (ctx.alter)
|
|
|
|
{
|
|
|
|
out.push_back('0');
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
out.push_back(ch); // Prepend 0x or 0X
|
|
|
|
write_hex(val, ch == 'X', ctx.prec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!ctx.dot || ctx.prec)
|
|
|
|
{
|
|
|
|
write_hex(val, ch == 'X', ctx.prec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t size2 = out.size() - start;
|
|
|
|
|
|
|
|
if (size2 < ctx.width)
|
|
|
|
{
|
|
|
|
// Add padding if necessary
|
|
|
|
if (ctx.zeros && !ctx.left && !ctx.dot)
|
|
|
|
{
|
|
|
|
out.insert(out.begin() + start + (ctx.alter && val ? 2 : 0), ctx.width - size2, '0');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out.insert(ctx.left ? out.end() : out.begin() + start, ctx.width - size2, ' ');
|
|
|
|
}
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src.skip(ctx.args);
|
|
|
|
ctx = {0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(!src.test(ctx.args)))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx.type)
|
|
|
|
{
|
2019-11-28 19:18:37 +01:00
|
|
|
ctx.type = static_cast<u8>(src.type(ctx.args));
|
2017-05-16 13:29:07 +02:00
|
|
|
|
|
|
|
if (!ctx.type)
|
|
|
|
{
|
|
|
|
ctx.type = src.size_int;
|
|
|
|
}
|
2016-08-05 18:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const u64 mask =
|
2019-07-26 18:00:56 +02:00
|
|
|
ctx.type == 1 ? 0xff :
|
|
|
|
ctx.type == 2 ? 0xffff :
|
|
|
|
ctx.type == 4 ? 0xffff'ffffu :
|
|
|
|
0xffff'ffff'ffff'ffffu;
|
2016-08-05 18:49:45 +02:00
|
|
|
|
2016-08-06 12:25:05 +02:00
|
|
|
// Trunc sign-extended signed types
|
2016-08-05 18:49:45 +02:00
|
|
|
const u64 val = src.template get<u64>(ctx.args) & mask;
|
|
|
|
|
|
|
|
const std::size_t start = out.size();
|
|
|
|
|
|
|
|
if (!ctx.dot || ctx.prec)
|
|
|
|
{
|
|
|
|
write_decimal(val, ctx.prec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t size2 = out.size() - start;
|
|
|
|
|
|
|
|
if (size2 < ctx.width)
|
|
|
|
{
|
|
|
|
// Add padding if necessary
|
|
|
|
out.insert(ctx.left ? out.end() : out.begin() + start, ctx.width - size2, ctx.zeros && !ctx.left && !ctx.dot ? '0' : ' ');
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src.skip(ctx.args);
|
|
|
|
ctx = {0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(!src.test(ctx.args) || ctx.type))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
const u64 val = src.template get<u64>(ctx.args);
|
|
|
|
|
|
|
|
const std::size_t start = out.size();
|
|
|
|
|
|
|
|
write_hex(val, false, sizeof(void*) * 2);
|
|
|
|
|
|
|
|
const std::size_t size2 = out.size() - start;
|
|
|
|
|
|
|
|
if (size2 < ctx.width)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
// Add padding if necessary
|
|
|
|
out.insert(ctx.left ? out.end() : out.begin() + start, ctx.width - size2, ' ');
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
2018-09-05 18:57:52 +02:00
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
src.skip(ctx.args);
|
|
|
|
ctx = {0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (UNLIKELY(!src.test(ctx.args) || ctx.type))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
// Fallback (TODO)
|
|
|
|
|
|
|
|
const std::string _fmt(fmt - ctx.size, fmt);
|
|
|
|
|
2017-03-05 19:42:54 +01:00
|
|
|
const f64 arg0 = src.template get<f64>(0);
|
2016-08-05 18:49:45 +02:00
|
|
|
const u64 arg1 = ctx.args >= 1 ? src.template get<u64>(1) : 0;
|
|
|
|
const u64 arg2 = ctx.args >= 2 ? src.template get<u64>(2) : 0;
|
|
|
|
|
|
|
|
if (const std::size_t _size = std::snprintf(0, 0, _fmt.c_str(), arg0, arg1, arg2))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
out.resize(out.size() + _size);
|
|
|
|
std::snprintf(&out.front() + out.size() - _size, _size + 1, _fmt.c_str(), arg0, arg1, arg2);
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src.skip(ctx.args);
|
|
|
|
ctx = {0};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'L': // long double, not supported
|
|
|
|
case 'n': // writeback, not supported
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
drop_sequence();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle unfinished sequence
|
2019-05-22 05:47:47 +02:00
|
|
|
if (ctx.size && ctx.size != size_dropped)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
fmt--, drop_sequence();
|
|
|
|
}
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
return out.size() - start_pos;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|