blob: ee8a35414393764970559c61214bcbb7308a6df5 (
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
|
#include "string.h"
#include "memory.h"
#include "utils/type.h"
#include <stdint.h>
#include <string.h>
size_t searchInStringArray(const char *array[], size_t array_size,
const char *str, size_t str_size) {
for (size_t i = 0; i < array_size; ++i) {
const size_t el_size = strlen(array[i]);
if (el_size == str_size && strncmp(array[i], str, str_size) == 0) {
return i;
}
}
return array_size;
}
u64 decimalToU64(char *str_begin, char *str_end, bool *success) {
u64 result = 0;
while (str_begin < str_end) {
if (*str_begin < '0' || *str_begin > '9') {
*success = false;
return 0;
}
result *= 10;
result += *str_begin - '0';
str_begin += 1;
}
*success = true;
return result;
}
f128 numberToFloat(char *str_begin, char *str_end, bool *success) {
f64 left = 0;
f64 right = 0;
bool isPastPoint = false;
while (str_begin < str_end) {
char c;
if (isPastPoint) {
c = *--str_end;
} else {
c = *str_begin++;
}
if (c >= '0' && c <= '9') {
if (isPastPoint) {
left *= 10;
left += c - '0';
} else {
right /= 10;
right += c - '0';
}
} else if (c == '.' && !isPastPoint) {
isPastPoint = true;
} else {
*success = false;
return 0;
}
}
*success = true;
return left + right;
}
char *u64ToString(u64 value) {
char *str = a404m_malloc(21 * sizeof(*str));
size_t i = 0;
do {
str[i] = (value % 10) + '0';
value /= 10;
++i;
} while (value != 0);
for (size_t j = 0; j < i / 2; ++j) {
char tmp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = tmp;
}
str[i] = '\0';
return a404m_realloc(str, (i + 1) * sizeof(*str));
}
|