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
#define VERY_DEBUG 0
#define RUN_TESTS 0
#define RUN_TESTS 1
typedef struct Context_Extra {
int monkee;

6
main.c
View file

@ -43,14 +43,14 @@ int oogabooga_main(int argc, char **argv) {
(void)hello;
#ifdef DEBUG
printf("Hello, balls! (debug)\n");
print("Hello, balls! (debug)\n");
#endif
#ifdef RELEASE
printf("Hello, balls! (release)\n");
print("Hello, balls! (release)\n");
#endif
printf("Program exit as expected\n");
print("Program exit as expected\n");
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
#include <Windows.h>
#define OS_WINDOWS

View file

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

View file

@ -51,7 +51,7 @@ typedef struct string {
void push_temp_allocator();
#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) {
u64 len = 0;

View file

@ -299,7 +299,7 @@ void test_strings() {
// Test print and printf (visual inspection)
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");
print(const_string("Number: %d\n"), 1234);