fx: update fx_iterator to return pointers to values

This commit is contained in:
2026-05-28 20:50:00 +01:00
parent 01cbcf712a
commit f79650698a
5 changed files with 84 additions and 129 deletions
+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)