2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-07 09:41:11 +00:00
|
|
|
#pragma once
|
2018-10-27 14:43:03 +00:00
|
|
|
|
2021-02-06 21:13:49 +00:00
|
|
|
#include <AK/Format.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>
|
2022-03-20 08:53:52 +00:00
|
|
|
#include <wchar.h>
|
2019-06-07 09:41:11 +00:00
|
|
|
|
2022-04-09 20:31:54 +00:00
|
|
|
#ifndef KERNEL
|
|
|
|
# include <math.h>
|
|
|
|
#endif
|
|
|
|
|
2022-10-09 21:23:23 +00:00
|
|
|
#ifdef AK_OS_SERENITY
|
2022-04-01 17:58:27 +00:00
|
|
|
extern "C" size_t strlen(char const*);
|
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
|
|
|
|
2021-01-12 18:21:59 +00:00
|
|
|
namespace PrintfImplementation {
|
|
|
|
|
2021-12-19 19:28:28 +00:00
|
|
|
template<typename PutChFunc, typename T, typename CharType>
|
2022-02-16 18:23:14 +00:00
|
|
|
ALWAYS_INLINE int print_hex(PutChFunc putch, CharType*& bufptr, T number, bool upper_case, bool alternate_form, bool left_pad, bool zero_pad, u32 field_width, bool has_precision, u32 precision)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr char const* printf_hex_digits_lower = "0123456789abcdef";
|
|
|
|
constexpr char const* printf_hex_digits_upper = "0123456789ABCDEF";
|
2019-08-13 15:19:13 +00:00
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
u32 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;
|
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
bool not_zero = number != 0;
|
2019-08-13 15:19:13 +00:00
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
char buf[16];
|
|
|
|
char* p = buf;
|
|
|
|
|
|
|
|
if (!(has_precision && precision == 0 && !not_zero)) {
|
|
|
|
if (number == 0) {
|
|
|
|
(*p++) = '0';
|
|
|
|
if (precision > 0)
|
|
|
|
precision--;
|
|
|
|
} else {
|
|
|
|
u8 shift_count = digits * 4;
|
|
|
|
while (shift_count) {
|
|
|
|
shift_count -= 4;
|
|
|
|
(*p++) = upper_case
|
|
|
|
? printf_hex_digits_upper[(number >> shift_count) & 0x0f]
|
|
|
|
: printf_hex_digits_lower[(number >> shift_count) & 0x0f];
|
|
|
|
if (precision > 0)
|
|
|
|
precision--;
|
|
|
|
}
|
2019-08-13 15:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
size_t numlen = p - buf;
|
|
|
|
|
|
|
|
if (!field_width || field_width < (numlen + has_precision * precision + (alternate_form * 2 * not_zero)))
|
|
|
|
field_width = numlen + has_precision * precision + alternate_form * 2 * not_zero;
|
|
|
|
|
|
|
|
if ((zero_pad && !has_precision) && (alternate_form && not_zero)) {
|
2019-08-13 15:19:13 +00:00
|
|
|
putch(bufptr, '0');
|
|
|
|
putch(bufptr, 'x');
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
if (!left_pad) {
|
|
|
|
for (unsigned i = 0; i < field_width - numlen - has_precision * precision - alternate_form * 2 * not_zero; ++i) {
|
|
|
|
putch(bufptr, (zero_pad && !has_precision) ? '0' : ' ');
|
2019-08-13 15:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
if (!(zero_pad && !has_precision) && (alternate_form && not_zero)) {
|
2019-08-13 15:19:13 +00:00
|
|
|
putch(bufptr, '0');
|
2022-02-16 18:23:14 +00:00
|
|
|
putch(bufptr, 'x');
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
if (has_precision) {
|
|
|
|
for (u32 i = 0; i < precision; ++i) {
|
|
|
|
putch(bufptr, '0');
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
|
2018-10-27 14:43:03 +00:00
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
if (left_pad) {
|
2022-02-16 18:23:14 +00:00
|
|
|
for (unsigned i = 0; i < field_width - numlen - has_precision * precision - alternate_form * 2 * not_zero; ++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
|
|
|
}
|
|
|
|
|
2021-12-19 19:28:28 +00:00
|
|
|
template<typename PutChFunc, typename CharType>
|
2022-02-16 18:23:14 +00:00
|
|
|
ALWAYS_INLINE int print_decimal(PutChFunc putch, CharType*& bufptr, u64 number, bool sign, bool always_sign, bool left_pad, bool zero_pad, u32 field_width, bool has_precision, u32 precision)
|
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;
|
2022-02-16 18:23:14 +00:00
|
|
|
char buf[21];
|
2019-03-21 12:36:40 +00:00
|
|
|
char* p = buf;
|
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
if (!(has_precision && precision == 0 && number == 0)) {
|
|
|
|
for (;;) {
|
|
|
|
ch = '0' + (number / divisor);
|
|
|
|
number %= divisor;
|
|
|
|
if (ch != '0')
|
|
|
|
padding = 0;
|
|
|
|
if (!padding || divisor == 1) {
|
|
|
|
*(p++) = ch;
|
|
|
|
if (precision > 0)
|
|
|
|
precision--;
|
|
|
|
}
|
|
|
|
if (divisor == 1)
|
|
|
|
break;
|
|
|
|
divisor /= 10;
|
|
|
|
}
|
2019-03-21 12:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t numlen = p - buf;
|
2022-02-16 18:23:14 +00:00
|
|
|
|
|
|
|
if (!field_width || field_width < (numlen + has_precision * precision + (sign || always_sign)))
|
|
|
|
field_width = numlen + has_precision * precision + (sign || always_sign);
|
|
|
|
|
|
|
|
if ((zero_pad && !has_precision) && (sign || always_sign)) {
|
|
|
|
putch(bufptr, sign ? '-' : '+');
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
if (!left_pad) {
|
2022-02-16 18:23:14 +00:00
|
|
|
for (unsigned i = 0; i < field_width - numlen - has_precision * precision - (sign || always_sign); ++i) {
|
|
|
|
putch(bufptr, (zero_pad && !has_precision) ? '0' : ' ');
|
2019-03-21 12:36:40 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
|
|
|
|
if (!(zero_pad && !has_precision) && (sign || always_sign)) {
|
|
|
|
putch(bufptr, sign ? '-' : '+');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_precision) {
|
|
|
|
for (u32 i = 0; i < precision; ++i) {
|
|
|
|
putch(bufptr, '0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:36:40 +00:00
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
if (left_pad) {
|
2022-02-16 18:23:14 +00:00
|
|
|
for (unsigned i = 0; i < field_width - numlen - has_precision * precision - (sign || always_sign); ++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
|
|
|
}
|
2022-04-09 20:31:54 +00:00
|
|
|
#ifndef KERNEL
|
2021-12-19 19:28:28 +00:00
|
|
|
template<typename PutChFunc, typename CharType>
|
2022-02-16 18:23:14 +00:00
|
|
|
ALWAYS_INLINE int print_double(PutChFunc putch, CharType*& bufptr, double number, bool always_sign, bool left_pad, bool zero_pad, u32 field_width, u32 precision)
|
2020-03-30 22:40:22 +00:00
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
u32 whole_width = (field_width >= precision + 1) ? field_width - precision - 1 : 0;
|
|
|
|
|
2022-03-01 08:50:19 +00:00
|
|
|
bool sign = signbit(number);
|
|
|
|
bool nan = isnan(number);
|
|
|
|
bool inf = isinf(number);
|
|
|
|
|
|
|
|
if (nan || inf) {
|
|
|
|
for (unsigned i = 0; i < field_width - 3 - sign; i++) {
|
|
|
|
putch(bufptr, ' ');
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
if (sign) {
|
|
|
|
putch(bufptr, '-');
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
if (nan) {
|
|
|
|
putch(bufptr, 'n');
|
|
|
|
putch(bufptr, 'a');
|
|
|
|
putch(bufptr, 'n');
|
|
|
|
} else {
|
|
|
|
putch(bufptr, 'i');
|
|
|
|
putch(bufptr, 'n');
|
|
|
|
putch(bufptr, 'f');
|
|
|
|
}
|
|
|
|
return length + 3;
|
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
|
|
|
|
if (sign)
|
|
|
|
number = -number;
|
|
|
|
|
|
|
|
length = print_decimal(putch, bufptr, (i64)number, sign, always_sign, left_pad, zero_pad, whole_width, false, 1);
|
|
|
|
if (precision > 0) {
|
|
|
|
putch(bufptr, '.');
|
2020-03-30 22:40:22 +00:00
|
|
|
length++;
|
2022-02-16 18:23:14 +00:00
|
|
|
double fraction = number - (i64)number;
|
2020-03-30 22:40:22 +00:00
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
for (u32 i = 0; i < precision; ++i)
|
|
|
|
fraction = fraction * 10;
|
2020-04-06 21:27:15 +00:00
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
return length + print_decimal(putch, bufptr, (i64)fraction, false, false, false, true, precision, false, 1);
|
|
|
|
}
|
2020-04-06 21:27:15 +00:00
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
return length;
|
2020-03-30 22:40:22 +00:00
|
|
|
}
|
2022-04-09 20:31:54 +00:00
|
|
|
#endif
|
2021-12-19 19:28:28 +00:00
|
|
|
template<typename PutChFunc, typename CharType>
|
2022-02-16 18:23:14 +00:00
|
|
|
ALWAYS_INLINE int print_octal_number(PutChFunc putch, CharType*& bufptr, u64 number, bool alternate_form, bool left_pad, bool zero_pad, u32 field_width, bool has_precision, u32 precision)
|
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;
|
|
|
|
|
2022-02-16 18:23:14 +00:00
|
|
|
if (alternate_form) {
|
|
|
|
(*p++) = '0';
|
|
|
|
if (precision > 0)
|
|
|
|
precision--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(has_precision && precision == 0 && number == 0)) {
|
|
|
|
for (;;) {
|
|
|
|
ch = '0' + (number / divisor);
|
|
|
|
number %= divisor;
|
|
|
|
if (ch != '0')
|
|
|
|
padding = 0;
|
|
|
|
if (!padding || divisor == 1) {
|
|
|
|
*(p++) = ch;
|
|
|
|
if (precision > 0)
|
|
|
|
precision--;
|
|
|
|
}
|
|
|
|
if (divisor == 1)
|
|
|
|
break;
|
|
|
|
divisor /= 8;
|
|
|
|
}
|
2019-01-29 03:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t numlen = p - buf;
|
2022-02-16 18:23:14 +00:00
|
|
|
|
|
|
|
if (!field_width || field_width < (numlen + has_precision * precision))
|
|
|
|
field_width = numlen + has_precision * precision;
|
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
if (!left_pad) {
|
2022-02-16 18:23:14 +00:00
|
|
|
for (unsigned i = 0; i < field_width - numlen - has_precision * precision; ++i) {
|
|
|
|
putch(bufptr, (zero_pad && !has_precision) ? '0' : ' ');
|
2019-01-29 03:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
|
|
|
|
if (has_precision) {
|
|
|
|
for (u32 i = 0; i < precision; ++i) {
|
|
|
|
putch(bufptr, '0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 03:55:08 +00:00
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
|
2020-05-04 07:40:17 +00:00
|
|
|
if (left_pad) {
|
2022-02-16 18:23:14 +00:00
|
|
|
for (unsigned i = 0; i < field_width - numlen - has_precision * precision; ++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
|
|
|
}
|
|
|
|
|
2022-03-20 08:53:52 +00:00
|
|
|
template<typename PutChFunc, typename T, typename CharType>
|
|
|
|
ALWAYS_INLINE int print_string(PutChFunc putch, CharType*& bufptr, T str, size_t len, bool left_pad, size_t field_width, bool dot, size_t precision, bool has_fraction)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
2021-02-01 20:53:03 +00:00
|
|
|
if (has_fraction)
|
2022-02-16 18:23:14 +00:00
|
|
|
len = min(len, precision);
|
2021-02-01 20:53:03 +00:00
|
|
|
|
2020-02-19 21:07:37 +00:00
|
|
|
if (!dot && (!field_width || field_width < len))
|
|
|
|
field_width = len;
|
2021-02-01 20:53:03 +00:00
|
|
|
|
|
|
|
if (has_fraction && !field_width)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-19 19:28:28 +00:00
|
|
|
template<typename PutChFunc, typename CharType>
|
2022-04-09 00:24:59 +00:00
|
|
|
ALWAYS_INLINE int print_signed_number(PutChFunc putch, CharType*& bufptr, i64 number, bool always_sign, bool left_pad, bool zero_pad, u32 field_width, bool has_precision, u32 precision)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
2022-04-09 00:24:59 +00:00
|
|
|
// FIXME: `0 - number` overflows if we are trying to negate the smallest possible value.
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_decimal(putch, bufptr, (number < 0) ? 0 - number : number, number < 0, always_sign, left_pad, zero_pad, field_width, has_precision, precision);
|
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 };
|
2022-02-16 18:23:14 +00:00
|
|
|
bool has_precision { false };
|
|
|
|
unsigned precision { 6 };
|
2022-03-11 20:18:33 +00:00
|
|
|
unsigned short_qualifiers { 0 }; // TODO: Unimplemented.
|
2020-08-18 18:37:27 +00:00
|
|
|
unsigned long_qualifiers { 0 };
|
2022-03-11 20:18:33 +00:00
|
|
|
bool intmax_qualifier { false }; // TODO: Unimplemented.
|
|
|
|
bool ptrdiff_qualifier { false }; // TODO: Unimplemented.
|
|
|
|
bool long_double_qualifier { false }; // TODO: Unimplemented.
|
|
|
|
bool size_qualifier { false }; // TODO: Unimplemented.
|
2020-08-18 18:37:27 +00:00
|
|
|
bool alternate_form { 0 };
|
|
|
|
bool always_sign { false };
|
|
|
|
};
|
|
|
|
|
2021-12-19 19:28:28 +00:00
|
|
|
template<typename PutChFunc, typename ArgumentListRefT, template<typename T, typename U = ArgumentListRefT> typename NextArgument, typename CharType = char>
|
2020-08-18 18:37:27 +00:00
|
|
|
struct PrintfImpl {
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE PrintfImpl(PutChFunc& putch, CharType*& bufptr, int const& nwritten)
|
2020-08-18 18:37:27 +00:00
|
|
|
: m_bufptr(bufptr)
|
|
|
|
, m_nwritten(nwritten)
|
|
|
|
, m_putch(putch)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_s(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-03-20 08:53:52 +00:00
|
|
|
// FIXME: Narrow characters should be converted to wide characters on the fly and vice versa.
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/wprintf.html
|
|
|
|
#ifndef KERNEL
|
|
|
|
if (state.long_qualifiers) {
|
2022-04-01 17:58:27 +00:00
|
|
|
wchar_t const* sp = NextArgument<wchar_t const*>()(ap);
|
2022-03-20 08:53:52 +00:00
|
|
|
if (!sp)
|
|
|
|
sp = L"(null)";
|
|
|
|
return print_string(m_putch, m_bufptr, sp, wcslen(sp), state.left_pad, state.field_width, state.dot, state.precision, state.has_precision);
|
|
|
|
}
|
|
|
|
#endif
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* sp = NextArgument<char const*>()(ap);
|
2020-08-18 18:37:27 +00:00
|
|
|
if (!sp)
|
|
|
|
sp = "(null)";
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_string(m_putch, m_bufptr, sp, strlen(sp), state.left_pad, state.field_width, state.dot, state.precision, state.has_precision);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_d(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-04-09 00:49:07 +00:00
|
|
|
i64 number = [&]() -> i64 {
|
|
|
|
if (state.long_qualifiers >= 2)
|
|
|
|
return NextArgument<long long int>()(ap);
|
|
|
|
if (state.long_qualifiers == 1)
|
|
|
|
return NextArgument<long int>()(ap);
|
|
|
|
return NextArgument<int>()(ap);
|
|
|
|
}();
|
2020-08-18 18:37:27 +00:00
|
|
|
|
2022-04-09 00:49:07 +00:00
|
|
|
return print_signed_number(m_putch, m_bufptr, number, state.always_sign, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_i(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
return format_d(state, ap);
|
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_u(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-04-09 00:49:07 +00:00
|
|
|
u64 number = [&]() -> u64 {
|
|
|
|
if (state.long_qualifiers >= 2)
|
|
|
|
return NextArgument<unsigned long long int>()(ap);
|
|
|
|
if (state.long_qualifiers == 1)
|
|
|
|
return NextArgument<unsigned long int>()(ap);
|
|
|
|
return NextArgument<unsigned int>()(ap);
|
|
|
|
}();
|
|
|
|
|
|
|
|
return print_decimal(m_putch, m_bufptr, number, false, false, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_Q(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_decimal(m_putch, m_bufptr, NextArgument<u64>()(ap), false, false, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_q(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<u64>()(ap), false, false, state.left_pad, state.zero_pad, 16, false, 1);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-09 20:31:54 +00:00
|
|
|
#ifndef KERNEL
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_g(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
return format_f(state, ap);
|
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_f(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_double(m_putch, m_bufptr, NextArgument<double>()(ap), state.always_sign, state.left_pad, state.zero_pad, state.field_width, state.precision);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-09 20:31:54 +00:00
|
|
|
#endif
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_o(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_octal_number(m_putch, m_bufptr, NextArgument<u32>()(ap), state.alternate_form, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-09 00:44:25 +00:00
|
|
|
ALWAYS_INLINE int format_unsigned_hex(ModifierState const& state, ArgumentListRefT ap, bool uppercase) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-04-09 00:49:07 +00:00
|
|
|
u64 number = [&]() -> u64 {
|
|
|
|
if (state.long_qualifiers >= 2)
|
|
|
|
return NextArgument<unsigned long long int>()(ap);
|
|
|
|
if (state.long_qualifiers == 1)
|
|
|
|
return NextArgument<unsigned long int>()(ap);
|
|
|
|
return NextArgument<unsigned int>()(ap);
|
|
|
|
}();
|
|
|
|
|
|
|
|
return print_hex(m_putch, m_bufptr, number, uppercase, state.alternate_form, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
|
2022-04-09 00:44:25 +00:00
|
|
|
}
|
|
|
|
ALWAYS_INLINE int format_x(ModifierState const& state, ArgumentListRefT ap) const
|
|
|
|
{
|
|
|
|
return format_unsigned_hex(state, ap, false);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_X(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-04-09 00:44:25 +00:00
|
|
|
return format_unsigned_hex(state, ap, true);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_n(ModifierState const&, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
*NextArgument<int*>()(ap) = m_nwritten;
|
|
|
|
return 0;
|
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_p(ModifierState const&, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<FlatPtr>()(ap), false, true, false, true, 8, false, 1);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_P(ModifierState const&, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_hex(m_putch, m_bufptr, NextArgument<FlatPtr>()(ap), true, true, false, true, 8, false, 1);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_percent(ModifierState const&, ArgumentListRefT) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
m_putch(m_bufptr, '%');
|
|
|
|
return 1;
|
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_c(ModifierState const& state, ArgumentListRefT ap) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
|
|
|
char c = NextArgument<int>()(ap);
|
2022-02-16 18:23:14 +00:00
|
|
|
return print_string(m_putch, m_bufptr, &c, 1, state.left_pad, state.field_width, state.dot, state.precision, state.has_precision);
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int format_unrecognized(CharType format_op, CharType const* fmt, ModifierState const&, ArgumentListRefT) const
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2020-10-07 12:02:42 +00:00
|
|
|
dbgln("printf_internal: Unimplemented format specifier {} (fmt: {})", format_op, fmt);
|
2020-08-18 18:37:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2021-12-19 19:28:28 +00:00
|
|
|
CharType*& m_bufptr;
|
2022-04-01 17:58:27 +00:00
|
|
|
int const& m_nwritten;
|
2020-08-18 18:37:27 +00:00
|
|
|
PutChFunc& m_putch;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename V>
|
|
|
|
struct VaArgNextArgument {
|
|
|
|
ALWAYS_INLINE T operator()(V ap) const
|
|
|
|
{
|
2022-09-20 12:56:51 +00:00
|
|
|
#ifdef AK_OS_WINDOWS
|
|
|
|
// GCC on msys2 complains about the type of ap,
|
2022-10-01 03:03:06 +00:00
|
|
|
// so let's force the compiler to believe it's a
|
2022-09-20 12:56:51 +00:00
|
|
|
// va_list.
|
|
|
|
return va_arg((va_list&)ap, T);
|
|
|
|
#else
|
2020-08-18 18:37:27 +00:00
|
|
|
return va_arg(ap, T);
|
2022-09-20 12:56:51 +00:00
|
|
|
#endif
|
2020-08-18 18:37:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PRINTF_IMPL_DELEGATE_TO_IMPL(c) \
|
|
|
|
case* #c: \
|
|
|
|
ret += impl.format_##c(state, ap); \
|
|
|
|
break;
|
|
|
|
|
2021-12-19 19:28:28 +00:00
|
|
|
template<typename PutChFunc, template<typename T, typename U, template<typename X, typename Y> typename V, typename C = char> typename Impl = PrintfImpl, typename ArgumentListT = va_list, template<typename T, typename V = decltype(declval<ArgumentListT&>())> typename NextArgument = VaArgNextArgument, typename CharType = char>
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE int printf_internal(PutChFunc putch, IdentityType<CharType>* buffer, CharType const*& fmt, ArgumentListT ap)
|
2020-08-18 18:37:27 +00:00
|
|
|
{
|
2018-10-27 14:43:03 +00:00
|
|
|
int ret = 0;
|
2021-12-19 19:28:28 +00:00
|
|
|
CharType* bufptr = buffer;
|
2018-10-27 14:43:03 +00:00
|
|
|
|
2021-12-19 19:28:28 +00:00
|
|
|
Impl<PutChFunc, ArgumentListT&, NextArgument, CharType> impl { putch, bufptr, ret };
|
2020-08-18 18:37:27 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
for (CharType const* p = fmt; *p; ++p) {
|
2020-08-18 18:37:27 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
if (!state.zero_pad && !state.field_width && !state.dot && *p == '0') {
|
2020-08-18 18:37:27 +00:00
|
|
|
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 {
|
2022-02-16 18:23:14 +00:00
|
|
|
if (!state.has_precision) {
|
|
|
|
state.has_precision = true;
|
|
|
|
state.precision = 0;
|
2020-07-25 05:22:29 +00:00
|
|
|
}
|
2022-02-16 18:23:14 +00:00
|
|
|
state.precision *= 10;
|
|
|
|
state.precision += *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 == '*') {
|
2021-05-30 16:06:10 +00:00
|
|
|
if (state.dot) {
|
2022-02-16 18:23:14 +00:00
|
|
|
state.has_precision = true;
|
2021-12-07 13:47:01 +00:00
|
|
|
state.zero_pad = true;
|
2022-02-16 18:23:14 +00:00
|
|
|
state.precision = NextArgument<int>()(ap);
|
2021-05-30 16:06:10 +00:00
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
2022-03-11 20:18:33 +00:00
|
|
|
if (*p == 'h') {
|
|
|
|
++state.short_qualifiers;
|
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2022-03-11 20:18:33 +00:00
|
|
|
if (*p == 'j') {
|
|
|
|
state.intmax_qualifier = true;
|
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
|
|
|
if (*p == 't') {
|
|
|
|
state.ptrdiff_qualifier = true;
|
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
2021-11-14 11:45:51 +00:00
|
|
|
if (*p == 'L') {
|
2022-03-11 20:18:33 +00:00
|
|
|
state.long_double_qualifier = true;
|
2021-11-14 11:45:51 +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(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(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;
|