added some utility functions

This commit is contained in:
DaniTheSkunk 2023-02-27 00:56:35 +00:00
parent 4cb2893b85
commit 12d7d0c823
6 changed files with 73 additions and 1 deletions

View File

@ -3,7 +3,7 @@
REM cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -GNinja .. && ninja && copy compile_commands.json .. && skunkworks.exe
REM cd build && meson compile && copy compile_commands.json .. && cd .. && build\gsa.exe
clang build.c src/str.c -Iinclude -Wno-everything -obuild.exe && build.exe && del build.exe && build\gsa_simple.exe
clang build.c src/str.c src/types.c -Iinclude -Wno-everything -obuild.exe && build.exe && del build.exe && build\gsa_simple.exe
EXIT /B %ERRORLEVEL%
REM build\skip.exe c src && exit /b 1
REM build\gsa.exe

View File

@ -97,6 +97,7 @@ bool file_exists(char *dir) {
void sys(char *cmd) {
if(system(cmd) != 0) {
fflush(stdout);
exit(-1);
}
}

View File

@ -1,9 +1,16 @@
#ifndef GUARD_0941C997CB4D2297C8FEA1374912B5D1
#define GUARD_0941C997CB4D2297C8FEA1374912B5D1
#include "types.h"
char *sw_str_concat(char *a, char *b);
char *sw_str_concat3(char *a, char *b, char *c);
char *sw_str_concat4(char *a, char *b, char *c, char *d);
char *sw_str_dup(char *str);
i32 sw_str_index(char *str, char *substr); /* returns -1 if not found */
/* allocates out1 and out2... unless split not found, then both are set to 0 */
void sw_str_split2(char *str, char *split, char **out1, char **out2);
bool sw_str_startswith(char *str, char *start);
char *sw_str_sub(char *str, u32 start, u32 len);
#endif /* GUARD_0941C997CB4D2297C8FEA1374912B5D1 */

View File

@ -19,6 +19,9 @@ typedef i8 bool;
i32 i32_max(i32 v1, i32 v2);
i32 i32_min(i32 v1, i32 v2);
u32 u32_max(u32 v1, u32 v2);
u32 u32_min(u32 v1, u32 v2);
f32 f32_max(f32 v1, f32 v2);
f32 f32_min(f32 v1, f32 v2);

View File

@ -64,3 +64,56 @@ char *sw_str_dup(char *str) {
memcpy(out, str, len);
return out;
}
i32 sw_str_index(char *str, char *substr) {
i32 i;
for(i = 0; *str; ++i) {
if(sw_str_startswith(str, substr)) {
return i;
}
++str;
}
return -1;
}
void sw_str_split2(char *str, char *split, char **out1, char **out2) {
i32 idx;
i32 idx2;
idx = sw_str_index(str, split);
if(idx == -1) {
*out1 = 0;
*out2 = 0;
return;
}
idx2 = idx + strlen(split);
*out1 = sw_str_sub(str, 0, idx);
*out2 = sw_str_sub(str, idx2, strlen(str) - idx2);
}
bool sw_str_startswith(char *str, char *start) {
while(*start) {
if(*str == 0 || *str != *start) {
return false;
}
++str;
++start;
}
return true;
}
char *sw_str_sub(char *str, u32 start, u32 len) {
char *sub;
len = u32_min(len, strlen(str));
sub = malloc(len + 1);
memcpy(sub, str + start, len);
sub[len] = 0;
return sub;
}

View File

@ -8,6 +8,14 @@ i32 i32_min(i32 v1, i32 v2) {
return v1 < v2 ? v1 : v2;
}
u32 u32_max(u32 v1, u32 v2) {
return v1 > v2 ? v1 : v2;
}
u32 u32_min(u32 v1, u32 v2) {
return v1 < v2 ? v1 : v2;
}
f32 f32_max(f32 v1, f32 v2) {
return v1 > v2 ? v1 : v2;
}