interpret/src/helper.h

34 lines
912 B
C

#include <stddef.h>
#include <errno.h>
#include <stdint.h>
/** `printf`-compatible substring. */
struct substring { const char *sub; size_t size; };
/** 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 <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;
}
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(); }