summaryrefslogtreecommitdiff
path: root/xs.h
diff options
context:
space:
mode:
authorGravatar default2025-01-28 07:42:08 +0100
committerGravatar default2025-01-28 07:42:08 +0100
commit492b91e4e47c43d15bb828ecbe1e4195c47c31f8 (patch)
tree2a0784dde0119899a8cda2ee8e4d87cd66bd7815 /xs.h
parentAdded a 'No more unseen posts' mark. (diff)
downloadpenes-snac2-492b91e4e47c43d15bb828ecbe1e4195c47c31f8.tar.gz
penes-snac2-492b91e4e47c43d15bb828ecbe1e4195c47c31f8.tar.xz
penes-snac2-492b91e4e47c43d15bb828ecbe1e4195c47c31f8.zip
Backport from xs.
Diffstat (limited to '')
-rw-r--r--xs.h21
1 files changed, 18 insertions, 3 deletions
diff --git a/xs.h b/xs.h
index 05d84f5..961425b 100644
--- a/xs.h
+++ b/xs.h
@@ -12,6 +12,7 @@
12#include <stdarg.h> 12#include <stdarg.h>
13#include <signal.h> 13#include <signal.h>
14#include <errno.h> 14#include <errno.h>
15#include <stdint.h>
15 16
16typedef enum { 17typedef enum {
17 XSTYPE_STRING = 0x02, /* C string (\0 delimited) (NOT STORED) */ 18 XSTYPE_STRING = 0x02, /* C string (\0 delimited) (NOT STORED) */
@@ -142,6 +143,7 @@ void xs_data_get(void *data, const xs_data *value);
142void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); 143void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
143 144
144unsigned int xs_hash_func(const char *data, int size); 145unsigned int xs_hash_func(const char *data, int size);
146uint64_t xs_hash64_func(const char *data, int size);
145 147
146#ifdef XS_ASSERT 148#ifdef XS_ASSERT
147#include <assert.h> 149#include <assert.h>
@@ -632,7 +634,7 @@ xs_str *xs_crop_i(xs_str *str, int start, int end)
632 end = sz + end; 634 end = sz + end;
633 635
634 /* crop from the top */ 636 /* crop from the top */
635 if (end > 0 && end < sz) 637 if (end >= 0 && end < sz)
636 str[end] = '\0'; 638 str[end] = '\0';
637 639
638 /* crop from the bottom */ 640 /* crop from the bottom */
@@ -1487,9 +1489,8 @@ unsigned int xs_hash_func(const char *data, int size)
1487/* a general purpose hashing function */ 1489/* a general purpose hashing function */
1488{ 1490{
1489 unsigned int hash = 0x666; 1491 unsigned int hash = 0x666;
1490 int n;
1491 1492
1492 for (n = 0; n < size; n++) { 1493 for (int n = 0; n < size; n++) {
1493 hash ^= (unsigned char)data[n]; 1494 hash ^= (unsigned char)data[n];
1494 hash *= 111111111; 1495 hash *= 111111111;
1495 } 1496 }
@@ -1498,6 +1499,20 @@ unsigned int xs_hash_func(const char *data, int size)
1498} 1499}
1499 1500
1500 1501
1502uint64_t xs_hash64_func(const char *data, int size)
1503/* a general purpose hashing function (64 bit) */
1504{
1505 uint64_t hash = 0x100;
1506
1507 for (int n = 0; n < size; n++) {
1508 hash ^= (unsigned char)data[n];
1509 hash *= 1111111111111111111;
1510 }
1511
1512 return hash;
1513}
1514
1515
1501#endif /* XS_IMPLEMENTATION */ 1516#endif /* XS_IMPLEMENTATION */
1502 1517
1503#endif /* _XS_H */ 1518#endif /* _XS_H */