diff options
| author | 2025-01-28 07:42:08 +0100 | |
|---|---|---|
| committer | 2025-01-28 07:42:08 +0100 | |
| commit | 492b91e4e47c43d15bb828ecbe1e4195c47c31f8 (patch) | |
| tree | 2a0784dde0119899a8cda2ee8e4d87cd66bd7815 /xs.h | |
| parent | Added a 'No more unseen posts' mark. (diff) | |
| download | snac2-492b91e4e47c43d15bb828ecbe1e4195c47c31f8.tar.gz snac2-492b91e4e47c43d15bb828ecbe1e4195c47c31f8.tar.xz snac2-492b91e4e47c43d15bb828ecbe1e4195c47c31f8.zip | |
Backport from xs.
Diffstat (limited to '')
| -rw-r--r-- | xs.h | 21 |
1 files changed, 18 insertions, 3 deletions
| @@ -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 | ||
| 16 | typedef enum { | 17 | typedef 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); | |||
| 142 | void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); | 143 | void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); |
| 143 | 144 | ||
| 144 | unsigned int xs_hash_func(const char *data, int size); | 145 | unsigned int xs_hash_func(const char *data, int size); |
| 146 | uint64_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 | ||
| 1502 | uint64_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 */ |