# HG changeset patch # User Mike Becker # Date 1766416340 -3600 # Node ID 6a842bd49fea6b79840d729dfb66ebcbb59800ea # Parent db8299984bfe393522eb4987fdc010ace302b54a adds cx_hash_key_as_string() diff -r db8299984bfe -r 6a842bd49fea CHANGELOG --- a/CHANGELOG Mon Dec 22 15:47:59 2025 +0100 +++ b/CHANGELOG Mon Dec 22 16:12:20 2025 +0100 @@ -10,6 +10,7 @@ * adds cxJsonCompare() * adds cxMapCompare() * adds line continuation support to CxProperties / CxPropertiesConfig + * adds cx_hash_key_as_string() * adds cxBufferMaximumCapacity() * adds cxBufferAppendString() * adds CX_BUFFER_DO_NOT_FREE buffer flag diff -r db8299984bfe -r 6a842bd49fea docs/Writerside/topics/about.md --- a/docs/Writerside/topics/about.md Mon Dec 22 15:47:59 2025 +0100 +++ b/docs/Writerside/topics/about.md Mon Dec 22 16:12:20 2025 +0100 @@ -37,6 +37,7 @@ * adds cxJsonCompare() * adds cxMapCompare() * adds line continuation support to CxProperties / CxPropertiesConfig +* adds cx_hash_key_as_string() * adds cxBufferMaximumCapacity() * adds cxBufferAppendString() * adds CX_BUFFER_DO_NOT_FREE buffer flag diff -r db8299984bfe -r 6a842bd49fea docs/Writerside/topics/buffer.h.md --- a/docs/Writerside/topics/buffer.h.md Mon Dec 22 15:47:59 2025 +0100 +++ b/docs/Writerside/topics/buffer.h.md Mon Dec 22 16:12:20 2025 +0100 @@ -45,7 +45,7 @@ // add headers CxMapIterator iter = cxMapIterator(header); cx_foreach(CxMapEntry*, entry, iter) { - cxstring name = cx_strn(entry->key->data, entry->key->len); + cxstring name = cx_hash_key_as_string(entry->key); cx_bprintf(&buf, "%" CX_PRIstr ": %s\r\n", CX_SFMT(name), entry->value ); diff -r db8299984bfe -r 6a842bd49fea docs/Writerside/topics/hash_key.h.md --- a/docs/Writerside/topics/hash_key.h.md Mon Dec 22 15:47:59 2025 +0100 +++ b/docs/Writerside/topics/hash_key.h.md Mon Dec 22 16:12:20 2025 +0100 @@ -21,6 +21,8 @@ CxHashKey cx_hash_key_u64(uint64_t x); int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right); + +cxstring cx_hash_key_as_string(const CxHashKey *key); ``` ## Description @@ -64,6 +66,8 @@ Hash keys are compared with `cx_hash_key_cmp()`. +When you have a key that stores a string, you can retrieve it by using `cx_hash_key_as_string()`. + hash_key.h diff -r db8299984bfe -r 6a842bd49fea docs/Writerside/topics/map.h.md --- a/docs/Writerside/topics/map.h.md Mon Dec 22 15:47:59 2025 +0100 +++ b/docs/Writerside/topics/map.h.md Mon Dec 22 16:12:20 2025 +0100 @@ -62,7 +62,7 @@ // Iterate through the contact list CxMapIterator iter = cxMapIterator(contacts); cx_foreach(CxMapEntry *, entry, iter) { - cxstring name = cx_strn(entry->key->data, entry->key->len); + cxstring name = cx_hash_key_as_string(entry->key); const char *phone = entry->value; printf("%" CX_PRIstr ": %s\n", CX_SFMT(name), phone); } @@ -160,8 +160,7 @@ // Almost the same iteration loop as above ... CxMapIterator iter = cxMapIterator(contacts); cx_foreach(CxMapEntry *, entry, iter) { - cxstring name = cx_strn(entry->key->data, - entry->key->len); + cxstring name = cx_hash_key_as_string(entry->key); // ... except that here we get cxmutstr* cxmutstr *phone = entry->value; printf("%" CX_PRIstr ": %" CX_PRIstr "\n", diff -r db8299984bfe -r 6a842bd49fea docs/Writerside/topics/mempool.h.md --- a/docs/Writerside/topics/mempool.h.md Mon Dec 22 15:47:59 2025 +0100 +++ b/docs/Writerside/topics/mempool.h.md Mon Dec 22 16:12:20 2025 +0100 @@ -179,7 +179,7 @@ // use the memory pool to allocate the target array cxstring* lines; size_t lc = cx_strsplit_a( - pool->allocator, contentstr, cx_str("\n"), SIZE_MAX, &lines + pool->allocator, contentstr, "\n", SIZE_MAX, &lines ); // skip the header and parse the remaining data into a linked list diff -r db8299984bfe -r 6a842bd49fea src/cx/hash_key.h --- a/src/cx/hash_key.h Mon Dec 22 15:47:59 2025 +0100 +++ b/src/cx/hash_key.h Mon Dec 22 16:12:20 2025 +0100 @@ -236,6 +236,14 @@ cx_attr_nodiscard cx_attr_nonnull CX_EXPORT int cx_hash_key_cmp(const void *left, const void *right); +/** + * Interprets the key data as a string and returns it. + * + * @param key the key + * @return the key data as a string + */ +CX_EXPORT cxstring cx_hash_key_as_string(const CxHashKey *key); + #ifdef __cplusplus } // extern "C" diff -r db8299984bfe -r 6a842bd49fea src/hash_key.c --- a/src/hash_key.c Mon Dec 22 15:47:59 2025 +0100 +++ b/src/hash_key.c Mon Dec 22 16:12:20 2025 +0100 @@ -170,3 +170,7 @@ if (left->len == 0) return 0; return memcmp(left->data, right->data, left->len); } + +cxstring cx_hash_key_as_string(const CxHashKey *key) { + return cx_strn(key->data, key->len); +} diff -r db8299984bfe -r 6a842bd49fea src/json.c --- a/src/json.c Mon Dec 22 15:47:59 2025 +0100 +++ b/src/json.c Mon Dec 22 16:12:20 2025 +0100 @@ -1226,7 +1226,7 @@ // the name actual += wfunc("\"", 1, 1, target); - cxstring key = cx_strn(member->key->data, member->key->len); + cxstring key = cx_hash_key_as_string(member->key); cxmutstr name = escape_string(key, settings->escape_slash); actual += wfunc(name.ptr, 1, name.length, target); actual += wfunc("\"", 1, 1, target); diff -r db8299984bfe -r 6a842bd49fea tests/test_hash_map.c --- a/tests/test_hash_map.c Mon Dec 22 15:47:59 2025 +0100 +++ b/tests/test_hash_map.c Mon Dec 22 16:12:20 2025 +0100 @@ -1611,7 +1611,8 @@ CX_TEST_ASSERT(keyiter.index == map->collection.size); // verify that all keys are mapped to values in reference map for (size_t i = 0 ; i < map->collection.size ; i++) { - cxmutstr ksz = cx_strdup(cx_strn(keys[i].data, keys[i].len)); + // need to create a zero-terminated string from the key + cxmutstr ksz = cx_strdup(cx_hash_key_as_string(&keys[i])); CX_TEST_ASSERT(test_map_reference_get(ksz.ptr) != NULL); cx_strfree(&ksz); } @@ -1652,7 +1653,7 @@ struct test_map_kv *pairs = calloc(map->collection.size, sizeof(struct test_map_kv)); cx_foreach(CxMapEntry*, entry, pairiter) { const CxHashKey *key = entry->key; - pairs[pairiter.index].key = cx_strdup(cx_strn(key->data, key->len)).ptr; + pairs[pairiter.index].key = cx_strdup(cx_hash_key_as_string(key)).ptr; pairs[pairiter.index].value = entry->value; } CX_TEST_ASSERT(pairiter.index == map->collection.size); diff -r db8299984bfe -r 6a842bd49fea tests/test_kv_list.c --- a/tests/test_kv_list.c Mon Dec 22 15:47:59 2025 +0100 +++ b/tests/test_kv_list.c Mon Dec 22 16:12:20 2025 +0100 @@ -592,9 +592,9 @@ CX_TEST_ASSERT(*y == 11); // removing the element - CX_TEST_ASSERT(0 == cxMapRemove(map, cx_strn(key->data, key->len))); + CX_TEST_ASSERT(0 == cxMapRemove(map, cx_hash_key_as_string(key))); key = cxKvListGetKey(list, 1); - CX_TEST_ASSERT(0 == cx_strcmp("efg", cx_strn(key->data, key->len))); + CX_TEST_ASSERT(0 == cx_strcmp("efg", cx_hash_key_as_string(key))); // remove the key of element CX_TEST_ASSERT(0 == cxKvListRemoveKey(list, 1));