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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
#ifndef BASE_STRING_H
#define Base_STRING_H
// - str8
typedef struct String8 String8;
typedef struct String16 String16;
typedef struct String32 String32;
typedef struct String8Node String8Node;
typedef struct String16Node String16Node;
typedef struct String32Node String32Node;
typedef struct String8List String8List;
typedef struct String16List String16List;
typedef struct String32List String32List;
#define CStringLen(s) cstring_len(s)
#define String8Len(s) (s.size)
//- strings
struct String8
{
u8 *data;
u64 size;
};
struct String16
{
u16 *data;
u64 size;
};
struct String32
{
u32 *data;
u64 size;
};
//- nodes
struct String8Node
{
String8Node *next;
String8 value;
};
struct String16Node
{
String16Node *next;
String8 string;
};
struct String32Node
{
String32Node *next;
String32 value;
};
//- lists
struct String8List
{
String8Node *first;
String8Node *last;
u64 count;
};
struct String16List
{
String16Node *next;
String16Node string;
};
struct string32_list
{
String32Node *first;
String32Node *last;
u64 count;
};
//- string tools
internal b32
str8_match(String8 a, String8 b)
{
if(a.size != b.size)
{
return 0;
}
return (b8)(memcmp(a.data, b.data, a.size) == 0);
}
internal void
str8_append_u8_element(String8 *buf, u8 c)
{
buf->data[buf->size] = c;
buf->size += 1;
}
internal String8
str8_zero()
{
return String8{0};
}
internal String8
str8_chop_left(String8 buffer, u64 count)
{
if(buffer.size < count)
{
// TODO: Think about how you should notify the user about this
return buffer;
}
for(u64 idx = 0; idx < count; ++idx)
{
buffer.data++;
}
return buffer;
}
internal String8
rtr8_chop_right(String8 buffer, u64 count)
{
if(buffer.size < count)
{
// TODO: Think about how you should notify the user about this
// couldn't we attach a reference some flags or something as IN and then modify them
// and then modify them if something happens. ? is this cheaper then logging and does it provide better edge case hanlding
return buffer;
}
buffer.size -= count;
return buffer;
}
// TODO: I think the flagging system I'm thinking of is also applicable here but I'm not sure
internal void
str8_list_push_node(String8List *list, String8Node *node)
{
// TODO: i can't think anymroe im tired
if(list->first)
{
list->first = node;
} else
{
list->last->next = node;
}
}
//- cstring implementations
internal b32
cstring_is_digit(u8 point)
{
return (point >= '0' && point <= '9');
}
internal b32
cstring_is_alpha(u8 point)
{
return ((point >= 'a' && point <= 'z') || (point >= 'A' && point <= 'Z') || (point == '_'));
}
internal b32
cstring_is_alnum(u8 point)
{
return (cstring_is_alpha(point) || cstring_is_digit(point));
}
internal b32
cstring_is_nl(u8 point)
{
return (point == '\n' || point == '\r');
}
internal b32
cstring_is_whitespace(u8 point)
{
return (point == ' ' || point == '\t');
}
internal b32
cstring_is_slash(u8 point)
{
return (point == '/' || point == '\\');
}
internal b32
cstring_is_newline(u8 point)
{
return (point == '\n');
}
internal u64
cstring_len(u8 *a)
{
if(!a)
{
return 0;
}
u64 idx = 0;
while(a[idx] != '\0')
{
++idx;
}
return idx;
}
internal b32
cstring_is_match(u8 *a, u8 *b)
{
u64 a_size = CStringLen(a);
u64 b_size = CStringLen(b);
if(a_size != b_size)
{
return false;
}
for(s32 idx = 0; idx < a_size; ++idx)
{
if(a[idx] != b[idx])
{
return false;
}
}
return true;
}
internal u64
cstring_find_u8_character(u8 *buffer, u64 start_pos, u8 element)
{
u8 *data = buffer;
u64 offset;
while(!cstring_is_match(data, &element))
{
++offset;
++data;
}
return offset + start_pos;
}
internal u64
cstring_find_element(u8 *buffer, u64 start_pos, u8 element)
{
for(u64 idx = start_pos; buffer[idx]; ++idx)
{
if(buffer[idx] == element)
{
return idx;
}
}
return 0;
}
//- cstring implementations
internal b32 cstring_is_alnum(u8 point);
internal b32 cstring_is_nil(u8 point);
internal b32 cstring_is_whitespace(u8 point);
internal b32 cstring_is_slash(u8 point);
internal b32 cstring_is_alpha(u8 point);
internal b32 cstring_is_digit(u8 point);
internal b32 cstring_is_newline(u8 point);
internal b32 cstring_is_match(u8 *a, u8 *b);
internal u64 cstring_find_element(u8 *buffer, u64 start_pos, u8 element);
//- string8 implementations
internal b32 str8_is_match(String8 a, String8 b);
internal u64 str8_skip_element(String8 *buffer, u64 start_pos, String8 *element);
internal u64 str8_find_element(String8 *buffer, u64 start_pos, String8 *element);
internal void str8_append_u8_element(String8 *buf, u8 *c);
internal void str8_append_str8_element(String8 *a, String8 *b);
internal String8 str8_chop_left(String8 *buffer, u64 count);
internal String8 str8_chop_right(String8 *buffer, u64 count);
internal String8 str8_set_zero();
//- macros
#define PushString8(arena, count) \
(String8) \
{ \
.data = (PushArrayZero(arena, u8, (count))), .size = (count) \
}
#define PushString16(arena, count) \
(String16) \
{ \
.data = (PushArrayZero(arena, u16, (count))), .size = (count) \
}
#define PushString32(arena, count) \
(String32) \
{ \
.data = (PushArrayZero(arena, u32, (count))), .size = (count) \
}
#define ToString8(data, size) \
String8 \
{ \
(u8 *)(data), (u64)(size) \
}
#define ToString16(data, size) \
String16 \
{ \
(u16 *)(data), (u64)(size) \
}
#define ToString32(data, size) \
String32 \
{ \
(u32 *)(data), (u64)(size) \
}
#define StringFmt "%.*s"
#define ULongFmt "%lu"
#define ULLongFmt "%llu"
//-
ReadOnly global_variable String8 str8_nil = {NULL, 0};
ReadOnly global_variable String16 str16_nil = {NULL, 0};
ReadOnly global_variable String32 str32_nil = {NULL, 0};
ReadOnly global_variable String8Node str8_nil_node = {NULL, NULL};
ReadOnly global_variable String16Node str16_nil_node = {NULL, NULL};
ReadOnly global_variable String32Node str32_nil_node = {NULL, NULL};
#endif /* BASE_STRING_H */
|