From 6978bb2afea908970aa937ae8f6fd2d38e9f1723 Mon Sep 17 00:00:00 2001 From: Timerix Date: Thu, 27 Nov 2025 01:32:34 +0500 Subject: [PATCH] changed TomlValue.value to anonymous union --- include/tlibtoml.h | 2 +- src/TomlTable.c | 14 ++++----- src/TomlValue.c | 38 ++++++++++++------------ src/toml_parse/toml_parse_array.c | 2 +- src/toml_parse/toml_parse_inline_table.c | 2 +- src/toml_parse/toml_walk_table_path.c | 24 +++++++-------- tests/main.c | 12 ++++---- 7 files changed, 47 insertions(+), 47 deletions(-) diff --git a/include/tlibtoml.h b/include/tlibtoml.h index 4c009d9..3e46f7f 100644 --- a/include/tlibtoml.h +++ b/include/tlibtoml.h @@ -102,7 +102,7 @@ typedef struct TomlValue { TomlArray* array; TomlTable* table; TomlDateTime* dt; - } value; + }; } TomlValue; List_declare(TomlValue); diff --git a/src/TomlTable.c b/src/TomlTable.c index 036f255..9e1b95a 100644 --- a/src/TomlTable.c +++ b/src/TomlTable.c @@ -44,7 +44,7 @@ Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key) TomlValue* v = TomlTable_get(self, key); try_assert_value_found(v, key); try_assert_value_type(v, key, TLIBTOML_TABLE); - Return RESULT_VALUE(p, v->value.table); + Return RESULT_VALUE(p, v->table); } Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key) @@ -53,7 +53,7 @@ Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key) TomlValue* v = TomlTable_get(self, key); try_assert_value_found(v, key); try_assert_value_type(v, key, TLIBTOML_ARRAY); - Return RESULT_VALUE(p, v->value.array); + Return RESULT_VALUE(p, v->array); } Result(str*) TomlTable_get_str(const TomlTable* self, str key) @@ -62,7 +62,7 @@ Result(str*) TomlTable_get_str(const TomlTable* self, str key) TomlValue* v = TomlTable_get(self, key); try_assert_value_found(v, key); try_assert_value_type(v, key, TLIBTOML_STRING); - Return RESULT_VALUE(p, v->value.s); + Return RESULT_VALUE(p, v->s); } Result(i64) TomlTable_get_integer(const TomlTable* self, str key) @@ -71,7 +71,7 @@ Result(i64) TomlTable_get_integer(const TomlTable* self, str key) TomlValue* v = TomlTable_get(self, key); try_assert_value_found(v, key); try_assert_value_type(v, key, TLIBTOML_INTEGER); - Return RESULT_VALUE(i, v->value.i); + Return RESULT_VALUE(i, v->i); } Result(f64) TomlTable_get_float(const TomlTable* self, str key) @@ -80,7 +80,7 @@ Result(f64) TomlTable_get_float(const TomlTable* self, str key) TomlValue* v = TomlTable_get(self, key); try_assert_value_found(v, key); try_assert_value_type(v, key, TLIBTOML_FLOAT); - Return RESULT_VALUE(f, v->value.f); + Return RESULT_VALUE(f, v->f); } Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key) @@ -89,7 +89,7 @@ Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key) TomlValue* v = TomlTable_get(self, key); try_assert_value_found(v, key); try_assert_value_type(v, key, TLIBTOML_DATETIME); - Return RESULT_VALUE(p, v->value.dt); + Return RESULT_VALUE(p, v->dt); } Result(bool) TomlTable_get_bool(const TomlTable* self, str key) @@ -98,5 +98,5 @@ Result(bool) TomlTable_get_bool(const TomlTable* self, str key) TomlValue* v = TomlTable_get(self, key); try_assert_value_found(v, key); try_assert_value_type(v, key, TLIBTOML_BOOLEAN); - Return RESULT_VALUE(i, v->value.b); + Return RESULT_VALUE(i, v->b); } diff --git a/src/TomlValue.c b/src/TomlValue.c index 52cb2ca..c211a9e 100644 --- a/src/TomlValue.c +++ b/src/TomlValue.c @@ -32,26 +32,26 @@ TomlValue TomlValue_new(TomlType type) assert(false && "invalid type"); break; case TLIBTOML_TABLE: - value.value.table = NULL; + value.table = NULL; break; case TLIBTOML_ARRAY: - value.value.array = NULL; + value.array = NULL; break; case TLIBTOML_STRING: - value.value.s = NULL; + value.s = NULL; break; case TLIBTOML_INTEGER: - value.value.i = 0; + value.i = 0; break; case TLIBTOML_FLOAT: - value.value.f = 0.0; + value.f = 0.0; break; case TLIBTOML_BOOLEAN: - value.value.b = false; + value.b = false; break; case TLIBTOML_DATETIME: - value.value.dt = (TomlDateTime*)malloc(sizeof(TomlDateTime)); - memset(value.value.dt, 0, sizeof(TomlDateTime)); + value.dt = (TomlDateTime*)malloc(sizeof(TomlDateTime)); + memset(value.dt, 0, sizeof(TomlDateTime)); break; } return value; @@ -64,8 +64,8 @@ TomlValue TomlValue_copy_str(str s) TomlValue TomlValue_move_str(str s){ TomlValue value = {0}; - value.value.s = (str*)malloc(sizeof(str)); - *value.value.s = s; + value.s = (str*)malloc(sizeof(str)); + *value.s = s; value.type = TLIBTOML_STRING; return value; } @@ -73,7 +73,7 @@ TomlValue TomlValue_move_str(str s){ TomlValue TomlValue_new_table(void) { TomlValue value = {0}; - value.value.table = TomlTable_new(); + value.table = TomlTable_new(); value.type = TLIBTOML_TABLE; return value; } @@ -81,7 +81,7 @@ TomlValue TomlValue_new_table(void) TomlValue TomlValue_new_array(void) { TomlValue value = {0}; - value.value.array = TomlArray_new(); + value.array = TomlArray_new(); value.type = TLIBTOML_ARRAY; return value; } @@ -89,7 +89,7 @@ TomlValue TomlValue_new_array(void) TomlValue TomlValue_new_integer(i64 integer) { TomlValue value = {0}; - value.value.i = integer; + value.i = integer; value.type = TLIBTOML_INTEGER; return value; } @@ -97,7 +97,7 @@ TomlValue TomlValue_new_integer(i64 integer) TomlValue TomlValue_new_float(f64 float_) { TomlValue value = {0}; - value.value.f = float_; + value.f = float_; value.type = TLIBTOML_FLOAT; return value; } @@ -110,7 +110,7 @@ TomlValue TomlValue_new_datetime(void) TomlValue TomlValue_new_bool(bool b) { TomlValue value = {0}; - value.value.b = b; + value.b = b; value.type = TLIBTOML_BOOLEAN; return value; } @@ -122,16 +122,16 @@ void TomlValue_destroy(TomlValue* self) switch (self->type) { case TLIBTOML_STRING: - str_free(self->value.s); + str_free(self->s); break; case TLIBTOML_TABLE: - TomlTable_free(self->value.table); + TomlTable_free(self->table); break; case TLIBTOML_ARRAY: - TomlArray_free(self->value.array); + TomlArray_free(self->array); break; case TLIBTOML_DATETIME: - free(self->value.dt); + free(self->dt); break; default: break; diff --git a/src/toml_parse/toml_parse_array.c b/src/toml_parse/toml_parse_array.c index 96b7b39..b8f9750 100644 --- a/src/toml_parse/toml_parse_array.c +++ b/src/toml_parse/toml_parse_array.c @@ -30,7 +30,7 @@ Result(void) toml_parse_array(TomlParser* self, TomlValue* out_value) TomlValue value; try_void(toml_parse_value(self, &value)); - TomlArray_append(array_value.value.array, value); + TomlArray_append(array_value.array, value); while (self->ptr < self->end) { if (isspace(*self->ptr)) { diff --git a/src/toml_parse/toml_parse_inline_table.c b/src/toml_parse/toml_parse_inline_table.c index 5120243..6f78fa9 100644 --- a/src/toml_parse/toml_parse_inline_table.c +++ b/src/toml_parse/toml_parse_inline_table.c @@ -71,7 +71,7 @@ Result(void) toml_parse_inline_table(TomlParser* self, TomlValue* out_value) TomlValue value; try_void(toml_parse_value(self, &value)); - TomlTable_set(table_value.value.table, key, value); + TomlTable_set(table_value.table, key, value); str_destroy(key); while (self->ptr < self->end && (*self->ptr == ' ' ||* self->ptr == '\t')) { diff --git a/src/toml_parse/toml_walk_table_path.c b/src/toml_parse/toml_walk_table_path.c index ea251a8..5376f7e 100644 --- a/src/toml_parse/toml_walk_table_path.c +++ b/src/toml_parse/toml_walk_table_path.c @@ -14,32 +14,32 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table, if (is_array) { u64 i = 0; for (; i < key_path->len - 1; i++) { - str part = *key_path->data[i].value.s; + str part = *key_path->data[i].s; TomlValue* t = TomlTable_get(real_table, part); if (t == NULL) { if (create_if_not_exist) { TomlValue new_table_value = TomlValue_new_table(); TomlTable_set(real_table, part, new_table_value); - real_table = new_table_value.value.table; + real_table = new_table_value.table; } else { Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: not found key '" FMT_str "'", parser->filename, parser->lineno, parser->colno, part.len, part.data); } } else { - real_table = t->value.table; + real_table = t->table; } } - str part = *key_path->data[i].value.s; + str part = *key_path->data[i].s; TomlValue* t = TomlTable_get(real_table, part); if (t == NULL) { if (create_if_not_exist) { TomlValue array_value = TomlValue_new_array(); TomlValue new_table_value = TomlValue_new_table(); - TomlArray_append(array_value.value.array, new_table_value); + TomlArray_append(array_value.array, new_table_value); TomlTable_set(real_table, part, array_value); - real_table = new_table_value.value.table; + real_table = new_table_value.table; } else { Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: not found key '" FMT_str "'", @@ -53,18 +53,18 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table, } TomlValue new_table_value = TomlValue_new_table(); - TomlArray_append(t->value.array, new_table_value); - real_table = new_table_value.value.table; + TomlArray_append(t->array, new_table_value); + real_table = new_table_value.table; } } else { for (u64 i = 0; i < key_path->len; i++) { - str part = *key_path->data[i].value.s; + str part = *key_path->data[i].s; TomlValue* t = TomlTable_get(real_table, part); if (t == NULL) { if (create_if_not_exist) { TomlValue new_table_value = TomlValue_new_table(); TomlTable_set(real_table, part, new_table_value); - real_table = new_table_value.value.table; + real_table = new_table_value.table; } else { Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: not found key '" FMT_str "'", @@ -72,9 +72,9 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table, } } else { if (t->type == TLIBTOML_ARRAY) { - real_table = t->value.array->data[t->value.array->len - 1].value.table; + real_table = t->array->data[t->array->len - 1].table; } else if (t->type == TLIBTOML_TABLE) { - real_table = t->value.table; + real_table = t->table; } } } diff --git a/tests/main.c b/tests/main.c index 9d95eb7..4c56ae4 100644 --- a/tests/main.c +++ b/tests/main.c @@ -32,25 +32,25 @@ void print_value(const TomlValue* value) assert(false && "invalid type"); break; case TLIBTOML_TABLE: - print_table(value->value.table); + print_table(value->table); break; case TLIBTOML_ARRAY: - print_array(value->value.array); + print_array(value->array); break; case TLIBTOML_STRING: - printf("'" FMT_str "'", value->value.s->len, value->value.s->data); + printf("'" FMT_str "'", value->s->len, value->s->data); break; case TLIBTOML_INTEGER: - printf("%" PRId64, value->value.i); + printf("%" PRId64, value->i); break; case TLIBTOML_FLOAT: - printf("%f", value->value.f); + printf("%f", value->f); break; case TLIBTOML_DATETIME: printf("(datetime)"); break; case TLIBTOML_BOOLEAN: - printf("%s", value->value.b ? "true" : "false"); + printf("%s", value->b ? "true" : "false"); break; } }