diff --git a/src/hash.h b/src/hash.h deleted file mode 100644 index 24d142c..0000000 --- a/src/hash.h +++ /dev/null @@ -1,9 +0,0 @@ -#include - -/** djb2 */ -static uint32_t djb2(const char *s) { - const unsigned char *str = (const unsigned char *)s; - uint32_t hash = 5381, c; - while(c = *str++) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ - return hash; -} diff --git a/src/helper.h b/src/helper.h deleted file mode 100644 index 7494073..0000000 --- a/src/helper.h +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include - -/** Parse unsigned; [`s`,`e`) => `n`. */ -static int helper_natural(const char *s, const char *const e, uint32_t *const n) -{ - uint32_t accum = 0; - while(s < e) { - unsigned next = accum * 10 + (unsigned)(*s - '0'); - if(accum >= next) return errno = ERANGE, 0; - accum = next; - s++; - } - *n = accum; - return 1; -} - -/** djb2 */ -static uint32_t djb2(const char *s) { - const unsigned char *str = (const unsigned char *)s; - uint32_t hash = 5381, c; - while(c = *str++) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ - return hash; -} - -static void unused_helper_coda(void); -static void unused_helper(void) - { helper_natural(0, 0, 0); djb2(0); unused_helper_coda(); } -static void unused_helper_coda(void) { unused_helper(); } diff --git a/src/pair.c b/src/pair.c index 886f88e..b1df775 100644 --- a/src/pair.c +++ b/src/pair.c @@ -1,6 +1,7 @@ #include "pair.h" #include #include +#include /** @return Constructs `a` and `b` as a pair. */ struct pair pair(const char *const a, const char *const b) { @@ -22,3 +23,21 @@ int pair_to_natural(const char *s, const char *const e, uint32_t *const n) { *n = accum; return 1; } + +int pair_is_equal(struct pair x, struct pair y) { + assert(x.a <= x.b && y.a <= y.b); + if(!x.a) return !y.a; + if(x.b - x.a != y.b - y.a) return 0; + while(x.a < x.b) { if(*x.a != *y.a) return 0; x.a++, y.a++; } + return 1; +} + +/** djb2 */ +uint32_t pair_djb2(struct pair p) { + uint32_t hash = 5381, c; + while(p.a < p.b) { + c = (unsigned char)*p.a++; + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + } + return hash; +} diff --git a/src/pair.h b/src/pair.h index 0b3d66a..f010a65 100644 --- a/src/pair.h +++ b/src/pair.h @@ -3,3 +3,5 @@ struct pair { const char *a, *b; }; struct pair pair(const char *const a, const char *const b); int pair_to_natural(const char *, const char *, uint32_t *); +int pair_is_equal(struct pair, struct pair); +uint32_t pair_djb2(const struct pair p); diff --git a/src/source.h b/src/source.h index f7f6486..3219e1c 100644 --- a/src/source.h +++ b/src/source.h @@ -1,12 +1,13 @@ #if defined BASE \ || !defined BASE && !defined GENERIC && !defined PROTO /* */ #if defined GENERIC \ || !defined BASE && !defined GENERIC && !defined PROTO /* */ #if defined PROTO \ || !defined BASE && !defined GENERIC && !defined PROTO /*