Make print a macro so we don't need to do const_string on fmt strings

This commit is contained in:
Charlie 2024-06-28 21:39:55 +02:00
parent 41795425d0
commit b8ae348ed9
6 changed files with 22 additions and 8 deletions

View file

@ -3,7 +3,7 @@
/// ///
// Build config stuff // Build config stuff
#define VERY_DEBUG 0 #define VERY_DEBUG 0
#define RUN_TESTS 0 #define RUN_TESTS 1
typedef struct Context_Extra { typedef struct Context_Extra {
int monkee; int monkee;

6
main.c
View file

@ -43,14 +43,14 @@ int oogabooga_main(int argc, char **argv) {
(void)hello; (void)hello;
#ifdef DEBUG #ifdef DEBUG
printf("Hello, balls! (debug)\n"); print("Hello, balls! (debug)\n");
#endif #endif
#ifdef RELEASE #ifdef RELEASE
printf("Hello, balls! (release)\n"); print("Hello, balls! (release)\n");
#endif #endif
printf("Program exit as expected\n"); print("Program exit as expected\n");
return 0; return 0;
} }

View file

@ -1,4 +1,14 @@
#if !defined(DEBUG) && !defined(RELEASE)
#ifdef _DEBUG
#define DEBUG
#elif defined(NDEBUG)
#define RELEASE
#endif
#endif
#ifdef _WIN32 #ifdef _WIN32
#include <Windows.h> #include <Windows.h>
#define OS_WINDOWS #define OS_WINDOWS

View file

@ -220,7 +220,7 @@ void print_va_list_buffered(const string fmt, va_list args) {
} }
// context.allocator (alloc & dealloc) // context.allocator (alloc & dealloc)
void print(const string fmt, ...) { void prints(const string fmt, ...) {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
print_va_list_buffered(fmt, args); print_va_list_buffered(fmt, args);
@ -237,7 +237,11 @@ void printf(const char* fmt, ...) {
va_end(args); va_end(args);
} }
#define FIRST_ARG(arg1, ...) arg1
#define print(...) _Generic((FIRST_ARG(__VA_ARGS__)), \
string: prints, \
default: printf \
)(__VA_ARGS__)
/// ///
/// ///
// Memory // Memory

View file

@ -51,7 +51,7 @@ typedef struct string {
void push_temp_allocator(); void push_temp_allocator();
#define cstr const_string #define cstr const_string
#define const_string(s) (string){ length_of_null_terminated_string(s), (u8*)s } #define const_string(s) ((string){ length_of_null_terminated_string(s), (u8*)s })
inline u64 length_of_null_terminated_string(const char* cstring) { inline u64 length_of_null_terminated_string(const char* cstring) {
u64 len = 0; u64 len = 0;

View file

@ -299,7 +299,7 @@ void test_strings() {
// Test print and printf (visual inspection) // Test print and printf (visual inspection)
printf("Expected output: Hello, World!\n"); printf("Expected output: Hello, World!\n");
print(const_string("Hello, %s!\n"), const_string("World")); print("Hello, %s!\n", const_string("World"));
printf("Expected output: Number: 1234\n"); printf("Expected output: Number: 1234\n");
print(const_string("Number: %d\n"), 1234); print(const_string("Number: %d\n"), 1234);