Move userspace to a templated StrFormat.

This commit is contained in:
Drew Galbraith 2023-11-03 02:48:21 -07:00
parent d9df1212b7
commit 26b61db021
25 changed files with 263 additions and 217 deletions

View file

@ -1,7 +1,6 @@
add_library(c STATIC
src/malloc.cpp
src/stdio.cpp
src/string.cpp
)

View file

@ -1,10 +0,0 @@
#pragma once
#include <stdarg.h>
// int fprintf(FILE *stream, const char *format, ...);
int printf(const char *format, ...);
int sprintf(char *str, const char *format, ...);
// int vfprintf(FILE *stream, const char *format, va_list arg);
int vprintf(const char *format, va_list arg);
int vsprintf(char *str, const char *format, va_list arg);

View file

@ -1,126 +0,0 @@
#include "stdio.h"
#include <stdint.h>
namespace {
uint32_t num_chars(uint64_t num, uint8_t base) {
uint32_t width = 0;
while (num > 0) {
num /= base;
width++;
}
return width;
}
int sprint_base(char *str, uint64_t num, uint32_t base) {
uint32_t width = num_chars(num, base);
if (width == 0) {
*str = '0';
return 1;
}
uint64_t place = 1;
while (num > 0) {
// FIXME: We seem to have an issue with loading globals.
const char *kHexChars = "0123456789abcdef";
str[width - place] = kHexChars[num % base];
place++;
num /= base;
}
return width;
}
} // namespace
int sprintf(char *str, const char *format, ...) {
va_list arg;
va_start(arg, format);
int ret = vsprintf(str, format, arg);
va_end(arg);
return ret;
}
int vsprintf(char *str, const char *format, va_list arg) {
uint32_t chars = 0;
while (*format != '\0') {
if (*format != '%') {
*str = *format;
chars++;
str++;
format++;
continue;
}
format++;
switch (*format) {
case '%':
*(str++) = *(format++);
chars++;
break;
case 'l': {
switch (*(++format)) {
case 'x': {
int width = sprint_base(str, va_arg(arg, uint64_t), 16);
if (width == -1) {
return -1;
}
chars += width;
str += width;
format++;
break;
}
case 'u': {
int width = sprint_base(str, va_arg(arg, uint64_t), 10);
if (width == -1) {
return -1;
}
chars += width;
str += width;
format++;
break;
}
}
break;
}
case 'x': {
int width = sprint_base(str, va_arg(arg, uint32_t), 16);
if (width == -1) {
return -1;
}
chars += width;
str += width;
format++;
break;
}
case 'u': {
int width = sprint_base(str, va_arg(arg, uint32_t), 10);
if (width == -1) {
return -1;
}
chars += width;
str += width;
format++;
break;
}
case 's': {
char *instr = va_arg(arg, char *);
int width = 0;
while (*instr != '\0') {
*(str++) = *(instr++);
width++;
}
format++;
break;
}
default:
*(str++) = *(format++);
chars++;
}
}
*str = '\0';
chars++;
return chars;
}