Sun, 20 Nov 2022 21:08:36 +0100
use //-style single line comments everywhere
--- a/docs/src/modules-ucx2.md Sun Nov 20 17:48:42 2022 +0100 +++ b/docs/src/modules-ucx2.md Sun Nov 20 21:08:36 2022 +0100 @@ -75,20 +75,20 @@ return result; } -/* ... */ +// ... -sstr_t* array = /* some standard array of strings */ -size_t arrlen = /* the length of the array */ +sstr_t* array = // some standard array of strings +size_t arrlen = // the length of the array UcxArray* result = create_unique(array,arrlen); -/* Iterate over the array and print the elements */ +// Iterate over the array and print the elements sstr_t* unique = result->data; for (size_t i = 0 ; i < result->size ; i++) { printf("%" PRIsstr "\n", SFMT(unique[i])); } -/* Free the array. */ +// Free the array. ucx_array_free(result); ``` ### Preventing out of bounds writes @@ -128,23 +128,18 @@ is to find all items within a time window `[t_start, t_end]`. With AVL Trees this is easy: ```C -/* --------------------- - * Somewhere in a header - */ +// Somewhere in a header typedef struct { time_t ts; - /* other important data */ + // other important data } MyObject; -/* ----------- - * Source code - */ - +// Source code UcxAVLTree* tree = ucx_avl_new(ucx_cmp_longint); -/* ... populate tree with objects, use '& MyObject.ts' as key ... */ +// ... populate tree with objects, use '& MyObject.ts' as key ... -/* Now find every item, with 30 <= ts <= 70 */ +// Now find every item, with 30 <= ts <= 70 time_t ts_start = 30; time_t ts_end = 70; @@ -212,18 +207,18 @@ UcxBuffer* linebuf = ucx_buffer_new( - NULL, /* the buffer should manage the memory area for us */ - 2*chunksize, /* initial size should be twice the chunk size */ - UCX_BUFFER_AUTOEXTEND); /* the buffer will grow when necessary */ + NULL, // the buffer should manage the memory area for us + 2*chunksize, // initial size should be twice the chunk size + UCX_BUFFER_AUTOEXTEND); // the buffer will grow when necessary size_t lineno = 1; do { - /* read line chunk */ + // read line chunk size_t read = ucx_stream_ncopy( input, linebuf, fread, ucx_buffer_write, chunksize); if (read == 0) break; - /* handle line endings */ + // handle line endings do { sstr_t bufstr = ucx_buffer_to_sstr(linebuf); sstr_t nl = sstrchr(bufstr, '\n'); @@ -234,13 +229,13 @@ printf("%zu: %" PRIsstr "\n", lineno++, SFMT(linestr)); - /* shift the buffer to the next line */ + // shift the buffer to the next line ucx_buffer_shift_left(linebuf, linelen+1); } while(1); } while(1); - /* print the 'noeol' line, if any */ + // print the 'noeol' line, if any sstr_t lastline = ucx_buffer_to_sstr(linebuf); if (lastline.length > 0) { printf("%zu: %" PRIsstr, lineno, SFMT(lastline)); @@ -286,27 +281,27 @@ return list; } -/* we will need this function to clean up the list contents later */ +// we will need this function to clean up the list contents later void free_sstr(void* ptr) { sstr_t* s = ptr; free(s->ptr); free(s); } -/* ... */ +// ... -sstr_t* array = /* some array of strings */ -size_t arrlen = /* the length of the array */ +sstr_t* array = // some array of strings +size_t arrlen = // the length of the array UcxList* list = remove_duplicates(array,arrlen); -/* Iterate over the list and print the elements */ +// Iterate over the list and print the elements UCX_FOREACH(elem, list) { sstr_t s = *((sstr_t*)elem->data); printf("%" PRIsstr "\n", SFMT(s)); } -/* Use our free function to free the duplicated strings. */ +// Use our free function to free the duplicated strings. ucx_list_free_content(list, free_sstr); ucx_list_free(list); ``` @@ -392,7 +387,7 @@ ucx_map_cstr_put(options, option, arg); option = NULL; } else { - /* ... handle argument that is not an option ... */ + // ... handle argument that is not an option ... } } if(option) { @@ -463,20 +458,20 @@ perror("Cannot open file"); return 1; } - /* close the file automatically at pool destruction*/ + // close the file automatically at pool destruction ucx_mempool_reg_destr(pool, f, (ucx_destructor) fclose); - /* create a buffer and register it at the memory pool for destruction */ + // create a buffer and register it at the memory pool for destruction UcxBuffer* content = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); ucx_mempool_reg_destr(pool, content, (ucx_destructor) ucx_buffer_free); - /* read the file and split it by lines first */ + // read the file and split it by lines first ucx_stream_copy(f, content, fread, ucx_buffer_write); sstr_t contentstr = ucx_buffer_to_sstr(content); ssize_t lc = 0; sstr_t* lines = sstrsplit_a(pool->allocator, contentstr, S("\n"), &lc); - /* skip the header and parse the remaining data */ + // skip the header and parse the remaining data UcxList* datalist = NULL; for (size_t i = 1 ; i < lc ; i++) { if (lines[i].length == 0) continue; @@ -494,7 +489,7 @@ datalist = ucx_list_append_a(pool->allocator, datalist, data); } - /* control output */ + // control output UCX_FOREACH(elem, datalist) { CSVData* data = elem->data; printf("Column A: %" PRIsstr " | " @@ -504,7 +499,7 @@ ); } - /* cleanup everything, no manual free() needed */ + // cleanup everything, no manual free() needed ucx_mempool_destroy(pool); return 0; @@ -519,10 +514,10 @@ ```C MyObject* obj = ucx_mempool_malloc(pool, sizeof(MyObject)); - /* some special initialization with own resource management */ + // some special initialization with own resource management my_object_init(obj); - /* register destructor function */ + // register destructor function ucx_mempool_set_destr(obj, (ucx_destructor) my_object_destroy); ``` Be aware, that your destructor function should not free any memory, that is @@ -544,30 +539,30 @@ ### Example: Loading properties from a file ```C -/* Open the file as usual */ +// Open the file as usual FILE* file = fopen("myprops.properties", "r"); if (!file) { // error handling return 1; } -/* Load the properties from the file */ +// Load the properties from the file UcxMap* myprops = ucx_map_new(16); if (ucx_properties_load(myprops, file)) { - /* ... error handling ... */ + // ... error handling ... fclose(file); ucx_map_free(myprops); return 1; } -/* Print out the key/value pairs */ +// Print out the key/value pairs char* propval; UcxMapIterator propiter = ucx_map_iterator(myprops); UCX_MAP_FOREACH(key, propval, propiter) { printf("%s = %s\n", (char*)key.data, propval); } -/* Don't forget to free the values before freeing the map */ +// Don't forget to free the values before freeing the map ucx_map_free_content(myprops, NULL); ucx_map_free(myprops); fclose(file); @@ -599,30 +594,30 @@ const char* str = "Hello!"; size_t strn = 7; - /* push the integer */ + // push the integer ucx_stack_push(&stack, sizeof(int), &i); - /* push the float and rember the address */ + // push the float and rember the address float* remember = ucx_stack_push(&stack, sizeof(float), &f); - /* push the string with zero terminator */ + // push the string with zero terminator ucx_stack_push(&stack, strn, str); - /* if we forget, how big an element was, we can ask the stack */ + // if we forget, how big an element was, we can ask the stack printf("Length of string: %zu\n", ucx_stack_topsize(&stack)-1); - /* retrieve the string as sstr_t, without zero terminator! */ + // retrieve the string as sstr_t, without zero terminator! sstr_t s; s.length = ucx_stack_topsize(&stack)-1; s.ptr = malloc(s.length); ucx_stack_popn(&stack, s.ptr, s.length); printf("%" PRIsstr "\n", SFMT(s)); - /* print the float directly from the stack and free it */ + // print the float directly from the stack and free it printf("Float: %f\n", *remember); ucx_stack_free(&stack, remember); - /* the last element is the integer */ + // the last element is the integer int j; ucx_stack_pop(&stack, &j); printf("Integer: %d\n", j); @@ -647,21 +642,21 @@ There are several ways to create an `sstr_t`: ```C -/* (1) sstr() uses strlen() internally, hence cstr MUST be zero-terminated */ +// (1) sstr() uses strlen() internally, hence cstr MUST be zero-terminated sstr_t a = sstr(cstr); -/* (2) cstr does not need to be zero-terminated, if length is specified */ +// (2) cstr does not need to be zero-terminated, if length is specified sstr_t b = sstrn(cstr, len); -/* (3) S() macro creates sstr_t from a string using sizeof() and using sstrn(). - This version is especially useful for function arguments */ +// (3) S() macro creates sstr_t from a string using sizeof() and using sstrn(). +// This version is especially useful for function arguments sstr_t c = S("hello"); -/* (4) SC() macro works like S(), but makes the string immutable using scstr_t. - (available since UCX 2.0) */ +// (4) SC() macro works like S(), but makes the string immutable using scstr_t. +// (available since UCX 2.0) scstr_t d = SC("hello"); -/* (5) ST() macro creates sstr_t struct literal using sizeof() */ +// (5) ST() macro creates sstr_t struct literal using sizeof() sstr_t e = ST("hello"); ``` @@ -716,12 +711,12 @@ sstr_t test = ST("here::are::some::strings"); sstr_t delim = ST("::"); -ssize_t count = 0; /* no limit */ +ssize_t count = 0; // no limit UcxMempool* pool = ucx_mempool_new_default(); sstr_t* result = sstrsplit_a(pool->allocator, test, delim, &count); for (ssize_t i = 0 ; i < count ; i++) { - /* don't forget to specify the length via the %*s format specifier */ + // don't forget to specify the length via the %*s format specifier printf("%*s\n", result[i].length, result[i].ptr); } @@ -762,32 +757,30 @@ You should declare test cases and subroutines in a header file per test unit and implement them as you would implement normal functions. ```C - /* myunit.h */ + // myunit.h UCX_TEST(function_name); - UCX_TEST_SUBROUTINE(subroutine_name, paramlist); /* optional */ + UCX_TEST_SUBROUTINE(subroutine_name, paramlist); // optional - /* myunit.c */ + // myunit.c UCX_TEST_SUBROUTINE(subroutine_name, paramlist) { - /* ... reusable tests with UCX_TEST_ASSERT() ... */ + // ... reusable tests with UCX_TEST_ASSERT() ... } UCX_TEST(function_name) { - /* ... resource allocation and other test preparation ... */ + // ... resource allocation and other test preparation ... - /* mandatory marker for the start of the tests */ + // mandatory marker for the start of the tests UCX_TEST_BEGIN - /* ... verifications with UCX_TEST_ASSERT() ... - * (and/or calls with UCX_TEST_CALL_SUBROUTINE()) - */ + // ... verifications with UCX_TEST_ASSERT() ... + // (and/or calls with UCX_TEST_CALL_SUBROUTINE()) - /* mandatory marker for the end of the tests */ + // mandatory marker for the end of the tests UCX_TEST_END - /* ... resource cleanup ... - * (all code after UCX_TEST_END is always executed) - */ + // ... resource cleanup ... + // (all code after UCX_TEST_END is always executed) } ``` If you want to use the `UCX_TEST_ASSERT()` macro in a function, you are @@ -800,8 +793,8 @@ UcxTestSuite* suite = ucx_test_suite_new(); ucx_test_register(suite, testMyTestCase01); ucx_test_register(suite, testMyTestCase02); - /* ... */ - ucx_test_run(suite, stdout); /* stdout, or any other FILE stream */ + // ... + ucx_test_run(suite, stdout); // stdout, or any other FILE stream ``` ## Utilities @@ -831,7 +824,7 @@ return 1; } - FILE *srcf = fopen(argv[1], "r"); /* insert error handling on your own */ + FILE *srcf = fopen(argv[1], "r"); // insert error handling on your own FILE *destf = fopen(argv[2], "w"); size_t n = ucx_stream_copy(srcf, destf, fread, fwrite); @@ -866,7 +859,7 @@ i, prime(i) ? "prime" : "not prime"); } -/* print the result to stdout */ +// print the result to stdout printf("%s", (char*)strbuffer->space); ucx_buffer_free(strbuffer);
--- a/src/allocator.c Sun Nov 20 17:48:42 2022 +0100 +++ b/src/allocator.c Sun Nov 20 21:08:36 2022 +0100 @@ -77,7 +77,7 @@ }; CxAllocator *cxDefaultAllocator = &cx_default_allocator; -/* IMPLEMENTATION OF HIGH LEVEL API */ +// IMPLEMENTATION OF HIGH LEVEL API void *cxMalloc( CxAllocator const *allocator,
--- a/src/array_list.c Sun Nov 20 17:48:42 2022 +0100 +++ b/src/array_list.c Sun Nov 20 21:08:36 2022 +0100 @@ -31,7 +31,7 @@ #include <string.h> #include <stdint.h> -/* LOW LEVEL ARRAY LIST FUNCTIONS */ +// LOW LEVEL ARRAY LIST FUNCTIONS enum cx_array_copy_result cx_array_copy( void **target, @@ -43,37 +43,37 @@ size_t elem_count, struct cx_array_reallocator_s *reallocator ) { - /* assert pointers */ + // assert pointers assert(target != NULL); assert(size != NULL); assert(src != NULL); - /* determine capacity */ + // determine capacity size_t cap = capacity == NULL ? *size : *capacity; - /* check if resize is required */ + // check if resize is required size_t minsize = index + elem_count; size_t newsize = *size < minsize ? minsize : *size; bool needrealloc = newsize > cap; - /* reallocate if possible */ + // reallocate if possible if (needrealloc) { - /* a reallocator and a capacity variable must be available */ + // a reallocator and a capacity variable must be available if (reallocator == NULL || capacity == NULL) { return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED; } - /* check, if we need to repair the src pointer */ + // check, if we need to repair the src pointer uintptr_t targetaddr = (uintptr_t) *target; uintptr_t srcaddr = (uintptr_t) src; bool repairsrc = targetaddr <= srcaddr && srcaddr < targetaddr + cap * elem_size; - /* calculate new capacity (next number divisible by 16) */ + // calculate new capacity (next number divisible by 16) cap = newsize - (newsize % 16) + 16; assert(cap > newsize); - /* perform reallocation */ + // perform reallocation void *newmem = reallocator->realloc( *target, cap, elem_size, reallocator ); @@ -81,25 +81,25 @@ return CX_ARRAY_COPY_REALLOC_FAILED; } - /* repair src pointer, if necessary */ + // repair src pointer, if necessary if (repairsrc) { src = ((char *) newmem) + (srcaddr - targetaddr); } - /* store new pointer and capacity */ + // store new pointer and capacity *target = newmem; *capacity = cap; } - /* determine target pointer */ + // determine target pointer char *start = *target; start += index * elem_size; - /* copy elements and set new size */ + // copy elements and set new size memmove(start, src, elem_count * elem_size); *size = newsize; - /* return successfully */ + // return successfully return CX_ARRAY_COPY_SUCCESS; } @@ -111,38 +111,38 @@ size_t idx1, size_t idx2 ) { - /* short circuit */ + // short circuit if (idx1 == idx2) return; char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; void *tmp; - /* decide if we can use the local buffer */ + // decide if we can use the local buffer if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { tmp = malloc(elem_size); - /* we don't want to enforce error handling */ + // we don't want to enforce error handling if (tmp == NULL) abort(); } else { tmp = sbo_mem; } - /* calculate memory locations */ + // calculate memory locations char *left = arr, *right = arr; left += idx1 * elem_size; right += idx2 * elem_size; - /* three-way swap */ + // three-way swap memcpy(tmp, left, elem_size); memcpy(left, right, elem_size); memcpy(right, tmp, elem_size); - /* free dynamic memory, if it was needed */ + // free dynamic memory, if it was needed if (tmp != sbo_mem) { free(tmp); } } -/* HIGH LEVEL ARRAY LIST FUNCTIONS */ +// HIGH LEVEL ARRAY LIST FUNCTIONS typedef struct { struct cx_list_s base; @@ -156,10 +156,10 @@ size_t elem_size, struct cx_array_reallocator_s *alloc ) { - /* retrieve the pointer to the list allocator */ + // retrieve the pointer to the list allocator CxAllocator const *al = alloc->ptr1; - /* use the list allocator to reallocate the memory */ + // use the list allocator to reallocate the memory return cxRealloc(al, array, capacity * elem_size); } @@ -197,7 +197,7 @@ } else { cx_array_list *arl = (cx_array_list *) list; - /* move elements starting at index to the right */ + // move elements starting at index to the right if (cx_array_copy( &arl->data, &list->size, @@ -211,7 +211,7 @@ return 1; } - /* place the element */ + // place the element memcpy(((char *) arl->data) + index * list->itemsize, elem, list->itemsize); @@ -247,18 +247,18 @@ struct cx_list_s *list, size_t index ) { - /* out-of-bounds check */ + // out-of-bounds check if (index >= list->size) { return 1; } - /* short-circuit removal of last element */ + // short-circuit removal of last element if (index == list->size - 1) { list->size--; return 0; } - /* just move the elements starting at index to the left */ + // just move the elements starting at index to the left cx_array_list *arl = (cx_array_list *) list; int result = cx_array_copy( &arl->data, @@ -271,7 +271,7 @@ &arl->reallocator ); if (result == 0) { - /* decrease the size */ + // decrease the size list->size--; } return result; @@ -417,7 +417,7 @@ list->base.itemsize = item_size; list->base.capacity = initial_capacity; - /* configure the reallocator */ + // configure the reallocator list->reallocator.realloc = cx_arl_realloc; list->reallocator.ptr1 = (void *) allocator;
--- a/src/cx/allocator.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/allocator.h Sun Nov 20 21:08:36 2022 +0100 @@ -254,7 +254,7 @@ __attribute__((__nonnull__)); #ifdef __cplusplus -} /* extern "C" */ +} // extern "C" #endif -#endif /* UCX_ALLOCATOR_H */ +#endif // UCX_ALLOCATOR_H
--- a/src/cx/array_list.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/array_list.h Sun Nov 20 21:08:36 2022 +0100 @@ -166,7 +166,7 @@ #ifdef __cplusplus -} /* extern "C" */ +} // extern "C" #endif -#endif /* UCX_ARRAY_LIST_H */ +#endif // UCX_ARRAY_LIST_H
--- a/src/cx/buffer.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/buffer.h Sun Nov 20 21:08:36 2022 +0100 @@ -411,4 +411,4 @@ } #endif -#endif /* UCX_BUFFER_H */ +#endif // UCX_BUFFER_H
--- a/src/cx/common.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/common.h Sun Nov 20 21:08:36 2022 +0100 @@ -110,12 +110,12 @@ #else #define __WORDSIZE 32 #endif -#endif /* __WORDSIZE */ -#else /* !_WIN32 */ +#endif // __WORDSIZE +#else // !_WIN32 #include <sys/types.h> -#endif /* _WIN32 */ +#endif // _WIN32 #ifndef __GNUC__ /** @@ -124,4 +124,4 @@ #define __attribute__(x) #endif -#endif /* UCX_COMMON_H */ +#endif // UCX_COMMON_H
--- a/src/cx/hash_key.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/hash_key.h Sun Nov 20 21:08:36 2022 +0100 @@ -125,4 +125,4 @@ } // extern "C" #endif -#endif /* UCX_HASH_KEY_H */ +#endif // UCX_HASH_KEY_H
--- a/src/cx/iterator.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/iterator.h Sun Nov 20 21:08:36 2022 +0100 @@ -164,4 +164,4 @@ #define cx_foreach(type, elem, iter) \ for (type elem; cxIteratorValid(&iter) && (elem = (type)cxIteratorCurrent(&iter)) != NULL ; cxIteratorNext(&iter)) // NOLINT(bugprone-macro-parentheses) -#endif /* UCX_ITERATOR_H */ +#endif // UCX_ITERATOR_H
--- a/src/cx/linked_list.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/linked_list.h Sun Nov 20 21:08:36 2022 +0100 @@ -444,7 +444,7 @@ ) __attribute__((__nonnull__(1))); #ifdef __cplusplus -} /* extern "C" */ +} // extern "C" #endif -#endif /* UCX_LINKED_LIST_H */ +#endif // UCX_LINKED_LIST_H
--- a/src/cx/list.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/list.h Sun Nov 20 21:08:36 2022 +0100 @@ -419,7 +419,7 @@ void cxListDestroy(CxList *list); #ifdef __cplusplus -} /* extern "C" */ +} // extern "C" #endif -#endif /* UCX_LIST_H */ +#endif // UCX_LIST_H
--- a/src/cx/tree.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/tree.h Sun Nov 20 21:08:36 2022 +0100 @@ -129,8 +129,8 @@ #ifdef __cplusplus -} /* extern "C" */ +} // extern "C" #endif -#endif /* UCX_TREE_H */ +#endif // UCX_TREE_H
--- a/src/cx/utils.h Sun Nov 20 17:48:42 2022 +0100 +++ b/src/cx/utils.h Sun Nov 20 21:08:36 2022 +0100 @@ -51,9 +51,7 @@ */ #define cx_for_n(varname, n) for (size_t varname = 0 ; (varname) < (n) ; (varname)++) -/* ---------------------- - * cx_szmul() definition. - * ---------------------- */ +// cx_szmul() definition #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN) #define CX_SZMUL_BUILTIN @@ -72,7 +70,7 @@ * otherwise */ #define cx_szmul(a, b, result) __builtin_umul_overflow(a, b, result) -#else /* __WORDSIZE != 32 */ +#else // __WORDSIZE != 32 /** * Alias for \c __builtin_umull_overflow. * @@ -86,9 +84,9 @@ * otherwise */ #define cx_szmul(a, b, result) __builtin_umull_overflow(a, b, result) -#endif /* __WORDSIZE */ +#endif // __WORDSIZE -#else /* no GNUC or clang bultin */ +#else // no GNUC or clang bultin /** * Performs a multiplication of size_t values and checks for overflow. @@ -122,4 +120,4 @@ } #endif -#endif /* UCX_UTILS_H */ +#endif // UCX_UTILS_H
--- a/src/hash_key.c Sun Nov 20 17:48:42 2022 +0100 +++ b/src/hash_key.c Sun Nov 20 21:08:36 2022 +0100 @@ -32,7 +32,7 @@ void cx_hash_murmur(CxHashKey *key) { unsigned char const *data = key->data.cbytes; if (data == NULL) { - /* extension: special value for NULL */ + // extension: special value for NULL key->hash = 1574210520u; return; } @@ -70,8 +70,8 @@ h ^= (data[i + 0] & 0xFF); h *= m; __attribute__((__fallthrough__)); - default: - /* do nothing */; + default: // do nothing + ; } h ^= h >> 13;
--- a/src/linked_list.c Sun Nov 20 17:48:42 2022 +0100 +++ b/src/linked_list.c Sun Nov 20 21:08:36 2022 +0100 @@ -32,7 +32,7 @@ #include <string.h> #include <assert.h> -/* LOW LEVEL LINKED LIST FUNCTIONS */ +// LOW LEVEL LINKED LIST FUNCTIONS #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) #define ll_prev(node) CX_LL_PTR(node, loc_prev) @@ -337,7 +337,7 @@ return ret; } -void cx_linked_list_sort( /* NOLINT(misc-no-recursion) - purposely recursive function */ +void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function void **begin, void **end, ptrdiff_t loc_prev, @@ -455,7 +455,7 @@ *begin = prev; } -/* HIGH LEVEL LINKED LIST IMPLEMENTATION */ +// HIGH LEVEL LINKED LIST IMPLEMENTATION typedef struct cx_linked_list_node cx_linked_list_node; struct cx_linked_list_node {
--- a/src/list.c Sun Nov 20 17:48:42 2022 +0100 +++ b/src/list.c Sun Nov 20 21:08:36 2022 +0100 @@ -57,10 +57,10 @@ CxList const *other ) { if (list->cl->compare == other->cl->compare) { - /* same compare function, lists are compatible */ + // same compare function, lists are compatible return list->cl->compare(list, other); } else { - /* different compare functions, use iterator */ + // different compare functions, use iterator if (list->size == other->size) { // TODO: we would need a const iterator CxIterator left = cxListBegin(list);
--- a/src/string.c Sun Nov 20 17:48:42 2022 +0100 +++ b/src/string.c Sun Nov 20 21:08:36 2022 +0100 @@ -35,9 +35,9 @@ #ifndef _WIN32 -#include <strings.h> /* for strncasecmp() */ +#include <strings.h> // for strncasecmp() -#endif /* _WIN32 */ +#endif // _WIN32 cxmutstr cx_mutstr(char *cstring) { return (cxmutstr) {cstring, strlen(cstring)}; @@ -236,7 +236,7 @@ return haystack; } - /* optimize for single-char needles */ + // optimize for single-char needles if (needle.length == 1) { return cx_strchr(haystack, *needle.ptr); } @@ -249,19 +249,19 @@ * and we want to avoid that. */ - /* local prefix table */ + // local prefix table size_t s_prefix_table[STRSTR_SBO_BUFLEN]; - /* check needle length and use appropriate prefix table */ - /* if the pattern exceeds static prefix table, allocate on the heap */ + // check needle length and use appropriate prefix table + // if the pattern exceeds static prefix table, allocate on the heap bool useheap = needle.length >= STRSTR_SBO_BUFLEN; register size_t *ptable = useheap ? calloc(needle.length + 1, sizeof(size_t)) : s_prefix_table; - /* keep counter in registers */ + // keep counter in registers register size_t i, j; - /* fill prefix table */ + // fill prefix table i = 0; j = 0; ptable[i] = j; @@ -274,7 +274,7 @@ ptable[i] = j; } - /* search */ + // search cxstring result = {NULL, 0}; i = 0; j = 1; @@ -292,7 +292,7 @@ } } - /* if prefix table was allocated on the heap, free it */ + // if prefix table was allocated on the heap, free it if (ptable != s_prefix_table) { free(ptable); } @@ -314,23 +314,24 @@ size_t limit, cxstring *output ) { - /* special case: output limit is zero */ + // special case: output limit is zero if (limit == 0) return 0; - /* special case: delimiter is empty */ + // special case: delimiter is empty if (delim.length == 0) { output[0] = string; return 1; } - /* special cases: delimiter is at least as large as the string */ + // special cases: delimiter is at least as large as the string if (delim.length >= string.length) { - /* exact match */ + // exact match if (cx_strcmp(string, delim) == 0) { output[0] = cx_strn(string.ptr, 0); output[1] = cx_strn(string.ptr + string.length, 0); return 2; - } else /* no match possible */ { + } else { + // no match possible output[0] = string; return 1; } @@ -342,21 +343,21 @@ ++n; cxstring match = cx_strstr(curpos, delim); if (match.length > 0) { - /* is the limit reached? */ + // is the limit reached? if (n < limit) { - /* copy the current string to the array */ + // copy the current string to the array cxstring item = cx_strn(curpos.ptr, match.ptr - curpos.ptr); output[n - 1] = item; size_t processed = item.length + delim.length; curpos.ptr += processed; curpos.length -= processed; } else { - /* limit reached, copy the _full_ remaining string */ + // limit reached, copy the _full_ remaining string output[n - 1] = curpos; break; } } else { - /* no more matches, copy last string */ + // no more matches, copy last string output[n - 1] = curpos; break; } @@ -372,24 +373,24 @@ size_t limit, cxstring **output ) { - /* find out how many splits we're going to make and allocate memory */ + // find out how many splits we're going to make and allocate memory size_t n = 0; cxstring curpos = string; while (1) { ++n; cxstring match = cx_strstr(curpos, delim); if (match.length > 0) { - /* is the limit reached? */ + // is the limit reached? if (n < limit) { size_t processed = match.ptr - curpos.ptr + delim.length; curpos.ptr += processed; curpos.length -= processed; } else { - /* limit reached */ + // limit reached break; } } else { - /* no more matches */ + // no more matches break; } } @@ -566,14 +567,14 @@ if (pattern.length == 0 || pattern.length > str.length || replmax == 0) return cx_strdup_a(allocator, str); - /* Compute expected buffer length */ + // Compute expected buffer length size_t ibufmax = str.length / pattern.length; size_t ibuflen = replmax < ibufmax ? replmax : ibufmax; if (ibuflen > REPLACE_INDEX_BUFFER_MAX) { ibuflen = REPLACE_INDEX_BUFFER_MAX; } - /* Allocate first index buffer */ + // Allocate first index buffer struct cx_strreplace_ibuf *firstbuf, *curbuf; firstbuf = curbuf = calloc(1, sizeof(struct cx_strreplace_ibuf)); if (!firstbuf) return cx_mutstrn(NULL, 0); @@ -583,13 +584,13 @@ return cx_mutstrn(NULL, 0); } - /* Search occurrences */ + // Search occurrences cxstring searchstr = str; size_t found = 0; do { cxstring match = cx_strstr(searchstr, pattern); if (match.length > 0) { - /* Allocate next buffer in chain, if required */ + // Allocate next buffer in chain, if required if (curbuf->len == ibuflen) { struct cx_strreplace_ibuf *nextbuf = calloc(1, sizeof(struct cx_strreplace_ibuf)); @@ -607,7 +608,7 @@ curbuf = nextbuf; } - /* Record match index */ + // Record match index found++; size_t idx = match.ptr - str.ptr; curbuf->buf[curbuf->len++] = idx; @@ -618,7 +619,7 @@ } } while (searchstr.length > 0 && found < replmax); - /* Allocate result string */ + // Allocate result string cxmutstr result; { ssize_t adjlen = (ssize_t) replacement.length - (ssize_t) pattern.length; @@ -636,13 +637,13 @@ } } - /* Build result string */ + // Build result string curbuf = firstbuf; size_t srcidx = 0; char *destptr = result.ptr; do { for (size_t i = 0; i < curbuf->len; i++) { - /* Copy source part up to next match*/ + // Copy source part up to next match size_t idx = curbuf->buf[i]; size_t srclen = idx - srcidx; if (srclen > 0) { @@ -651,7 +652,7 @@ srcidx += srclen; } - /* Copy the replacement and skip the source pattern */ + // Copy the replacement and skip the source pattern srcidx += pattern.length; memcpy(destptr, replacement.ptr, replacement.length); destptr += replacement.length; @@ -660,10 +661,10 @@ } while (curbuf); memcpy(destptr, str.ptr + srcidx, str.length - srcidx); - /* Result is guaranteed to be zero-terminated */ + // Result is guaranteed to be zero-terminated result.ptr[result.length] = '\0'; - /* Free index buffer */ + // Free index buffer cx_strrepl_free_ibuf(firstbuf); return result;
--- a/test/test_string.cpp Sun Nov 20 17:48:42 2022 +0100 +++ b/test/test_string.cpp Sun Nov 20 21:08:36 2022 +0100 @@ -272,22 +272,22 @@ cxstring list[8]; size_t n; - /* special case: empty string */ + // special case: empty string n = cx_strsplit(test, cx_str(""), capa, list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); - /* no delimiter occurrence */ + // no delimiter occurrence n = cx_strsplit(test, cx_str("z"), capa, list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); - /* partially matching delimiter */ + // partially matching delimiter n = cx_strsplit(test, cx_str("is,not"), capa, list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); - /* matching single-char delimiter */ + // matching single-char delimiter n = cx_strsplit(test, cx_str(","), capa, list); ASSERT_EQ(n, 5); EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); @@ -296,65 +296,65 @@ EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0); EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0); - /* matching multi-char delimiter */ + // matching multi-char delimiter n = cx_strsplit(test, cx_str("is"), capa, list); ASSERT_EQ(n, 3); EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0); EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0); - /* bounded list using single-char delimiter */ + // bounded list using single-char delimiter n = cx_strsplit(test, cx_str(","), 3, list); ASSERT_EQ(n, 3); EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); - /* bounded list using multi-char delimiter */ + // bounded list using multi-char delimiter n = cx_strsplit(test, cx_str("is"), 2, list); ASSERT_EQ(n, 2); EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); - /* start with delimiter */ + // start with delimiter n = cx_strsplit(test, cx_str("this"), capa, list); ASSERT_EQ(n, 2); EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); - /* end with delimiter */ + // end with delimiter n = cx_strsplit(test, cx_str("string"), capa, list); ASSERT_EQ(n, 2); EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); - /* end with delimiter exceed bound */ + // end with delimiter exceed bound n = cx_strsplit(cx_str("a,b,c,"), cx_str(","), 3, list); ASSERT_EQ(n, 3); EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0); EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0); - /* exact match */ + // exact match n = cx_strsplit(test, cx_str("this,is,a,csv,string"), capa, list); ASSERT_EQ(n, 2); EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); - /* string to be split is only substring */ + // string to be split is only substring n = cx_strsplit(test, cx_str("this,is,a,csv,string,with,extension"), capa, list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); - /* subsequent encounter of delimiter (the string between is empty) */ + // subsequent encounter of delimiter (the string between is empty) n = cx_strsplit(test, cx_str("is,"), capa, list); ASSERT_EQ(n, 3); EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); - /* call the _m variant just for coverage */ + // call the _m variant just for coverage auto mtest = cx_strdup(test); cxmutstr mlist[4]; n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist); @@ -373,25 +373,25 @@ cxstring *list; size_t n; - /* special case: empty string */ + // special case: empty string n = cx_strsplit_a(&alloc, test, cx_str(""), capa, &list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); cxFree(&alloc, list); - /* no delimiter occurrence */ + // no delimiter occurrence n = cx_strsplit_a(&alloc, test, cx_str("z"), capa, &list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); cxFree(&alloc, list); - /* partially matching delimiter */ + // partially matching delimiter n = cx_strsplit_a(&alloc, test, cx_str("is,not"), capa, &list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); cxFree(&alloc, list); - /* matching single-char delimiter */ + // matching single-char delimiter n = cx_strsplit_a(&alloc, test, cx_str(","), capa, &list); ASSERT_EQ(n, 5); EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); @@ -401,7 +401,7 @@ EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0); cxFree(&alloc, list); - /* matching multi-char delimiter */ + // matching multi-char delimiter n = cx_strsplit_a(&alloc, test, cx_str("is"), capa, &list); ASSERT_EQ(n, 3); EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); @@ -409,7 +409,7 @@ EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0); cxFree(&alloc, list); - /* bounded list using single-char delimiter */ + // bounded list using single-char delimiter n = cx_strsplit_a(&alloc, test, cx_str(","), 3, &list); ASSERT_EQ(n, 3); EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); @@ -417,28 +417,28 @@ EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); cxFree(&alloc, list); - /* bounded list using multi-char delimiter */ + // bounded list using multi-char delimiter n = cx_strsplit_a(&alloc, test, cx_str("is"), 2, &list); ASSERT_EQ(n, 2); EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); cxFree(&alloc, list); - /* start with delimiter */ + // start with delimiter n = cx_strsplit_a(&alloc, test, cx_str("this"), capa, &list); ASSERT_EQ(n, 2); EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); cxFree(&alloc, list); - /* end with delimiter */ + // end with delimiter n = cx_strsplit_a(&alloc, test, cx_str("string"), capa, &list); ASSERT_EQ(n, 2); EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); cxFree(&alloc, list); - /* end with delimiter exceed bound */ + // end with delimiter exceed bound n = cx_strsplit_a(&alloc, cx_str("a,b,c,"), cx_str(","), 3, &list); ASSERT_EQ(n, 3); EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0); @@ -446,20 +446,20 @@ EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0); cxFree(&alloc, list); - /* exact match */ + // exact match n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string"), capa, &list); ASSERT_EQ(n, 2); EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); cxFree(&alloc, list); - /* string to be split is only substring */ + // string to be split is only substring n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string,with,extension"), capa, &list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); cxFree(&alloc, list); - /* subsequent encounter of delimiter (the string between is empty) */ + // subsequent encounter of delimiter (the string between is empty) n = cx_strsplit_a(&alloc, test, cx_str("is,"), capa, &list); ASSERT_EQ(n, 3); EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); @@ -467,7 +467,7 @@ EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); cxFree(&alloc, list); - /* call the _m variant just for coverage */ + // call the _m variant just for coverage auto mtest = cx_strdup(test); cxmutstr *mlist; n = cx_strsplit_ma(&alloc, mtest, cx_str("is,"), 4, &mlist); @@ -496,7 +496,7 @@ EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0); EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0); - /* call the _m variant just for coverage */ + // call the _m variant just for coverage cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t ")); EXPECT_EQ(cx_strcmp(cx_strcast(m1), cx_str("ein test")), 0); }