forked from cosmonaut/wraith-lang
39 lines
651 B
C
39 lines
651 B
C
#include "util.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
char *strdup(const char *s)
|
|
{
|
|
size_t slen = strlen(s);
|
|
char *result = (char *)malloc(sizeof(char) * (slen + 1));
|
|
if (result == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
memcpy(result, s, slen + 1);
|
|
return result;
|
|
}
|
|
|
|
char *w_strcat(char *s, char *s2)
|
|
{
|
|
size_t slen = strlen(s);
|
|
size_t slen2 = strlen(s2);
|
|
s = realloc(s, sizeof(char) * (slen + slen2 + 1));
|
|
strcat(s, s2);
|
|
return s;
|
|
}
|
|
|
|
uint64_t str_hash(char *str)
|
|
{
|
|
uint64_t hash = 5381;
|
|
size_t c;
|
|
|
|
while ((c = *str++))
|
|
{
|
|
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
|
}
|
|
|
|
return hash;
|
|
}
|