1 #include <kiba/format/format.h>
4 #include <kiba/containers/hash_table.h>
6 #include <kiba/format/defaults/boolean.h>
7 #include <kiba/format/defaults/floating.h>
8 #include <kiba/format/defaults/integer.h>
9 #include <kiba/format/defaults/pointer.h>
10 #include <kiba/format/defaults/string.h>
15 b8 format_system_initialized =
false;
17 b8 format_initialize(
void) {
19 && hash_table_create(&format_read_functions, 64, 0.8, &format_allocator)
20 && hash_table_create(&format_write_functions, 64, 0.8, &format_allocator)
21 && format_register_read_function(
"u64", &format_read_u64)
22 && format_register_write_function(
"u64", &format_write_u64)
23 && format_register_read_function(
"i64", &format_read_i64)
24 && format_register_write_function(
"i64", &format_write_i64)
25 && format_register_read_function(
"u32", &format_read_u32)
26 && format_register_write_function(
"u32", &format_write_u32)
27 && format_register_read_function(
"i32", &format_read_i32)
28 && format_register_write_function(
"i32", &format_write_i32)
29 && format_register_read_function(
"u16", &format_read_u16)
30 && format_register_write_function(
"u16", &format_write_u16)
31 && format_register_read_function(
"i16", &format_read_i16)
32 && format_register_write_function(
"i16", &format_write_i16)
33 && format_register_read_function(
"u8", &format_read_u8)
34 && format_register_write_function(
"u8", &format_write_u8)
35 && format_register_read_function(
"i8", &format_read_i8)
36 && format_register_write_function(
"i8", &format_write_i8)
37 && format_register_read_function(
"usize", &format_read_usize)
38 && format_register_write_function(
"usize", &format_write_usize)
39 && format_register_read_function(
"uptr", &format_read_uptr)
40 && format_register_write_function(
"uptr", &format_write_uptr)
41 && format_register_read_function(
"pointer", &format_read_pointer)
42 && format_register_write_function(
"pointer", &format_write_pointer)
43 && format_register_read_function(
"b8", &format_read_b8)
44 && format_register_write_function(
"b8", &format_write_b8)
45 && format_register_read_function(
"b32", &format_read_b32)
46 && format_register_write_function(
"b32", &format_write_b32)
47 && format_register_read_function(
"f32", &format_read_f32)
48 && format_register_write_function(
"f32", &format_write_f32)
49 && format_register_read_function(
"f64", &format_read_f64)
50 && format_register_write_function(
"f64", &format_write_f64)
51 && format_register_write_function(
"string_view", &format_write_string_view)
52 && format_register_write_function(
"string", &format_write_string)
53 && format_register_write_function(
"raw_string", &format_write_raw_string)
54 && format_register_write_function(
"char", &format_write_char) && (format_system_initialized =
true);
57 b8 format_register_read_function(
const char *placeholder, format_function func) {
58 return hash_table_set(&format_read_functions,
64 b8 format_register_write_function(
const char *placeholder, format_function func) {
65 return hash_table_set(&format_write_functions,
71 void format_shutdown(
void) {
72 format_system_initialized =
false;
73 hash_table_destroy(&format_read_functions);
74 hash_table_destroy(&format_write_functions);
82 "static format buffer must be creatable from options string view");
86 while (format_buffer_peak_char(&buf, &c)) {
89 KB_WARN(
"invalid options string (multiple dots): {string_view}", view);
93 format_buffer_advance(&buf);
95 if (pre_dot && format_read_unsigned_decimal(&buf, format_default_format_options(), &temp)) {
96 options->width = (u32) temp;
97 if (format_buffer_peak_char(&buf, &c)) {
100 options->alignment = FORMAT_ALIGN_RIGHT;
101 format_buffer_advance(&buf);
104 options->alignment = FORMAT_ALIGN_LEFT;
105 format_buffer_advance(&buf);
108 options->alignment = FORMAT_ALIGN_CENTER;
109 format_buffer_advance(&buf);
114 KB_WARN(
"invalid options string (unrecognized alignment {char}): {string_view}", c, view);
120 if (!pre_dot && format_read_unsigned_decimal(&buf, format_default_format_options(), &temp)) {
121 options->precision = (u32) temp;
124 KB_WARN(
"invalid options string (invalid format): {string_view}", view);
134 if (!format_process_options(options_view, &options)) {
141 case FORMAT_BUFFER_READ:
142 table = &format_read_functions;
144 case FORMAT_BUFFER_WRITE:
145 table = &format_write_functions;
148 KB_ERROR(
"invalid mode found for format_buffer: {i32}", buf->mode);
153 format_function f = (format_function) raw;
154 return f !=
KB_NULL && f(buf, options, args);
156 KB_WARN(
"unrecognized formatting placeholder {string_view}", view);
163 b8 ok = format_va_list(buf, fmt, &args);
168 b8 format_va_list(
format_buffer *buf,
const char *fmt, VA_LIST *args) {
169 if (!format_system_initialized) {
173 &format_buffer_add_string_view};
174 b8 (*char_funcs[2])(
format_buffer *, char) = {&format_buffer_match_char, &format_buffer_add_char};
180 if (!from_placeholder_view.
is_valid) {
181 ok &= string_view_funcs[buf->mode](buf, fmt_view);
182 fmt_view = from_placeholder_view;
183 }
else if (from_placeholder_view.
offset > 1 && fmt[from_placeholder_view.
offset - 2] ==
'\\') {
184 ok &= string_view_funcs[buf->mode](
187 && char_funcs[buf->mode](buf,
'{');
188 fmt_view = from_placeholder_view;
190 ok &= string_view_funcs[buf->mode](
194 ok &= placeholder_view.is_valid;
196 ok &= format_process_placeholder(buf, placeholder_view, args);
b8 allocator_create(allocator *alloc, allocator_type type, usize size)
Create an allocator of a specific type.
void allocator_destroy(allocator *alloc)
Destroy an allocator.
Central header providing allocator functionality.
string_view string_view_after_next_char(const string_view view, const char c)
Create a new string_view of an existing string_view after a certain character.
string_view string_view_at_offset(const string_view src, usize offset)
Create a new string_view of an existing string_view.
const char * string_view_data(const string_view view)
Get raw pointer to beginning of the string_view data.
string_view string_view_subview(const string_view view, usize start, usize length)
Create a new string_view of an existing string_view.
string_view string_view_from_string(const string str)
Create a new string view of a whole string.
string_view string_view_until_next_char(const string_view view, const char c)
Create a new string_view of an existing string_view until a certain character.
string string_from_raw(const char *raw)
Construct a string from a raw string.
struct string_view string_view
Non owning views on actual strings.
#define KB_NULL
Value of an invalid ptr (nullptr).
#define KB_ASSERT(expr,...)
Perform runtime assertion and log failures.
#define KB_WARN(...)
Log entry with warn log level.
#define KB_ERROR(...)
Log entry with error log level.
Central allocator structure.
Non owning views on actual strings.
b8 is_valid
Indicates if the view is valid and can be used/accessed.
usize length
The length of the view.
usize offset
The offset into the string.