ds: formatting tweaks

This commit is contained in:
2026-04-20 22:03:20 +01:00
parent 321622ac16
commit c76008e498
6 changed files with 466 additions and 152 deletions
+11 -4
View File
@@ -31,7 +31,8 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
{
if (array->ar_cap < new_capacity) {
void *new_data = realloc(
array->ar_data, new_capacity * sizeof(struct fx_dsref *));
array->ar_data,
new_capacity * sizeof(struct fx_dsref *));
if (!new_data) {
return FX_ERR_NO_MEMORY;
}
@@ -43,7 +44,8 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
}
void *new_data = realloc(
array->ar_data, new_capacity * sizeof(struct fx_dsref *));
array->ar_data,
new_capacity * sizeof(struct fx_dsref *));
if (!new_data) {
return FX_ERR_NO_MEMORY;
}
@@ -59,7 +61,10 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
return FX_SUCCESS;
}
static fx_status array_insert(struct fx_array_p *array, fx_object *value, size_t at)
static fx_status array_insert(
struct fx_array_p *array,
fx_object *value,
size_t at)
{
if (at == FX_NPOS) {
at = array->ar_len;
@@ -193,7 +198,9 @@ static void array_clear(struct fx_array_p *array)
/*** PUBLIC FUNCTIONS *********************************************************/
fx_array *fx_array_create_with_values(fx_object *const *values, size_t nr_values)
fx_array *fx_array_create_with_values(
fx_object *const *values,
size_t nr_values)
{
fx_array *array = fx_array_create();
if (!array) {
+36 -13
View File
@@ -36,9 +36,16 @@ struct fx_dict_iterator_p {
/*** MISC FUNCTIONS ***********************************************************/
static FX_BST_DEFINE_SIMPLE_GET(
struct fx_dict_bucket, uint64_t, bk_node, bk_hash, get_bucket);
struct fx_dict_bucket,
uint64_t,
bk_node,
bk_hash,
get_bucket);
static FX_BST_DEFINE_SIMPLE_INSERT(
struct fx_dict_bucket, bk_node, bk_hash, put_bucket);
struct fx_dict_bucket,
bk_node,
bk_hash,
put_bucket);
uint64_t fx_cstr_hash(const char *s)
{
@@ -79,7 +86,10 @@ static struct fx_dict_bucket_item *create_bucket_item(void)
return item;
}
static fx_status dict_put(struct fx_dict_p *dict, const char *key, fx_object *value)
static fx_status dict_put(
struct fx_dict_p *dict,
const char *key,
fx_object *value)
{
uint64_t hash = fx_cstr_hash(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
@@ -107,7 +117,9 @@ static fx_status dict_put(struct fx_dict_p *dict, const char *key, fx_object *va
}
static fx_status dict_put_sk(
struct fx_dict_p *dict, const fx_string *key, fx_object *value)
struct fx_dict_p *dict,
const fx_string *key,
fx_object *value)
{
uint64_t hash = fx_string_hash(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
@@ -250,8 +262,10 @@ static bool dict_is_empty(const struct fx_dict_p *dict)
}
static bool get_next_node(
struct fx_bst_node *cur_node, struct fx_queue_entry *cur_entry,
struct fx_bst_node **out_next_node, struct fx_queue_entry **out_next_entry)
struct fx_bst_node *cur_node,
struct fx_queue_entry *cur_entry,
struct fx_bst_node **out_next_node,
struct fx_queue_entry **out_next_entry)
{
struct fx_dict_bucket *cur_bucket
= fx_unbox(struct fx_dict_bucket, cur_node, bk_node);
@@ -298,7 +312,8 @@ static bool get_next_node(
}
static fx_status delete_item(
struct fx_dict_p *dict, struct fx_dict_bucket *bucket,
struct fx_dict_p *dict,
struct fx_dict_bucket *bucket,
struct fx_dict_bucket_item *item)
{
fx_queue_delete(&bucket->bk_items, &item->bi_entry);
@@ -401,11 +416,15 @@ static void dict_fini(fx_object *obj, void *priv)
struct fx_bst_node *next_node = fx_bst_next(node);
fx_bst_delete(&dict->d_buckets, node);
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
struct fx_queue_entry *entry
= fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item = fx_unbox(
struct fx_dict_bucket_item, entry, bi_entry);
struct fx_queue_entry *next_entry = fx_queue_next(entry);
struct fx_dict_bucket_item,
entry,
bi_entry);
struct fx_queue_entry *next_entry
= fx_queue_next(entry);
fx_queue_delete(&bucket->bk_items, entry);
free(item->bi_str);
@@ -440,16 +459,20 @@ static void dict_to_string(const fx_object *obj, fx_stream *out)
struct fx_dict_bucket *bucket
= fx_unbox(struct fx_dict_bucket, node, bk_node);
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
struct fx_queue_entry *entry
= fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item = fx_unbox(
struct fx_dict_bucket_item, entry, bi_entry);
struct fx_dict_bucket_item,
entry,
bi_entry);
fx_object_to_string(item->bi_str, out);
fx_stream_write_cstr(out, ": ", NULL);
bool is_string = fx_object_is_type(
item->bi_value, FX_TYPE_STRING);
item->bi_value,
FX_TYPE_STRING);
if (is_string) {
fx_stream_write_char(out, '"');
+30 -19
View File
@@ -1,12 +1,12 @@
#ifndef FX_DS_STRING_H_
#define FX_DS_STRING_H_
#include <ctype.h>
#include <fx/core/encoding.h>
#include <fx/core/iterator.h>
#include <fx/core/macros.h>
#include <fx/core/status.h>
#include <fx/core/stringstream.h>
#include <ctype.h>
FX_DECLS_BEGIN;
@@ -77,30 +77,36 @@ FX_API fx_status fx_string_append_wc(fx_string *dest, fx_wchar c);
FX_API fx_status fx_string_append_s(fx_string *dest, const fx_string *src);
FX_API fx_status fx_string_append_cstr(fx_string *dest, const char *src);
FX_API fx_status fx_string_append_wstr(fx_string *dest, const fx_wchar *src);
FX_API fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...);
FX_API fx_status
fx_string_append_cstrf(fx_string *dest, const char *format, ...);
FX_API fx_status fx_string_prepend_c(fx_string *dest, char c);
FX_API fx_status fx_string_prepend_wc(fx_string *dest, fx_wchar c);
FX_API fx_status fx_string_prepend_cstr(fx_string *dest, const char *src);
FX_API fx_status fx_string_prepend_wstr(fx_string *dest, const fx_wchar *src);
FX_API fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...);
FX_API fx_status
fx_string_prepend_cstrf(fx_string *dest, const char *format, ...);
FX_API fx_status fx_string_insert_c(fx_string *dest, char c, size_t at);
FX_API fx_status fx_string_insert_wc(fx_string *dest, fx_wchar c, size_t at);
FX_API fx_status fx_string_insert_s(fx_string *dest, const fx_string *src, size_t at);
FX_API fx_status fx_string_insert_cstr(fx_string *dest, const char *src, size_t at);
FX_API fx_status fx_string_insert_wstr(
fx_string *dest, const fx_wchar *src, size_t at);
FX_API fx_status fx_string_insert_cstrn(
fx_string *dest, const char *src, size_t len, size_t at);
FX_API fx_status fx_string_insert_wstrn(
fx_string *dest, const char *src, size_t len, size_t at);
FX_API fx_status fx_string_insert_cstrf(
fx_string *dest, size_t at, const char *format, ...);
FX_API fx_status
fx_string_insert_s(fx_string *dest, const fx_string *src, size_t at);
FX_API fx_status
fx_string_insert_cstr(fx_string *dest, const char *src, size_t at);
FX_API fx_status
fx_string_insert_wstr(fx_string *dest, const fx_wchar *src, size_t at);
FX_API fx_status
fx_string_insert_cstrn(fx_string *dest, const char *src, size_t len, size_t at);
FX_API fx_status
fx_string_insert_wstrn(fx_string *dest, const char *src, size_t len, size_t at);
FX_API fx_status
fx_string_insert_cstrf(fx_string *dest, size_t at, const char *format, ...);
FX_API void fx_string_clear(fx_string *str);
FX_API fx_iterator *fx_string_tokenise(
fx_string *str, const char *delims[], size_t nr_delims,
fx_string *str,
const char *delims[],
size_t nr_delims,
fx_string_tokenise_flags flags);
FX_API size_t fx_string_get_size(const fx_string *str, fx_strlen_flags flags);
@@ -108,15 +114,20 @@ FX_API size_t fx_string_get_capacity(const fx_string *str);
FX_API bool fx_string_compare(const fx_string *a, const fx_string *b);
FX_API char fx_string_front(const fx_string *str);
FX_API char fx_string_back(const fx_string *str);
FX_API char fx_string_get_first_char(const fx_string *str);
FX_API char fx_string_get_last_char(const fx_string *str);
FX_API void fx_string_pop_back(fx_string *str);
FX_API const char *fx_string_ptr(const fx_string *str);
FX_API fx_string *fx_string_substr(const fx_string *str, size_t start, size_t len);
FX_API const char *fx_string_get_cstr(const fx_string *str);
FX_API fx_string *fx_string_get_substr(
const fx_string *str,
size_t start,
size_t len);
FX_API int fx_string_iterator_begin(const fx_string *string, fx_string_iterator *it);
FX_API int fx_string_iterator_begin(
const fx_string *string,
fx_string_iterator *it);
FX_API bool fx_string_iterator_next(fx_string_iterator *it);
// FX_API fx_status fx_string_iterator_erase(fx_string_iterator *it);
FX_API bool fx_string_iterator_is_valid(const fx_string_iterator *it);
+130 -35
View File
@@ -34,7 +34,8 @@ struct fx_number_p {
typedef int (*number_converter_t)(const struct fx_number_p *, void *);
static number_converter_t converters[FX_NUMBER_TYPE_COUNT][FX_NUMBER_TYPE_COUNT];
static number_converter_t converters[FX_NUMBER_TYPE_COUNT]
[FX_NUMBER_TYPE_COUNT];
/*** PRIVATE FUNCTIONS ********************************************************/
@@ -44,7 +45,9 @@ static fx_number_type number_get_number_type(const struct fx_number_p *number)
}
static int number_get_value(
const struct fx_number_p *number, fx_number_type type, void *value_ptr)
const struct fx_number_p *number,
fx_number_type type,
void *value_ptr)
{
fx_number_type srb_type = number->n_type;
fx_number_type dest_type = type;
@@ -738,27 +741,37 @@ fx_number *fx_number_create(fx_number_type type, void *value_ptr)
memcpy(&p->n_value.v_int8, value_ptr, sizeof p->n_value.v_int8);
break;
case FX_NUMBER_INT16:
memcpy(&p->n_value.v_int16, value_ptr, sizeof p->n_value.v_int16);
memcpy(&p->n_value.v_int16,
value_ptr,
sizeof p->n_value.v_int16);
break;
case FX_NUMBER_INT32:
memcpy(&p->n_value.v_int32, value_ptr, sizeof p->n_value.v_int32);
memcpy(&p->n_value.v_int32,
value_ptr,
sizeof p->n_value.v_int32);
break;
case FX_NUMBER_INT64:
memcpy(&p->n_value.v_int64, value_ptr, sizeof p->n_value.v_int64);
memcpy(&p->n_value.v_int64,
value_ptr,
sizeof p->n_value.v_int64);
break;
case FX_NUMBER_FLOAT32:
memcpy(&p->n_value.v_float32, value_ptr,
memcpy(&p->n_value.v_float32,
value_ptr,
sizeof p->n_value.v_float32);
break;
case FX_NUMBER_FLOAT64:
memcpy(&p->n_value.v_float64, value_ptr,
memcpy(&p->n_value.v_float64,
value_ptr,
sizeof p->n_value.v_float64);
break;
case FX_NUMBER_CHAR:
memcpy(&p->n_value.v_char, value_ptr, sizeof p->n_value.v_char);
break;
case FX_NUMBER_SHORT:
memcpy(&p->n_value.v_short, value_ptr, sizeof p->n_value.v_short);
memcpy(&p->n_value.v_short,
value_ptr,
sizeof p->n_value.v_short);
break;
case FX_NUMBER_INT:
memcpy(&p->n_value.v_int, value_ptr, sizeof p->n_value.v_int);
@@ -767,17 +780,24 @@ fx_number *fx_number_create(fx_number_type type, void *value_ptr)
memcpy(&p->n_value.v_long, value_ptr, sizeof p->n_value.v_long);
break;
case FX_NUMBER_LONGLONG:
memcpy(&p->n_value.v_longlong, value_ptr,
memcpy(&p->n_value.v_longlong,
value_ptr,
sizeof p->n_value.v_longlong);
break;
case FX_NUMBER_FLOAT:
memcpy(&p->n_value.v_float, value_ptr, sizeof p->n_value.v_float);
memcpy(&p->n_value.v_float,
value_ptr,
sizeof p->n_value.v_float);
break;
case FX_NUMBER_DOUBLE:
memcpy(&p->n_value.v_double, value_ptr, sizeof p->n_value.v_double);
memcpy(&p->n_value.v_double,
value_ptr,
sizeof p->n_value.v_double);
break;
case FX_NUMBER_SIZE_T:
memcpy(&p->n_value.v_size_t, value_ptr, sizeof p->n_value.v_size_t);
memcpy(&p->n_value.v_size_t,
value_ptr,
sizeof p->n_value.v_size_t);
break;
default:
break;
@@ -788,13 +808,23 @@ fx_number *fx_number_create(fx_number_type type, void *value_ptr)
fx_number_type fx_number_get_number_type(const fx_number *number)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_get_number_type, number);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_NUMBER,
number_get_number_type,
number);
}
int fx_number_get_value(const fx_number *number, fx_number_type type, void *value_ptr)
int fx_number_get_value(
const fx_number *number,
fx_number_type type,
void *value_ptr)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_NUMBER, number_get_value, number, type, value_ptr);
FX_TYPE_NUMBER,
number_get_value,
number,
type,
value_ptr);
}
bool fx_number_is_integer(const fx_number *number)
@@ -814,12 +844,18 @@ bool fx_number_is_inf(const fx_number *number)
bool fx_number_is_inf_positive(const fx_number *number)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_inf_positive, number);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_NUMBER,
number_is_inf_positive,
number);
}
bool fx_number_is_inf_negative(const fx_number *number)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_inf_negative, number);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_NUMBER,
number_is_inf_negative,
number);
}
bool fx_number_is_nan(const fx_number *number)
@@ -829,32 +865,54 @@ bool fx_number_is_nan(const fx_number *number)
bool fx_number_is_nan_positive(const fx_number *number)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_nan_positive, number);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_NUMBER,
number_is_nan_positive,
number);
}
bool fx_number_is_nan_negative(const fx_number *number)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_NUMBER, number_is_nan_negative, number);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_NUMBER,
number_is_nan_negative,
number);
}
void fx_number_set_inf_positive(fx_number *number, bool v)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_NUMBER, number_set_inf_positive, number, v);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_NUMBER,
number_set_inf_positive,
number,
v);
}
void fx_number_set_inf_negative(fx_number *number, bool v)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_NUMBER, number_set_inf_negative, number, v);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_NUMBER,
number_set_inf_negative,
number,
v);
}
void fx_number_set_nan_positive(fx_number *number, bool v)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_NUMBER, number_set_nan_positive, number, v);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_NUMBER,
number_set_nan_positive,
number,
v);
}
void fx_number_set_nan_negative(fx_number *number, bool v)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_NUMBER, number_set_nan_negative, number, v);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_NUMBER,
number_set_nan_negative,
number,
v);
}
size_t fx_number_data_size(const fx_number *number)
@@ -890,22 +948,42 @@ static void number_to_string(const fx_object *obj, fx_stream *out)
switch (number->n_type) {
case FX_NUMBER_INT8:
fx_stream_write_fmt(out, NULL, "%" PRIu8, number->n_value.v_int8);
fx_stream_write_fmt(
out,
NULL,
"%" PRIu8,
number->n_value.v_int8);
break;
case FX_NUMBER_INT16:
fx_stream_write_fmt(out, NULL, "%" PRIu16, number->n_value.v_int16);
fx_stream_write_fmt(
out,
NULL,
"%" PRIu16,
number->n_value.v_int16);
break;
case FX_NUMBER_INT32:
fx_stream_write_fmt(out, NULL, "%" PRIu32, number->n_value.v_int32);
fx_stream_write_fmt(
out,
NULL,
"%" PRIu32,
number->n_value.v_int32);
break;
case FX_NUMBER_INT64:
fx_stream_write_fmt(out, NULL, "%" PRIu64, number->n_value.v_int64);
fx_stream_write_fmt(
out,
NULL,
"%" PRIu64,
number->n_value.v_int64);
break;
case FX_NUMBER_FLOAT32:
fx_stream_write_fmt(out, NULL, "%f", number->n_value.v_float32);
break;
case FX_NUMBER_FLOAT64:
fx_stream_write_fmt(out, NULL, "%lf", number->n_value.v_float64);
fx_stream_write_fmt(
out,
NULL,
"%lf",
number->n_value.v_float64);
break;
case FX_NUMBER_CHAR:
fx_stream_write_fmt(out, NULL, "%d", number->n_value.v_char);
@@ -920,7 +998,11 @@ static void number_to_string(const fx_object *obj, fx_stream *out)
fx_stream_write_fmt(out, NULL, "%ld", number->n_value.v_long);
break;
case FX_NUMBER_LONGLONG:
fx_stream_write_fmt(out, NULL, "%lld", number->n_value.v_longlong);
fx_stream_write_fmt(
out,
NULL,
"%lld",
number->n_value.v_longlong);
break;
case FX_NUMBER_FLOAT:
fx_stream_write_fmt(out, NULL, "%f", number->n_value.v_float);
@@ -933,7 +1015,10 @@ static void number_to_string(const fx_object *obj, fx_stream *out)
break;
case FX_NUMBER_HANDLE:
fx_stream_write_fmt(
out, NULL, "%016" PRIx64, number->n_value.v_size_t);
out,
NULL,
"%016" PRIx64,
number->n_value.v_size_t);
break;
default:
break;
@@ -1364,7 +1449,9 @@ static int float32_to_long(const struct fx_number_p *number, void *value_ptr)
return 0;
}
static int float32_to_longlong(const struct fx_number_p *number, void *value_ptr)
static int float32_to_longlong(
const struct fx_number_p *number,
void *value_ptr)
{
*(long long *)value_ptr = number->n_value.v_float32;
return 0;
@@ -1450,7 +1537,9 @@ static int float64_to_long(const struct fx_number_p *number, void *value_ptr)
return 0;
}
static int float64_to_longlong(const struct fx_number_p *number, void *value_ptr)
static int float64_to_longlong(
const struct fx_number_p *number,
void *value_ptr)
{
*(long long *)value_ptr = number->n_value.v_float64;
return 0;
@@ -1844,13 +1933,17 @@ static int longlong_to_int64(const struct fx_number_p *number, void *value_ptr)
return 0;
}
static int longlong_to_float32(const struct fx_number_p *number, void *value_ptr)
static int longlong_to_float32(
const struct fx_number_p *number,
void *value_ptr)
{
*(float *)value_ptr = number->n_value.v_longlong;
return 0;
}
static int longlong_to_float64(const struct fx_number_p *number, void *value_ptr)
static int longlong_to_float64(
const struct fx_number_p *number,
void *value_ptr)
{
*(double *)value_ptr = number->n_value.v_longlong;
return 0;
@@ -1880,7 +1973,9 @@ static int longlong_to_long(const struct fx_number_p *number, void *value_ptr)
return 0;
}
static int longlong_to_longlong(const struct fx_number_p *number, void *value_ptr)
static int longlong_to_longlong(
const struct fx_number_p *number,
void *value_ptr)
{
*(long long *)value_ptr = number->n_value.v_longlong;
return 0;
+213 -62
View File
@@ -1,7 +1,7 @@
#include <ctype.h>
#include <fx/core/stream.h>
#include <fx/core/stringstream.h>
#include <fx/ds/string.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
@@ -27,7 +27,8 @@ enum iterator_mode {
struct fx_string_p {
/* length of string in bytes, not including null-terminator.
* a multi-byte utf-8 codepoint will be counted as multiple bytes here */
* a multi-byte utf-8 codepoint will be counted as multiple bytes here
*/
unsigned int s_len;
/* length of string in codepoints, not including null-terminator.
* a multi-byte utf-8 codepoint will be counted as one codepoint here */
@@ -76,8 +77,11 @@ static char *string_ptr(const struct fx_string_p *str)
}
static enum fx_status convert_codepoint_range_to_byte_range(
const struct fx_string_p *str, size_t cp_start, size_t cp_length,
size_t *out_byte_start, size_t *out_byte_length)
const struct fx_string_p *str,
size_t cp_start,
size_t cp_length,
size_t *out_byte_start,
size_t *out_byte_length)
{
const char *s = string_ptr(str);
size_t byte_offset = 0, byte_length = 0;
@@ -146,7 +150,9 @@ static char *get_next_codepoint(struct fx_string_p *str, char *this_codepoint)
return this_codepoint + len;
}
static char *get_previous_codepoint(struct fx_string_p *str, char *this_codepoint)
static char *get_previous_codepoint(
struct fx_string_p *str,
char *this_codepoint)
{
char *start = string_ptr(str);
char *end = this_codepoint - 1;
@@ -260,7 +266,8 @@ static int string_change_capacity(struct fx_string_p *str, size_t capacity)
}
if (!is_now_inline) {
/* string was inline, and now large enough to require a buffer. */
/* string was inline, and now large enough to require a buffer.
*/
return string_make_large(str, capacity);
}
@@ -322,7 +329,10 @@ static fx_status string_reserve(struct fx_string_p *str, size_t capacity)
}
static enum fx_status replace_ansi(
struct fx_string_p *str, size_t start, size_t length, const char *new_data)
struct fx_string_p *str,
size_t start,
size_t length,
const char *new_data)
{
fx_status status = FX_SUCCESS;
size_t new_data_len = strlen(new_data);
@@ -361,7 +371,10 @@ static enum fx_status replace_ansi(
}
static enum fx_status replace_utf8(
struct fx_string_p *str, size_t start, size_t length, const char *new_data)
struct fx_string_p *str,
size_t start,
size_t length,
const char *new_data)
{
if (start >= str->s_codepoints) {
return FX_ERR_INVALID_ARGUMENT;
@@ -382,12 +395,17 @@ static enum fx_status replace_utf8(
size_t old_data_offset = 0, old_data_nr_bytes = 0;
size_t old_data_nr_codepoints = length;
enum fx_status status = convert_codepoint_range_to_byte_range(
str, start, length, &old_data_offset, &old_data_nr_bytes);
str,
start,
length,
&old_data_offset,
&old_data_nr_bytes);
if (!FX_OK(status)) {
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);
}
@@ -415,7 +433,10 @@ static enum fx_status replace_utf8(
}
static fx_status string_replace(
struct fx_string_p *str, size_t start, size_t length, const char *new_data)
struct fx_string_p *str,
size_t start,
size_t length,
const char *new_data)
{
if (str->s_len == str->s_codepoints) {
return replace_ansi(str, start, length, new_data);
@@ -424,7 +445,9 @@ static fx_status string_replace(
return replace_utf8(str, start, length, new_data);
}
static fx_status string_replace_all(struct fx_string_p *str, const char *new_data)
static fx_status string_replace_all(
struct fx_string_p *str,
const char *new_data)
{
size_t new_len = strlen(new_data);
string_reserve(str, new_len);
@@ -437,7 +460,8 @@ static fx_status string_replace_all(struct fx_string_p *str, const char *new_dat
}
static fx_status string_replace_all_with_stringstream(
struct fx_string_p *str, const fx_stringstream *new_data)
struct fx_string_p *str,
const fx_stringstream *new_data)
{
size_t new_len = fx_stringstream_get_length(new_data);
string_reserve(str, new_len);
@@ -451,7 +475,10 @@ static fx_status string_replace_all_with_stringstream(
return FX_SUCCESS;
}
static enum fx_status remove_ansi(struct fx_string_p *str, size_t start, size_t length)
static enum fx_status remove_ansi(
struct fx_string_p *str,
size_t start,
size_t length)
{
fx_status status = FX_SUCCESS;
@@ -479,11 +506,18 @@ static enum fx_status remove_ansi(struct fx_string_p *str, size_t start, size_t
return FX_SUCCESS;
}
static enum fx_status remove_utf8(struct fx_string_p *str, size_t start, size_t length)
static enum fx_status remove_utf8(
struct fx_string_p *str,
size_t start,
size_t length)
{
size_t remove_offset = 0, remove_nr_bytes = 0;
enum fx_status status = convert_codepoint_range_to_byte_range(
str, start, length, &remove_offset, &remove_nr_bytes);
str,
start,
length,
&remove_offset,
&remove_nr_bytes);
if (!FX_OK(status)) {
return status;
}
@@ -506,7 +540,9 @@ static enum fx_status remove_utf8(struct fx_string_p *str, size_t start, size_t
}
static enum fx_status string_remove(
struct fx_string_p *str, size_t start, size_t length)
struct fx_string_p *str,
size_t start,
size_t length)
{
if (str->s_len == str->s_codepoints) {
return remove_ansi(str, start, length);
@@ -515,7 +551,9 @@ static enum fx_status string_remove(
return remove_utf8(str, start, length);
}
static fx_status string_transform(struct fx_string_p *str, int (*transformer)(int))
static fx_status string_transform(
struct fx_string_p *str,
int (*transformer)(int))
{
char *s = string_ptr(str);
for (size_t i = 0; i < str->s_len; i++) {
@@ -612,7 +650,10 @@ static fx_status string_trim(struct fx_string_p *str)
}
static enum fx_status string_insert_cstr_ansi(
struct fx_string_p *dest, const char *src, size_t nr_bytes, size_t at)
struct fx_string_p *dest,
const char *src,
size_t nr_bytes,
size_t at)
{
if (at >= dest->s_len) {
at = dest->s_len;
@@ -637,7 +678,9 @@ static enum fx_status string_insert_cstr_ansi(
}
static enum fx_status string_insert_cstr_utf8(
struct fx_string_p *dest, const char *src, size_t nr_bytes,
struct fx_string_p *dest,
const char *src,
size_t nr_bytes,
size_t codepoint_offset)
{
if (codepoint_offset >= dest->s_codepoints) {
@@ -651,7 +694,11 @@ static enum fx_status string_insert_cstr_utf8(
byte_offset = dest->s_len;
} else {
status = convert_codepoint_range_to_byte_range(
dest, 0, codepoint_offset, NULL, &byte_offset);
dest,
0,
codepoint_offset,
NULL,
&byte_offset);
}
if (!FX_OK(status)) {
@@ -678,7 +725,10 @@ static enum fx_status string_insert_cstr_utf8(
}
static enum fx_status string_insert_wstr_ansi(
struct fx_string_p *dest, const fx_wchar *src, size_t nr_codepoints, size_t at)
struct fx_string_p *dest,
const fx_wchar *src,
size_t nr_codepoints,
size_t at)
{
if (at >= dest->s_len) {
at = dest->s_len;
@@ -723,7 +773,9 @@ static enum fx_status string_insert_wstr_ansi(
}
static enum fx_status string_insert_wstr_utf8(
struct fx_string_p *dest, const fx_wchar *src, size_t nr_codepoints,
struct fx_string_p *dest,
const fx_wchar *src,
size_t nr_codepoints,
size_t codepoint_offset)
{
if (codepoint_offset >= dest->s_codepoints) {
@@ -748,7 +800,11 @@ static enum fx_status string_insert_wstr_utf8(
move_offset = dest->s_len;
} else {
status = convert_codepoint_range_to_byte_range(
dest, 0, codepoint_offset, NULL, &move_offset);
dest,
0,
codepoint_offset,
NULL,
&move_offset);
}
if (!FX_OK(status)) {
@@ -783,7 +839,10 @@ static enum fx_status string_insert_wstr_utf8(
}
static enum fx_status string_insert_cstr(
struct fx_string_p *dest, const char *src, size_t nr_bytes, size_t at)
struct fx_string_p *dest,
const char *src,
size_t nr_bytes,
size_t at)
{
if (dest->s_len == dest->s_codepoints) {
return string_insert_cstr_ansi(dest, src, nr_bytes, at);
@@ -793,7 +852,10 @@ static enum fx_status string_insert_cstr(
}
static enum fx_status string_insert_wstr(
struct fx_string_p *dest, const fx_wchar *src, size_t nr_codepoints, size_t at)
struct fx_string_p *dest,
const fx_wchar *src,
size_t nr_codepoints,
size_t at)
{
if (dest->s_len == dest->s_codepoints) {
return string_insert_wstr_ansi(dest, src, nr_codepoints, at);
@@ -803,25 +865,36 @@ static enum fx_status string_insert_wstr(
}
static enum fx_status string_insertf(
struct fx_string_p *dest, size_t at, const char *format, va_list arg)
struct fx_string_p *dest,
size_t at,
const char *format,
va_list arg)
{
char buf[1024];
size_t len = vsnprintf(buf, sizeof buf, format, arg);
return string_insert_cstr(dest, buf, len, at);
}
static enum fx_status string_insert_c(struct fx_string_p *dest, char c, size_t at)
static enum fx_status string_insert_c(
struct fx_string_p *dest,
char c,
size_t at)
{
return string_insert_cstr(dest, &c, 1, at);
}
static enum fx_status string_insert_wc(struct fx_string_p *dest, fx_wchar c, size_t at)
static enum fx_status string_insert_wc(
struct fx_string_p *dest,
fx_wchar c,
size_t at)
{
return string_insert_wstr(dest, &c, 1, at);
}
static enum fx_status string_insert_s(
struct fx_string_p *dest, const struct fx_string_p *src, size_t at)
struct fx_string_p *dest,
const struct fx_string_p *src,
size_t at)
{
return string_insert_cstr(dest, string_ptr(src), src->s_len, at);
}
@@ -858,7 +931,9 @@ static bool has_prefix(const char *s, const char *prefix, size_t *prefix_len)
}
static bool has_prefixes(
const char *s, const char **prefixes, size_t nr_prefixes,
const char *s,
const char **prefixes,
size_t nr_prefixes,
size_t *selected_prefix_len)
{
for (size_t i = 0; i < nr_prefixes; i++) {
@@ -929,14 +1004,16 @@ static enum fx_status find_next_token(struct fx_string_iterator_p *it)
}
it->_ds = offset + prefix_len;
it->string_value = fx_string_ptr(it->_tmp);
it->string_value = fx_string_get_cstr(it->_tmp);
it->string_length = it->_tmp_p->s_len;
it->string_codepoints = it->_tmp_p->s_codepoints;
return FX_SUCCESS;
}
static fx_iterator *string_tokenise(
struct fx_string_p *str, const char *delims[], size_t nr_delims,
struct fx_string_p *str,
const char *delims[],
size_t nr_delims,
fx_string_tokenise_flags flags)
{
if (!nr_delims) {
@@ -970,7 +1047,9 @@ static fx_iterator *string_tokenise(
return it_obj;
}
static size_t string_get_size(const struct fx_string_p *str, fx_strlen_flags flags)
static size_t string_get_size(
const struct fx_string_p *str,
fx_strlen_flags flags)
{
switch (flags) {
case FX_STRLEN_NORMAL:
@@ -987,7 +1066,9 @@ static size_t string_get_capacity(const struct fx_string_p *str)
return str->s_max;
}
static bool string_compare(const struct fx_string_p *a, const struct fx_string_p *b)
static bool string_compare(
const struct fx_string_p *a,
const struct fx_string_p *b)
{
if (a->s_len != b->s_len) {
return false;
@@ -1041,7 +1122,10 @@ static void string_pop_back(struct fx_string_p *str)
str->s_len--;
}
static fx_string *string_substr(const struct fx_string_p *str, size_t start, size_t len)
static fx_string *string_substr(
const struct fx_string_p *str,
size_t start,
size_t len)
{
if (start > string_get_size(str, FX_STRLEN_NORMAL)) {
return NULL;
@@ -1052,7 +1136,8 @@ static fx_string *string_substr(const struct fx_string_p *str, size_t start, siz
}
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;
@@ -1146,32 +1231,57 @@ fx_status fx_string_reserve(fx_string *str, size_t capacity)
}
fx_status fx_string_replace(
fx_string *str, size_t start, size_t length, const char *new_data)
fx_string *str,
size_t start,
size_t length,
const char *new_data)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_STRING, string_replace, str, start, length, new_data);
FX_TYPE_STRING,
string_replace,
str,
start,
length,
new_data);
}
fx_status fx_string_replace_all(fx_string *str, const char *new_data)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_replace_all, str, new_data);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_STRING,
string_replace_all,
str,
new_data);
}
fx_status fx_string_replace_all_with_stringstream(
fx_string *str, const fx_stringstream *new_data)
fx_string *str,
const fx_stringstream *new_data)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_STRING, string_replace_all_with_stringstream, str, new_data);
FX_TYPE_STRING,
string_replace_all_with_stringstream,
str,
new_data);
}
enum fx_status fx_string_remove(fx_string *str, size_t start, size_t length)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_remove, str, start, length);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_STRING,
string_remove,
str,
start,
length);
}
fx_status fx_string_transform(fx_string *str, int (*transformer)(int))
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_transform, str, transformer);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_STRING,
string_transform,
str,
transformer);
}
fx_status fx_string_trim(fx_string *str)
@@ -1189,29 +1299,46 @@ enum fx_status fx_string_insert_wc(fx_string *dest, fx_wchar c, size_t at)
FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_insert_wc, dest, c, at);
}
enum fx_status fx_string_insert_s(fx_string *dest, const fx_string *src, size_t at)
enum fx_status fx_string_insert_s(
fx_string *dest,
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);
}
enum fx_status fx_string_insert_cstr(fx_string *dest, const char *src, size_t at)
enum fx_status fx_string_insert_cstr(
fx_string *dest,
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);
}
enum fx_status fx_string_insert_wstr(fx_string *dest, const fx_wchar *src, size_t at)
enum fx_status fx_string_insert_wstr(
fx_string *dest,
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);
}
enum fx_status fx_string_insert_cstrf(
fx_string *dest, size_t at, const char *format, ...)
fx_string *dest,
size_t at,
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);
@@ -1222,15 +1349,24 @@ enum fx_status fx_string_insert_cstrf(
}
enum fx_status fx_string_insert_cstrn(
fx_string *dest, const char *src, size_t len, size_t at)
fx_string *dest,
const char *src,
size_t len,
size_t at)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_STRING, string_insert_cstr, dest, src, len, at);
FX_TYPE_STRING,
string_insert_cstr,
dest,
src,
len,
at);
}
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);
@@ -1242,7 +1378,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);
@@ -1258,11 +1395,18 @@ void fx_string_clear(fx_string *str)
}
fx_iterator *fx_string_tokenise(
fx_string *str, const char *delims[], size_t nr_delims,
fx_string *str,
const char *delims[],
size_t nr_delims,
fx_string_tokenise_flags flags)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_STRING, string_tokenise, str, delims, nr_delims, flags);
FX_TYPE_STRING,
string_tokenise,
str,
delims,
nr_delims,
flags);
}
size_t fx_string_get_size(const fx_string *str, fx_strlen_flags flags)
@@ -1304,7 +1448,12 @@ const char *fx_string_get_cstr(const fx_string *str)
fx_string *fx_string_substr(const fx_string *str, size_t start, size_t len)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_STRING, string_substr, str, start, len);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_STRING,
string_substr,
str,
start,
len);
}
uint64_t fx_string_hash(const fx_string *str)
@@ -1495,12 +1644,14 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
}
}
static fx_iterator_value chars_iterator_get_value(struct fx_string_iterator_p *it)
static fx_iterator_value chars_iterator_get_value(
struct fx_string_iterator_p *it)
{
return FX_ITERATOR_VALUE_INT(it->char_value);
}
static fx_iterator_value tokens_iterator_get_value(struct fx_string_iterator_p *it)
static fx_iterator_value tokens_iterator_get_value(
struct fx_string_iterator_p *it)
{
return FX_ITERATOR_VALUE_CPTR(it->string_value);
}
+46 -19
View File
@@ -1,7 +1,7 @@
#include <ctype.h>
#include <fx/core/stringstream.h>
#include <fx/ds/string.h>
#include <fx/ds/uuid.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -15,31 +15,43 @@ struct fx_uuid_p {
/*** PRIVATE FUNCTIONS ********************************************************/
static fx_status uuid_to_cstr(
const struct fx_uuid_p *uuid, char out[FX_UUID_STRING_MAX])
const struct fx_uuid_p *uuid,
char out[FX_UUID_STRING_MAX])
{
snprintf(
out, FX_UUID_STRING_MAX,
out,
FX_UUID_STRING_MAX,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%"
"02x%02x",
uuid->uuid_bytes.uuid_bytes[0], uuid->uuid_bytes.uuid_bytes[1],
uuid->uuid_bytes.uuid_bytes[2], uuid->uuid_bytes.uuid_bytes[3],
uuid->uuid_bytes.uuid_bytes[4], uuid->uuid_bytes.uuid_bytes[5],
uuid->uuid_bytes.uuid_bytes[6], uuid->uuid_bytes.uuid_bytes[7],
uuid->uuid_bytes.uuid_bytes[8], uuid->uuid_bytes.uuid_bytes[9],
uuid->uuid_bytes.uuid_bytes[10], uuid->uuid_bytes.uuid_bytes[11],
uuid->uuid_bytes.uuid_bytes[12], uuid->uuid_bytes.uuid_bytes[13],
uuid->uuid_bytes.uuid_bytes[14], uuid->uuid_bytes.uuid_bytes[15]);
uuid->uuid_bytes.uuid_bytes[0],
uuid->uuid_bytes.uuid_bytes[1],
uuid->uuid_bytes.uuid_bytes[2],
uuid->uuid_bytes.uuid_bytes[3],
uuid->uuid_bytes.uuid_bytes[4],
uuid->uuid_bytes.uuid_bytes[5],
uuid->uuid_bytes.uuid_bytes[6],
uuid->uuid_bytes.uuid_bytes[7],
uuid->uuid_bytes.uuid_bytes[8],
uuid->uuid_bytes.uuid_bytes[9],
uuid->uuid_bytes.uuid_bytes[10],
uuid->uuid_bytes.uuid_bytes[11],
uuid->uuid_bytes.uuid_bytes[12],
uuid->uuid_bytes.uuid_bytes[13],
uuid->uuid_bytes.uuid_bytes[14],
uuid->uuid_bytes.uuid_bytes[15]);
return FX_SUCCESS;
}
static void uuid_get_bytes(
const struct fx_uuid_p *uuid, unsigned char bytes[FX_UUID_NBYTES])
const struct fx_uuid_p *uuid,
unsigned char bytes[FX_UUID_NBYTES])
{
memcpy(bytes, uuid->uuid_bytes.uuid_bytes, FX_UUID_NBYTES);
}
static void uuid_get_uuid_bytes(
const struct fx_uuid_p *uuid, union fx_uuid_bytes *bytes)
const struct fx_uuid_p *uuid,
union fx_uuid_bytes *bytes)
{
memcpy(bytes, &uuid->uuid_bytes, sizeof *bytes);
}
@@ -52,11 +64,22 @@ static union fx_uuid_bytes *uuid_ptr(struct fx_uuid_p *uuid)
/*** PUBLIC FUNCTIONS *********************************************************/
fx_uuid *fx_uuid_create_from_bytes(
unsigned char u00, unsigned char u01, unsigned char u02,
unsigned char u03, unsigned char u04, unsigned char u05,
unsigned char u06, unsigned char u07, unsigned char u08,
unsigned char u09, unsigned char u10, unsigned char u11,
unsigned char u12, unsigned char u13, unsigned char u14, unsigned char u15)
unsigned char u00,
unsigned char u01,
unsigned char u02,
unsigned char u03,
unsigned char u04,
unsigned char u05,
unsigned char u06,
unsigned char u07,
unsigned char u08,
unsigned char u09,
unsigned char u10,
unsigned char u11,
unsigned char u12,
unsigned char u13,
unsigned char u14,
unsigned char u15)
{
fx_uuid *uuid = fx_uuid_create();
if (!uuid) {
@@ -171,7 +194,11 @@ void fx_uuid_get_bytes(const fx_uuid *uuid, unsigned char bytes[FX_UUID_NBYTES])
void fx_uuid_get_uuid_bytes(const fx_uuid *uuid, union fx_uuid_bytes *bytes)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_UUID, uuid_get_uuid_bytes, uuid, bytes);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_UUID,
uuid_get_uuid_bytes,
uuid,
bytes);
}
union fx_uuid_bytes *fx_uuid_ptr(fx_uuid *uuid)