summaryrefslogtreecommitdiff
path: root/misc.c
blob: dc6076e08d5cddcbb6e8acc68f02fca7ca557d20 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* 
Miscellaneous C functions which toc uses.

This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/


/* 
A better alternative to strncpy. dest is guaranteed to be a null-terminated string
after this function is run.
Returns the number of characters copied to dest, not including the null character.
destsz must be greater than 0.
*/
size_t str_copy(char *dest, size_t destsz, const char *src) {
	assert(destsz);
	if (!*src) {
		*dest = 0;
		return 0;
	}
	for (size_t i = 0; i < destsz-1; ++i) {
		*dest = *src;
		if (!*src) {
			*dest = 0;
			return i;
		}
		++src; ++dest;
	}
	*dest = 0;
	return destsz-1;
}

static char *str_dup(const char *s) {
	size_t bufsz = strlen(s)+1;
	char *ret = err_malloc(bufsz);
	memcpy(ret, s, bufsz);
	return ret;	
}

static char *cstr(const char *str, size_t len) {
	char *ret = err_malloc(len+1);
	memcpy(ret, str, len);
	ret[len] = 0;
	return ret;
}

static inline char *str_to_cstr(String s) {
	return cstr(s.str, s.len);
}

static inline bool str_eq_cstr(String s, const char *str) {
	return strncmp(s.str, str, s.len) == 0;
}

static inline bool streq(const char *a, const char *b) {
	return strcmp(a, b) == 0;
}

static inline U32 rand_u32(U32 seed) {
	U64 seed64 = (U64)seed;
	return (U32)((seed64 * 0x832f0fda4e1a8642 + 0x41d49cd5459a2ab4) >> 32);
}

#define plural_suffix(x) ((x) == 1 ? "" : "s")
static const char *indefinite_article(const char *s) {
	// usually, words starting with "u" use "a" - "a unique thing", "a u64"
	if (*s == 'a' || *s == 'e' || *s == 'i' || *s == 'o')
		return "an";
	return "a";
}


static U32 *bsearch_u32(U32 *data, size_t count, U32 search) {
	size_t lo = 0;
	size_t hi = count;
	while (hi > lo) {
		size_t mid = (lo+hi) / 2;
		U32 datum = data[mid];
		if (datum > search)
			hi = mid;
		else if (datum < search)
			lo = mid + 1;
		else
			return &data[mid];
	}
	return NULL;
}

static void print_str(String s) {
	fwrite(s.str, 1, s.len, stdout);
	printf("\n");
}