42 lines
621 B
C
42 lines
621 B
C
#ifndef NCTREF_UTILS_H
|
|
#define NCTREF_UTILS_H
|
|
|
|
#include<stddef.h>
|
|
#include<stdbool.h>
|
|
#include<errno.h>
|
|
#include<stdlib.h>
|
|
|
|
inline static size_t djb2(const char *str) {
|
|
size_t hash = 5381;
|
|
|
|
int c;
|
|
while((c = *str++)) {
|
|
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
|
|
inline static bool unstupid_strtol(const char *str, char **endptr, int base, long *result) {
|
|
errno = 0;
|
|
|
|
char *endptr2 = NULL;
|
|
*result = strtol(str, &endptr2, base);
|
|
|
|
if(endptr2 == str) {
|
|
return false;
|
|
}
|
|
|
|
if(errno == ERANGE) {
|
|
return false;
|
|
}
|
|
|
|
if(endptr) {
|
|
*endptr = endptr2;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
#endif
|