replaced numeric types to those from tlibc

This commit is contained in:
Timerix 2025-11-10 00:11:41 +05:00
parent a5aff56fb1
commit f889990728
3 changed files with 347 additions and 385 deletions

View File

@ -11,18 +11,8 @@ extern "C" {
#include "tlibc/std.h"
#include <time.h>
#if defined(__cplusplus) && __cplusplus >= 201103L
#define ATTRIBUTE_THREAD_LOCAL thread_local
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define ATTRIBUTE_THREAD_LOCAL _Thread_local
#elif defined(_MSC_VER)
#define ATTRIBUTE_THREAD_LOCAL __declspec(thread)
#else
#define ATTRIBUTE_THREAD_LOCAL __thread
#endif
#define TOML_FALSE 0
#define TOML_TRUE 1
#define false 0
#define true 1
typedef enum {
TOML_OK,
@ -36,28 +26,28 @@ typedef enum {
typedef struct {
TomlErrCode code;
char* message;
int _is_literal;
i32 _is_literal;
} TomlErr;
typedef struct {
char* str;
size_t len;
size_t _capacity;
u64 len;
u64 _capacity;
} TomlString;
typedef struct _TomlValue TomlValue;
typedef struct {
TomlValue** elements;
size_t len;
size_t _capacity;
u64 len;
u64 _capacity;
} TomlArray;
typedef struct _TomlKeyValue TomlKeyValue;
typedef struct {
size_t _capacity;
size_t len;
u64 _capacity;
u64 len;
TomlKeyValue* _keyvals;
} TomlTable;
@ -82,14 +72,10 @@ struct _TomlValue {
TomlTable* table;
TomlArray* array;
TomlString* string;
#if defined(_MSC_VER) || defined(__APPLE__)
long long integer;
#else
long integer;
#endif
double float_;
i64 integer;
f64 float_;
struct tm datetime;
int boolean;
bool boolean;
} value;
};
@ -99,59 +85,55 @@ struct _TomlKeyValue {
};
typedef struct {
void* (*malloc)(void *context, size_t size);
void* (*realloc)(void *context, void *p, size_t size);
void* (*malloc)(void* context, u64 size);
void* (*realloc)(void* context, void* p, u64 size);
void (*free)(void* context, void* p);
} TomlAllocFuncs;
void toml_set_allocator(void* context, const TomlAllocFuncs *funcs);
void* toml_malloc(size_t size);
void* toml_realloc(void *p, size_t size);
void* toml_malloc(u64 size);
void* toml_realloc(void* p, u64 size);
void toml_free(void* p);
char* toml_strdup(const char *str);
char* toml_strndup(const char *str, size_t n);
int toml_vasprintf(char **str, const char *format, va_list args);
int toml_asprintf(char **str, const char *format, ...);
char* toml_strdup(cstr str);
char* toml_strndup(cstr str, u64 n);
i32 toml_vasprintf(char** str, cstr format, va_list args);
i32 toml_asprintf(char** str, cstr format, ...);
const TomlErr* toml_err(void);
void toml_err_clear(void);
TomlString* toml_string_new(void);
TomlString* toml_string_from_str(const char *str);
TomlString* toml_string_from_nstr(const char *str, size_t len);
TomlString* toml_string_from_str(cstr str);
TomlString* toml_string_from_nstr(cstr str, u64 len);
void toml_string_append_char(TomlString* self, char ch);
void toml_string_append_str(TomlString *self, const char *str);
void toml_string_append_nstr(TomlString *self, const char *str, size_t len);
void toml_string_append_str(TomlString* self, cstr str);
void toml_string_append_nstr(TomlString* self, cstr str, u64 len);
TomlString* toml_string_clone(const TomlString* self);
void toml_string_free(TomlString* self);
int toml_string_equals(const TomlString *self, const TomlString *other);
i32 toml_string_equals(const TomlString* self, const TomlString* other);
TomlTable* toml_table_new(void);
void toml_table_free(TomlTable* self);
void toml_table_set_by_string(TomlTable* self, TomlString* key, TomlValue* value);
TomlValue* toml_table_get_by_string(const TomlTable* self, const TomlString* key);
void toml_table_set(TomlTable *self, const char *key, TomlValue *value);
void toml_table_setn(TomlTable *self, const char *key, size_t key_len, TomlValue *value);
TomlValue* toml_table_get(const TomlTable *self, const char *key);
TomlTable* toml_table_get_as_table(const TomlTable *self, const char *key);
TomlArray* toml_table_get_as_array(const TomlTable *self, const char *key);
TomlString* toml_table_get_as_string(const TomlTable *self, const char *key);
#if defined(_MSC_VER) || defined(__APPLE__)
long long toml_table_get_as_integer(const TomlTable *self, const char *key);
#else
long toml_table_get_as_integer(const TomlTable *self, const char *key);
#endif
double toml_table_get_as_float(const TomlTable *self, const char *key);
const struct tm* toml_table_get_as_datetime(const TomlTable *self, const char *key);
int toml_table_get_as_boolean(const TomlTable *self, const char *key);
TomlValue* toml_table_getn(const TomlTable *self, const char *key, size_t key_len);
void toml_table_set(TomlTable* self, cstr key, TomlValue* value);
void toml_table_setn(TomlTable* self, cstr key, u64 key_len, TomlValue* value);
TomlValue* toml_table_get(const TomlTable* self, cstr key);
TomlTable* toml_table_get_as_table(const TomlTable* self, cstr key);
TomlArray* toml_table_get_as_array(const TomlTable* self, cstr key);
TomlString* toml_table_get_as_string(const TomlTable* self, cstr key);
i64 toml_table_get_as_integer(const TomlTable* self, cstr key);
f64 toml_table_get_as_float(const TomlTable* self, cstr key);
const struct tm* toml_table_get_as_datetime(const TomlTable* self, cstr key);
i32 toml_table_get_as_boolean(const TomlTable* self, cstr key);
TomlValue* toml_table_getn(const TomlTable* self, cstr key, u64 key_len);
TomlTableIter toml_table_iter_new(TomlTable* table);
TomlKeyValue* toml_table_iter_get(TomlTableIter* self);
int toml_table_iter_has_next(TomlTableIter *self);
i32 toml_table_iter_has_next(TomlTableIter* self);
void toml_table_iter_next(TomlTableIter* self);
TomlArray* toml_array_new(void);
@ -162,22 +144,18 @@ TomlValue* toml_value_new(TomlType type);
TomlValue* toml_value_new_string(TomlType type);
TomlValue* toml_value_new_table(void);
TomlValue* toml_value_new_array(void);
#if defined(_MSC_VER) || defined(__APPLE__)
TomlValue *toml_value_new_integer(long long integer);
#else
TomlValue *toml_value_new_integer(long integer);
#endif
TomlValue* toml_value_new_float(double flt);
TomlValue* toml_value_new_integer(i64 integer);
TomlValue* toml_value_new_float(f64 flt);
TomlValue* toml_value_new_datetime(void);
TomlValue* toml_value_new_boolean(int boolean);
TomlValue* toml_value_from_str(const char *str);
TomlValue* toml_value_from_nstr(const char *str, size_t len);
TomlValue* toml_value_new_boolean(i32 boolean);
TomlValue* toml_value_from_str(cstr str);
TomlValue* toml_value_from_nstr(cstr str, u64 len);
void toml_value_free(TomlValue* self);
TomlTable* toml_load_str(const char *str);
TomlTable* toml_load_nstr(const char *str, size_t len);
TomlTable* toml_load_str(cstr str);
TomlTable* toml_load_nstr(cstr str, u64 len);
TomlTable* toml_load_file(FILE* file);
TomlTable* toml_load_filename(const char *filename);
TomlTable* toml_load_filename(cstr filename);
/* TODO: implement dump functions
char* toml_dump_str(const TomlTable* self, TomlErr *err);

View File

@ -10,9 +10,9 @@
#include <pthread.h>
#include "tlibtoml/toml.h"
static ATTRIBUTE_THREAD_LOCAL TomlErr g_err = {TOML_OK, (char*)"", TOML_TRUE};
static ATTRIBUTE_THREAD_LOCAL TomlErr g_err = {TOML_OK, (char*)"", true};
static void* toml_default_malloc(void *context, size_t size)
static void* toml_default_malloc(void* context, u64 size)
{
(void)context;
void* p = malloc(size);
@ -20,7 +20,7 @@ static void* toml_default_malloc(void *context, size_t size)
return p;
}
static void* toml_default_realloc(void *context, void *p, size_t size)
static void* toml_default_realloc(void* context, void* p, u64 size)
{
(void)context;
void* ptr = realloc(p, size);
@ -49,12 +49,12 @@ void toml_set_allocator(void *context, const TomlAllocFuncs *funcs)
g_alloc_funcs = funcs;
}
void* toml_malloc(size_t size)
void* toml_malloc(u64 size)
{
return g_alloc_funcs->malloc(g_alloc_context, size);
}
void* toml_realloc(void *p, size_t size)
void* toml_realloc(void* p, u64 size)
{
return g_alloc_funcs->realloc(g_alloc_context, p, size);
}
@ -66,9 +66,9 @@ void toml_free(void *p)
}
}
char* toml_strdup(const char *str)
char* toml_strdup(cstr str)
{
size_t len = strlen(str) + 1;
u64 len = strlen(str) + 1;
void* new = toml_malloc(len);
if (new == NULL)
return NULL;
@ -76,7 +76,7 @@ char* toml_strdup(const char *str)
return memcpy(new, str, len);
}
char *toml_strndup(const char *str, size_t n)
char* toml_strndup(cstr str, u64 n)
{
char* result = toml_malloc(n + 1);
if (result == NULL)
@ -86,31 +86,31 @@ char *toml_strndup(const char *str, size_t n)
return memcpy(result, str, n);
}
int toml_vasprintf(char **str, const char *format, va_list args)
i32 toml_vasprintf(char** str, cstr format, va_list args)
{
int size = 0;
i32 size = 0;
va_list args_copy;
va_copy(args_copy, args);
size = vsnprintf(NULL, (size_t)size, format, args_copy);
size = vsnprintf(NULL, (u64)size, format, args_copy);
va_end(args_copy);
if (size < 0) {
return size;
}
*str = toml_malloc((size_t)size + 1);
*str = toml_malloc((u64)size + 1);
if (*str == NULL)
return -1;
return vsprintf(*str, format, args);
}
int toml_asprintf(char **str, const char *format, ...)
i32 toml_asprintf(char** str, cstr format, ...)
{
va_list args;
va_start(args, format);
int size = toml_vasprintf(str, format, args);
i32 size = toml_vasprintf(str, format, args);
va_end(args);
return size;
}
@ -128,46 +128,38 @@ void toml_err_clear(void)
}
g_err.code = TOML_OK;
g_err.message = (char*)"";
g_err._is_literal = TOML_TRUE;
g_err._is_literal = true;
}
}
static inline void toml_err_set(TomlErrCode code, const char *format, ...)
static inline void toml_err_set(TomlErrCode code, cstr format, ...)
{
assert(g_err.code == TOML_OK);
va_list args;
va_start(args, format);
g_err.code = code;
toml_vasprintf(&g_err.message, format, args);
g_err._is_literal = TOML_FALSE;
g_err._is_literal = false;
va_end(args);
}
static inline void toml_err_set_literal(TomlErrCode code, const char *message)
static inline void toml_err_set_literal(TomlErrCode code, cstr message)
{
assert(g_err.code == TOML_OK);
g_err.code = code;
g_err.message = (char* )message;
g_err._is_literal = TOML_TRUE;
g_err._is_literal = true;
}
static inline size_t toml_roundup_pow_of_two_size_t(size_t x)
static inline u64 toml_roundup_pow_of_two_u64(u64 v)
{
size_t v = x;
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
#if SIZE_MAX == 0xffff
v |= v >> 8;
#elif SIZE_MAX == 0xffffffff
v |= v >> 8;
v |= v >> 16;
#elif SIZE_MAX == 0xffffffffffffffffll
v |= v >> 8;
v |= v >> 16;
v |= v >> 32;
#endif
v++;
return v;
}
@ -181,24 +173,24 @@ TomlString *toml_string_new(void)
return self;
}
TomlString *toml_string_from_str(const char *str)
TomlString* toml_string_from_str(cstr str)
{
TomlString* self = toml_string_new();
toml_string_append_str(self, str);
return self;
}
TomlString *toml_string_from_nstr(const char *str, size_t len)
TomlString* toml_string_from_nstr(cstr str, u64 len)
{
TomlString* self = toml_string_new();
toml_string_append_nstr(self, str, len);
return self;
}
static inline void toml_string_expand_if_necessary(TomlString *self, size_t len_to_add)
static inline void toml_string_expand_if_necessary(TomlString* self, u64 len_to_add)
{
if (self->len + len_to_add + 1 > self->_capacity) {
size_t new_capacity = toml_roundup_pow_of_two_size_t(self->len + len_to_add + 1);
u64 new_capacity = toml_roundup_pow_of_two_u64(self->len + len_to_add + 1);
new_capacity = new_capacity >= 8 ? new_capacity : 8;
self->str = toml_realloc(self->str, new_capacity);
self->_capacity = new_capacity;
@ -213,15 +205,15 @@ void toml_string_append_char(TomlString *self, char ch)
self->len++;
}
void toml_string_append_str(TomlString *self, const char *str)
void toml_string_append_str(TomlString* self, cstr str)
{
size_t len = strlen(str);
u64 len = strlen(str);
toml_string_expand_if_necessary(self, len);
memcpy(&self->str[self->len], str, len + 1);
self->len += len;
}
void toml_string_append_nstr(TomlString *self, const char *str, size_t len)
void toml_string_append_nstr(TomlString* self, cstr str, u64 len)
{
toml_string_expand_if_necessary(self, len);
memcpy(&self->str[self->len], str, len);
@ -242,27 +234,27 @@ TomlString *toml_string_clone(const TomlString *self)
return toml_string_from_nstr(self->str, self->len);
}
int toml_string_equals(const TomlString *self, const TomlString *other)
i32 toml_string_equals(const TomlString* self, const TomlString* other)
{
if (self == other) {
return TOML_TRUE;
return true;
}
if (self->len != other->len) {
return TOML_FALSE;
return false;
}
if (self->str == other->str) {
return TOML_TRUE;
return true;
}
for (size_t i = 0; i < self->len; i++) {
for (u64 i = 0; i < self->len; i++) {
if (self->str[i] != other->str[i]) {
return TOML_FALSE;
return false;
}
}
return TOML_TRUE;
return true;
}
TomlTable* toml_table_new(void)
@ -277,7 +269,7 @@ TomlTable *toml_table_new(void)
void toml_table_free(TomlTable* self)
{
if (self != NULL) {
for (size_t i = 0; i < self->len; i++) {
for (u64 i = 0; i < self->len; i++) {
toml_string_free(self->_keyvals[i].key);
toml_value_free(self->_keyvals[i].value);
}
@ -289,7 +281,7 @@ void toml_table_free(TomlTable *self)
void toml_table_expand_if_necessary(TomlTable* self)
{
if (self->len + 1 > self->_capacity) {
size_t new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8;
u64 new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8;
void* p = toml_realloc(self->_keyvals, sizeof(TomlKeyValue) * new_capacity);
self->_keyvals = p;
self->_capacity = new_capacity;
@ -299,7 +291,7 @@ void toml_table_expand_if_necessary(TomlTable *self)
void toml_table_set_by_string(TomlTable* self, TomlString* key, TomlValue* value)
{
TomlValue** slot = NULL;
for (size_t i = 0; i < self->len; i++) {
for (u64 i = 0; i < self->len; i++) {
if (toml_string_equals(self->_keyvals[i].key, key)) {
slot = &self->_keyvals[i].value;
}
@ -318,7 +310,7 @@ void toml_table_set_by_string(TomlTable *self, TomlString *key, TomlValue *value
TomlValue* toml_table_get_by_string(const TomlTable* self, const TomlString* key)
{
TomlValue* value = NULL;
for (size_t i = 0; i < self->len; i++) {
for (u64 i = 0; i < self->len; i++) {
if (toml_string_equals(self->_keyvals[i].key, key)) {
value = self->_keyvals[i].value;
}
@ -326,18 +318,18 @@ TomlValue *toml_table_get_by_string(const TomlTable *self, const TomlString *key
return value;
}
TomlValue *toml_table_getn(const TomlTable *self, const char *key, size_t key_len)
TomlValue* toml_table_getn(const TomlTable* self, cstr key, u64 key_len)
{
TomlString str = {(char* )key, key_len, 0};
return toml_table_get_by_string(self, &str);
}
TomlValue *toml_table_get(const TomlTable *self, const char *key)
TomlValue* toml_table_get(const TomlTable* self, cstr key)
{
return toml_table_getn(self, key, strlen(key));
}
TomlTable* toml_table_get_as_table(const TomlTable *self, const char *key)
TomlTable* toml_table_get_as_table(const TomlTable* self, cstr key)
{
TomlValue* v = toml_table_get(self, key);
assert(v != NULL);
@ -345,7 +337,7 @@ TomlTable* toml_table_get_as_table(const TomlTable *self, const char *key)
return v->value.table;
}
TomlArray* toml_table_get_as_array(const TomlTable *self, const char *key)
TomlArray* toml_table_get_as_array(const TomlTable* self, cstr key)
{
TomlValue* v = toml_table_get(self, key);
assert(v != NULL);
@ -353,7 +345,7 @@ TomlArray* toml_table_get_as_array(const TomlTable *self, const char *key)
return v->value.array;
}
TomlString* toml_table_get_as_string(const TomlTable *self, const char *key)
TomlString* toml_table_get_as_string(const TomlTable* self, cstr key)
{
TomlValue* v = toml_table_get(self, key);
assert(v != NULL);
@ -361,11 +353,7 @@ TomlString* toml_table_get_as_string(const TomlTable *self, const char *key)
return v->value.string;
}
#if defined(_MSC_VER) || defined(__APPLE__)
long long toml_table_get_as_integer(const TomlTable *self, const char *key)
#else
long toml_table_get_as_integer(const TomlTable *self, const char *key)
#endif
i64 toml_table_get_as_integer(const TomlTable* self, cstr key)
{
TomlValue* v = toml_table_get(self, key);
assert(v != NULL);
@ -373,7 +361,7 @@ long toml_table_get_as_integer(const TomlTable *self, const char *key)
return v->value.integer;
}
double toml_table_get_as_float(const TomlTable *self, const char *key)
f64 toml_table_get_as_float(const TomlTable* self, cstr key)
{
TomlValue* v = toml_table_get(self, key);
assert(v != NULL);
@ -381,7 +369,7 @@ double toml_table_get_as_float(const TomlTable *self, const char *key)
return v->value.float_;
}
const struct tm* toml_table_get_as_datetime(const TomlTable *self, const char *key)
const struct tm* toml_table_get_as_datetime(const TomlTable* self, cstr key)
{
TomlValue* v = toml_table_get(self, key);
assert(v != NULL);
@ -389,7 +377,7 @@ const struct tm* toml_table_get_as_datetime(const TomlTable *self, const char *k
return &v->value.datetime;
}
int toml_table_get_as_boolean(const TomlTable *self, const char *key)
i32 toml_table_get_as_boolean(const TomlTable* self, cstr key)
{
TomlValue* v = toml_table_get(self, key);
assert(v != NULL);
@ -397,13 +385,13 @@ int toml_table_get_as_boolean(const TomlTable *self, const char *key)
return v->value.boolean;
}
void toml_table_setn(TomlTable *self, const char *key, size_t key_len, TomlValue *value)
void toml_table_setn(TomlTable* self, cstr key, u64 key_len, TomlValue* value)
{
TomlString* str = toml_string_from_nstr(key, key_len);
toml_table_set_by_string(self, str, value);
}
void toml_table_set(TomlTable *self, const char *key, TomlValue *value)
void toml_table_set(TomlTable* self, cstr key, TomlValue* value)
{
toml_table_setn(self, key, strlen(key), value);
}
@ -419,7 +407,7 @@ TomlKeyValue* toml_table_iter_get(TomlTableIter *self)
return self->_keyval;
}
int toml_table_iter_has_next(TomlTableIter *self)
i32 toml_table_iter_has_next(TomlTableIter* self)
{
return self->_keyval != NULL;
}
@ -447,7 +435,7 @@ TomlArray *toml_array_new(void)
void toml_array_free(TomlArray* self)
{
if (self != NULL) {
for (size_t i = 0; i < self->len; i++) {
for (u64 i = 0; i < self->len; i++) {
toml_value_free(self->elements[i]);
}
free(self->elements);
@ -458,7 +446,7 @@ void toml_array_free(TomlArray *self)
void toml_array_expand_if_necessary(TomlArray* self)
{
if (self->len + 1 > self->_capacity) {
size_t new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8;
u64 new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8;
void* p = toml_realloc(self->elements, sizeof(TomlValue* ) * new_capacity);
self->elements = p;
self->_capacity = new_capacity;
@ -492,7 +480,7 @@ TomlValue *toml_value_new(TomlType type)
self->value.float_ = 0.0;
break;
case TOML_BOOLEAN:
self->value.boolean = TOML_FALSE;
self->value.boolean = false;
break;
case TOML_DATETIME:
memset(&self->value.datetime, 0, sizeof(struct tm));
@ -501,7 +489,7 @@ TomlValue *toml_value_new(TomlType type)
return self;
}
TomlValue *toml_value_from_str(const char *str)
TomlValue* toml_value_from_str(cstr str)
{
TomlValue* self = toml_malloc(sizeof(TomlValue));
self->value.string = toml_string_from_str(str);
@ -509,7 +497,7 @@ TomlValue *toml_value_from_str(const char *str)
return self;
}
TomlValue *toml_value_from_nstr(const char *str, size_t len)
TomlValue* toml_value_from_nstr(cstr str, u64 len)
{
TomlValue* self = toml_malloc(sizeof(TomlValue));
self->value.string = toml_string_from_nstr(str, len);
@ -533,11 +521,7 @@ TomlValue *toml_value_new_array(void)
return self;
}
#if defined(_MSC_VER) || defined(__APPLE__)
TomlValue *toml_value_new_integer(long long integer)
#else
TomlValue *toml_value_new_integer(long integer)
#endif
TomlValue* toml_value_new_integer(i64 integer)
{
TomlValue* self = toml_malloc(sizeof(TomlValue));
self->value.integer = integer;
@ -545,7 +529,7 @@ TomlValue *toml_value_new_integer(long integer)
return self;
}
TomlValue *toml_value_new_float(double float_)
TomlValue* toml_value_new_float(f64 float_)
{
TomlValue* self = toml_malloc(sizeof(TomlValue));
self->value.float_ = float_;
@ -558,7 +542,7 @@ TomlValue *toml_value_new_datetime(void)
return toml_value_new(TOML_DATETIME);
}
TomlValue *toml_value_new_boolean(int boolean)
TomlValue* toml_value_new_boolean(i32 boolean)
{
TomlValue* self = toml_malloc(sizeof(TomlValue));
self->value.boolean = boolean;
@ -590,15 +574,15 @@ void toml_value_free(TomlValue *self)
}
typedef struct _TomlParser {
const char* begin;
const char* end;
const char* ptr;
int lineno;
int colno;
cstr begin;
cstr end;
cstr ptr;
i32 lineno;
i32 colno;
char* filename;
} TomlParser;
TomlParser *toml_parser_new(const char *str, size_t len)
TomlParser *toml_parser_new(cstr str, u64 len)
{
TomlParser* self = toml_malloc(sizeof(TomlParser));
self->begin = str;
@ -631,17 +615,17 @@ void toml_move_next(TomlParser *self)
}
}
void toml_next_n(TomlParser *self, int n)
void toml_next_n(TomlParser* self, i32 n)
{
for (int i = 0; i < n; i++) {
for (i32 i = 0; i < n; i++) {
toml_move_next(self);
}
}
TomlString* toml_parse_bare_key(TomlParser* self)
{
const char *str = self->ptr;
size_t len = 0;
cstr str = self->ptr;
u64 len = 0;
while (self->ptr < self->end) {
char ch =* self->ptr;
@ -669,9 +653,9 @@ char toml_hex_char_to_int(char ch)
return 0;
}
int toml_encode_unicode_scalar(TomlString *result, TomlParser *parser, int n)
i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n)
{
unsigned int scalar = 0;
u32 scalar = 0;
if (parser->ptr + n > parser->end) {
toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar",
@ -679,10 +663,10 @@ int toml_encode_unicode_scalar(TomlString *result, TomlParser *parser, int n)
return TOML_ERR_UNICODE;
}
for (int i = 0; i < n; i++) {
for (i32 i = 0; i < n; i++) {
char ch = *parser->ptr;
if (isxdigit(ch)) {
scalar = scalar * 16 + (unsigned int)toml_hex_char_to_int(ch);
scalar = scalar * 16 + (u32)toml_hex_char_to_int(ch);
toml_move_next(parser);
} else {
toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar",
@ -984,7 +968,7 @@ TomlValue* toml_parse_multi_line_literal_string(TomlParser *self)
return value;
}
TomlValue* toml_parse_datetime(const char *str, size_t len)
TomlValue* toml_parse_datetime(cstr str, u64 len)
{
(void)str;
(void)len;
@ -997,7 +981,7 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser *self)
TomlValue* result = NULL;
char type = 'i';
int base = 10;
i32 base = 10;
str = toml_string_new();
@ -1032,13 +1016,13 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser *self)
}
char last_char = 0;
int has_exp = TOML_FALSE;
i32 has_exp = false;
while (self->ptr < self->end) {
if (*self->ptr == '+' ||* self->ptr == '-') {
if (last_char == 0 || ((last_char == 'e' || last_char == 'E') && !has_exp)) {
if (last_char != 0) {
type = 'f';
has_exp = TOML_TRUE;
has_exp = true;
}
toml_string_append_char(str,* self->ptr);
} else {
@ -1094,7 +1078,7 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser *self)
if (type == 'i') {
char* end = NULL;
long long n = strtoll(str->str, &end, base);
i64 n = strtoll(str->str, &end, base);
if (end < str->str + str->len) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer",
self->filename, self->lineno, self->colno);
@ -1104,7 +1088,7 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser *self)
result = toml_value_new_integer(n);
} else if (type == 'f') {
char* end = NULL;
double n = strtod(str->str, &end);
f64 n = strtod(str->str, &end);
if (end < str->str + str->len) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float");
goto cleanup;
@ -1124,13 +1108,13 @@ TomlValue* toml_parse_bool(TomlParser *self)
if (self->ptr + 4 <= self->end && strncmp(self->ptr, "true", 4) == 0 &&
(self->ptr + 4 == self->end || isspace(*(self->ptr + 4)) || *(self->ptr + 4) == ',' || *(self->ptr + 4) == ']' || *(self->ptr + 4) == '}')) {
toml_next_n(self, 4);
return toml_value_new_boolean(TOML_TRUE);
return toml_value_new_boolean(true);
}
if (self->ptr + 5 <= self->end && strncmp(self->ptr, "false", 5) == 0 &&
(self->ptr + 5 == self->end || isspace(*(self->ptr + 5)) || *(self->ptr + 5) == ',' || *(self->ptr + 5) == ']' || *(self->ptr + 5) == '}')) {
toml_next_n(self, 5);
return toml_value_new_boolean(TOML_FALSE);
return toml_value_new_boolean(false);
}
return NULL;
@ -1451,8 +1435,8 @@ end:
}
TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable* table,
TomlArray *key_path, int is_array,
int create_if_not_exist)
TomlArray *key_path, i32 is_array,
i32 create_if_not_exist)
{
TomlTable* real_table = table;
TomlValue* new_table = NULL;
@ -1460,7 +1444,7 @@ TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable *table,
TomlString* part_copy = NULL;
if (is_array) {
size_t i = 0;
u64 i = 0;
for (; i < key_path->len - 1; i++) {
TomlString* part = key_path->elements[i]->value.string;
TomlValue* t = toml_table_get_by_string(real_table, part);
@ -1509,7 +1493,7 @@ TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable *table,
real_table = new_table->value.table;
}
} else {
for (size_t i = 0; i < key_path->len; i++) {
for (u64 i = 0; i < key_path->len; i++) {
TomlString* part = key_path->elements[i]->value.string;
TomlValue* t = toml_table_get_by_string(real_table, part);
if (t == NULL) {
@ -1549,14 +1533,14 @@ end:
TomlErrCode toml_parse_table(TomlParser* self, TomlTable* table)
{
TomlArray *key_path = NULL;
int is_array = TOML_FALSE;
i32 is_array = false;
TomlTable* real_table = table;
TomlErrCode rc = 0;
key_path = toml_array_new();
if (self->ptr < self->end &&* self->ptr == '[') {
is_array = TOML_TRUE;
is_array = true;
toml_move_next(self);
}
@ -1631,7 +1615,7 @@ TomlErrCode toml_parse_table(TomlParser *self, TomlTable *table)
goto cleanup;
}
real_table = toml_walk_table_path(self, table, key_path, is_array, TOML_TRUE);
real_table = toml_walk_table_path(self, table, key_path, is_array, true);
if (real_table == NULL)
goto error;
@ -1686,8 +1670,8 @@ TomlTable* toml_parse(TomlParser *self)
return table;
}
TomlTable* toml_load_nstr_filename(const char *str, size_t len,
const char *filename)
TomlTable* toml_load_nstr_filename(cstr str, u64 len,
cstr filename)
{
TomlParser *parser = NULL;
TomlTable* table = NULL;
@ -1701,17 +1685,17 @@ TomlTable* toml_load_nstr_filename(const char *str, size_t len,
return table;
}
TomlTable* toml_load_nstr(const char *str, size_t len)
TomlTable* toml_load_nstr(cstr str, u64 len)
{
return toml_load_nstr_filename(str, len, "<string>");
}
TomlTable* toml_load_str(const char *str)
TomlTable* toml_load_str(cstr str)
{
return toml_load_nstr(str, sizeof(str));
}
TomlTable* toml_load_file_filename(FILE *file, const char *filename)
TomlTable* toml_load_file_filename(FILE* file, cstr filename)
{
TomlTable* table = NULL;
TomlString* str = NULL;
@ -1720,8 +1704,8 @@ TomlTable* toml_load_file_filename(FILE *file, const char *filename)
toml_string_expand_if_necessary(str, 4095);
size_t count;
size_t bytes_to_read;
u64 count;
u64 bytes_to_read;
do {
bytes_to_read = str->_capacity - str->len - 1;
@ -1758,7 +1742,7 @@ TomlTable* toml_load_file(FILE *file)
return toml_load_file_filename(file, "<stream>");
}
TomlTable* toml_load_filename(const char *filename)
TomlTable* toml_load_filename(cstr filename)
{
TomlTable* table = NULL;
FILE* f = NULL;

View File

@ -15,7 +15,7 @@ void print_value(const TomlValue *value);
void print_array(const TomlArray *array)
{
printf("[");
for (size_t i = 0; i < array->len; i++) {
for (u64 i = 0; i < array->len; i++) {
if (i > 0) {
printf(", ");
}
@ -62,7 +62,7 @@ void print_table(const TomlTable *table)
TomlTableIter it = toml_table_iter_new((TomlTable* )table);
printf("{");
size_t i = 0;
u64 i = 0;
while (toml_table_iter_has_next(&it)) {
TomlKeyValue *keyval = toml_table_iter_get(&it);
@ -77,10 +77,10 @@ void print_table(const TomlTable *table)
printf("}");
}
int test_run(const char *filename)
i32 test_run(cstr filename)
{
TomlTable* table = NULL;
int rc = 0;
i32 rc = 0;
table = toml_load_filename(filename);
if (table == NULL)
@ -94,15 +94,15 @@ cleanup:
if (toml_err()->code != TOML_OK) {
fprintf(stderr, "%s\n", toml_err()->message);
rc = (int)toml_err()->code;
rc = (i32)toml_err()->code;
}
toml_err_clear();
return rc;
}
int main(void)
i32 main(void)
{
static const char *const filenames[] = {
static cstr const filenames[] = {
/* should parse */
PROJECT_SOURCE_DIR "/tests/key-values.toml",
PROJECT_SOURCE_DIR "/tests/complex-structure.toml",
@ -117,12 +117,12 @@ int main(void)
PROJECT_SOURCE_DIR "/tests/hard_example_unicode.toml"
};
int total_tests = sizeof(filenames) / sizeof(char *);
int num_passed = 0;
int num_failed = 0;
i32 total_tests = sizeof(filenames) / sizeof(char* );
i32 num_passed = 0;
i32 num_failed = 0;
for (int i = 0; i < total_tests; i++) {
int rc = test_run(filenames[i]);
for (i32 i = 0; i < total_tests; i++) {
i32 rc = test_run(filenames[i]);
if (rc == 0) {
printf("test %d success\n", i);
num_passed++;