-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfstr_convert.c
More file actions
262 lines (204 loc) · 4.69 KB
/
fstr_convert.c
File metadata and controls
262 lines (204 loc) · 4.69 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// Created by tobin on 3/18/2024.
//
#include <stdlib.h>
#include <wchar.h>
#include <math.h>
#include "fstr_convert.h"
#include <stdio.h>
//Also in fstr.h
//#define USING_WCHAR (sizeof(chr) == sizeof(wchar_t))
//#define USING_CHAR (sizeof(chr) == sizeof(char))
#define i64 int64_t
#define u64 uint64_t
#define i32 int32_t
#define u32 uint32_t
#define i16 int16_t
#define u16 uint16_t
#define i8 int8_t
#define u8 uint8_t
#define bool uint8_t
#define FAILURE (fstr_result) {0}
bool is_alpha(chr c)
{
if (USING_CHAR)
{
return (chr_is_lower(c) || chr_is_upper(c));
}
else
{
return iswalpha(c);
}
}
bool is_digit(chr c)
{
if (USING_CHAR)
{
return (c >= 48 && c <= 57);
}
else
{
return iswdigit(c);
}
}
uint8_t lookup_char(chr c)
{
if (USING_CHAR)
{
return c - 48;
}
else
{
chr tmpStr[] = {0, c};
return wcstol(&tmpStr, NULL, 10);
}
}
bool is_neg(chr c)
{
if (USING_CHAR)
{
return c == '-';
}
else
{
return c == L'-';
}
}
u64 to_u64(i64 val)
{
if (val < 0)
{
val = -val;
}
return (u64)val;
}
// fstr_result fstr_to_double(const fstr* str)
// {
// fstr_result res = fstr_to_i64(str);
// i64 tmp = (i64)res.u_val;
// res.f_val = (float)tmp;
// return res;
// }
fstr_result fstr_to_i64(const fstr* str)
{
//Make a copy of the string
fstr* cop = fstr_copy(str);
//Remove newlines, spaces, etc.
fstr_trim(cop, 0);
usize len = fstr_length(cop);
if (len == 0)
{
return FAILURE;
}
i8 sign = 1;
usize start = 0;
//Check if the string starts with a negative sign, if so we want to skip this chr
if (is_neg(cop->data[0]))
{
//Set the starting u_val to one and the sign to -1
start = 1;
sign = -1;
}
i64 final_val = 0;
fstr_result result = {1, 0};
usize i;
for (i = start; i < len; i++)
{
char c = cop->data[i];
//If its not a digit we want to quit, we also set outValue just for the sake of getting what number has been made so far
if (!is_digit(c))
{
result.success = 0;
break;
}
uint8_t digit = lookup_char(c);
//If adding a new digit would cause an overflow, we just set it to the max
if ((final_val * 10) + digit < 0)
{
final_val = INT64_MAX;
result.success = 0;
}
//Increment the final value by the current power (think of digit position) times the current character as a digit
final_val *= 10;
final_val += digit;
}
result.u_val = final_val * sign;
free(cop);
return result;
}
fstr_result fstr_u64_from_bin_ex(fstr* instr, chr True, chr False)
{
fstr str = *instr;
//Skip the first two chars
if (fstr_starts_with_C(&str, "0b") || fstr_starts_with_C(&str, "0B"))
{
str.data += 2;
}
fstr_result result = {0};
int count = 0;
int i = fstr_length(&str) - 1;
usize sum = 0;
//Read right to left
while (i >= 0)
{
chr b = str.data[i];
//If using C chars
if (b == True)
{
sum += pow(2, count);
}
//Skip any non 0 and 1 numbers and don't increment the counter.
else if (b != False)
{
i--;
continue;
}
count++;
i--;
}
result.success = 1;
result.u_val = sum;
return result;
}
fstr_result fstr_u64_from_bin(fstr* str)
{
//Handle digits based on char version
chr True = '1';
chr False = '0';
if (sizeof(chr) == sizeof(wchar_t))
{
True = L'1';
False = L'0';
}
return fstr_u64_from_bin_ex(str, True, False);
}
fstr_result fstr_to_double(fstr* str)
{
fstr_result result = {0};
//Get everything before the decimal place
long double r = fstr_to_i64(str).i_val;
//Get the digits after the decimal place
fstr_result decPlace = fstr_index_of_chr(str, '.');
//If we don't have a decimal place, skip all this decimal place stuff
if (!decPlace.success)
{
goto AfterDecimal;
}
//Get a slice of the contents after the decimal
usize decIndex = decPlace.u_val;
fstr slice = (fstr){str->end, STR_ERR_None, str->data + decIndex + 1};
//Get the decimal place value
long double dec = fstr_to_i64(&slice).i_val;
if (dec != 0)
{
//5 -> 0.5, 10001 -> 0.10001
double len = (double)fstr_length(&slice);
long double div = powl(10, len);
dec /= div;
r += dec;
}
AfterDecimal:
result.f_val = (double)r;
result.success = 1;
return result;
}