15 Commits

39 changed files with 738 additions and 713 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ SpacesBeforeTrailingComments: 3
TabWidth: 8
UseTab: AlignWithSpaces
BreakAfterReturnType: Automatic
PenaltyBreakAssignment: 1000000
PenaltyBreakAssignment: 0
PenaltyReturnTypeOnItsOwnLine: 100000000
PenaltyExcessCharacter: 10000000
PenaltyBreakOpenParenthesis: 0
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h"
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h"
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/misc.h>
#include <fx/string.h>
#include <fx/term/print.h>
+1 -1
View File
@@ -1,7 +1,7 @@
#include "command.h"
#include <fx/bst.h>
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/string.h>
#include <fx/term/print.h>
#include <fx/term/tty.h>
+1 -1
View File
@@ -2,7 +2,7 @@
#define _FX_COMMAND_H_
#include <fx/bst.h>
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/queue.h>
#include <fx/string.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h"
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
+13 -5
View File
@@ -1,4 +1,4 @@
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <stdio.h>
enum {
@@ -27,16 +27,24 @@ const char *text
"great pleasure.";
static int test_command(
const fx_command *self, const fx_arglist *opt, const fx_array *args)
const fx_command *self,
const fx_arglist *opt,
const fx_array *args)
{
printf("Hello, world!\n");
fx_arglist_iterator it;
fx_arglist_iterator_begin(
opt, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, &it);
opt,
FX_COMMAND_INVALID_ID,
FX_COMMAND_INVALID_ID,
&it);
while (fx_arglist_iterator_is_valid(&it)) {
printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id,
it.value->val_id, it.i, it.value->val_str);
printf("opt:%u,arg:%u,i:%zu,value: %s\n",
it.opt_id,
it.value->val_id,
it.i,
it.value->val_str);
fx_arglist_iterator_next(&it);
}
+6 -8
View File
@@ -17,7 +17,7 @@ struct fx_array_p {
};
struct fx_array_iterator_p {
fx_array *_a;
const fx_array *_a;
struct fx_array_p *_a_p;
/** The index of the current value */
@@ -170,9 +170,7 @@ static fx_value array_get(struct fx_array_p *array, size_t at)
return FX_VALUE_EMPTY;
}
fx_value result;
fx_value_copy(&result, &array->ar_data[at]);
return result;
return fx_value_copy_return(array->ar_data[at]);
}
static size_t array_get_size(const struct fx_array_p *array)
@@ -373,7 +371,7 @@ static fx_status array_to_string(
/*** ITERATOR FUNCTIONS *******************************************************/
static fx_iterator *iterable_begin(fx_object *obj)
static const fx_iterator *iterable_begin(const fx_object *obj)
{
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it = fx_object_get_private(
@@ -437,7 +435,7 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS;
}
static fx_value iterator_get_value(const fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_array_iterator_p *it = fx_object_get_private(
obj,
@@ -445,10 +443,10 @@ static fx_value iterator_get_value(const fx_iterator *obj)
struct fx_array_p *array = it->_a_p;
if (it->i >= array->ar_len) {
return FX_VALUE_EMPTY;
return NULL;
}
return *it->value;
return it->value;
}
static enum fx_status iterator_is_valid(const fx_iterator *obj)
+6 -5
View File
@@ -32,8 +32,9 @@ struct fx_hashmap_p {
struct fx_hashmap_iterator_p {
size_t i;
fx_hashmap_item item;
fx_value item_value;
fx_hashmap *_h;
const fx_hashmap *_h;
struct fx_hashmap_p *_h_p;
fx_bst_node *_cbn;
fx_queue_entry *_cqe;
@@ -420,7 +421,7 @@ bool fx_hashmap_is_empty(const fx_hashmap *hashmap)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_is_empty, hashmap);
}
fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap)
{
fx_hashmap_iterator *it_obj = fx_object_create(
FX_TYPE_HASHMAP_ITERATOR);
@@ -617,12 +618,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS;
}
static fx_value iterator_get_value(const fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_hashmap_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_HASHMAP_ITERATOR);
return FX_POINTER(&it->item);
it->item_value = FX_POINTER(&it->item);
return &it->item_value;
}
/*** CLASS DEFINITION *********************************************************/
@@ -635,7 +637,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_hashmap_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_hashmap)
+111 -56
View File
@@ -1,3 +1,4 @@
#include <assert.h>
#include <fx/collections/hashtable.h>
#include <fx/misc.h>
#include <fx/reflection/property.h>
@@ -47,8 +48,9 @@ struct fx_hashtable_p {
struct fx_hashtable_iterator_p {
size_t i;
fx_hashtable_item *item;
fx_value item_value;
fx_hashtable *_h;
const fx_hashtable *_h;
struct fx_hashtable_p *_h_p;
};
@@ -67,12 +69,14 @@ static fx_status convert_key_to_index(
}
size_t index = hash % table_size, i = 1;
while (table[index].i_item && i < table_size) {
bool index_found = false;
while (table[index].i_item && i <= table_size) {
const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item,
FX_TYPE_HASHTABLE_ITEM);
int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) {
index_found = true;
break;
}
@@ -80,11 +84,16 @@ static fx_status convert_key_to_index(
i++;
}
if (!index_found) {
return FX_ERR_NO_ENTRY;
}
assert(index < table_size);
*out_index = index;
return FX_SUCCESS;
}
static fx_status find_key(
static fx_status allocate_index_for_key(
const fx_value *v,
struct table_item *table,
size_t table_size,
@@ -96,15 +105,21 @@ static fx_status find_key(
return status;
}
bool found = false;
size_t index = hash % table_size, i = 1;
while (table[index].i_item && i < table_size) {
size_t index = hash % table_size;
bool ok = false;
for (size_t i = 1; i < table_size; i++) {
if (!table[index].i_item) {
ok = true;
break;
}
const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item,
FX_TYPE_HASHTABLE_ITEM);
int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) {
found = true;
ok = true;
break;
}
@@ -112,10 +127,11 @@ static fx_status find_key(
i++;
}
if (!found) {
return FX_ERR_NO_ENTRY;
if (!ok) {
return FX_ERR_NO_SPACE;
}
assert(index < table_size);
*out_index = index;
return FX_SUCCESS;
}
@@ -126,10 +142,9 @@ static fx_status add_item_to_table(
size_t table_capacity)
{
size_t index = 0;
struct fx_hashtable_item_p *item_p = fx_object_get_private(
item,
FX_TYPE_HASHTABLE_ITEM);
fx_status status = convert_key_to_index(
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_status status = allocate_index_for_key(
&item_p->i_key,
table,
table_capacity,
@@ -231,26 +246,43 @@ static fx_status hashtable_put(
if (!FX_OK(status)) {
return status;
}
capacity = primes[hashtable->t_max_index];
}
size_t index = 0;
status = convert_key_to_index(
do {
status = allocate_index_for_key(
key,
hashtable->t_items,
capacity,
&index);
if (status == FX_ERR_NO_SPACE) {
status = resize_table(
hashtable,
hashtable->t_max_index + 1);
if (!FX_OK(status)) {
return status;
}
capacity = primes[hashtable->t_max_index];
continue;
}
if (!FX_OK(status)) {
return status;
}
break;
} while (1);
fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
if (!item) {
return FX_ERR_NO_MEMORY;
}
struct fx_hashtable_item_p *item_p = fx_object_get_private(
item,
FX_TYPE_HASHTABLE_ITEM);
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(&item_p->i_key, key);
fx_value_copy(&item_p->i_value, value);
@@ -279,9 +311,8 @@ static const fx_value *hashtable_get(
return NULL;
}
const struct fx_hashtable_item_p *item_p = fx_object_get_private(
item,
FX_TYPE_HASHTABLE_ITEM);
const struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
return &item_p->i_value;
}
@@ -295,14 +326,16 @@ static bool hashtable_is_empty(const struct fx_hashtable_p *hashtable)
return hashtable->t_count == 0;
}
static fx_value hashtable_item_get_key(const struct fx_hashtable_item_p *item)
static const fx_value *hashtable_item_get_key(
const struct fx_hashtable_item_p *item)
{
return item->i_key;
return &item->i_key;
}
static fx_value hashtable_item_get_value(const struct fx_hashtable_item_p *item)
static const fx_value *hashtable_item_get_value(
const struct fx_hashtable_item_p *item)
{
return item->i_value;
return &item->i_value;
}
/*** PUBLIC FUNCTIONS *********************************************************/
@@ -347,7 +380,7 @@ bool fx_hashtable_is_empty(const fx_hashtable *hashtable)
hashtable);
}
fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item)
const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM,
@@ -355,7 +388,7 @@ fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item)
item);
}
fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item)
const fx_value *fx_hashtable_item_get_value(const fx_hashtable_item *item)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM,
@@ -363,13 +396,12 @@ fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item)
item);
}
fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable)
const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable)
{
fx_hashtable_iterator *it_obj = fx_object_create(
FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_HASHTABLE_ITERATOR);
fx_hashtable_iterator *it_obj
= fx_object_create(FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_HASHTABLE_ITERATOR);
it->_h = hashtable;
it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE);
@@ -387,6 +419,7 @@ fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable)
it->i++;
}
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return it_obj;
}
@@ -405,6 +438,30 @@ static void hashtable_init(fx_object *obj, void *priv)
static void hashtable_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_p *map = priv;
size_t capacity = primes[map->t_max_index];
size_t nr_destroyed = 0;
for (size_t i = 0; i < capacity; i++) {
if (map->t_items[i].i_item) {
nr_destroyed++;
fx_hashtable_item_unref(map->t_items[i].i_item);
}
}
free(map->t_items);
}
static void hashtable_item_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_item_p *item = priv;
fx_value_unset(&item->i_key);
fx_value_unset(&item->i_value);
}
static void hashtable_iterator_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_iterator_p *it = priv;
fx_value_unset(&it->item_value);
}
static fx_status to_string(
@@ -412,9 +469,8 @@ static fx_status to_string(
fx_stream *out,
const char *format)
{
struct fx_hashtable_p *hashtable = fx_object_get_private(
obj->v_object,
FX_TYPE_HASHTABLE);
struct fx_hashtable_p *hashtable
= fx_object_get_private(obj->v_object, FX_TYPE_HASHTABLE);
if (!hashtable->t_count) {
fx_stream_write_cstr(out, "{}", NULL);
@@ -434,9 +490,8 @@ static fx_status to_string(
}
nr_written++;
struct fx_hashtable_item_p *item_p = fx_object_get_private(
item,
FX_TYPE_HASHTABLE_ITEM);
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
const char *cstr = NULL;
fx_value_get_cstr(&item_p->i_key, &cstr);
@@ -487,9 +542,8 @@ static fx_status get_key(
return FX_ERR_NOT_SUPPORTED;
}
struct fx_hashtable_item_p *item_p = fx_object_get_private(
item,
FX_TYPE_HASHTABLE_ITEM);
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_key);
return FX_SUCCESS;
@@ -506,9 +560,8 @@ static fx_status get_value(
return FX_ERR_NOT_SUPPORTED;
}
struct fx_hashtable_item_p *item_p = fx_object_get_private(
item,
FX_TYPE_HASHTABLE_ITEM);
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_value);
return FX_SUCCESS;
@@ -518,9 +571,10 @@ static fx_status get_value(
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
fx_value_unset(&it->item_value);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
@@ -533,14 +587,14 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA;
}
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return FX_SUCCESS;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
@@ -562,19 +616,18 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS;
}
static fx_value iterator_get_value(const fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
if (table[it->i].i_item) {
return FX_VALUE_OBJECT(table[it->i].i_item);
return &it->item_value;
}
return FX_VALUE_EMPTY;
return NULL;
}
/*** CLASS DEFINITION *********************************************************/
@@ -619,6 +672,7 @@ FX_TYPE_DEFINITION_BEGIN(fx_hashtable_iterator)
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_hashtable_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p);
FX_TYPE_INSTANCE_FINI(hashtable_iterator_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_iterator)
// ---- fx_hashtable_item DEFINITION
@@ -637,4 +691,5 @@ FX_TYPE_DEFINITION_BEGIN(fx_hashtable_item)
FX_TYPE_EXTENDS(FX_TYPE_OBJECT);
FX_TYPE_CLASS(fx_hashtable_item_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p);
FX_TYPE_INSTANCE_FINI(hashtable_item_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_item)
@@ -83,7 +83,7 @@ FX_API bool fx_hashmap_has_key(
FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap);
FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap);
FX_API fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap);
FX_API const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap);
FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap);
FX_DECLS_END;
@@ -45,10 +45,11 @@ FX_API const fx_value *fx_hashtable_get(
FX_API size_t fx_hashtable_get_count(const fx_hashtable *hashtable);
FX_API bool fx_hashtable_is_empty(const fx_hashtable *hashtable);
FX_API fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable);
FX_API const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable);
FX_API fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item);
FX_API fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item);
FX_API const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item);
FX_API const fx_value *fx_hashtable_item_get_value(
const fx_hashtable_item *item);
FX_DECLS_END;
+1 -2
View File
@@ -59,8 +59,7 @@ FX_API void fx_list_delete_all(fx_list *q);
FX_API void *fx_list_entry_value(const fx_list_entry *entry);
FX_API fx_iterator *fx_list_begin(fx_list *q);
FX_API const fx_iterator *fx_list_cbegin(const fx_list *q);
FX_API const fx_iterator *fx_list_begin(const fx_list *q);
FX_DECLS_END;
+5 -24
View File
@@ -23,6 +23,7 @@ struct fx_list_iterator_p {
size_t i;
void *item;
fx_value item_value;
fx_list_entry *entry;
};
@@ -401,27 +402,7 @@ void *fx_list_entry_value(const struct fx_list_entry *entry)
return entry ? entry->e_data : NULL;
}
fx_iterator *fx_list_begin(fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_LIST_ITERATOR);
it->_q = q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
it->_q_entry = fx_queue_first(&it->_q_p->l_queue);
it->i = 0;
it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return 0;
}
const fx_iterator *fx_list_cbegin(const fx_list *q)
const fx_iterator *fx_list_begin(const fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
@@ -508,13 +489,14 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS;
}
static fx_value iterator_get_value(const fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_list_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_LIST_ITERATOR);
return FX_POINTER(it->item);
it->item_value = FX_POINTER(it->item);
return &it->item_value;
}
/*** CLASS DEFINITION *********************************************************/
@@ -527,7 +509,6 @@ FX_TYPE_CLASS_BEGIN(fx_list)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_list_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_list_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_list)
+5 -5
View File
@@ -19,13 +19,13 @@ int main(void)
printf("no value\n");
}
fx_iterator *it = fx_hashtable_begin(ht);
const fx_iterator *it = fx_hashtable_begin(ht);
fx_foreach(val, it)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(&val, &item);
fx_value_get_object(val, &item);
printf("item %p\n", item);
fx_value key, value;
const fx_value *key, *value;
key = fx_hashtable_item_get_key(item);
value = fx_hashtable_item_get_value(item);
@@ -34,10 +34,10 @@ int main(void)
fx_foreach(prop_val, prop_it)
{
fx_property *prop;
fx_value_get_object(&prop_val, &prop);
fx_value_get_object(prop_val, &prop);
printf("%s = ", fx_property_get_name(prop));
fx_value value = FX_VALUE_EMPTY;
fx_property_get_value(prop, &val, &value);
fx_property_get_value(prop, val, &value);
fx_value_to_string(&value, fx_stdout, NULL);
printf("\n");
}
+6 -11
View File
@@ -35,6 +35,7 @@ struct fx_directory_iterator_p {
fx_directory *root;
fx_directory_entry entry;
fx_value entry_value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
@@ -375,8 +376,6 @@ fx_result fx_directory_open_temp(fx_directory **out)
fx_path_unlink(rpath);
fx_path_unref(rpath);
return status;
}
}
@@ -512,6 +511,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
fx_value_unset(&it->entry_value);
it->entry_value = FX_POINTER(&it->entry);
it->entry.filename = ent->fts_name;
it->entry.filepath = path;
@@ -619,12 +620,7 @@ fx_iterator *fx_directory_begin(
return it_obj;
}
static fx_iterator *iterator_begin(fx_object *obj)
{
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(
(fx_object *)obj,
@@ -698,13 +694,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return iterator_move_next(obj);
}
static fx_value iterator_get_value(const fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_DIRECTORY_ITERATOR);
return FX_POINTER(&it->entry);
return &it->entry_value;
}
/*** CLASS DEFINITION *********************************************************/
@@ -717,7 +713,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_directory)
+1 -1
View File
@@ -26,7 +26,7 @@ int main(int argc, const char **argv)
fx_foreach(val, it)
{
fx_directory_entry *entry = NULL;
fx_value_get_pointer(&val, (void **)&entry);
fx_value_get_pointer(val, (void **)&entry);
printf("%s\n", fx_path_ptr(entry->filepath));
}
+64 -48
View File
@@ -71,10 +71,12 @@ struct fx_assembly_p {
struct fx_assembly_iterator_p {
fx_namemap_entry *it_cur;
fx_value it_value;
};
struct fx_assembly_type_iterator_p {
struct type *it_item;
fx_value it_value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
@@ -165,11 +167,11 @@ static struct map_item *map_item_next(struct map_item *item)
struct map_bucket *bucket = item->i_entry.e_bucket;
struct map_entry *next_item = NULL;
if (bucket) {
struct fx_queue_entry *q_item = fx_queue_next(
&item->i_entry.e_entry);
struct fx_queue_entry *q_item
= fx_queue_next(&item->i_entry.e_entry);
if (!q_item) {
struct fx_bst_node *node = fx_bst_next(
&bucket->b_entry.e_node);
struct fx_bst_node *node
= fx_bst_next(&bucket->b_entry.e_node);
next_item = fx_unbox(struct map_entry, node, e_node);
} else {
next_item = fx_unbox(struct map_entry, q_item, e_entry);
@@ -185,8 +187,8 @@ static struct map_item *map_item_next(struct map_item *item)
if (next_item->e_type == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)next_item;
struct fx_queue_entry *q_item = fx_queue_first(
&bucket->b_items);
struct fx_queue_entry *q_item
= fx_queue_first(&bucket->b_items);
next_item = fx_unbox(struct map_entry, q_item, e_entry);
}
@@ -255,10 +257,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries);
while (cur_node) {
struct map_entry *entry = fx_unbox(
struct map_entry,
cur_node,
e_node);
struct map_entry *entry
= fx_unbox(struct map_entry, cur_node, e_node);
switch (entry->e_type) {
case MAP_ENTRY_ITEM: {
struct map_item *item = (struct map_item *)entry;
@@ -268,8 +268,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
case MAP_ENTRY_BUCKET: {
struct map_bucket *bucket = (struct map_bucket *)entry;
fx_queue_entry *cur_qentry = fx_queue_first(
&bucket->b_items);
fx_queue_entry *cur_qentry
= fx_queue_first(&bucket->b_items);
while (cur_qentry) {
struct map_item *item = fx_unbox(
struct map_item,
@@ -289,8 +289,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
{
fx_assembly_type_iterator *it = fx_object_create(
fx_assembly_type_iterator_get_type());
fx_assembly_type_iterator *it
= fx_object_create(fx_assembly_type_iterator_get_type());
if (!it) {
return NULL;
}
@@ -299,12 +299,15 @@ static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
it,
fx_assembly_type_iterator_get_type());
struct map_item *item = map_first_item(&assembly->a_types);
if (item) {
it_p->it_item = fx_unbox(struct type, item, e_map_item);
} else {
if (!item) {
fx_iterator_set_status(it, FX_ERR_NO_DATA);
return it;
}
it_p->it_item = fx_unbox(struct type, item, e_map_item);
const fx_type *ty = fx_type_get_by_id(it_p->it_item->e_type);
it_p->it_value = FX_VALUE_OBJECT_REF(ty);
return it;
}
@@ -341,20 +344,25 @@ static void assembly_get_version(
fx_iterator *fx_assembly_get_all(void)
{
fx_assembly_iterator *it = fx_object_create(
fx_assembly_iterator_get_type());
fx_assembly_iterator *it
= fx_object_create(fx_assembly_iterator_get_type());
if (!it) {
return NULL;
}
struct fx_assembly_iterator_p *it_p = fx_object_get_private(
it,
fx_assembly_iterator_get_type());
struct fx_assembly_iterator_p *it_p
= fx_object_get_private(it, fx_assembly_iterator_get_type());
it_p->it_cur = fx_namemap_first(&assembly_map);
if (!it_p->it_cur) {
fx_iterator_set_status(it, FX_ERR_NO_DATA);
return it;
}
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, it_p->it_cur, a_entry);
it_p->it_value = FX_VALUE_OBJECT_REF(assembly->a_self);
return it;
}
@@ -365,10 +373,8 @@ const fx_assembly *fx_assembly_get_by_name(const char *name)
return NULL;
}
struct fx_assembly_p *assembly = fx_unbox(
struct fx_assembly_p,
entry,
a_entry);
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, entry, a_entry);
return assembly->a_self;
}
@@ -483,51 +489,61 @@ static enum fx_status type_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA;
}
fx_value_unset(&it->it_value);
struct map_item *next = map_item_next(&it->it_item->e_map_item);
it->it_item = fx_unbox(struct type, next, e_map_item);
return it->it_item ? FX_SUCCESS : FX_ERR_NO_DATA;
if (!it->it_item) {
return FX_ERR_NO_DATA;
}
static fx_value type_iterator_get_value(const fx_iterator *obj)
const fx_type *ty = fx_type_get_by_id(it->it_item->e_type);
it->it_value = FX_VALUE_OBJECT_REF(ty);
return FX_SUCCESS;
}
static const fx_value *type_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_type_iterator_p *it = fx_object_get_private(
obj,
fx_assembly_type_iterator_get_type());
if (!it->it_item) {
return FX_VALUE_EMPTY;
return NULL;
}
const fx_type *ty = fx_type_get_by_id(it->it_item->e_type);
return FX_VALUE_OBJECT(ty);
return &it->it_value;
}
static enum fx_status assembly_iterator_move_next(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it = fx_object_get_private(
obj,
fx_assembly_iterator_get_type());
struct fx_assembly_iterator_p *it
= fx_object_get_private(obj, fx_assembly_iterator_get_type());
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(&assembly_map, it->it_cur);
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA;
}
static fx_value assembly_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it = fx_object_get_private(
obj,
fx_assembly_iterator_get_type());
if (!it->it_cur) {
return FX_VALUE_EMPTY;
return FX_ERR_NO_DATA;
}
struct fx_assembly_p *assembly = fx_unbox(
struct fx_assembly_p,
it->it_cur,
a_entry);
return FX_VALUE_OBJECT(assembly->a_self);
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, it->it_cur, a_entry);
it->it_value = FX_VALUE_OBJECT_REF(assembly->a_self);
return FX_SUCCESS;
}
static const fx_value *assembly_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it
= fx_object_get_private(obj, fx_assembly_iterator_get_type());
if (!it->it_cur) {
return NULL;
}
return &it->it_value;
}
/*** CLASS DEFINITION *********************************************************/
+47 -31
View File
@@ -31,12 +31,14 @@ struct fx_type_p {
struct fx_type_function_iterator_p {
struct fx_namemap *it_src;
struct fx_namemap_entry *it_cur;
fx_value it_value;
};
struct fx_type_property_iterator_p {
const fx_type_info *it_cur_type;
struct fx_namemap *it_src;
struct fx_namemap_entry *it_cur;
fx_value it_value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
@@ -77,8 +79,8 @@ const fx_function *type_get_function(
fx_iterator *type_get_functions(const struct fx_type_p *ty)
{
fx_type_function_iterator *it_obj = fx_object_create(
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
fx_type_function_iterator *it_obj
= fx_object_create(FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
struct fx_type_function_iterator_p *it = fx_object_get_private(
it_obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
@@ -95,9 +97,8 @@ const fx_property *type_get_property(
{
struct fx_type_info *cur = ty->ty_info;
while (cur) {
const fx_property *prop = fx_type_info_get_property_by_name(
cur,
name);
const fx_property *prop
= fx_type_info_get_property_by_name(cur, name);
if (prop) {
return prop;
}
@@ -114,8 +115,8 @@ const fx_property *type_get_property(
static fx_iterator *type_get_properties(const struct fx_type_p *ty)
{
fx_type_property_iterator *it_obj = fx_object_create(
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
fx_type_property_iterator *it_obj
= fx_object_create(FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
struct fx_type_property_iterator_p *it = fx_object_get_private(
it_obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
@@ -129,8 +130,9 @@ static fx_iterator *type_get_properties(const struct fx_type_p *ty)
break;
}
const struct fx_type_info *next_type = fx_type_info_get_by_id(
&it->it_cur_type->ty_parent_id);
const struct fx_type_info *next_type
= fx_type_info_get_by_id(&it->it_cur_type
->ty_parent_id);
if (!next_type) {
break;
}
@@ -143,6 +145,10 @@ static fx_iterator *type_get_properties(const struct fx_type_p *ty)
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
}
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
it->it_value = FX_VALUE_OBJECT_REF(prop->p_prop);
return it_obj;
}
@@ -176,9 +182,8 @@ fx_type *__fx_type_create(struct fx_type_info *type_info)
return NULL;
}
struct fx_type_p *p = fx_object_get_private(
out,
FX_REFLECTION_TYPE_TYPE);
struct fx_type_p *p
= fx_object_get_private(out, FX_REFLECTION_TYPE_TYPE);
p->ty_info = type_info;
return out;
@@ -300,25 +305,29 @@ static enum fx_status function_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA;
}
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA;
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
static fx_value function_iterator_get_value(const fx_iterator *obj)
struct fx_type_function *func
= fx_unbox(struct fx_type_function, it->it_cur, f_entry);
it->it_value = FX_VALUE_OBJECT_REF(func->f_func);
return FX_SUCCESS;
}
static const fx_value *function_iterator_get_value(const fx_iterator *obj)
{
struct fx_type_function_iterator_p *it = fx_object_get_private(
obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
if (!it->it_cur) {
return FX_VALUE_EMPTY;
return NULL;
}
struct fx_type_function *func = fx_unbox(
struct fx_type_function,
it->it_cur,
f_entry);
return FX_VALUE_OBJECT(func->f_func);
return &it->it_value;
}
static enum fx_status property_iterator_move_next(const fx_iterator *obj)
@@ -337,8 +346,9 @@ static enum fx_status property_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA;
}
const struct fx_type_info *next_type = fx_type_info_get_by_id(
&it->it_cur_type->ty_parent_id);
const struct fx_type_info *next_type
= fx_type_info_get_by_id(&it->it_cur_type
->ty_parent_id);
if (!next_type) {
return FX_ERR_NO_DATA;
}
@@ -347,24 +357,30 @@ static enum fx_status property_iterator_move_next(const fx_iterator *obj)
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
}
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA;
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
static fx_value property_iterator_get_value(const fx_iterator *obj)
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
it->it_value = FX_VALUE_OBJECT_REF(prop->p_prop);
return FX_SUCCESS;
}
static const fx_value *property_iterator_get_value(const fx_iterator *obj)
{
struct fx_type_property_iterator_p *it = fx_object_get_private(
obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
if (!it->it_cur) {
return FX_VALUE_EMPTY;
return NULL;
}
struct fx_type_property *prop = fx_unbox(
struct fx_type_property,
it->it_cur,
p_entry);
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
return FX_VALUE_OBJECT(prop->p_prop);
return &it->it_value;
}
/*** CLASS DEFINITION *********************************************************/
+2 -2
View File
@@ -5,7 +5,7 @@
static struct fx_error *bitcode_serialise(
fx_serial_ctx *serial,
fx_object *src,
const fx_value *src,
fx_stream *dest,
enum fx_serial_flags flags)
{
@@ -15,7 +15,7 @@ static struct fx_error *bitcode_serialise(
static struct fx_error *bitcode_deserialise(
fx_serial_ctx *serial,
fx_stream *src,
fx_object **dest,
fx_value *dest,
enum fx_serial_flags flags)
{
return FX_RESULT_ERR(NOT_SUPPORTED);
+2 -2
View File
@@ -44,7 +44,7 @@ FX_TYPE_DEFINITION_END(fx_serial_ctx)
fx_result fx_serial_ctx_serialise(
fx_serial_ctx *ctx,
fx_object *src,
const fx_value *src,
fx_stream *dest,
enum fx_serial_flags flags)
{
@@ -62,7 +62,7 @@ fx_result fx_serial_ctx_serialise(
fx_result fx_serial_ctx_deserialise(
fx_serial_ctx *ctx,
fx_stream *src,
fx_object **dest,
fx_value *dest,
enum fx_serial_flags flags)
{
FX_CLASS_DISPATCH_VIRTUAL(
+5 -4
View File
@@ -6,6 +6,7 @@
#include <fx/object.h>
#include <fx/status.h>
#include <fx/stream.h>
#include <fx/value.h>
FX_DECLS_BEGIN;
@@ -21,13 +22,13 @@ FX_DECLARE_TYPE(fx_serial_ctx);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx)
fx_result (*s_serialise)(
fx_serial_ctx *,
fx_object *,
const fx_value *,
fx_stream *,
fx_serial_flags);
fx_result (*s_deserialise)(
fx_serial_ctx *,
fx_stream *,
fx_object **,
fx_value *,
fx_serial_flags);
FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx)
@@ -39,14 +40,14 @@ FX_API fx_type_id fx_serial_ctx_get_type(void);
FX_API fx_result fx_serial_ctx_serialise(
fx_serial_ctx *ctx,
fx_object *src,
const fx_value *src,
fx_stream *dest,
fx_serial_flags flags);
FX_API fx_result fx_serial_ctx_deserialise(
fx_serial_ctx *ctx,
fx_stream *src,
fx_object **dest,
fx_value *dest,
fx_serial_flags flags);
FX_DECLS_END;
+53 -75
View File
@@ -1,24 +1,26 @@
#include <fx/bool.h>
#include <fx/collections/array.h>
#include <fx/collections/datetime.h>
#include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/float.h>
#include <fx/int.h>
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
#include <fx/string.h>
#include <inttypes.h>
#include <math.h>
void write_tagged_value(fx_object *data);
void write_tagged_value(const fx_value *data);
void write_raw_string(const fx_string *data)
{
fx_stream_write_cstr(fx_stdout, "\"", NULL);
const fx_iterator *it = fx_iterator_cbegin(data);
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it)
{
fx_wchar c;
fx_value_get_wchar(&val, &c);
fx_value_get_wchar(val, &c);
if (c >= 0x10000) {
c -= 0x10000;
long hi = 0xD800 | ((c >> 10) & 0x3FF);
@@ -54,75 +56,49 @@ void write_tagged_string(fx_string *data)
fx_stream_write_cstr(fx_stdout, " }", NULL);
}
void write_tagged_integer(fx_int *data)
void write_tagged_integer(const fx_value *data)
{
#if 0
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"integer\", \"value\": \"",
NULL);
if (fx_int_is_inf_positive(data)) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_int_is_inf_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_int_is_nan_positive(data)) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_int_is_nan_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else {
fx_stream_write_fmt(
fx_stdout,
NULL,
"%lld",
fx_int_get_value(data),
NULL);
}
fx_stream_write_fmt(fx_stdout, NULL, "%lld", data->v_i64, NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
#endif
}
void write_tagged_float(fx_double *data)
void write_tagged_float(const fx_value *data)
{
#if 0
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"float\", \"value\": \"",
NULL);
if (fx_double_is_inf_positive(data)) {
double d = data->v_double;
if (d == INFINITY) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_double_is_inf_negative(data)) {
} else if (d == -INFINITY) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_double_is_nan_positive(data)) {
} else if (d == NAN) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_double_is_nan_negative(data)) {
} else if (d == -NAN) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else if (d <= 1e-9 || d >= 1e9) {
fx_stream_write_fmt(fx_stdout, NULL, "%g", d, NULL);
} else {
double v = fx_double_get_value(data);
if ((v <= 0.00000001 && v > 0) || (v >= -0.00000001 && v < 0)
|| (v >= 1000000000) || (v <= -1000000000)) {
fx_stream_write_fmt(fx_stdout, NULL, "%.15e", v, NULL);
} else {
fx_stream_write_fmt(fx_stdout, NULL, "%.15f", v, NULL);
}
fx_stream_write_fmt(fx_stdout, NULL, "%.9lf", d, NULL);
}
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
#endif
}
void write_tagged_bool(fx_bool *data)
void write_tagged_bool(const fx_value *data)
{
#if 0
fx_stream_write_fmt(
fx_stdout,
NULL,
"{ \"type\": \"bool\", \"value\": \"%s\" }",
fx_bool_get_value(data) ? "true" : "false",
data->v_bool ? "true" : "false",
NULL);
#endif
}
void write_tagged_datetime(fx_datetime *data)
@@ -152,75 +128,78 @@ void write_tagged_datetime(fx_datetime *data)
fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL);
fx_string *new_data = fx_string_create();
fx_stringstream *new_data = fx_stringstream_create();
fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data);
fx_stream_write_cstr(fx_stdout, fx_string_get_cstr(new_data), NULL);
fx_stream_write_cstr(fx_stdout, fx_stringstream_ptr(new_data), NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
fx_string_unref(new_data);
fx_stringstream_unref(new_data);
}
#if 0
void write_tagged_dict(fx_dict *data)
void write_tagged_hashtable(fx_hashtable *data)
{
fx_stream_write_cstr(fx_stdout, "{ ", NULL);
int i = 0;
fx_iterator *it = fx_iterator_begin(data);
fx_foreach(fx_dict_item *, item, it)
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(v, it)
{
fx_hashtable_item *item;
fx_value_get_object(v, &item);
if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL);
}
write_raw_string(item->key);
const fx_value *key = fx_hashtable_item_get_key(item);
const fx_value *value = fx_hashtable_item_get_value(item);
fx_string *key_str;
fx_value_get_object(key, &key_str);
write_raw_string(key_str);
fx_stream_write_cstr(fx_stdout, ": ", NULL);
write_tagged_value(item->value);
write_tagged_value(value);
}
fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " }", NULL);
}
#endif
void write_tagged_array(fx_array *data)
{
fx_stream_write_cstr(fx_stdout, "[ ", NULL);
int i = 0;
fx_iterator *it = fx_iterator_begin(data);
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it)
{
fx_object *obj;
fx_value_get_object(&val, &obj);
if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL);
}
write_tagged_value(obj);
write_tagged_value(val);
}
fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " ]", NULL);
}
void write_tagged_value(fx_object *data)
void write_tagged_value(const fx_value *data)
{
#if 0
if (fx_object_is_type(data, FX_TYPE_DICT)) {
write_tagged_dict(data);
} else
#endif
if (fx_object_is_type(data, FX_TYPE_ARRAY)) {
write_tagged_array(data);
} else if (fx_object_is_type(data, FX_TYPE_STRING)) {
write_tagged_string(data);
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data);
} else if (fx_object_is_type(data, FX_TYPE_BOOL)) {
if (fx_value_is_type(data, FX_TYPE_HASHTABLE)) {
write_tagged_hashtable(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_ARRAY)) {
write_tagged_array(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_STRING)) {
write_tagged_string(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_BOOL)) {
write_tagged_bool(data);
} else if (fx_value_is_type(data, FX_TYPE_I64)) {
write_tagged_integer(data);
} else if (fx_value_is_type(data, FX_TYPE_DOUBLE)) {
write_tagged_float(data);
}
}
@@ -231,19 +210,18 @@ int main(void)
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
fx_object *data;
fx_value data;
fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
if (fx_result_is_error(result)) {
fx_throw(result);
return -1;
return 1;
}
write_tagged_value(data);
write_tagged_value(&data);
fx_stream_write_char(fx_stdout, '\n');
fx_serial_ctx_unref(ctx);
fx_object_unref(data);
fx_value_unset(&data);
return 0;
}
+134 -181
View File
@@ -1,8 +1,8 @@
#include <fx/bool.h>
#include <fx/collections/array.h>
#include <fx/collections/datetime.h>
#include <fx/collections/hashmap.h>
#include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/error.h>
#include <fx/float.h>
#include <fx/int.h>
@@ -11,6 +11,7 @@
#include <fx/status.h>
#include <fx/string.h>
#include <fx/stringstream.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@@ -116,9 +117,9 @@ struct ctx {
fx_stream *ctx_src;
fx_string *ctx_wordbuf;
fx_string *ctx_linebuf;
fx_iterator *ctx_linebuf_ptr;
const fx_iterator *ctx_linebuf_ptr;
fx_result ctx_result;
fx_hashmap *ctx_objects_flags;
fx_hashtable *ctx_objects_flags;
fx_queue ctx_tokens;
};
@@ -132,29 +133,18 @@ static void ctx_set_object_flags(
return;
}
fx_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(fx_object *),
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *old_value = fx_hashmap_get(
ctx->ctx_objects_flags,
&key);
fx_value ptr = FX_POINTER(obj);
const fx_value *old_value
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
enum object_flags new_flags = 0;
if (old_value) {
new_flags = (enum object_flags)(uintptr_t)old_value->value_data;
new_flags = (enum object_flags)old_value->v_i32;
}
new_flags |= flags;
fx_hashmap_value value = {
.value_data = (void *)new_flags,
.value_size = sizeof new_flags,
};
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
}
static void ctx_clear_object_flags(
@@ -166,29 +156,18 @@ static void ctx_clear_object_flags(
return;
}
fx_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(fx_object *),
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *old_value = fx_hashmap_get(
ctx->ctx_objects_flags,
&key);
fx_value ptr = FX_POINTER(obj);
const fx_value *old_value
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
enum object_flags new_flags = 0;
if (old_value) {
new_flags = (enum object_flags)(uintptr_t)old_value->value_data;
new_flags = (enum object_flags)old_value->v_i32;
}
new_flags &= ~mask;
fx_hashmap_value value = {
.value_data = (void *)new_flags,
.value_size = sizeof new_flags,
};
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
}
static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
@@ -197,17 +176,13 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
return 0;
}
fx_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(fx_object *),
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
fx_value ptr = FX_POINTER(obj);
const fx_value *old_value
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
const fx_hashmap_value *value = fx_hashmap_get(
ctx->ctx_objects_flags,
&key);
if (value) {
return (enum object_flags)(uintptr_t)value->value_data;
enum object_flags new_flags = 0;
if (old_value) {
return (enum object_flags)old_value->v_i32;
}
return 0;
@@ -297,7 +272,7 @@ static fx_wchar advance_char(struct ctx *ctx)
const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int;
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr)->v_wchar;
if (!is_valid_char(c)) {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
@@ -330,7 +305,7 @@ static fx_wchar peek_char(struct ctx *ctx)
const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int;
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr)->v_wchar;
if (!is_valid_char(c)) {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
@@ -929,7 +904,7 @@ static void split_word(struct ctx *ctx, fx_string *wordbuf)
fx_foreach(tokv, it)
{
const char *tok = NULL;
fx_value_get_cstr(&tokv, &tok);
fx_value_get_cstr(tokv, &tok);
if (i > 0) {
enqueue_token(ctx, TOK_DOT);
@@ -1032,11 +1007,11 @@ static void read_word(struct ctx *ctx)
return;
}
fx_iterator *it = fx_iterator_begin(wordbuf);
const fx_iterator *it = fx_iterator_begin(wordbuf);
fx_foreach(cv, it)
{
fx_wchar c = FX_WCHAR_INVALID;
fx_value_get_wchar(&cv, &c);
fx_value_get_wchar(cv, &c);
/* only allow ASCII numbers/letters here */
bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
if (!ok) {
@@ -1184,8 +1159,8 @@ static void read_string(struct ctx *ctx, bool squote)
}
if (c != '\n') {
ctx->ctx_result = FX_RESULT_ERR(
BAD_FORMAT);
ctx->ctx_result
= FX_RESULT_ERR(BAD_FORMAT);
fail = true;
break;
}
@@ -1230,18 +1205,16 @@ static void read_string(struct ctx *ctx, bool squote)
case 'U':
c = read_unicode_sequence(ctx);
if (c == FX_WCHAR_INVALID) {
ctx->ctx_result = FX_RESULT_ERR(
BAD_FORMAT);
ctx->ctx_result
= FX_RESULT_ERR(BAD_FORMAT);
fail = true;
break;
}
ctx->ctx_result = FX_OK(fx_string_append_wc(
str,
c))
ctx->ctx_result
= FX_OK(fx_string_append_wc(str, c))
? FX_SUCCESS
: FX_RESULT_ERR(
BAD_FORMAT);
: FX_RESULT_ERR(BAD_FORMAT);
fail = !FX_OK(ctx->ctx_result);
break;
default:
@@ -1482,14 +1455,24 @@ static fx_result advance_token(struct ctx *ctx)
start:
c = peek_char(ctx);
while (isspace(c) && c != '\n' && c != '\r') {
advance_char(ctx);
c = peek_char(ctx);
if (c <= 0) {
if (fx_error_get_status_code(ctx->ctx_result)
== FX_ERR_NO_DATA) {
ctx->ctx_flags |= CTX_EOF;
}
if (c == -1) {
ctx->ctx_flags |= CTX_EOF;
return FX_RESULT_ERR(NO_DATA);
return ctx->ctx_result;
}
if (c <= 31 && c != '\n' && c != '\r' && c != '\t') {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
return ctx->ctx_result;
}
while (fx_wchar_is_space(c) && c != '\n' && c != '\r') {
advance_char(ctx);
c = peek_char(ctx);
}
#if 1
@@ -1559,7 +1542,7 @@ static void ctx_cleanup(struct ctx *ctx)
}
if (ctx->ctx_objects_flags) {
fx_hashmap_unref(ctx->ctx_objects_flags);
fx_hashtable_unref(ctx->ctx_objects_flags);
ctx->ctx_objects_flags = NULL;
}
}
@@ -1571,14 +1554,14 @@ static fx_result ctx_init(struct ctx *ctx)
ctx->ctx_linebuf = fx_string_create();
ctx->ctx_wordbuf = fx_string_create();
ctx->ctx_objects_flags = fx_hashmap_create(NULL, NULL);
ctx->ctx_objects_flags = fx_hashtable_create();
return FX_RESULT_SUCCESS;
}
static fx_result toml_serialise(
fx_serial_ctx *serial,
fx_object *src,
const fx_value *src,
fx_stream *dest,
enum fx_serial_flags flags)
{
@@ -1672,20 +1655,20 @@ static void print_token(struct token *tok)
}
}
static fx_result parse_value(struct ctx *ctx, fx_object **result);
static fx_result parse_value(struct ctx *ctx, fx_value *result);
static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container);
static fx_result parse_timestamp(struct ctx *ctx, fx_object **result)
static fx_result parse_timestamp(struct ctx *ctx, fx_value *result)
{
struct token *tok = peek_token(ctx);
fx_datetime *dt = tok->tok_value.time;
tok->tok_value.time = NULL;
*result = (dt);
*result = FX_VALUE_OBJECT(dt);
return FX_RESULT_SUCCESS;
}
static fx_result parse_string(struct ctx *ctx, fx_object **result)
static fx_result parse_string(struct ctx *ctx, fx_value *result)
{
struct token *tok = peek_token(ctx);
fx_string *str = fx_string_duplicate(tok->tok_str);
@@ -1693,81 +1676,48 @@ static fx_result parse_string(struct ctx *ctx, fx_object **result)
return FX_RESULT_ERR(NO_MEMORY);
}
*result = (str);
*result = FX_VALUE_OBJECT(str);
return FX_RESULT_SUCCESS;
}
static fx_result parse_int(struct ctx *ctx, fx_object **result)
static fx_result parse_int(struct ctx *ctx, fx_value *result)
{
#if 0
struct token *tok = peek_token(ctx);
fx_int *val = fx_int_create(tok->tok_value.i.v);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
if (tok->tok_value.i.inf) {
if (tok->tok_value.i.v >= 0) {
fx_int_set_value_inf(val);
} else {
fx_int_set_value_inf_negative(val);
}
} else if (tok->tok_value.i.nan) {
if (tok->tok_value.i.v >= 0) {
fx_int_set_value_nan(val);
} else {
fx_int_set_value_nan_negative(val);
}
}
*result = (val);
#endif
*result = FX_I64(tok->tok_value.i.v);
return FX_RESULT_SUCCESS;
}
static fx_result parse_float(struct ctx *ctx, fx_object **result)
static fx_result parse_float(struct ctx *ctx, fx_value *result)
{
#if 0
struct token *tok = peek_token(ctx);
fx_double *val = fx_double_create(tok->tok_value.f.v);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
if (tok->tok_value.f.inf) {
if (tok->tok_value.f.v >= 0) {
fx_double_set_value_inf(val);
*result = FX_DOUBLE(INFINITY);
} else {
fx_double_set_value_inf_negative(val);
*result = FX_DOUBLE(-INFINITY);
}
} else if (tok->tok_value.f.nan) {
if (tok->tok_value.f.v >= 0) {
fx_double_set_value_nan(val);
*result = FX_DOUBLE(NAN);
} else {
fx_double_set_value_nan_negative(val);
*result = FX_DOUBLE(-NAN);
}
} else {
*result = FX_DOUBLE(tok->tok_value.f.v);
}
*result = (val);
#endif
return FX_RESULT_SUCCESS;
}
static fx_result parse_bool(struct ctx *ctx, fx_object **result)
static fx_result parse_bool(struct ctx *ctx, fx_value *result)
{
#if 0
struct token *tok = peek_token(ctx);
fx_bool *val = fx_bool_create(tok->tok_value.b);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
*result = (val);
#endif
*result = FX_BOOL(tok->tok_value.b);
return FX_RESULT_SUCCESS;
}
static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
static fx_result parse_table_inline(struct ctx *ctx, fx_value *result)
{
DISABLE_EXTENDED_LEXING(ctx);
@@ -1780,7 +1730,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
struct token *tok = peek_token(ctx);
if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
*result = (table);
*result = FX_VALUE_OBJECT(table);
return FX_RESULT_SUCCESS;
}
@@ -1814,7 +1764,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
}
}
*result = (table);
*result = FX_VALUE_OBJECT(table);
return FX_RESULT_SUCCESS;
}
@@ -1828,7 +1778,7 @@ static void skip_newlines(struct ctx *ctx)
}
}
static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
static fx_result parse_array_inline(struct ctx *ctx, fx_value *result)
{
bool done = false;
ENABLE_EXTENDED_LEXING(ctx);
@@ -1865,16 +1815,15 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
break;
}
fx_object *value;
fx_value value;
fx_result status = parse_value(ctx, &value);
if (!FX_OK(status)) {
fx_array_unref(array);
return status;
}
#if 0
fx_array_append(array, FX_RV(value));
#endif
fx_array_push_back(array, value);
fx_value_unset(&value);
ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx);
@@ -1897,11 +1846,11 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
}
DISABLE_EXTENDED_LEXING(ctx);
*result = (array);
*result = FX_VALUE_OBJECT(array);
return FX_RESULT_SUCCESS;
}
static fx_result parse_value(struct ctx *ctx, fx_object **result)
static fx_result parse_value(struct ctx *ctx, fx_value *result)
{
struct token *tok = peek_token(ctx);
@@ -1948,9 +1897,13 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
}
while (tok && tok->tok_type == TOK_DOT) {
const fx_value *child = fx_hashtable_get(
container,
&FX_VALUE_OBJECT(key));
const fx_value *child
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
if (child && !fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_hashmap *subtable = NULL;
fx_value_get_object(child, &subtable);
if (!subtable) {
@@ -1959,18 +1912,13 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(subtable));
} else if (
subtable
&& !fx_object_is_type(subtable, FX_TYPE_HASHTABLE)) {
free(key);
return FX_RESULT_ERR(BAD_FORMAT);
}
#if 1
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
if (flags
& (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) {
free(key);
fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT);
}
#endif
@@ -1980,7 +1928,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
advance_token(ctx);
tok = peek_token(ctx);
if (!IS_VALID_KEY_COMPONENT(tok)) {
free(key);
fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT);
}
@@ -2010,7 +1958,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx);
fx_object *value = NULL;
fx_value value = FX_VALUE_EMPTY;
fx_result result = parse_value(ctx, &value);
DISABLE_EXTENDED_LEXING(ctx);
@@ -2025,17 +1973,17 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
return fx_result_propagate(result);
}
fx_hashtable_put(
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(value));
fx_object_unref(value);
fx_hashtable_put(container, &FX_VALUE_OBJECT(key), &value);
if (fx_object_is_type(value, FX_TYPE_HASHTABLE)
|| fx_object_is_type(value, FX_TYPE_ARRAY)) {
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
if (fx_value_is_type(&value, FX_TYPE_HASHTABLE)
|| fx_value_is_type(&value, FX_TYPE_ARRAY)) {
ctx_set_object_flags(
ctx,
value.v_object,
OBJECT_KV_END_DEFINED);
}
fx_value_unset(&value);
return FX_RESULT_SUCCESS;
}
@@ -2062,26 +2010,24 @@ static fx_result parse_table_header(
}
while (tok && tok->tok_type == TOK_DOT) {
const fx_value *value = fx_hashtable_get(
container,
&FX_VALUE_OBJECT(key));
const fx_value *value
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
fx_object *subtable = NULL;
fx_value_get_object(value, &subtable);
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
if (!subtable) {
if (!value) {
subtable = (fx_hashtable_create());
fx_hashtable_put(
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(subtable));
fx_object_unref(subtable);
} else if (fx_object_is_type(subtable, FX_TYPE_ARRAY)) {
#if 0
subtable = fx_array_at(
} else if (fx_value_is_type(value, FX_TYPE_ARRAY)) {
value = fx_array_get_ref(
subtable,
fx_array_size(subtable) - 1);
#endif
} else if (!fx_object_is_type(subtable, FX_TYPE_HASHTABLE)) {
fx_array_get_size(subtable) - 1);
fx_value_get_object(value, &subtable);
} else if (!fx_value_is_type(value, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
@@ -2112,9 +2058,12 @@ static fx_result parse_table_header(
return FX_RESULT_ERR(BAD_FORMAT);
}
const fx_value *child = fx_hashtable_get(
container,
&FX_VALUE_OBJECT(key));
const fx_value *child
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
if (child && !fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_hashtable *new_table = NULL;
fx_value_get_object(child, &new_table);
@@ -2122,7 +2071,7 @@ static fx_result parse_table_header(
new_table = fx_hashtable_create();
if (!new_table) {
free(key);
fx_string_unref(key);
return FX_RESULT_ERR(NO_MEMORY);
}
@@ -2175,26 +2124,29 @@ static fx_result parse_array_header(
}
while (tok && tok->tok_type == TOK_DOT) {
const fx_value *child = fx_hashtable_get(
container,
&FX_VALUE_OBJECT(key));
const fx_value *child
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
fx_object *sub_dict = NULL;
fx_value_get_object(child, &sub_dict);
if (!sub_dict) {
enum object_flags flags = ctx_get_object_flags(ctx, (sub_dict));
if (flags & OBJECT_KV_END_DEFINED) {
return FX_RESULT_ERR(BAD_FORMAT);
}
if (!child) {
sub_dict = (fx_hashtable_create());
fx_hashtable_put(
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(sub_dict));
fx_hashtable_unref(sub_dict);
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
#if 0
sub_dict = fx_array_at(
} else if (fx_value_is_type(child, FX_TYPE_ARRAY)) {
child = fx_array_get_ref(
sub_dict,
fx_array_size(sub_dict) - 1);
#endif
} else if (!fx_object_is_type(sub_dict, FX_TYPE_HASHTABLE)) {
fx_array_get_size(sub_dict) - 1);
sub_dict = child->v_object;
} else if (!fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
@@ -2219,9 +2171,12 @@ static fx_result parse_array_header(
return FX_RESULT_ERR(BAD_FORMAT);
}
const fx_value *child = fx_hashtable_get(
container,
&FX_VALUE_OBJECT(key));
const fx_value *child
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
if (child && !fx_value_is_type(child, FX_TYPE_ARRAY)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_array *array = NULL;
fx_value_get_object(child, &array);
@@ -2234,11 +2189,11 @@ static fx_result parse_array_header(
} else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
free(key);
fx_string_unref(key);
enum object_flags flags = ctx_get_object_flags(ctx, (array));
if (flags & OBJECT_KV_END_DEFINED) {
return FX_RESULT_ERR(NO_MEMORY);
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_hashtable *new_table = fx_hashtable_create();
@@ -2246,9 +2201,7 @@ static fx_result parse_array_header(
return FX_RESULT_ERR(NO_MEMORY);
}
#if 0
fx_array_append(array, FX_RV(new_table));
#endif
fx_array_push_back(array, FX_VALUE_OBJECT(new_table));
advance_token(ctx);
*new_container = new_table;
@@ -2330,7 +2283,7 @@ static fx_result parse_root(struct ctx *ctx, fx_hashtable **out)
static fx_result toml_deserialise(
fx_serial_ctx *serial,
fx_stream *src,
fx_object **dest,
fx_value *dest,
enum fx_serial_flags flags)
{
struct ctx ctx = {0};
@@ -2351,7 +2304,7 @@ static fx_result toml_deserialise(
}
if (ctx.ctx_flags & CTX_EOF) {
*dest = (fx_hashtable_create());
*dest = FX_VALUE_OBJECT(fx_hashtable_create());
return FX_RESULT_SUCCESS;
}
@@ -2361,7 +2314,7 @@ static fx_result toml_deserialise(
return fx_result_propagate(ctx.ctx_result);
}
*dest = (data);
*dest = FX_VALUE_OBJECT(data);
#if 0
ctx.ctx_flags
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
+4 -5
View File
@@ -1,4 +1,4 @@
#include <fx/collections/datetime.h>
#include <fx/datetime.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <fx/value.h>
@@ -339,7 +339,7 @@ static enum fx_status encode_rfc3339(
dt->dt_sec);
if (dt->dt_msec > 0) {
fx_stream_write_fmt(out, NULL, ".%04ld", dt->dt_msec);
fx_stream_write_fmt(out, NULL, ".%03ld", dt->dt_msec);
}
if (!dt->dt_localtime) {
@@ -572,9 +572,8 @@ static fx_status _datetime_to_string(
fx_stream *out,
const char *format)
{
struct fx_datetime_p *dt = fx_object_get_private(
obj->v_object,
FX_TYPE_DATETIME);
struct fx_datetime_p *dt
= fx_object_get_private(obj->v_object, FX_TYPE_DATETIME);
if (dt->dt_has_date) {
fx_stream_write_fmt(
+1
View File
@@ -94,6 +94,7 @@ FX_TYPE_DEFINITION_BEGIN(fx_double)
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
FX_TYPE_NAME("fx.double");
FX_TYPE_CLASS(fx_double_class);
FX_TYPE_INSTANCE_PRIVATE(double);
+5 -12
View File
@@ -10,15 +10,10 @@
FX_DECLS_BEGIN;
#define fx_foreach(var, iterator) \
for (fx_value var = fx_iterator_get_value(iterator); \
for (const fx_value *var = fx_iterator_get_value(iterator); \
FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \
var = fx_iterator_get_value(iterator))
#define fx_foreach_ptr(type, var, iterator) \
for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \
FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \
var = (type *)fx_iterator_get_value(iterator).v_ptr)
#define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)})
#define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)})
@@ -36,12 +31,11 @@ FX_DECLARE_TYPE(fx_iterable);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator)
fx_status (*it_move_next)(const fx_iterator *);
fx_status (*it_erase)(fx_iterator *);
fx_value (*it_get_value)(const fx_iterator *);
const fx_value *(*it_get_value)(const fx_iterator *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable)
fx_iterator *(*it_begin)(fx_iterable *);
const fx_iterator *(*it_cbegin)(const fx_iterable *);
const fx_iterator *(*it_begin)(const fx_iterable *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterable)
FX_API fx_type_id fx_iterator_get_type(void);
@@ -56,15 +50,14 @@ static inline void fx_iterator_unref(const fx_iterator *p)
fx_object_unref((fx_object *)p);
}
FX_API fx_iterator *fx_iterator_begin(fx_iterable *it);
FX_API const fx_iterator *fx_iterator_cbegin(const fx_iterable *it);
FX_API const fx_iterator *fx_iterator_begin(const fx_iterable *it);
FX_API fx_status fx_iterator_get_status(const fx_iterator *it);
FX_API fx_status
fx_iterator_set_status(const fx_iterator *it, fx_status status);
FX_API fx_status fx_iterator_move_next(const fx_iterator *it);
FX_API fx_value fx_iterator_get_value(const fx_iterator *it);
FX_API const fx_value *fx_iterator_get_value(const fx_iterator *it);
FX_API fx_status fx_iterator_erase(fx_iterator *it);
static inline bool fx_iterator_is_valid(const fx_iterator *it)
{
+4 -1
View File
@@ -26,7 +26,10 @@ typedef struct fx_namemap_entry {
const char *e_name;
} fx_namemap_entry;
FX_API fx_status fx_namemap_cleanup(fx_namemap *map);
typedef void (*fx_namemap_cleanup_callback)(fx_namemap_entry *);
FX_API fx_status
fx_namemap_cleanup(fx_namemap *map, fx_namemap_cleanup_callback callback);
FX_API fx_status
fx_namemap_put(fx_namemap *map, const char *name, fx_namemap_entry *entry);
+5
View File
@@ -45,6 +45,11 @@
#define FX_WSTR(v) __FX_VALUE_CREATE(WSTR, v_wstr, v)
#define FX_CSTR(v) __FX_VALUE_CREATE(CSTR, v_cstr, v)
#define FX_POINTER(v) __FX_VALUE_CREATE(POINTER, v_pointer, v)
#define FX_VALUE_OBJECT_REF(v) \
((fx_value) { \
.v_type = fx_object_query_type((fx_object *)v), \
.v_object = fx_object_ref((fx_object *)(v)), \
})
#define FX_VALUE_OBJECT(v) \
((fx_value) { \
.v_type = fx_object_query_type((fx_object *)v), \
+3 -13
View File
@@ -23,7 +23,7 @@ static enum fx_status iterator_set_status(
/*** PUBLIC FUNCTIONS *********************************************************/
fx_iterator *fx_iterator_begin(fx_iterable *it)
const fx_iterator *fx_iterator_begin(const fx_iterable *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable,
@@ -33,16 +33,6 @@ fx_iterator *fx_iterator_begin(fx_iterable *it)
it);
}
const fx_iterator *fx_iterator_cbegin(const fx_iterable *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable,
FX_TYPE_ITERABLE,
NULL,
it_cbegin,
it);
}
enum fx_status fx_iterator_get_status(const fx_iterator *it)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it);
@@ -74,12 +64,12 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
return status;
}
fx_value fx_iterator_get_value(const fx_iterator *it)
const fx_value *fx_iterator_get_value(const fx_iterator *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterator,
FX_TYPE_ITERATOR,
FX_VALUE_EMPTY,
NULL,
it_get_value,
it);
}
+63 -29
View File
@@ -45,8 +45,50 @@ static struct map_bucket *map_item_convert_to_bucket(
return bucket;
}
enum fx_status fx_namemap_cleanup(struct fx_namemap *map)
static void cleanup_bucket(
struct map_bucket *bucket,
fx_namemap_cleanup_callback callback)
{
fx_queue_entry *cur = fx_queue_pop_front(&bucket->b_items);
while (cur) {
struct __fx_namemap_entry *entry
= fx_unbox(struct __fx_namemap_entry, cur, e_entry);
struct fx_namemap_entry *item
= (struct fx_namemap_entry *)entry;
if (callback) {
callback(item);
}
cur = fx_queue_pop_front(&bucket->b_items);
}
free(bucket);
}
enum fx_status fx_namemap_cleanup(
struct fx_namemap *map,
fx_namemap_cleanup_callback callback)
{
fx_bst_node *cur_node = fx_bst_first(&map->m_entries);
while (cur_node) {
struct __fx_namemap_entry *entry
= fx_unbox(struct __fx_namemap_entry, cur_node, e_node);
fx_bst_delete(&map->m_entries, cur_node);
if (entry->e_flags == MAP_ENTRY_BUCKET) {
struct map_bucket *bucket = (struct map_bucket *)entry;
cleanup_bucket(bucket, callback);
} else {
struct fx_namemap_entry *item
= (struct fx_namemap_entry *)entry;
if (callback) {
callback(item);
}
}
cur_node = fx_bst_first(&map->m_entries);
}
return FX_SUCCESS;
}
@@ -137,10 +179,8 @@ fx_namemap_entry *fx_namemap_get(const fx_namemap *map, const char *name)
struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map)
{
struct fx_bst_node *first = fx_bst_first(&map->m_entries);
struct __fx_namemap_entry *entry = fx_unbox(
struct __fx_namemap_entry,
first,
e_node);
struct __fx_namemap_entry *entry
= fx_unbox(struct __fx_namemap_entry, first, e_node);
if (!entry) {
return NULL;
}
@@ -158,10 +198,8 @@ struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map)
struct fx_namemap_entry *fx_namemap_last(const struct fx_namemap *map)
{
struct fx_bst_node *last = fx_bst_last(&map->m_entries);
struct __fx_namemap_entry *entry = fx_unbox(
struct __fx_namemap_entry,
last,
e_node);
struct __fx_namemap_entry *entry
= fx_unbox(struct __fx_namemap_entry, last, e_node);
if (entry->e_flags == MAP_ENTRY_ITEM) {
return (struct fx_namemap_entry *)entry;
}
@@ -179,11 +217,11 @@ struct fx_namemap_entry *fx_namemap_next(
struct map_bucket *bucket = entry->e_opaque.e_bucket;
struct __fx_namemap_entry *next_entry = NULL;
if (bucket) {
struct fx_queue_entry *q_entry = fx_queue_next(
&entry->e_opaque.e_entry);
struct fx_queue_entry *q_entry
= fx_queue_next(&entry->e_opaque.e_entry);
if (!q_entry) {
struct fx_bst_node *node = fx_bst_next(
&bucket->b_entry.e_node);
struct fx_bst_node *node
= fx_bst_next(&bucket->b_entry.e_node);
next_entry = fx_unbox(
struct __fx_namemap_entry,
node,
@@ -205,12 +243,10 @@ struct fx_namemap_entry *fx_namemap_next(
if (next_entry->e_flags == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)next_entry;
struct fx_queue_entry *q_entry = fx_queue_first(
&bucket->b_items);
next_entry = fx_unbox(
struct __fx_namemap_entry,
q_entry,
e_entry);
struct fx_queue_entry *q_entry
= fx_queue_first(&bucket->b_items);
next_entry
= fx_unbox(struct __fx_namemap_entry, q_entry, e_entry);
}
return (struct fx_namemap_entry *)next_entry;
@@ -223,11 +259,11 @@ struct fx_namemap_entry *fx_namemap_prev(
struct map_bucket *bucket = entry->e_opaque.e_bucket;
struct __fx_namemap_entry *prev_entry = NULL;
if (bucket) {
struct fx_queue_entry *q_entry = fx_queue_prev(
&entry->e_opaque.e_entry);
struct fx_queue_entry *q_entry
= fx_queue_prev(&entry->e_opaque.e_entry);
if (!q_entry) {
struct fx_bst_node *node = fx_bst_prev(
&bucket->b_entry.e_node);
struct fx_bst_node *node
= fx_bst_prev(&bucket->b_entry.e_node);
prev_entry = fx_unbox(
struct __fx_namemap_entry,
node,
@@ -249,12 +285,10 @@ struct fx_namemap_entry *fx_namemap_prev(
if (prev_entry->e_flags == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)prev_entry;
struct fx_queue_entry *q_entry = fx_queue_last(
&bucket->b_items);
prev_entry = fx_unbox(
struct __fx_namemap_entry,
q_entry,
e_entry);
struct fx_queue_entry *q_entry
= fx_queue_last(&bucket->b_items);
prev_entry
= fx_unbox(struct __fx_namemap_entry, q_entry, e_entry);
}
return (struct fx_namemap_entry *)prev_entry;
+23 -27
View File
@@ -41,16 +41,14 @@ fx_result fx_object_instantiate(
out->obj_type = type;
out->obj_ref = 1;
struct fx_queue_entry *entry = fx_queue_first(
&type->ty_class_hierarchy);
struct fx_queue_entry *entry
= fx_queue_first(&type->ty_class_hierarchy);
while (entry) {
struct fx_type_component *comp = fx_unbox(
struct fx_type_component,
entry,
c_entry);
struct fx_type_component *comp
= fx_unbox(struct fx_type_component, entry, c_entry);
const struct fx_type_info *class_info = comp->c_type;
void *private_data = (char *)out
+ comp->c_instance_private_data_offset;
void *private_data
= (char *)out + comp->c_instance_private_data_offset;
if (class_info->ty_instance_init) {
class_info->ty_instance_init(out, private_data);
@@ -90,7 +88,7 @@ fx_status fx_object_to_string(
fx_stream *out,
const char *format)
{
fx_value value = FX_VALUE_OBJECT(p);
fx_value value = FX_VALUE_OBJECT_REF(p);
fx_object_class *iface = fx_object_get_interface(p, FX_TYPE_OBJECT);
if (iface && iface->to_string) {
return iface->to_string(&value, out, format);
@@ -102,13 +100,16 @@ fx_status fx_object_to_string(
bool fx_object_is_type(const struct _fx_object *p, fx_type_id type)
{
if (!p) {
return false;
}
if (fx_type_id_compare(&p->obj_type->ty_id, type) == 0) {
return true;
}
struct fx_type_component *comp = fx_type_get_component(
&p->obj_type->ty_components,
type);
struct fx_type_component *comp
= fx_type_get_component(&p->obj_type->ty_components, type);
return comp != NULL;
}
@@ -125,9 +126,8 @@ void *fx_object_get_private(const struct _fx_object *object, fx_type_id type)
return (char *)object + object->obj_main_priv_offset;
}
struct fx_type_component *comp = fx_type_get_component(
&object->obj_type->ty_components,
type);
struct fx_type_component *comp
= fx_type_get_component(&object->obj_type->ty_components, type);
if (!comp) {
return NULL;
}
@@ -143,9 +143,8 @@ void *fx_object_get_protected(const struct _fx_object *object, fx_type_id type)
assert(object->obj_magic == FX_OBJECT_MAGIC);
struct fx_type_component *comp = fx_type_get_component(
&object->obj_type->ty_components,
type);
struct fx_type_component *comp
= fx_type_get_component(&object->obj_type->ty_components, type);
if (!comp) {
return NULL;
}
@@ -177,9 +176,8 @@ enum fx_status fx_object_get_data(
assert(object->obj_magic == FX_OBJECT_MAGIC);
struct fx_type_component *comp = fx_type_get_component(
&object->obj_type->ty_components,
type);
struct fx_type_component *comp
= fx_type_get_component(&object->obj_type->ty_components, type);
if (!comp) {
return FX_ERR_INVALID_ARGUMENT;
}
@@ -218,14 +216,12 @@ void fx_object_unref(struct _fx_object *p)
struct fx_queue_entry *cur = fx_queue_last(&type->ty_class_hierarchy);
while (cur) {
struct fx_type_component *comp = fx_unbox(
struct fx_type_component,
cur,
c_entry);
struct fx_type_component *comp
= fx_unbox(struct fx_type_component, cur, c_entry);
const struct fx_type_info *class_info = comp->c_type;
void *private_data = (char *)p
+ comp->c_instance_private_data_offset;
void *private_data
= (char *)p + comp->c_instance_private_data_offset;
if (class_info->ty_instance_fini) {
class_info->ty_instance_fini(p, private_data);
+34
View File
@@ -1,17 +1,51 @@
#include <fx/comparable.h>
#include <fx/pointer.h>
#include <fx/value-type.h>
#include <fx/value.h>
static fx_status hash(const fx_value *v, uint64_t *out_hash)
{
*out_hash = (uint64_t)v->v_pointer;
return FX_SUCCESS;
}
static i32 compare(const fx_value *left, const fx_value *right)
{
void *left_v = left->v_pointer;
void *right_v = 0;
if (!FX_OK(fx_value_get_pointer(right, &right_v))) {
return -1;
}
uintptr_t left_p = (uintptr_t)left_v;
uintptr_t right_p = (uintptr_t)right_v;
if (left_p < right_p) {
return -1;
} else if (left_p > right_p) {
return 1;
} else {
return 0;
}
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_pointer)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_INTERFACE_ENTRY(hash) = hash;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
FX_INTERFACE_ENTRY(c_compare) = compare;
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
FX_TYPE_CLASS_END(fx_pointer)
FX_TYPE_DEFINITION_BEGIN(fx_pointer)
__FX_VALUE_TYPE_ID(POINTER);
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
FX_TYPE_NAME("fx.pointer");
FX_TYPE_CLASS(fx_pointer_class);
FX_TYPE_INSTANCE_PRIVATE(void *);
+35 -40
View File
@@ -101,7 +101,7 @@
// Default precision for the floating point conversion specifiers (the C
// standard sets this at 6)
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION
#define PRINTF_DEFAULT_FLOAT_PRECISION 6
#define PRINTF_DEFAULT_FLOAT_PRECISION 13
#endif
// Default choice of type to use for internal floating-point computations
@@ -623,8 +623,8 @@ static void print_integer(
} else {
do {
const char digit = (char)(value % base);
buf[len++] = (char)(digit < 10
? '0' + digit
buf[len++]
= (char)(digit < 10 ? '0' + digit
: (flags & FLAGS_UPPERCASE
? 'A'
: 'a')
@@ -698,15 +698,14 @@ static struct floating_point_components get_components(
number_.is_negative = get_sign_bit(number);
floating_point_t abs_number = (number_.is_negative) ? -number : number;
number_.integral = (int_fast64_t)abs_number;
floating_point_t scaled_remainder = (abs_number
- (floating_point_t)
number_.integral)
floating_point_t scaled_remainder
= (abs_number - (floating_point_t)number_.integral)
* powers_of_10[precision];
number_.fractional = (int_fast64_t)
scaled_remainder; // for precision == 0U, this will be 0
floating_point_t remainder = scaled_remainder
- (floating_point_t)number_.fractional;
floating_point_t remainder
= scaled_remainder - (floating_point_t)number_.fractional;
const floating_point_t one_half = (floating_point_t)0.5;
if (remainder > one_half) {
@@ -779,18 +778,18 @@ static struct scaling_factor update_normalization(
result.raw_factor = sf.raw_factor * extra_multiplicative_factor;
} else {
int factor_exp2 = get_exp2(get_bit_access(sf.raw_factor));
int extra_factor_exp2 = get_exp2(
get_bit_access(extra_multiplicative_factor));
int extra_factor_exp2
= get_exp2(get_bit_access(extra_multiplicative_factor));
// Divide the larger-exponent raw raw_factor by the smaller
if (PRINTF_ABS(factor_exp2) > PRINTF_ABS(extra_factor_exp2)) {
result.multiply = false;
result.raw_factor = sf.raw_factor
/ extra_multiplicative_factor;
result.raw_factor
= sf.raw_factor / extra_multiplicative_factor;
} else {
result.multiply = true;
result.raw_factor = extra_multiplicative_factor
/ sf.raw_factor;
result.raw_factor
= extra_multiplicative_factor / sf.raw_factor;
}
}
return result;
@@ -818,25 +817,24 @@ static struct floating_point_components get_normalized_components(
return get_components(negative ? -scaled : scaled, precision);
}
components.integral = (int_fast64_t)scaled;
floating_point_t remainder = non_normalized
floating_point_t remainder
= non_normalized
- unapply_scaling(
(floating_point_t)
components.integral,
(floating_point_t)components.integral,
normalization);
floating_point_t prec_power_of_10 = powers_of_10[precision];
struct scaling_factor account_for_precision = update_normalization(
normalization,
prec_power_of_10);
floating_point_t scaled_remainder = apply_scaling(
remainder,
account_for_precision);
struct scaling_factor account_for_precision
= update_normalization(normalization, prec_power_of_10);
floating_point_t scaled_remainder
= apply_scaling(remainder, account_for_precision);
floating_point_t rounding_threshold = 0.5;
components.fractional = (int_fast64_t)
scaled_remainder; // when precision == 0, the assigned value
// should be 0
scaled_remainder -= (floating_point_t)components
.fractional; // when precision == 0, this
scaled_remainder
-= (floating_point_t)
components.fractional; // when precision == 0, this
// will not change
// scaled_remainder
@@ -960,9 +958,8 @@ static void print_decimal_number(
char *buf,
printf_size_t len)
{
struct floating_point_components value_ = get_components(
number,
precision);
struct floating_point_components value_
= get_components(number, precision);
print_broken_up_decimal(
value_,
output,
@@ -1105,9 +1102,9 @@ static void print_exponential_number(
abs_exp10_covered_by_powers_table
= PRINTF_ABS(floored_exp10)
< PRINTF_MAX_PRECOMPUTED_POWER_OF_10;
normalization.raw_factor = abs_exp10_covered_by_powers_table
? powers_of_10[PRINTF_ABS(
floored_exp10)]
normalization.raw_factor
= abs_exp10_covered_by_powers_table
? powers_of_10[PRINTF_ABS(floored_exp10)]
: p10;
}
@@ -1120,9 +1117,8 @@ static void print_exponential_number(
bool fall_back_to_decimal_only_mode = false;
if (flags & FLAGS_ADAPT_EXP) {
int required_significant_digits = (precision == 0)
? 1
: (int)precision;
int required_significant_digits
= (precision == 0) ? 1 : (int)precision;
// Should we want to fall-back to "%f" mode, and only print the
// decimal part?
fall_back_to_decimal_only_mode
@@ -1133,10 +1129,10 @@ static void print_exponential_number(
// "%g" mode, "precision" is the number of _significant digits_,
// and this is when we "translate" the precision value to an
// actual number of decimal digits.
int precision_ = fall_back_to_decimal_only_mode
int precision_
= fall_back_to_decimal_only_mode
? (int)precision - 1 - floored_exp10
: (int)precision
- 1; // the presence of the
: (int)precision - 1; // the presence of the
// exponent ensures only
// one significant digit
// comes before the
@@ -1573,9 +1569,8 @@ static inline void format_string_loop(
if (flags & FLAGS_LONG_LONG) {
#if PRINTF_SUPPORT_LONG_LONG
const long long value = va_arg(
args,
long long);
const long long value
= va_arg(args, long long);
print_integer(
output,
ABS_FOR_PRINTING(value),
+55 -75
View File
@@ -68,6 +68,7 @@ struct fx_string_iterator_p {
const char *string_value;
size_t string_length;
size_t string_codepoints;
fx_value value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
@@ -293,9 +294,8 @@ static fx_string *string_duplicate(const struct fx_string_p *str)
return NULL;
}
struct fx_string_p *new_str_p = fx_object_get_private(
new_str,
FX_TYPE_STRING);
struct fx_string_p *new_str_p
= fx_object_get_private(new_str, FX_TYPE_STRING);
string_change_capacity(new_str_p, str->s_len);
const char *src = string_ptr(str);
@@ -397,9 +397,8 @@ static enum fx_status replace_utf8(
}
size_t new_data_nr_bytes = strlen(new_data);
size_t new_data_nr_codepoints = fx_wchar_utf8_codepoint_count(
new_data,
new_data_nr_bytes);
size_t new_data_nr_codepoints
= fx_wchar_utf8_codepoint_count(new_data, new_data_nr_bytes);
if (new_data_nr_codepoints == 0) {
/* new_data is not a valid utf-8 string */
return FX_ERR_INVALID_ARGUMENT;
@@ -417,8 +416,8 @@ static enum fx_status replace_utf8(
return status;
}
size_t new_total_bytes = str->s_len - old_data_nr_bytes
+ new_data_nr_bytes;
size_t new_total_bytes
= str->s_len - old_data_nr_bytes + new_data_nr_bytes;
if (new_total_bytes > str->s_max) {
status = string_reserve(str, new_total_bytes);
}
@@ -747,9 +746,8 @@ static enum fx_status string_insert_wstr_ansi(
at = dest->s_len;
}
size_t utf8_encoded_size = fx_wchar_utf8_string_encoded_size(
src,
nr_codepoints);
size_t utf8_encoded_size
= fx_wchar_utf8_string_encoded_size(src, nr_codepoints);
if (utf8_encoded_size == 0) {
return FX_ERR_INVALID_ARGUMENT;
}
@@ -796,9 +794,8 @@ static enum fx_status string_insert_wstr_utf8(
codepoint_offset = dest->s_codepoints;
}
size_t utf8_encoded_size = fx_wchar_utf8_string_encoded_size(
src,
nr_codepoints);
size_t utf8_encoded_size
= fx_wchar_utf8_string_encoded_size(src, nr_codepoints);
if (utf8_encoded_size == 0) {
return FX_ERR_INVALID_ARGUMENT;
}
@@ -1041,9 +1038,8 @@ static fx_iterator *string_tokenise(
}
fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR);
it->_m = ITERATOR_MODE_TOKENS;
it->_d = delims;
@@ -1152,9 +1148,8 @@ static fx_string *string_substr(
}
fx_string *newstr = fx_string_create();
struct fx_string_p *newstr_p = fx_object_get_private(
newstr,
FX_TYPE_STRING);
struct fx_string_p *newstr_p
= fx_object_get_private(newstr, FX_TYPE_STRING);
string_reserve(newstr_p, len);
const char *src = string_ptr(str) + start;
@@ -1304,12 +1299,10 @@ enum fx_status fx_string_insert_s(
const fx_string *src,
size_t at)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
const struct fx_string_p *src_p = fx_object_get_private(
src,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
const struct fx_string_p *src_p
= fx_object_get_private(src, FX_TYPE_STRING);
return string_insert_s(dest_p, src_p, at);
}
@@ -1318,9 +1311,8 @@ enum fx_status fx_string_insert_cstr(
const char *src,
size_t at)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
return string_insert_cstr(dest_p, src, strlen(src), at);
}
@@ -1329,9 +1321,8 @@ enum fx_status fx_string_insert_wstr(
const fx_wchar *src,
size_t at)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
return string_insert_wstr(dest_p, src, fx_wstrlen(src), at);
}
@@ -1341,9 +1332,8 @@ enum fx_status fx_string_insert_cstrf(
const char *format,
...)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
va_list arg;
va_start(arg, format);
@@ -1370,9 +1360,8 @@ enum fx_status fx_string_insert_cstrn(
enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
va_list arg;
va_start(arg, format);
@@ -1384,9 +1373,8 @@ enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...)
enum fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
va_list arg;
va_start(arg, format);
@@ -1539,9 +1527,8 @@ static fx_status string_to_string(
fx_stream *out,
const char *format)
{
struct fx_string_p *str = fx_object_get_private(
obj->v_object,
FX_TYPE_STRING);
struct fx_string_p *str
= fx_object_get_private(obj->v_object, FX_TYPE_STRING);
const char *s = string_ptr(str);
for (size_t i = 0; i < str->s_len; i++) {
fx_stream_write_char(out, s[i]);
@@ -1552,9 +1539,8 @@ static fx_status string_to_string(
static fx_status string_hash(const fx_value *v, uint64_t *out_hash)
{
struct fx_string_p *str = fx_object_get_private(
v->v_object,
FX_TYPE_STRING);
struct fx_string_p *str
= fx_object_get_private(v->v_object, FX_TYPE_STRING);
const char *s = string_ptr(str);
*out_hash = fx_hash_cstr(s);
return FX_SUCCESS;
@@ -1570,7 +1556,9 @@ static i32 compare(const fx_value *left, const fx_value *right)
return -1;
}
return strcmp(left_cstr, right_cstr);
int result = strcmp(left_cstr, right_cstr);
return result;
}
static fx_status get_length(
@@ -1588,9 +1576,8 @@ static fx_status get_length(
static void iterator_fini(fx_iterator *obj)
{
struct fx_string_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
if (it->_tmp) {
fx_string_unref(it->_tmp);
}
@@ -1598,12 +1585,11 @@ static void iterator_fini(fx_iterator *obj)
memset(it, 0x0, sizeof *it);
}
static fx_iterator *iterator_begin(fx_object *obj)
static const fx_iterator *iterator_begin(const fx_object *obj)
{
fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR);
struct fx_string_p *p = fx_object_get_private(obj, FX_TYPE_STRING);
if (!p->s_len) {
@@ -1623,11 +1609,6 @@ static fx_iterator *iterator_begin(fx_object *obj)
return it_obj;
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{
return iterator_begin((fx_object *)obj);
}
static enum fx_status chars_iterator_move_next(struct fx_string_iterator_p *it)
{
if (!it->_s_p) {
@@ -1676,9 +1657,8 @@ static enum fx_status tokens_iterator_move_next(struct fx_string_iterator_p *it)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_string_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
switch (it->_m) {
case ITERATOR_MODE_CHARS:
@@ -1690,21 +1670,22 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
}
}
static fx_value chars_iterator_get_value(struct fx_string_iterator_p *it)
static fx_value *chars_iterator_get_value(struct fx_string_iterator_p *it)
{
return FX_U32(it->char_value);
it->value = FX_WCHAR(it->char_value);
return &it->value;
}
static fx_value tokens_iterator_get_value(struct fx_string_iterator_p *it)
static fx_value *tokens_iterator_get_value(struct fx_string_iterator_p *it)
{
return FX_CSTR(it->string_value);
it->value = FX_CSTR(it->string_value);
return &it->value;
}
static fx_value iterator_get_value(const fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_string_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
switch (it->_m) {
case ITERATOR_MODE_CHARS:
@@ -1712,7 +1693,7 @@ static fx_value iterator_get_value(const fx_iterator *obj)
case ITERATOR_MODE_TOKENS:
return tokens_iterator_get_value(it);
default:
return FX_VALUE_EMPTY;
return NULL;
}
}
@@ -1720,8 +1701,8 @@ static fx_status add(const fx_value *l, const fx_value *r, fx_value *out)
{
fx_string *left = l->v_object;
fx_string *right = r->v_object;
fx_string *result = fx_string_create_from_cstr(
fx_string_get_cstr(left));
fx_string *result
= fx_string_create_from_cstr(fx_string_get_cstr(left));
if (!result) {
return FX_ERR_NO_MEMORY;
}
@@ -1775,7 +1756,6 @@ FX_TYPE_CLASS_BEGIN(fx_string)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
+1 -1
View File
@@ -20,7 +20,7 @@ int main(void)
fx_foreach(val, it)
{
const char *tok = NULL;
fx_value_get_cstr(&val, &tok);
fx_value_get_cstr(val, &tok);
printf("%s\n", tok);
}
fx_iterator_unref(it);
+17 -24
View File
@@ -126,9 +126,8 @@ int fx_value_compare(const fx_value *left, const fx_value *right)
return FX_ERR_NOT_SUPPORTED;
}
fx_comparable_class *comparable = fx_class_get_interface(
c,
FX_TYPE_COMPARABLE);
fx_comparable_class *comparable
= fx_class_get_interface(c, FX_TYPE_COMPARABLE);
if (!comparable || !comparable->c_compare) {
return FX_ERR_NOT_SUPPORTED;
}
@@ -181,8 +180,8 @@ static fx_status value_change_string(fx_value *in, fx_value *out)
return status;
}
fx_string *result = fx_string_create_from_cstr(
fx_stringstream_ptr(strm));
fx_string *result
= fx_string_create_from_cstr(fx_stringstream_ptr(strm));
fx_stringstream_unref(strm);
if (!result) {
@@ -212,9 +211,8 @@ fx_status fx_value_change_type(
return FX_ERR_NOT_SUPPORTED;
}
fx_convertible_class *convertible = fx_class_get_interface(
c,
FX_TYPE_CONVERTIBLE);
fx_convertible_class *convertible
= fx_class_get_interface(c, FX_TYPE_CONVERTIBLE);
if (!convertible) {
return FX_ERR_NOT_SUPPORTED;
}
@@ -375,17 +373,15 @@ fx_status fx_value_change_type(
return FX_ERR_NOT_SUPPORTED; \
} \
\
fx_convertible_class *convertible = fx_class_get_interface( \
c, \
FX_TYPE_CONVERTIBLE); \
if (!convertible) { \
fx_convertible_class *convertible \
= fx_class_get_interface(c, FX_TYPE_CONVERTIBLE); \
if (!convertible || !convertible->c_to_##fx_type_name) { \
return FX_ERR_NOT_SUPPORTED; \
} \
\
c_type_name result; \
fx_status status = convertible->c_to_##fx_type_name( \
v, \
&result); \
fx_status status \
= convertible->c_to_##fx_type_name(v, &result); \
if (!FX_OK(status)) { \
return status; \
} \
@@ -657,9 +653,8 @@ FX_API fx_type_id fx_value_get_common_type(const fx_value *a, const fx_value *b)
return FX_ERR_NOT_SUPPORTED; \
} \
\
fx_operable_class *operable = fx_class_get_interface( \
c, \
FX_TYPE_OPERABLE); \
fx_operable_class *operable \
= fx_class_get_interface(c, FX_TYPE_OPERABLE); \
if (!operable || !operable->op_##name) { \
return FX_ERR_NOT_SUPPORTED; \
} \
@@ -674,9 +669,8 @@ FX_API fx_type_id fx_value_get_common_type(const fx_value *a, const fx_value *b)
return FX_ERR_NOT_SUPPORTED; \
} \
\
fx_operable_class *operable = fx_class_get_interface( \
c, \
FX_TYPE_OPERABLE); \
fx_operable_class *operable \
= fx_class_get_interface(c, FX_TYPE_OPERABLE); \
if (!operable || !operable->op_##name) { \
return FX_ERR_NOT_SUPPORTED; \
} \
@@ -695,9 +689,8 @@ FX_API fx_type_id fx_value_get_common_type(const fx_value *a, const fx_value *b)
return FX_ERR_NOT_SUPPORTED; \
} \
\
fx_operable_class *operable = fx_class_get_interface( \
c, \
FX_TYPE_OPERABLE); \
fx_operable_class *operable \
= fx_class_get_interface(c, FX_TYPE_OPERABLE); \
if (!operable || !operable->op_##name) { \
return FX_ERR_NOT_SUPPORTED; \
} \