src/cx/hash_key.h

changeset 1402
6fa42f7e2624
parent 1400
7bc88ae62755
--- a/src/cx/hash_key.h	Sat Sep 27 17:53:41 2025 +0200
+++ b/src/cx/hash_key.h	Sun Sep 28 22:32:42 2025 +0200
@@ -138,6 +138,24 @@
 CxHashKey cx_hash_key_str(const char *str);
 
 /**
+ * Computes a hash key from a string.
+ *
+ * Use this function when the string is represented
+ * as an unsigned char array.
+ *
+ * The string needs to be zero-terminated.
+ *
+ * @param str the string
+ * @return the hash key
+ */
+cx_attr_nodiscard
+cx_attr_cstr_arg(1)
+cx_attr_export
+static inline CxHashKey cx_hash_key_ustr(const unsigned char *str) {
+    return cx_hash_key_str((const char*)str);
+}
+
+/**
  * Computes a hash key from a byte array.
  *
  * @param bytes the array
@@ -185,6 +203,54 @@
 /**
  * Computes a hash key from a UCX string.
  *
+ * @param str the string
+ * @return the hash key
+ */
+cx_attr_nodiscard
+static inline CxHashKey cx_hash_key_mutstr(cxmutstr str) {
+    return cx_hash_key(str.ptr, str.length);
+}
+
+/**
+ * The identity function for the CX_HASH_KEY() macro.
+ * You should never need to use this manually.
+ *
+ * @param key the key
+ * @return a copy of the key
+ */
+cx_attr_nodiscard
+static inline CxHashKey cx_hash_key_identity(CxHashKey key) {
+    return key;
+}
+
+/**
+ * Creates a hash key from any of the supported types with implicit length.
+ *
+ * Does nothing when passing a CxHashkey.
+ *
+ * Supported types are UCX strings, zero-terminated C strings,
+ * and 32-bit or 64-bit unsigned integers.
+ *
+ * @param key the key data
+ * @returns the @c CxHashKey
+ */
+#define CX_HASH_KEY(key) _Generic((key), \
+        CxHashKey: cx_hash_key_identity, \
+        cxstring: cx_hash_key_cxstr, \
+        cxmutstr: cx_hash_key_mutstr, \
+        char*: cx_hash_key_str, \
+        const char*: cx_hash_key_str, \
+        unsigned char*: cx_hash_key_ustr, \
+        const unsigned char*: cx_hash_key_ustr, \
+        uint32_t: cx_hash_key_u32, \
+        uint64_t: cx_hash_key_u64) \
+        (key)
+
+
+/**
+ * Computes a hash key from a UCX string.
+ * Convenience macro that accepts both cxstring and cxmutstr.
+ * @deprecated use the CX_HASH_KEY() macro instead
  * @param str (@c cxstring or @c cxmutstr) the string
  * @return (@c CxHashKey) the hash key
  */

mercurial