2022-12-29 02:54:59 -05:00
|
|
|
#include <stdint.h>
|
2023-02-03 02:13:55 -05:00
|
|
|
#include <errno.h>
|
2023-02-01 03:58:51 -05:00
|
|
|
|
2022-12-29 02:54:59 -05:00
|
|
|
/** 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;
|
|
|
|
}
|
2023-02-02 21:53:59 -05:00
|
|
|
|
2023-02-03 00:31:24 -05:00
|
|
|
/** djb2 <http://www.cse.yorku.ca/~oz/hash.html> */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-02-02 21:53:59 -05:00
|
|
|
static void unused_helper_coda(void);
|
|
|
|
static void unused_helper(void)
|
2023-02-03 00:31:24 -05:00
|
|
|
{ helper_natural(0, 0, 0); djb2(0); unused_helper_coda(); }
|
2023-02-02 21:53:59 -05:00
|
|
|
static void unused_helper_coda(void) { unused_helper(); }
|