diff options
Diffstat (limited to 'xs.h')
| -rw-r--r-- | xs.h | 36 |
1 files changed, 28 insertions, 8 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 */ |
| @@ -989,16 +991,20 @@ xs_str *xs_join(const xs_list *list, const char *sep) | |||
| 989 | xs_list *xs_split_n(const char *str, const char *sep, int times) | 991 | xs_list *xs_split_n(const char *str, const char *sep, int times) |
| 990 | /* splits a string into a list upto n times */ | 992 | /* splits a string into a list upto n times */ |
| 991 | { | 993 | { |
| 994 | xs_list *list = xs_list_new(); | ||
| 995 | |||
| 996 | if (!xs_is_string(str) || !xs_is_string(sep)) | ||
| 997 | return list; | ||
| 998 | |||
| 992 | int sz = strlen(sep); | 999 | int sz = strlen(sep); |
| 993 | char *ss; | 1000 | char *ss; |
| 994 | xs_list *list; | ||
| 995 | |||
| 996 | list = xs_list_new(); | ||
| 997 | 1001 | ||
| 998 | while (times > 0 && (ss = strstr(str, sep)) != NULL) { | 1002 | while (times > 0 && (ss = strstr(str, sep)) != NULL) { |
| 999 | /* create a new string with this slice and add it to the list */ | 1003 | /* create a new string with this slice and add it to the list */ |
| 1000 | xs *s = xs_str_new_sz(str, ss - str); | 1004 | xs *s = xs_str_new_sz(str, ss - str); |
| 1001 | list = xs_list_append(list, s); | 1005 | |
| 1006 | if (xs_is_string(s)) | ||
| 1007 | list = xs_list_append(list, s); | ||
| 1002 | 1008 | ||
| 1003 | /* skip past the separator */ | 1009 | /* skip past the separator */ |
| 1004 | str = ss + sz; | 1010 | str = ss + sz; |
| @@ -1007,7 +1013,8 @@ xs_list *xs_split_n(const char *str, const char *sep, int times) | |||
| 1007 | } | 1013 | } |
| 1008 | 1014 | ||
| 1009 | /* add the rest of the string */ | 1015 | /* add the rest of the string */ |
| 1010 | list = xs_list_append(list, str); | 1016 | if (xs_is_string(str)) |
| 1017 | list = xs_list_append(list, str); | ||
| 1011 | 1018 | ||
| 1012 | return list; | 1019 | return list; |
| 1013 | } | 1020 | } |
| @@ -1487,9 +1494,8 @@ unsigned int xs_hash_func(const char *data, int size) | |||
| 1487 | /* a general purpose hashing function */ | 1494 | /* a general purpose hashing function */ |
| 1488 | { | 1495 | { |
| 1489 | unsigned int hash = 0x666; | 1496 | unsigned int hash = 0x666; |
| 1490 | int n; | ||
| 1491 | 1497 | ||
| 1492 | for (n = 0; n < size; n++) { | 1498 | for (int n = 0; n < size; n++) { |
| 1493 | hash ^= (unsigned char)data[n]; | 1499 | hash ^= (unsigned char)data[n]; |
| 1494 | hash *= 111111111; | 1500 | hash *= 111111111; |
| 1495 | } | 1501 | } |
| @@ -1498,6 +1504,20 @@ unsigned int xs_hash_func(const char *data, int size) | |||
| 1498 | } | 1504 | } |
| 1499 | 1505 | ||
| 1500 | 1506 | ||
| 1507 | uint64_t xs_hash64_func(const char *data, int size) | ||
| 1508 | /* a general purpose hashing function (64 bit) */ | ||
| 1509 | { | ||
| 1510 | uint64_t hash = 0x100; | ||
| 1511 | |||
| 1512 | for (int n = 0; n < size; n++) { | ||
| 1513 | hash ^= (unsigned char)data[n]; | ||
| 1514 | hash *= 1111111111111111111; | ||
| 1515 | } | ||
| 1516 | |||
| 1517 | return hash; | ||
| 1518 | } | ||
| 1519 | |||
| 1520 | |||
| 1501 | #endif /* XS_IMPLEMENTATION */ | 1521 | #endif /* XS_IMPLEMENTATION */ |
| 1502 | 1522 | ||
| 1503 | #endif /* _XS_H */ | 1523 | #endif /* _XS_H */ |