summaryrefslogtreecommitdiff
path: root/qstd/str.c
blob: 5dd97c4615cefead61d0cb632e07a244ea7f0753 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "memory.h"
#include "str.h"

uint64_t fnv1a64(uint8_t* buf, size_t size) {
	size_t i;
	uint64_t hash = 0xcbf29ce484222325;
	for (i = 0; i < size; i++, buf++) {
		hash ^= *buf;
		hash *= 0x100000001b3;
	}
	return hash;
}

uint64_t fnv1a64_2(uint64_t hash, uint8_t* buf, size_t size) {
	size_t i;
	for (i = 0; i < size; i++, buf++) {
		hash ^= *buf;
		hash *= 0x100000001b3;
	}
	return hash;
}

uint32_t hash_string(const char* s) {
	uint32_t h = 2166136261u;
	for (; *s; s++) {
		h ^= *(uint8_t*)s;
		h *= 16777619;
	}
	return h;
}

int string_equal(const char* a, const char* b) {
	while (*a && *b) {
		if (*a != *b) { return 0; }
		a++; b++;
	}
	return 1;
}

int string_copy(char* dst, const char* src) {
	int i;
	for (i = 0; *src; src++, dst++, i++)
		*dst = *src;
	*dst = 0;
	return i;
}

int string_len(const char* s) {
	int l;
	for (l = 0; *s; s++, l++);
	return l;
}

char* dup_string(Arena* a, const char* s) {
	int size = string_len(s) + 1;
	char* d = arena_alloc_aligned(a, size, 1);
	string_copy(d, s);
	return d;
}