4coder/custom/4coder_stringf.cpp

80 lines
2.1 KiB
C++
Raw Normal View History

2019-06-01 23:58:28 +00:00
/*
* Printf style operations for strings and what-not
*/
// TOP
2019-10-05 02:48:05 +00:00
#if !defined(FCODER_STRINGF_CPP)
#define FCODER_STRINGF_CPP
2019-06-01 23:58:28 +00:00
#include <stdarg.h>
#include <stdio.h>
static String_Const_u8
2019-06-18 22:56:09 +00:00
push_stringfv(Arena *arena, char *format, va_list args){
va_list args2;
va_copy(args2, args);
2019-06-01 23:58:28 +00:00
i32 size = vsnprintf(0, 0, format, args);
String_Const_u8 result = string_const_u8_push(arena, size + 1);
vsnprintf((char*)result.str, (size_t)result.size, format, args2);
2019-06-01 23:58:28 +00:00
result.size -= 1;
result.str[result.size] = 0;
return(result);
}
static String_Const_u8
2019-06-18 22:56:09 +00:00
push_stringf(Arena *arena, char *format, ...){
2019-06-01 23:58:28 +00:00
va_list args;
va_start(args, format);
String_Const_u8 result = push_stringfv(arena, format, args);
2019-06-01 23:58:28 +00:00
va_end(args);
return(result);
}
static String_Const_u8
2019-06-18 22:56:09 +00:00
push_u8_stringfv(Arena *arena, char *format, va_list args){
return(push_stringfv(arena, format, args));
2019-06-01 23:58:28 +00:00
}
static String_Const_u8
2019-06-18 22:56:09 +00:00
push_u8_stringf(Arena *arena, char *format, ...){
2019-06-01 23:58:28 +00:00
va_list args;
va_start(args, format);
String_Const_u8 result = push_stringfv(arena, format, args);
2019-06-01 23:58:28 +00:00
va_end(args);
return(result);
}
static void
string_list_pushfv(Arena *arena, List_String_Const_char *list, char *format, va_list args){
String_Const_u8 string = push_stringfv(arena, format, args);
if (arena->alignment < sizeof(u64)){
push_align(arena, sizeof(u64));
2019-06-01 23:58:28 +00:00
}
string_list_push(arena, list, SCchar(string));
2019-06-01 23:58:28 +00:00
}
static void
string_list_pushf(Arena *arena, List_String_Const_char *list, char *format, ...){
va_list args;
va_start(args, format);
string_list_pushfv(arena, list, format, args);
va_end(args);
}
static void
string_list_pushfv(Arena *arena, List_String_Const_u8 *list, char *format, va_list args){
2019-06-18 22:56:09 +00:00
String_Const_u8 string = push_u8_stringfv(arena, format, args);
if (arena->alignment < sizeof(u64)){
push_align(arena, sizeof(u64));
2019-06-01 23:58:28 +00:00
}
string_list_push(arena, list, string);
}
static void
string_list_pushf(Arena *arena, List_String_Const_u8 *list, char *format, ...){
va_list args;
va_start(args, format);
string_list_pushfv(arena, list, format, args);
va_end(args);
}
2019-10-05 02:48:05 +00:00
#endif
2019-06-01 23:58:28 +00:00
// BOTTOM