25#include "pedigree/kernel/processor/types.h"
26#include "pedigree/kernel/utilities/assert.h"
27#include "pedigree/kernel/utilities/utility.h"
32#pragma GCC diagnostic ignored "-Wgnu-statement-expression"
36#define is_digit(c) ((c) >= '0' && (c) <= '9')
38static int skip_atoi(
const char** s) {
42 i = i * 10 + *((*s)++) -
'0';
54static char* number(
char* str, int64_t num,
int base,
int size,
int precision,
int type) {
55 char c, sign, tmp[36];
56 const char* digits =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
57 uint64_t magnitude = (uint64_t)num;
61 digits =
"0123456789abcdefghijklmnopqrstuvwxyz";
64 if (base < 2 || base > 36)
66 c = (type & ZEROPAD) ?
'0' :
' ';
67 if (type & SIGN && num < 0) {
70 magnitude = (uint64_t)0 - magnitude;
72 sign = (type & PLUS) ?
'+' : ((type & SPACE) ?
' ' : 0);
85 while (magnitude != 0) {
86 int d = (int)(magnitude % (uint64_t)base);
87 magnitude /= (uint64_t)base;
95 if (!(type & (ZEROPAD + LEFT)))
100 if (type & SPECIAL) {
103 else if (base == 16) {
111 while (i < precision--)
120int VStringFormat(
char* buf,
const char* fmt, va_list args) {
134 for (str = buf; *fmt; ++fmt) {
165 field_width = skip_atoi(&fmt);
166 else if (*fmt ==
'*') {
168 field_width = va_arg(args,
int);
169 if (field_width < 0) {
170 field_width = -field_width;
180 precision = skip_atoi(&fmt);
181 else if (*fmt ==
'*') {
183 precision = va_arg(args,
int);
191 if (*fmt ==
'h' || *fmt ==
'l' || *fmt ==
'L') {
199 while (--field_width > 0)
201 *str++ = (
unsigned char)va_arg(args,
int);
202 while (--field_width > 0)
207 s = va_arg(args,
char*);
208 len = StringLength(s);
211 else if (len > precision)
215 while (len < field_width--)
217 for (i = 0; i < len; ++i)
219 while (len < field_width--)
224 str = number(str, va_arg(args,
unsigned long), 8, field_width, precision, flags);
228 if (field_width == -1) {
229 field_width =
sizeof(
void*) * 2;
232 str = number(str, (uintptr_t)va_arg(args,
void*), 16, field_width, precision, flags);
239 str = number(str, va_arg(args,
unsigned long), 16, field_width, precision, flags);
247 str = number(str, va_arg(args,
unsigned long), 10, field_width, precision, flags);
251 ip = va_arg(args,
int*);