-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstandard.c
More file actions
232 lines (219 loc) · 3.85 KB
/
standard.c
File metadata and controls
232 lines (219 loc) · 3.85 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "standard.h"
int64_t
strtoll_2(
char const* str
) {
// TODO it seems the C2X's changes for the strto* hasn't landed yet
uint8_t base = 0; // 0 = auto
if ((str[0] == '0') && (str[1] == 'b')) {
base = 2;
str += 2;
}
return strtoll(str, NULL, base);
}
uint64_t
strtoull_2(
char const* str
) {
// TODO it seems the C2X's changes for the strto* hasn't landed yet
uint8_t base = 0; // 0 = auto
if ((str[0] == '0') && (str[1] == 'b')) {
base = 2;
str += 2;
}
return strtoull(str, NULL, base);
}
bool
is_number(
char const* str
) {
// test string if it's a decimal integer
if (str) {
do {
if (0 == isdigit(*str)) {
return false;
}
str++;
} while (*str);
return true;
} else {
return false;
}
}
char*
stopcopy(
char* restrict dest,
char const* restrict src
) {
// custom stpcpy
while(*src) {
*dest = *src;
dest++;
src++;
}
*dest = '\0';
return dest;
}
bool
char_in_string(
char const ch,
char const* str
) {
while (*str) {
if (ch == *str) {
return true;
}
str++;
}
return false;
}
size_t
length_of_word(
char const* const str
) {
constexpr uint8_t uppercase_mask = 0xDF;
char const* c = str;
bool is_word;
do {
uint8_t num = *c - '0';
uint8_t alpha = (*c & uppercase_mask) - 'A';
is_word = ( // probably a faster way to do this.
(alpha <= 25)
|| ('_' == *c)
|| (num <= 9)
);
c++;
} while (is_word);
return c-str-1;
}
void
error_emit(
struct error* const err,
char const* const message
) {
err->message[0] = '\0';
if (message) {
memccpy(err->message, message, '\0', lengthof(err->message));
err->message[lengthof(err->message)-1] = '\0';
switch (err->severity) {
case ERROR_WARNING:
fprintf(stderr, "Warning: %s\n", err->message);
break;
case ERROR_ABORT:
fprintf(stderr, "Error: %s\n", err->message);
break;
case ERROR_CRASH:
fprintf(stderr, "Catastrophic error: %s\n", err->message);
break;
case NO_ERROR:
break;
};
}
switch (err->severity) {
case ERROR_WARNING: return;
case ERROR_ABORT: longjmp(err->env, 0);
case ERROR_CRASH: abort();
case NO_ERROR: return;
};
}
void*
cralloc(
size_t const size
) {
if (0 == size) {
return NULL;
}
void* const ptr = malloc(size);
if (ptr) {
return ptr;
}
fprintf(stderr,
"Catastrophic error: can't get memory from OS. Crashing.\n"
);
abort();
}
void*
crealloc(
void* const old,
size_t const size
) {
void* const ptr = realloc(old, size);
if (ptr || 0 == size) {
return ptr;
}
fprintf(stderr,
"Catastrophic error: can't get memory from OS. Crashing.\n"
);
abort();
}
void*
cralloc0(
size_t const size
) {
if (0 == size) {
return NULL;
}
void* const ptr = calloc(1,size);
if (ptr) {
return ptr;
}
fprintf(stderr,
"Catastrophic error: can't get memory from OS. Crashing.\n"
);
abort();
}
void
arena_init(
struct mem_arena* const arena,
size_t const arena_size,
bool const zeroed
) {
if (zeroed) {
arena->start = cralloc0(arena_size);
} else {
arena->start = cralloc(arena_size);
}
arena->pos = arena->start;
arena->end = arena->start + arena_size;
}
void* arena_alloc(
struct mem_arena* const arena,
struct error* const err,
size_t const alloc_size
) {
void* const old_pos = arena->pos;
void* const new_pos = arena->pos + alloc_size;
error_assert(err, ERROR_ABORT,
"arena size too small",
old_pos < arena->end
);
arena->pos = new_pos;
return old_pos;
}
uint32_t
fletcher32(
const void* data,
size_t length
) {
const uint16_t* pptr = data;
uint32_t c0 = 0;
uint32_t c1 = 0;
length = (length+1) & (~UINT64_C(1));
while (length) {
size_t block_length;
if (length > (360*2)) {
block_length = 360*2;
} else {
block_length = length;
}
length -= block_length;
do {
c0 += *(pptr++);
c1 += c0;
block_length -= 2;
} while (block_length);
c0 %= 0xFFFF;
c1 %= 0xFFFF;
}
return (c1 << 16) | c0;
}