From eedd3e7e4cdacc419ee30e4f619529cb66bf1c19 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov <0x7fffff@guattari.ru> Date: Sat, 6 Aug 2022 17:40:39 +0400 Subject: short names --- source/kit/array_ref.c | 6 ++-- source/kit/array_ref.h | 86 +++++++++++++++++++++++++++------------------- source/kit/dynamic_array.c | 10 +++--- source/kit/dynamic_array.h | 82 +++++++++++++++++++++++++------------------ source/kit/input_buffer.c | 31 +++++++++-------- source/kit/input_buffer.h | 23 +++++++++---- source/kit/input_stream.c | 30 ++++++++-------- source/kit/input_stream.h | 32 +++++++++++------ source/kit/string_ref.h | 15 +++++--- 9 files changed, 189 insertions(+), 126 deletions(-) diff --git a/source/kit/array_ref.c b/source/kit/array_ref.c index 2e46324..c1367f4 100644 --- a/source/kit/array_ref.c +++ b/source/kit/array_ref.c @@ -2,7 +2,7 @@ #include -_Bool ar_equal_bytes(ptrdiff_t left_element_size, ptrdiff_t left_size, +_Bool kit_ar_equal_bytes(ptrdiff_t left_element_size, ptrdiff_t left_size, void const *left_data, ptrdiff_t right_element_size, ptrdiff_t right_size, void const *right_data) { @@ -18,10 +18,10 @@ _Bool ar_equal_bytes(ptrdiff_t left_element_size, ptrdiff_t left_size, return 1; } -int ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, +int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, void const *left_data, ptrdiff_t right_element_size, ptrdiff_t right_size, void const *right_data, - ar_compare_fn compare) { + kit_ar_compare_fn compare) { if (left_element_size < right_element_size) return -1; if (left_element_size > right_element_size) diff --git a/source/kit/array_ref.h b/source/kit/array_ref.h index 649af8b..20a6723 100644 --- a/source/kit/array_ref.h +++ b/source/kit/array_ref.h @@ -7,52 +7,66 @@ extern "C" { #endif -typedef int (*ar_compare_fn)(void const *left, void const *right); - -_Bool ar_equal_bytes(ptrdiff_t left_element_size, ptrdiff_t left_size, - void const *left_data, - ptrdiff_t right_element_size, - ptrdiff_t right_size, void const *right_data); - -int ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, - void const *left_data, ptrdiff_t right_element_size, - ptrdiff_t right_size, void const *right_data, - ar_compare_fn compare); - -#define AR(name_, type_) \ - struct { \ - ptrdiff_t size; \ - type_ *values; \ +typedef int (*kit_ar_compare_fn)(void const *left, void const *right); + +_Bool kit_ar_equal_bytes(ptrdiff_t left_element_size, + ptrdiff_t left_size, void const *left_data, + ptrdiff_t right_element_size, + ptrdiff_t right_size, + void const *right_data); + +int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, + void const *left_data, + ptrdiff_t right_element_size, ptrdiff_t right_size, + void const *right_data, kit_ar_compare_fn compare); + +#define KIT_AR(name_, type_) \ + struct { \ + ptrdiff_t size; \ + type_ *values; \ } name_ -#define AR_CONST(name_, type_) \ - struct { \ - ptrdiff_t size; \ - type_ const *values; \ +#define KIT_AR_CONST(name_, type_) \ + struct { \ + ptrdiff_t size; \ + type_ const *values; \ } name_ -#define AR_TYPE(name_, element_type_) \ - struct name_ { \ - ptrdiff_t size; \ - element_type_ *values; \ +#define KIT_AR_TYPE(name_, element_type_) \ + struct name_ { \ + ptrdiff_t size; \ + element_type_ *values; \ } -#define AR_TYPE_CONST(name_, element_type_) \ - struct name_ { \ - ptrdiff_t size; \ - element_type_ const *values; \ +#define KIT_AR_TYPE_CONST(name_, element_type_) \ + struct name_ { \ + ptrdiff_t size; \ + element_type_ const *values; \ } -#define AR_EQUAL(left_, right_) \ - ar_equal_bytes(sizeof((left_).values[0]), (left_).size, \ +#define KIT_AR_EQUAL(left_, right_) \ + kit_ar_equal_bytes(sizeof((left_).values[0]), (left_).size, \ + (left_).values, sizeof((right_).values[0]), \ + (right_).size, (right_).values) + +#define KIT_AR_COMPARE(left_, right_, compare_) \ + kit_ar_compare(sizeof((left_).values[0]), (left_).size, \ (left_).values, sizeof((right_).values[0]), \ - (right_).size, (right_).values) + (right_).size, (right_).values, \ + (kit_ar_compare_fn) (compare_)) + +#ifndef KIT_DISABLE_SHORT_NAMES +# define ar_compare_fn kit_ar_compare_fn +# define ar_equal_bytes kit_ar_equal_bytes +# define ar_compare kit_ar_compare -#define AR_COMPARE(left_, right_, compare_) \ - ar_compare(sizeof((left_).values[0]), (left_).size, \ - (left_).values, sizeof((right_).values[0]), \ - (right_).size, (right_).values, \ - (ar_compare_fn) (compare_)) +# define AR KIT_AR +# define AR_CONST KIT_AR_CONST +# define AR_TYPE KIT_AR_TYPE +# define AR_TYPE_CONST KIT_AR_TYPE_CONST +# define AR_EQUAL KIT_AR_EQUAL +# define AR_COMPARE KIT_AR_COMPARE +#endif #ifdef __cplusplus } diff --git a/source/kit/dynamic_array.c b/source/kit/dynamic_array.c index a3ba3dc..f9d4405 100644 --- a/source/kit/dynamic_array.c +++ b/source/kit/dynamic_array.c @@ -2,9 +2,9 @@ #include -void da_init(struct da_void *array, ptrdiff_t element_size, - ptrdiff_t size, struct kit_allocator alloc) { - memset(array, 0, sizeof(struct da_void)); +void kit_da_init(struct kit_da_void *array, ptrdiff_t element_size, + ptrdiff_t size, struct kit_allocator alloc) { + memset(array, 0, sizeof(struct kit_da_void)); if (size > 0) array->values = alloc.allocate(alloc.state, element_size * size); @@ -26,8 +26,8 @@ static ptrdiff_t eval_capacity(ptrdiff_t current_cap, return cap; } -void da_resize(struct da_void *array, ptrdiff_t element_size, - ptrdiff_t size) { +void kit_da_resize(struct kit_da_void *array, ptrdiff_t element_size, + ptrdiff_t size) { if (size <= array->capacity) { array->size = size; } else { diff --git a/source/kit/dynamic_array.h b/source/kit/dynamic_array.h index 245da63..fde28dc 100644 --- a/source/kit/dynamic_array.h +++ b/source/kit/dynamic_array.h @@ -7,54 +7,54 @@ extern "C" { #endif -struct da_void { +struct kit_da_void { ptrdiff_t capacity; ptrdiff_t size; void *values; struct kit_allocator alloc; }; -void da_init(struct da_void *array, ptrdiff_t element_size, - ptrdiff_t size, struct kit_allocator alloc); +void kit_da_init(struct kit_da_void *array, ptrdiff_t element_size, + ptrdiff_t size, struct kit_allocator alloc); -void da_resize(struct da_void *array, ptrdiff_t element_size, - ptrdiff_t size); +void kit_da_resize(struct kit_da_void *array, ptrdiff_t element_size, + ptrdiff_t size); /* Declare dynamic array type. */ -#define DA_TYPE(name_, element_type_) \ - struct name_ { \ - ptrdiff_t capacity; \ - ptrdiff_t size; \ - element_type_ *values; \ - struct kit_allocator alloc; \ +#define KIT_DA_TYPE(name_, element_type_) \ + struct name_ { \ + ptrdiff_t capacity; \ + ptrdiff_t size; \ + element_type_ *values; \ + struct kit_allocator alloc; \ } /* Declare dynamic array. */ -#define DA(name_, element_type_) \ - struct { \ - ptrdiff_t capacity; \ - ptrdiff_t size; \ - element_type_ *values; \ - struct kit_allocator alloc; \ +#define KIT_DA(name_, element_type_) \ + struct { \ + ptrdiff_t capacity; \ + ptrdiff_t size; \ + element_type_ *values; \ + struct kit_allocator alloc; \ } name_ /* Initialize dynamic array. */ -#define DA_INIT(array_, size_, alloc_) \ - da_init((struct da_void *) &(array_), sizeof((array_).values[0]), \ - (size_), (alloc_)) +#define KIT_DA_INIT(array_, size_, alloc_) \ + kit_da_init((struct kit_da_void *) &(array_), \ + sizeof((array_).values[0]), (size_), (alloc_)) /* Declare and initialize dynamic array. */ -#define DA_CREATE(name_, element_type_, size_) \ - DA(name_, element_type_); \ - DA_INIT(name_, (size_), kit_alloc_default()) +#define KIT_DA_CREATE(name_, element_type_, size_) \ + KIT_DA(name_, element_type_); \ + KIT_DA_INIT(name_, (size_), kit_alloc_default()) /* Destroy dynamic array. */ -#define DA_DESTROY(array_) \ +#define KIT_DA_DESTROY(array_) \ { \ if ((array_).values != NULL) \ (array_).alloc.deallocate((array_).alloc.state, \ @@ -63,27 +63,27 @@ void da_resize(struct da_void *array, ptrdiff_t element_size, /* Resize dynamic array. */ -#define DA_RESIZE(array_, size_) \ - da_resize((struct da_void *) &(array_), \ - sizeof((array_).values[0]), size_) +#define KIT_DA_RESIZE(array_, size_) \ + kit_da_resize((struct kit_da_void *) &(array_), \ + sizeof((array_).values[0]), size_) /* Append a value to dynamic array. */ -#define DA_APPEND(array_, value_) \ +#define KIT_DA_APPEND(array_, value_) \ { \ ptrdiff_t const kit_index_back_ = (array_).size; \ - DA_RESIZE((array_), kit_index_back_ + 1); \ + KIT_DA_RESIZE((array_), kit_index_back_ + 1); \ if (kit_index_back_ < (array_).size) \ (array_).values[kit_index_back_] = (value_); \ } /* Insert a value into dynamic array. */ -#define DA_INSERT(array_, index_, value_) \ +#define KIT_DA_INSERT(array_, index_, value_) \ { \ ptrdiff_t const kit_index_back_ = (array_).size; \ ptrdiff_t const kit_indert_n_ = (index_); \ - DA_RESIZE((array_), kit_index_back_ + 1); \ + KIT_DA_RESIZE((array_), kit_index_back_ + 1); \ if (kit_index_back_ + 1 == (array_).size) { \ for (ptrdiff_t kit_i_ = kit_index_back_; \ kit_i_ > kit_indert_n_; kit_i_--) \ @@ -94,13 +94,29 @@ void da_resize(struct da_void *array, ptrdiff_t element_size, /* Erase a value from dynamic array. */ -#define DA_ERASE(array_, index_) \ +#define KIT_DA_ERASE(array_, index_) \ { \ for (ptrdiff_t i_ = (index_) + 1; i_ < (array_).size; i_++) \ (array_).values[i_ - 1] = (array_).values[i_]; \ - DA_RESIZE((array_), (array_).size - 1); \ + KIT_DA_RESIZE((array_), (array_).size - 1); \ } +#ifndef KIT_DISABLE_SHORT_NAMES +# define da_void kit_da_void +# define da_init kit_da_init +# define da_resize kit_da_resize + +# define DA_TYPE KIT_DA_TYPE +# define DA KIT_DA +# define DA_INIT KIT_DA_INIT +# define DA_CREATE KIT_DA_CREATE +# define DA_DESTROY KIT_DA_DESTROY +# define DA_RESIZE KIT_DA_RESIZE +# define DA_APPEND KIT_DA_APPEND +# define DA_INSERT KIT_DA_INSERT +# define DA_ERASE KIT_DA_ERASE +#endif + #ifdef __cplusplus } #endif diff --git a/source/kit/input_buffer.c b/source/kit/input_buffer.c index e1e0cea..6b4d24c 100644 --- a/source/kit/input_buffer.c +++ b/source/kit/input_buffer.c @@ -4,12 +4,12 @@ struct internal_buffer { ptrdiff_t ref_count; - struct is_handle upstream; + struct kit_is_handle upstream; struct kit_allocator alloc; DA(data, char); }; -static struct internal_buffer *buf_init(struct is_handle upstream, +static struct internal_buffer *buf_init(struct kit_is_handle upstream, struct kit_allocator alloc) { struct internal_buffer *buf; buf = alloc.allocate(alloc.state, sizeof *buf); @@ -45,9 +45,9 @@ static void buf_adjust(void *p, ptrdiff_t size) { ptrdiff_t offset = buf->data.size; if (offset < size) { DA_RESIZE(buf->data, size); - out_str destination = { .size = size - offset, - .values = buf->data.values + offset }; - ptrdiff_t n = IS_READ(buf->upstream, destination); + kit_out_str destination = { .size = size - offset, + .values = buf->data.values + offset }; + ptrdiff_t n = KIT_IS_READ(buf->upstream, destination); DA_RESIZE(buf->data, offset + n); } } @@ -59,16 +59,16 @@ static ptrdiff_t min(ptrdiff_t a, ptrdiff_t b) { } static ptrdiff_t buf_read(void *p, ptrdiff_t offset, - out_str destination) { + kit_out_str destination) { struct internal_buffer *buf = (struct internal_buffer *) p; ptrdiff_t n = min(destination.size, buf->data.size - offset); memcpy(destination.values, buf->data.values + offset, n); return n; } -struct ib_handle ib_wrap(struct is_handle upstream, - struct kit_allocator alloc) { - struct ib_handle buf; +struct kit_ib_handle kit_ib_wrap(struct kit_is_handle upstream, + struct kit_allocator alloc) { + struct kit_ib_handle buf; memset(&buf, 0, sizeof buf); buf.error = 0; DA_INIT(buf.data, 0, alloc); @@ -78,8 +78,9 @@ struct ib_handle ib_wrap(struct is_handle upstream, return buf; } -struct ib_handle ib_read(struct ib_handle buf, ptrdiff_t size) { - struct ib_handle next; +struct kit_ib_handle kit_ib_read(struct kit_ib_handle buf, + ptrdiff_t size) { + struct kit_ib_handle next; memset(&next, 0, sizeof next); if (buf.error) { next.error = 1; @@ -89,9 +90,9 @@ struct ib_handle ib_read(struct ib_handle buf, ptrdiff_t size) { DA_INIT(next.data, size, buf_alloc(buf.internal)); if (next.data.size != size) next.error = 1; - out_str destination = { .size = next.data.size, - .values = next.data.values }; - ptrdiff_t n = buf_read(buf.internal, buf.offset, destination); + kit_out_str destination = { .size = next.data.size, + .values = next.data.values }; + ptrdiff_t n = buf_read(buf.internal, buf.offset, destination); next.offset = buf.offset + n; next.internal = buf.internal; DA_RESIZE(next.data, n); @@ -101,7 +102,7 @@ struct ib_handle ib_read(struct ib_handle buf, ptrdiff_t size) { return next; } -void ib_destroy(struct ib_handle buf) { +void kit_ib_destroy(struct kit_ib_handle buf) { buf_release(buf.internal); DA_DESTROY(buf.data); } diff --git a/source/kit/input_buffer.h b/source/kit/input_buffer.h index 38ece8d..2ecef3b 100644 --- a/source/kit/input_buffer.h +++ b/source/kit/input_buffer.h @@ -8,21 +8,32 @@ extern "C" { #endif -struct ib_handle { +struct kit_ib_handle { _Bool error; ptrdiff_t offset; void *internal; DA(data, char); }; -struct ib_handle ib_wrap(struct is_handle upstream, - struct kit_allocator alloc); +struct kit_ib_handle kit_ib_wrap(struct kit_is_handle upstream, + struct kit_allocator alloc); -struct ib_handle ib_read(struct ib_handle buf, ptrdiff_t size); +struct kit_ib_handle kit_ib_read(struct kit_ib_handle buf, + ptrdiff_t size); -void ib_destroy(struct ib_handle buf); +void kit_ib_destroy(struct kit_ib_handle buf); -#define IB_WRAP(upstream) ib_wrap(upstream, kit_alloc_default()) +#define KIT_IB_WRAP(upstream) \ + kit_ib_wrap(upstream, kit_alloc_default()) + +#ifndef KIT_DISABLE_SHORT_NAMES +# define ib_handle kit_ib_handle +# define ib_wrap kit_ib_wrap +# define ib_read kit_ib_read +# define ib_destroy kit_ib_destroy + +# define IB_WRAP KIT_IB_WRAP +#endif #ifdef __cplusplus } diff --git a/source/kit/input_stream.c b/source/kit/input_stream.c index 500e83d..186e96c 100644 --- a/source/kit/input_stream.c +++ b/source/kit/input_stream.c @@ -4,19 +4,20 @@ enum input_stream_type { input_stream_cstr }; -struct is_state_basic { +struct kit_is_state_basic { ptrdiff_t type; struct kit_allocator alloc; }; -struct is_state_cstr { +struct kit_is_state_cstr { ptrdiff_t type; struct kit_allocator alloc; - cstr string; + kit_cstr string; }; static _Bool check_type(void *state, ptrdiff_t type) { - struct is_state_basic *basic = (struct is_state_basic *) state; + struct kit_is_state_basic *basic = (struct kit_is_state_basic *) + state; return basic != NULL && basic->type == type; } @@ -26,10 +27,10 @@ static ptrdiff_t min(ptrdiff_t a, ptrdiff_t b) { return b; } -static ptrdiff_t read_cstr(void *state, out_str destination) { +static ptrdiff_t read_cstr(void *state, kit_out_str destination) { if (!check_type(state, input_stream_cstr)) return 0; - struct is_state_cstr *cstr = (struct is_state_cstr *) state; + struct kit_is_state_cstr *cstr = (struct kit_is_state_cstr *) state; ptrdiff_t size = min(destination.size, cstr->string.size); memcpy(destination.values, cstr->string.values, size); cstr->string.values += size; @@ -37,13 +38,13 @@ static ptrdiff_t read_cstr(void *state, out_str destination) { return size; } -struct is_handle is_wrap_string(cstr string, - struct kit_allocator alloc) { - struct is_handle in; +struct kit_is_handle kit_is_wrap_string(kit_cstr string, + struct kit_allocator alloc) { + struct kit_is_handle in; memset(&in, 0, sizeof in); - struct is_state_cstr *state = - (struct is_state_cstr *) alloc.allocate( - alloc.state, sizeof(struct is_state_cstr)); + struct kit_is_state_cstr *state = + (struct kit_is_state_cstr *) alloc.allocate( + alloc.state, sizeof(struct kit_is_state_cstr)); if (state != NULL) { memset(state, 0, sizeof *state); state->type = input_stream_cstr; @@ -55,8 +56,9 @@ struct is_handle is_wrap_string(cstr string, return in; } -void is_destroy(struct is_handle in) { - struct is_state_basic *basic = (struct is_state_basic *) in.state; +void kit_is_destroy(struct kit_is_handle in) { + struct kit_is_state_basic *basic = (struct kit_is_state_basic *) + in.state; if (basic != NULL) basic->alloc.deallocate(basic->alloc.state, in.state); } diff --git a/source/kit/input_stream.h b/source/kit/input_stream.h index c59a720..a0d00e7 100644 --- a/source/kit/input_stream.h +++ b/source/kit/input_stream.h @@ -8,22 +8,34 @@ extern "C" { #endif -typedef ptrdiff_t (*is_read_fn)(void *state, out_str destination); +typedef ptrdiff_t (*kit_is_read_fn)(void *state, + kit_out_str destination); -struct is_handle { - void *state; - is_read_fn read; +struct kit_is_handle { + void *state; + kit_is_read_fn read; }; -struct is_handle is_wrap_string(cstr string, - struct kit_allocator alloc); +struct kit_is_handle kit_is_wrap_string(kit_cstr string, + struct kit_allocator alloc); -void is_destroy(struct is_handle in); +void kit_is_destroy(struct kit_is_handle in); -#define IS_WRAP_STRING(string) \ - is_wrap_string((string), kit_alloc_default()) +#define KIT_IS_WRAP_STRING(string) \ + kit_is_wrap_string((string), kit_alloc_default()) -#define IS_READ(in, destination) (in).read((in).state, (destination)) +#define KIT_IS_READ(in, destination) \ + (in).read((in).state, (destination)) + +#ifndef KIT_DISABLE_SHORT_NAMES +# define is_read_fn kit_is_read_fn +# define is_handle kit_is_handle +# define is_wrap_string kit_is_wrap_string +# define is_destroy kit_is_destroy + +# define IS_WRAP_STRING KIT_IS_WRAP_STRING +# define IS_READ KIT_IS_READ +#endif #ifdef __cplusplus } diff --git a/source/kit/string_ref.h b/source/kit/string_ref.h index d1af90f..19206dc 100644 --- a/source/kit/string_ref.h +++ b/source/kit/string_ref.h @@ -7,11 +7,18 @@ extern "C" { #endif -AR_TYPE(string_ref, char); -AR_TYPE_CONST(string_cref, char); +KIT_AR_TYPE(kit_string_ref, char); +KIT_AR_TYPE_CONST(kit_string_cref, char); -typedef struct string_ref out_str; -typedef struct string_cref cstr; +typedef struct kit_string_ref kit_out_str; +typedef struct kit_string_cref kit_cstr; + +#ifndef KIT_DISABLE_SHORT_NAMES +# define string_ref kit_string_ref +# define string_cref kit_string_cref +# define out_str kit_out_str +# define cstr kit_cstr +#endif #ifdef __cplusplus } -- cgit v1.2.3