2019-06-07 09:41:11 +00:00
|
|
|
#pragma once
|
2018-10-27 14:43:03 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
static constexpr const char* printf_hex_digits = "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
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
2018-10-27 14:43:03 +00:00
|
|
|
|
2019-03-21 12:36:40 +00:00
|
|
|
template<typename PutChFunc, typename T>
|
|
|
|
[[gnu::always_inline]] inline int print_hex(PutChFunc putch, char*& bufptr, T number, byte fields)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
byte shr_count = fields * 4;
|
|
|
|
while (shr_count) {
|
|
|
|
shr_count -= 4;
|
2019-06-07 09:41:11 +00:00
|
|
|
putch(bufptr, printf_hex_digits[(number >> shr_count) & 0x0F]);
|
2018-10-27 14:43:03 +00:00
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename PutChFunc>
|
2019-02-25 18:05:51 +00:00
|
|
|
[[gnu::always_inline]] inline int print_number(PutChFunc putch, char*& bufptr, dword number, bool leftPad, bool zeroPad, dword fieldWidth)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
|
|
|
dword divisor = 1000000000;
|
|
|
|
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;
|
2018-10-31 21:40:10 +00:00
|
|
|
if (!fieldWidth || fieldWidth < numlen)
|
2018-10-27 14:43:03 +00:00
|
|
|
fieldWidth = numlen;
|
|
|
|
if (!leftPad) {
|
|
|
|
for (unsigned i = 0; i < fieldWidth - numlen; ++i) {
|
|
|
|
putch(bufptr, zeroPad ? '0' : ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
|
|
|
if (leftPad) {
|
|
|
|
for (unsigned i = 0; i < fieldWidth - numlen; ++i) {
|
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fieldWidth;
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:36:40 +00:00
|
|
|
template<typename PutChFunc>
|
|
|
|
[[gnu::always_inline]] inline int print_qword(PutChFunc putch, char*& bufptr, qword number, bool leftPad, bool zeroPad, dword fieldWidth)
|
|
|
|
{
|
|
|
|
qword divisor = 10000000000000000000LLU;
|
|
|
|
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;
|
|
|
|
if (!fieldWidth || fieldWidth < numlen)
|
|
|
|
fieldWidth = numlen;
|
|
|
|
if (!leftPad) {
|
|
|
|
for (unsigned i = 0; i < fieldWidth - numlen; ++i) {
|
|
|
|
putch(bufptr, zeroPad ? '0' : ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
|
|
|
if (leftPad) {
|
|
|
|
for (unsigned i = 0; i < fieldWidth - numlen; ++i) {
|
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fieldWidth;
|
|
|
|
}
|
|
|
|
|
2019-06-18 12:47:52 +00:00
|
|
|
template<typename PutChFunc>
|
|
|
|
[[gnu::always_inline]] inline int print_signed_qword(PutChFunc putch, char*& bufptr, signed_qword number, bool leftPad, bool zeroPad, dword fieldWidth)
|
|
|
|
{
|
|
|
|
if (number < 0) {
|
|
|
|
putch(bufptr, '-');
|
|
|
|
return print_qword(putch, bufptr, 0 - number, leftPad, zeroPad, fieldWidth) + 1;
|
|
|
|
}
|
|
|
|
return print_qword(putch, bufptr, number, leftPad, zeroPad, fieldWidth);
|
|
|
|
}
|
|
|
|
|
2019-01-29 03:55:08 +00:00
|
|
|
template<typename PutChFunc>
|
2019-02-25 18:05:51 +00:00
|
|
|
[[gnu::always_inline]] inline int print_octal_number(PutChFunc putch, char*& bufptr, dword number, bool leftPad, bool zeroPad, dword fieldWidth)
|
2019-01-29 03:55:08 +00:00
|
|
|
{
|
|
|
|
dword divisor = 134217728;
|
|
|
|
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;
|
|
|
|
if (!fieldWidth || fieldWidth < numlen)
|
|
|
|
fieldWidth = numlen;
|
|
|
|
if (!leftPad) {
|
|
|
|
for (unsigned i = 0; i < fieldWidth - numlen; ++i) {
|
|
|
|
putch(bufptr, zeroPad ? '0' : ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < numlen; ++i) {
|
|
|
|
putch(bufptr, buf[i]);
|
|
|
|
}
|
|
|
|
if (leftPad) {
|
|
|
|
for (unsigned i = 0; i < fieldWidth - numlen; ++i) {
|
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fieldWidth;
|
|
|
|
}
|
|
|
|
|
2018-10-27 14:43:03 +00:00
|
|
|
template<typename PutChFunc>
|
2019-02-25 18:05:51 +00:00
|
|
|
[[gnu::always_inline]] inline int print_string(PutChFunc putch, char*& bufptr, const char* str, bool leftPad, dword fieldWidth)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
|
|
|
size_t len = strlen(str);
|
2018-10-31 21:40:10 +00:00
|
|
|
if (!fieldWidth || fieldWidth < len)
|
2018-10-27 14:43:03 +00:00
|
|
|
fieldWidth = len;
|
|
|
|
if (!leftPad) {
|
|
|
|
for (unsigned i = 0; i < fieldWidth - len; ++i)
|
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < len; ++i) {
|
|
|
|
putch(bufptr, str[i]);
|
|
|
|
}
|
|
|
|
if (leftPad) {
|
|
|
|
for (unsigned i = 0; i < fieldWidth - len; ++i)
|
|
|
|
putch(bufptr, ' ');
|
|
|
|
}
|
|
|
|
return fieldWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename PutChFunc>
|
2019-02-25 18:05:51 +00:00
|
|
|
[[gnu::always_inline]] inline int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool leftPad, bool zeroPad, dword fieldWidth)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
|
|
|
if (number < 0) {
|
|
|
|
putch(bufptr, '-');
|
2019-01-31 16:31:23 +00:00
|
|
|
return print_number(putch, bufptr, 0 - number, leftPad, zeroPad, fieldWidth) + 1;
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
return print_number(putch, bufptr, number, leftPad, zeroPad, fieldWidth);
|
2018-10-27 14:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename PutChFunc>
|
2019-06-14 04:43:56 +00:00
|
|
|
[[gnu::always_inline]] inline int printf_internal(PutChFunc putch, char* buffer, const char*& fmt, va_list ap)
|
2018-10-27 14:43:03 +00:00
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
const char* p;
|
2018-10-27 14:43:03 +00:00
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
char* bufptr = buffer;
|
|
|
|
|
|
|
|
for (p = fmt; *p; ++p) {
|
2019-06-22 13:52:45 +00:00
|
|
|
bool left_pad = false;
|
2018-10-27 14:43:03 +00:00
|
|
|
bool zeroPad = false;
|
|
|
|
unsigned fieldWidth = 0;
|
2019-02-27 07:38:17 +00:00
|
|
|
unsigned long_qualifiers = 0;
|
2019-03-27 00:29:07 +00:00
|
|
|
bool alternate_form = 0;
|
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;
|
2019-06-22 13:52:45 +00:00
|
|
|
if (*p == '-') {
|
|
|
|
left_pad = true;
|
2018-10-27 14:43:03 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
|
|
|
if (!zeroPad && !fieldWidth && *p == '0') {
|
|
|
|
zeroPad = true;
|
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
|
|
|
if (*p >= '0' && *p <= '9') {
|
|
|
|
fieldWidth *= 10;
|
|
|
|
fieldWidth += *p - '0';
|
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
2019-02-27 07:38:17 +00:00
|
|
|
if (*p == 'l') {
|
|
|
|
++long_qualifiers;
|
2019-03-27 00:29:07 +00:00
|
|
|
if (*(p + 1))
|
|
|
|
goto one_more;
|
|
|
|
}
|
|
|
|
if (*p == '#') {
|
|
|
|
alternate_form = true;
|
|
|
|
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 's': {
|
|
|
|
const char* sp = va_arg(ap, const char*);
|
2019-06-22 13:52:45 +00:00
|
|
|
ret += print_string(putch, bufptr, sp ? sp : "(null)", left_pad, fieldWidth);
|
2019-05-28 09:53:16 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case 'd':
|
2019-06-22 13:52:45 +00:00
|
|
|
ret += print_signed_number(putch, bufptr, va_arg(ap, int), left_pad, zeroPad, fieldWidth);
|
2019-05-28 09:53:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
2019-06-22 13:52:45 +00:00
|
|
|
ret += print_number(putch, bufptr, va_arg(ap, dword), left_pad, zeroPad, fieldWidth);
|
2019-05-28 09:53:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Q':
|
2019-06-22 13:52:45 +00:00
|
|
|
ret += print_qword(putch, bufptr, va_arg(ap, qword), left_pad, zeroPad, fieldWidth);
|
2019-05-28 09:53:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
ret += print_hex(putch, bufptr, va_arg(ap, qword), 16);
|
|
|
|
break;
|
2019-03-21 12:36:40 +00:00
|
|
|
|
2019-04-22 21:38:33 +00:00
|
|
|
#ifndef KERNEL
|
2019-06-18 07:46:39 +00:00
|
|
|
case 'g':
|
2019-05-28 09:53:16 +00:00
|
|
|
case 'f':
|
|
|
|
// FIXME: Print as float!
|
2019-06-22 13:52:45 +00:00
|
|
|
ret += print_signed_qword(putch, bufptr, (qword)va_arg(ap, double), left_pad, zeroPad, fieldWidth);
|
2019-05-28 09:53:16 +00:00
|
|
|
break;
|
2019-04-22 21:38:33 +00:00
|
|
|
#endif
|
2019-02-28 15:20:29 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
case 'o':
|
|
|
|
if (alternate_form) {
|
|
|
|
putch(bufptr, '0');
|
2019-03-22 00:50:32 +00:00
|
|
|
++ret;
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-06-22 13:52:45 +00:00
|
|
|
ret += print_octal_number(putch, bufptr, va_arg(ap, dword), left_pad, zeroPad, fieldWidth);
|
2019-05-28 09:53:16 +00:00
|
|
|
break;
|
2019-03-22 00:50:32 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
case 'x':
|
|
|
|
if (alternate_form) {
|
2018-10-27 14:43:03 +00:00
|
|
|
putch(bufptr, '0');
|
|
|
|
putch(bufptr, 'x');
|
|
|
|
ret += 2;
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
|
|
|
ret += print_hex(putch, bufptr, va_arg(ap, dword), 8);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
ret += print_hex(putch, bufptr, va_arg(ap, int), 4);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
ret += print_hex(putch, bufptr, va_arg(ap, int), 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
putch(bufptr, (char)va_arg(ap, int));
|
|
|
|
++ret;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
putch(bufptr, '%');
|
|
|
|
++ret;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
putch(bufptr, '0');
|
|
|
|
putch(bufptr, 'x');
|
|
|
|
ret += 2;
|
|
|
|
ret += print_hex(putch, bufptr, va_arg(ap, dword), 8);
|
|
|
|
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;
|
|
|
|
}
|