2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-06-07 09:41:11 +00:00
|
|
|
#pragma once
|
2018-10-27 14:43:03 +00:00
|
|
|
|
2019-09-02 05:32:22 +00:00
|
|
|
#include <AK/Assertions.h>
|
2019-09-06 18:27:58 +00:00
|
|
|
#include <AK/LogStream.h>
|
2020-02-19 22:01:07 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-06-07 09:41:11 +00:00
|
|
|
#include <AK/Types.h>
|
2019-06-14 04:43:56 +00:00
|
|
|
#include <stdarg.h>
|
2019-06-07 09:41:11 +00:00
|
|
|
|
2020-08-18 18:37:27 +00:00
|
|
|
namespace PrintfImplementation {
|
|
|
|
|
2019-08-13 15:19:13 +00:00
|
|
|
static constexpr const char* printf_hex_digits_lower = "0123456789abcdef";
|
|
|
|
static constexpr const char* printf_hex_digits_upper = "0123456789ABCDEF";
|
2018-10-27 14:43:03 +00:00
|
|
|
|
2019-06-14 04:43:56 +00:00
|
|
|
#ifdef __serenity__
|
2019-06-07 09:41:11 +00:00
|
|
|
extern "C" size_t strlen(const char*);
|
2019-06-14 04:43:56 +00:00
|
|
|
#else
|
2019-09-06 18:27:58 +00:00
|
|
|
# include <string.h>
|
2019-06-14 04:43:56 +00:00
|
|
|
#endif
|
2018-10-27 14:43:03 +00:00
|
|
|
|
2019-03-21 12:36:40 +00:00
|
|
|
template<typename PutChFunc, typename T>
|
2020-05-04 07:40:17 +00:00
|
|
|
ALWAYS_INLINE int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper_case, bool alternate_form, bool left_pad, bool zero_pad, u8 field_width)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2019-08-13 15:19:13 +00:00
|
|
|
|
|
|
|
int digits = 0;
|
2019-08-28 00:13:08 +00:00
|
|
|
for (T n = number; n > 0; n >>= 4)
|
2019-08-13 15:19:13 +00:00
|
|
|
++digits;
|
|
|
|
if (digits == 0)
|
|
|
|
digits = 1;
|
|
|
|
|
|
|
|
if (left_pad) {
|
2020-05-04 07:40:17 +00:00
|
|
|
int stop_at = field_width - digits;
|
2019-08-13 15:19:13 +00:00
|
|
|
if (alternate_form)
|
|
|
|
stop_at -= 2;
|
|
|
|
|
|
|
|
while (ret < stop_at) {
|
|
|
|
putch(bufptr, ' ');
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alternate_form) {
|
|
|
|
putch(bufptr, '0');
|
|
|
|
putch(bufptr, 'x');
|
|
|
|
ret += 2;
|
2020-05-04 07:40:17 +00:00
|
|
|
field_width += 2;
|
2019-08-13 15:19:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
if (zero_pad) {
|
|
|
|
while (ret < field_width - digits) {
|
2019-08-13 15:19:13 +00:00
|
|
|
putch(bufptr, '0');
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (number == 0) {
|
|
|
|
putch(bufptr, '0');
|
2018-10-27 14:43:03 +00:00
|
|
|
++ret;
|
2019-08-13 15:19:13 +00:00
|
|
|
} else {
|
|
|
|
u8 shift_count = digits * 4;
|
|
|
|
while (shift_count) {
|
|
|
|
shift_count -= 4;
|
|
|
|
putch(bufptr,
|
|
|
|
upper_case
|
|
|
|
? printf_hex_digits_upper[(number >> shift_count) & 0x0f]
|
|
|
|
: printf_hex_digits_lower[(number >> shift_count) & 0x0f]);
|
|
|
|
++ret;
|
|
|
|
}
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
2019-08-13 15:19:13 +00:00
|
|
|
|
2018-10-27 14:43:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename PutChFunc>
|
2020-05-04 07:40:17 +00:00
|
|
|
ALWAYS_INLINE int print_number(PutChFunc putch, char*& bufptr, u32 number, bool left_pad, bool zero_pad, u32 field_width)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 divisor = 1000000000;
|
2018-10-27 14:43:03 +00:00
|
|
|
char ch;
|
|
|
|
char padding = 1;
|
|
|
|
char buf[16];
|
|
|
|
char* p = buf;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ch = '0' + (number / divisor);
|
|
|
|
number %= divisor;
|
|
|
|
if (ch != '0')
|
|
|
|
padding = 0;
|
|
|
|
if (!padding || divisor == 1)
|
|
|
|
*(p++) = ch;
|
|
|
|
if (divisor == 1)
|
|
|
|
break;
|
|
|
|
divisor /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t numlen = p - buf;
|
2020-05-04 07:40:17 +00:00
|
|
|
if (!field_width || field_width < numlen)
|
|
|
|
field_width = numlen;
|
|
|
|
if (!left_pad) {
|
|
|
|
for (unsigned i = 0; i < field_width - numlen; ++i) {
|
|
|
|
putch(bufptr, zero_pad ? '0' : ' ');
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
2020-05-04 07:40:17 +00:00
|
|
|
if (left_pad) {
|
|
|
|
for (unsigned i = 0; i < field_width - numlen; ++i) {
|
2018-10-27 14:43:03 +00:00
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
return field_width;
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 12:36:40 +00:00
|
|
|
template<typename PutChFunc>
|
2020-05-04 07:40:17 +00:00
|
|
|
ALWAYS_INLINE int print_u64(PutChFunc putch, char*& bufptr, u64 number, bool left_pad, bool zero_pad, u32 field_width)
|
2019-03-21 12:36:40 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
u64 divisor = 10000000000000000000LLU;
|
2019-03-21 12:36:40 +00:00
|
|
|
char ch;
|
|
|
|
char padding = 1;
|
|
|
|
char buf[16];
|
|
|
|
char* p = buf;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ch = '0' + (number / divisor);
|
|
|
|
number %= divisor;
|
|
|
|
if (ch != '0')
|
|
|
|
padding = 0;
|
|
|
|
if (!padding || divisor == 1)
|
|
|
|
*(p++) = ch;
|
|
|
|
if (divisor == 1)
|
|
|
|
break;
|
|
|
|
divisor /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t numlen = p - buf;
|
2020-05-04 07:40:17 +00:00
|
|
|
if (!field_width || field_width < numlen)
|
|
|
|
field_width = numlen;
|
|
|
|
if (!left_pad) {
|
|
|
|
for (unsigned i = 0; i < field_width - numlen; ++i) {
|
|
|
|
putch(bufptr, zero_pad ? '0' : ' ');
|
2019-03-21 12:36:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
2020-05-04 07:40:17 +00:00
|
|
|
if (left_pad) {
|
|
|
|
for (unsigned i = 0; i < field_width - numlen; ++i) {
|
2019-03-21 12:36:40 +00:00
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
return field_width;
|
2019-03-21 12:36:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 22:40:22 +00:00
|
|
|
template<typename PutChFunc>
|
2020-07-25 05:22:29 +00:00
|
|
|
ALWAYS_INLINE int print_double(PutChFunc putch, char*& bufptr, double number, bool left_pad, bool zero_pad, u32 field_width, u32 fraction_length)
|
2020-03-30 22:40:22 +00:00
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
if (number < 0) {
|
|
|
|
putch(bufptr, '-');
|
|
|
|
length++;
|
|
|
|
number = 0 - number;
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
length = print_u64(putch, bufptr, (i64)number, left_pad, zero_pad, field_width);
|
2020-03-30 22:40:22 +00:00
|
|
|
putch(bufptr, '.');
|
|
|
|
length++;
|
|
|
|
double fraction = number - (i64)number;
|
2020-04-06 21:27:15 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < fraction_length; ++i)
|
|
|
|
fraction = fraction * 10;
|
|
|
|
|
|
|
|
return length + print_u64(putch, bufptr, (i64)fraction, false, true, fraction_length);
|
2020-03-30 22:40:22 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 12:47:52 +00:00
|
|
|
template<typename PutChFunc>
|
2020-05-04 07:40:17 +00:00
|
|
|
ALWAYS_INLINE int print_i64(PutChFunc putch, char*& bufptr, i64 number, bool left_pad, bool zero_pad, u32 field_width)
|
2019-06-18 12:47:52 +00:00
|
|
|
{
|
|
|
|
if (number < 0) {
|
|
|
|
putch(bufptr, '-');
|
2020-05-04 07:40:17 +00:00
|
|
|
return print_u64(putch, bufptr, 0 - number, left_pad, zero_pad, field_width) + 1;
|
2019-06-18 12:47:52 +00:00
|
|
|
}
|
2020-05-04 07:40:17 +00:00
|
|
|
return print_u64(putch, bufptr, number, left_pad, zero_pad, field_width);
|
2019-06-18 12:47:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 03:55:08 +00:00
|
|
|
template<typename PutChFunc>
|
2020-05-04 07:40:17 +00:00
|
|
|
ALWAYS_INLINE int print_octal_number(PutChFunc putch, char*& bufptr, u32 number, bool left_pad, bool zero_pad, u32 field_width)
|
2019-01-29 03:55:08 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 divisor = 134217728;
|
2019-01-29 03:55:08 +00:00
|
|
|
char ch;
|
|
|
|
char padding = 1;
|
|
|
|
char buf[32];
|
|
|
|
char* p = buf;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ch = '0' + (number / divisor);
|
|
|
|
number %= divisor;
|
|
|
|
if (ch != '0')
|
|
|
|
padding = 0;
|
|
|
|
if (!padding || divisor == 1)
|
|
|
|
*(p++) = ch;
|
|
|
|
if (divisor == 1)
|
|
|
|
break;
|
|
|
|
divisor /= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t numlen = p - buf;
|
2020-05-04 07:40:17 +00:00
|
|
|
if (!field_width || field_width < numlen)
|
|
|
|
field_width = numlen;
|
|
|
|
if (!left_pad) {
|
|
|
|
for (unsigned i = 0; i < field_width - numlen; ++i) {
|
|
|
|
putch(bufptr, zero_pad ? '0' : ' ');
|
2019-01-29 03:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
2020-05-04 07:40:17 +00:00
|
|
|
if (left_pad) {
|
|
|
|
for (unsigned i = 0; i < field_width - numlen; ++i) {
|
2019-01-29 03:55:08 +00:00
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
return field_width;
|
2019-01-29 03:55:08 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 14:43:03 +00:00
|
|
|
template<typename PutChFunc>
|
2020-06-04 19:43:51 +00:00
|
|
|
ALWAYS_INLINE int print_string(PutChFunc putch, char*& bufptr, const char* str, size_t len, bool left_pad, size_t field_width, bool dot)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
2020-02-19 21:07:37 +00:00
|
|
|
if (!dot && (!field_width || field_width < len))
|
|
|
|
field_width = len;
|
2020-02-19 22:01:07 +00:00
|
|
|
size_t pad_amount = field_width > len ? field_width - len : 0;
|
|
|
|
|
|
|
|
if (!left_pad) {
|
|
|
|
for (size_t i = 0; i < pad_amount; ++i)
|
2018-10-27 14:43:03 +00:00
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
2020-02-19 22:01:07 +00:00
|
|
|
for (size_t i = 0; i < min(len, field_width); ++i) {
|
2018-10-27 14:43:03 +00:00
|
|
|
putch(bufptr, str[i]);
|
|
|
|
}
|
2020-02-19 22:01:07 +00:00
|
|
|
if (left_pad) {
|
|
|
|
for (size_t i = 0; i < pad_amount; ++i)
|
2018-10-27 14:43:03 +00:00
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
2020-02-19 21:07:37 +00:00
|
|
|
return field_width;
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename PutChFunc>
|
2020-05-04 07:40:17 +00:00
|
|
|
ALWAYS_INLINE int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool left_pad, bool zero_pad, u32 field_width, bool always_sign)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
|
|
|
if (number < 0) {
|
|
|
|
putch(bufptr, '-');
|
2020-05-04 07:40:17 +00:00
|
|
|
return print_number(putch, bufptr, 0 - number, left_pad, zero_pad, field_width) + 1;
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
2020-01-19 10:00:02 +00:00
|
|
|
if (always_sign)
|
|
|
|
putch(bufptr, '+');
|
2020-05-04 07:40:17 +00:00
|
|
|
return print_number(putch, bufptr, number, left_pad, zero_pad, field_width);
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 18:37:27 +00:00
|
|
|
struct ModifierState {
|
|
|
|
bool left_pad { false };
|
|
|
|
bool zero_pad { false };
|
|
|
|
bool dot { false };
|
|
|
|
unsigned field_width { 0 };
|
|
|
|
bool has_fraction_length { false };
|
|
|
|
unsigned fraction_length { 6 };
|
|
|
|
unsigned long_qualifiers { 0 };
|
|
|
|
bool size_qualifier { false };
|
|
|
|
bool alternate_form { 0 };
|
|
|
|
bool always_sign { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename PutChFunc, typename ArgumentListRefT, template<typename T, typename U = ArgumentListRefT> typename NextArgument>
|
|
|
|
struct PrintfImpl {
|
|
|
|
ALWAYS_INLINE PrintfImpl(PutChFunc& putch, char*& bufptr, const int& nwritten)
|
|
|
|
: m_bufptr(bufptr)
|
|
|
|
, m_nwritten(nwritten)
|
|
|
|
, m_putch(putch)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE int format_s(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
const char* sp = NextArgument<const char*>()(ap);
|
|
|
|
if (!sp)
|
|
|
|
sp = "(null)";
|
|
|
|
return print_string(m_putch, m_bufptr, sp, strlen(sp), state.left_pad, state.field_width, state.dot);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_d(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
if (state.long_qualifiers >= 2)
|
|
|
|
return print_i64(m_putch, m_bufptr, NextArgument<i64>()(ap), state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
|
|
|
|
return print_signed_number(m_putch, m_bufptr, NextArgument<int>()(ap), state.left_pad, state.zero_pad, state.field_width, state.always_sign);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_i(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return format_d(state, ap);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_u(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
if (state.long_qualifiers >= 2)
|
|
|
|
return print_u64(m_putch, m_bufptr, NextArgument<u64>()(ap), state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
return print_number(m_putch, m_bufptr, NextArgument<u32>()(ap), state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_Q(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return print_u64(m_putch, m_bufptr, NextArgument<u64>()(ap), state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_q(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<u64>()(ap), false, false, state.left_pad, state.zero_pad, 16);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_g(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return format_f(state, ap);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_f(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return print_double(m_putch, m_bufptr, NextArgument<double>()(ap), state.left_pad, state.zero_pad, state.field_width, state.fraction_length);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_o(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
if (state.alternate_form)
|
|
|
|
m_putch(m_bufptr, '0');
|
2018-10-27 14:43:03 +00:00
|
|
|
|
2020-08-18 18:37:27 +00:00
|
|
|
return (state.alternate_form ? 1 : 0) + print_octal_number(m_putch, m_bufptr, NextArgument<u32>()(ap), state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_x(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
if (state.long_qualifiers >= 2)
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<u64>()(ap), false, state.alternate_form, state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<u64>()(ap), false, state.alternate_form, state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_X(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
if (state.long_qualifiers >= 2)
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<u64>()(ap), true, state.alternate_form, state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<u32>()(ap), true, state.alternate_form, state.left_pad, state.zero_pad, state.field_width);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_n(const ModifierState&, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
*NextArgument<int*>()(ap) = m_nwritten;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_p(const ModifierState&, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<u32>()(ap), false, true, false, true, 8);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_P(const ModifierState&, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<u32>()(ap), true, true, false, true, 8);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_percent(const ModifierState&, ArgumentListRefT) const
|
|
|
|
{
|
|
|
|
m_putch(m_bufptr, '%');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_w(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<int>()(ap), false, state.alternate_form, false, true, 4);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_b(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<int>()(ap), false, state.alternate_form, false, true, 2);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_c(const ModifierState& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
char c = NextArgument<int>()(ap);
|
|
|
|
return print_string(m_putch, m_bufptr, &c, 1, state.left_pad, state.field_width, state.dot);
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_unrecognized(char format_op, const char* fmt, const ModifierState&, ArgumentListRefT) const
|
|
|
|
{
|
|
|
|
dbg() << "printf_internal: Unimplemented format specifier " << format_op << " (fmt: " << fmt << ")";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
char*& m_bufptr;
|
|
|
|
const int& m_nwritten;
|
|
|
|
PutChFunc& m_putch;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename V>
|
|
|
|
struct VaArgNextArgument {
|
|
|
|
ALWAYS_INLINE T operator()(V ap) const
|
|
|
|
{
|
|
|
|
return va_arg(ap, T);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PRINTF_IMPL_DELEGATE_TO_IMPL(c) \
|
|
|
|
case* #c: \
|
|
|
|
ret += impl.format_##c(state, ap); \
|
|
|
|
break;
|
|
|
|
|
|
|
|
template<typename PutChFunc, template<typename T, typename U, template<typename X, typename Y> typename V> typename Impl = PrintfImpl, typename ArgumentListT = va_list, template<typename T, typename V = decltype(declval<ArgumentListT&>())> typename NextArgument = VaArgNextArgument>
|
|
|
|
ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fmt, ArgumentListT ap)
|
|
|
|
{
|
2018-10-27 14:43:03 +00:00
|
|
|
int ret = 0;
|
|
|
|
char* bufptr = buffer;
|
|
|
|
|
2020-08-18 18:37:27 +00:00
|
|
|
Impl<PutChFunc, ArgumentListT&, NextArgument> impl { putch, bufptr, ret };
|
|
|
|
|
|
|
|
for (const char* p = fmt; *p; ++p) {
|
|
|
|
ModifierState state;
|
2018-10-27 14:43:03 +00:00
|
|
|
if (*p == '%' && *(p + 1)) {
|
2019-05-28 09:53:16 +00:00
|
|
|
one_more:
|
2018-10-27 14:43:03 +00:00
|
|
|
++p;
|
2020-02-19 21:07:37 +00:00
|
|
|
if (*p == '.') {
|
2020-08-18 18:37:27 +00:00
|
|
|
state.dot = true;
|
2020-02-19 21:07:37 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
2019-06-22 13:52:45 +00:00
|
|
|
if (*p == '-') {
|
2020-08-18 18:37:27 +00:00
|
|
|
state.left_pad = true;
|
2018-10-27 14:43:03 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
2020-01-19 10:00:02 +00:00
|
|
|
if (*p == '+') {
|
2020-08-18 18:37:27 +00:00
|
|
|
state.always_sign = true;
|
2020-01-19 10:00:02 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
2020-08-18 18:37:27 +00:00
|
|
|
if (!state.zero_pad && !state.field_width && *p == '0') {
|
|
|
|
state.zero_pad = true;
|
2018-10-27 14:43:03 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
|
|
|
if (*p >= '0' && *p <= '9') {
|
2020-08-18 18:37:27 +00:00
|
|
|
if (!state.dot) {
|
|
|
|
state.field_width *= 10;
|
|
|
|
state.field_width += *p - '0';
|
2020-04-06 21:27:15 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
} else {
|
2020-08-18 18:37:27 +00:00
|
|
|
if (!state.has_fraction_length) {
|
|
|
|
state.has_fraction_length = true;
|
|
|
|
state.fraction_length = 0;
|
2020-07-25 05:22:29 +00:00
|
|
|
}
|
2020-08-18 18:37:27 +00:00
|
|
|
state.fraction_length *= 10;
|
|
|
|
state.fraction_length += *p - '0';
|
2020-04-06 21:27:15 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
2019-08-09 20:24:53 +00:00
|
|
|
if (*p == '*') {
|
2020-08-18 18:37:27 +00:00
|
|
|
state.field_width = NextArgument<int>()(ap);
|
2019-09-06 18:27:58 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
2019-08-09 20:24:53 +00:00
|
|
|
}
|
2019-02-27 07:38:17 +00:00
|
|
|
if (*p == 'l') {
|
2020-08-18 18:37:27 +00:00
|
|
|
++state.long_qualifiers;
|
2019-03-27 00:29:07 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
2019-06-22 14:30:32 +00:00
|
|
|
if (*p == 'z') {
|
2020-08-18 18:37:27 +00:00
|
|
|
state.size_qualifier = true;
|
2019-06-22 14:30:32 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
2019-03-27 00:29:07 +00:00
|
|
|
if (*p == '#') {
|
2020-08-18 18:37:27 +00:00
|
|
|
state.alternate_form = true;
|
2019-03-27 00:29:07 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
2019-02-27 07:38:17 +00:00
|
|
|
}
|
2019-05-28 09:53:16 +00:00
|
|
|
switch (*p) {
|
|
|
|
case '%':
|
2020-08-18 18:37:27 +00:00
|
|
|
ret += impl.format_percent(state, ap);
|
2019-12-05 10:19:25 +00:00
|
|
|
break;
|
|
|
|
|
2020-08-18 18:37:27 +00:00
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(P);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(Q);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(X);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(b);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(c);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(d);
|
|
|
|
#ifndef KERNEL
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(f);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(g);
|
|
|
|
#endif
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(i);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(n);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(o);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(p);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(q);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(s);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(u);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(w);
|
|
|
|
PRINTF_IMPL_DELEGATE_TO_IMPL(x);
|
2019-09-02 05:32:22 +00:00
|
|
|
default:
|
2020-08-18 18:37:27 +00:00
|
|
|
ret += impl.format_unrecognized(*p, fmt, state, ap);
|
|
|
|
break;
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
2019-05-28 09:53:16 +00:00
|
|
|
} else {
|
2018-10-27 14:43:03 +00:00
|
|
|
putch(bufptr, *p);
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2020-08-18 18:37:27 +00:00
|
|
|
|
|
|
|
#undef PRINTF_IMPL_DELEGATE_TO_IMPL
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using PrintfImplementation::printf_internal;
|