-
Notifications
You must be signed in to change notification settings - Fork 0
/
cstring.h
300 lines (248 loc) · 7.35 KB
/
cstring.h
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#pragma once
#ifndef KIGU_CSTRING_H
#define KIGU_CSTRING_H
#include "common.h"
#include "profiling.h"
global inline void
advance(cstring* s, upt count = 1){DPZoneScoped;
s->str += count; s->count -= count;
}
global inline b32
equals(cstring a, cstring b){DPZoneScoped;
return (a.count == b.count) && (strncmp(a.str, b.str, a.count) == 0);
}
global inline cstring
eat_until_char(cstring s, char c){DPZoneScoped;
for(upt i=0; i<s.count; ++i){ if(s[i] == c){ return cstring{s.str, i}; } }
return cstring{};
}
global inline cstring
eat_until_char_skip_quotes(cstring s, char c){DPZoneScoped;
bool in_quotes = false;
for(upt i=0; i<s.count; ++i){
if(s[i] == '\"') in_quotes = !in_quotes;
if(!in_quotes && s[i] == c){
return cstring{s.str, i};
}
}
return cstring{};
}
global inline cstring
eat_spaces(cstring s){DPZoneScoped;
while(*s.str == ' '){ s.str++; s.count--; }
return s;
}
global inline char*
to_c_string(cstring s, Allocator* a = stl_allocator){DPZoneScoped;
char* cs = (char*)a->reserve((s.count+1)*sizeof(char));
memcpy(cs, s.str, s.count);
return cs;
}
global inline upt
isnumber(char c){DPZoneScoped;
return (c >= '0' && c <= '9') ? true : false;
}
global inline upt
ishex(char c){DPZoneScoped;
return ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'F') ||
(c >= 'a' && c <= 'f') ||
(c == 'x' || c == 'X')) ? true : false;
}
global inline u64
b10tou64(cstring s, cstring* next = 0){DPZoneScoped;
u64 result = 0;
u64 sign = 1;
u32 idx = 0;
//error cases
if(!s.str) return 0;
if(*s.str == '\0') return 0;
if(s.count == 0) return 0;
//skip whitespace
while(*s.str == ' '){ s.str++; idx++; }
//check for sign
if(*s.str == '-'){
sign = -1;
s.str++; idx--;
}
if(*s.str == '+'){
s.str++; idx--;
}
while(*s.str != '\0' && idx < s.count){
if(!isnumber(*s.str)) break;
result = (result*10) + (*s.str - (upt)'0');
s.str++; idx++;
}
if(next){
next->str = s.str;
next->count = s.count - idx;
}
return sign * result;
}
global inline u64
b16tou64(cstring s, cstring* next = 0){DPZoneScoped;
u64 result = 0;
u64 sign = 1;
u32 idx = 0;
//error cases
if(!s.str) return 0;
if(*s.str == '\0') return 0;
if(s.count == 0) return 0;
//skip whitespace
while(*s.str == ' '){ s.str++; idx++; }
//check for sign
if(*s.str == '-'){
sign = -1;
s.str++; idx++;
}
if(*s.str == '+'){
s.str++; idx++;
}
//check for 0x prefix
if(s.count > 2 && *s.str == '0' && *(s.str+1) == 'x'){
s.str+=2; idx+=2;
}
while(*s.str != '\0' && idx < s.count){
if (*s.str >= '0' && *s.str <= '9'){
result = (result*16) + (*s.str - (upt)'0');
}else if(*s.str >= 'A' && *s.str <= 'F'){
result = (result*16) + (*s.str - (upt)'A') + 10;
}else if(*s.str >= 'a' && *s.str <= 'f'){
result = (result*16) + (*s.str - (upt)'a') + 10;
}else{
break;
}
s.str++; idx++;
}
if(next){
next->str = s.str;
next->count = s.count - idx;
}
return sign * result;
}
//wcstring
// NOTE(sushi) commenting this entire thing out because I used windows specific string functions in it
// and we don't use it, so we might just want to get rid of it anyways
// struct wcstring{
// wchar_t* str;
// u64 count;
// inline wchar_t operator[](u32 idx){DPZoneScoped; return str[idx]; }
// inline explicit operator bool(){DPZoneScoped; return count; }
// inline bool operator==(wcstring s){DPZoneScoped; return (count == s.count) && (wcsncmp(str, s.str, count) == 0); }
// };
// #define wcstring_lit(s) wcstring{(wchar_t*)s, sizeof(s)/2-1}
// #define wcstr_lit(s) wcstring{(wchar_t*)s, sizeof(s)/2-1}
// global inline void
// advance(wcstring* s, u64 count = 1){DPZoneScoped;
// s->str += count; s->count -= count;
// }
// global inline wcstring
// eat_until_char(wcstring s, wchar_t c){DPZoneScoped;
// for (u64 i = 0; i < s.count; ++i){ if(s[i] == c){ return wcstring{ s.str, i }; } }
// return wcstring{};
// }
// global inline wcstring
// eat_until_wchar_t_skip_quotes(wcstring s, wchar_t c){DPZoneScoped;
// bool in_quotes = false;
// for (u64 i = 0; i < s.count; ++i){
// if(s[i] == '\"') in_quotes = !in_quotes;
// if(!in_quotes && s[i] == c){
// return wcstring{ s.str, i };
// }
// }
// return wcstring{};
// }
// global inline wcstring
// eat_spaces(wcstring s){DPZoneScoped;
// while(*s.str == ' '){ s.str++; s.count--; }
// return s;
// }
// global inline wchar_t*
// to_c_string(wcstring s, Allocator* a = stl_allocator){DPZoneScoped;
// wchar_t* cs = (wchar_t*)a->reserve((s.count + 1)*sizeof(wchar_t));
// memcpy(cs, s.str, s.count);
// return cs;
// }
// global inline u64
// isnumber(wchar_t c){DPZoneScoped;
// return (c >= '0' && c <= '9') ? true : false;
// }
// global inline u64
// ishex(wchar_t c){DPZoneScoped;
// return ((c >= '0' && c <= '9') ||
// (c >= 'A' && c <= 'F') ||
// (c >= 'a' && c <= 'f') ||
// (c == 'x' || c == 'X')) ? true : false;
// }
// global inline u64
// b10tou64(wcstring s, wcstring* next = 0){DPZoneScoped;
// u64 result = 0;
// u64 sign = 1;
// u32 idx = 0;
// //error cases
// if(!s.str) return 0;
// if(*s.str == '\0') return 0;
// if(s.count == 0) return 0;
// //skip whitespace
// while(*s.str == ' '){ s.str++; idx++; }
// //check for sign
// if(*s.str == '-'){
// sign = -1;
// s.str++; idx--;
// }
// if(*s.str == '+'){
// s.str++; idx--;
// }
// while(*s.str != '\0' && idx < s.count){
// if(!isnumber(*s.str)) break;
// result = (result * 10) + (*s.str - '0');
// s.str++; idx++;
// }
// if(next){
// next->str = s.str;
// next->count = s.count - idx;
// }
// return sign * result;
// }
// global inline u64
// b16tou64(wcstring s, wcstring* next = 0){DPZoneScoped;
// u64 result = 0;
// u64 sign = 1;
// u32 idx = 0;
// //error cases
// if(!s.str) return 0;
// if(*s.str == '\0') return 0;
// if(s.count == 0) return 0;
// //skip whitespace
// while(*s.str == ' '){ s.str++; idx++; }
// //check for sign
// if(*s.str == '-'){
// sign = -1;
// s.str++; idx++;
// }
// if(*s.str == '+'){
// s.str++; idx++;
// }
// //check for 0x prefix
// if(s.count > 2 && *s.str == '0' && *(s.str + 1) == 'x'){
// s.str += 2; idx += 2;
// }
// while(*s.str != '\0' && idx < s.count){
// if (*s.str >= '0' && *s.str <= '9'){
// result = (result * 16) + (*s.str - '0');
// }else if(*s.str >= 'A' && *s.str <= 'F'){
// result = (result * 16) + (*s.str - 'A') + 10;
// }else if(*s.str >= 'a' && *s.str <= 'f'){
// result = (result * 16) + (*s.str - 'a') + 10;
// }else {
// break;
// }
// s.str++; idx++;
// }
// if(next){
// next->str = s.str;
// next->count = s.count - idx;
// }
// return sign * result;
// }
#endif //KIGU_CSTRING_H