From 12d7d0c8233803ba94c2a69fd72535d781f75fcf Mon Sep 17 00:00:00 2001 From: DaniTheSkunk <> Date: Mon, 27 Feb 2023 00:56:35 +0000 Subject: [PATCH] added some utility functions --- build.bat | 2 +- build.c | 1 + include/str.h | 7 +++++++ include/types.h | 3 +++ src/str.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ src/types.c | 8 ++++++++ 6 files changed, 73 insertions(+), 1 deletion(-) diff --git a/build.bat b/build.bat index afae0d5..3156928 100644 --- a/build.bat +++ b/build.bat @@ -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 diff --git a/build.c b/build.c index a3d91f8..031ed1d 100644 --- a/build.c +++ b/build.c @@ -97,6 +97,7 @@ bool file_exists(char *dir) { void sys(char *cmd) { if(system(cmd) != 0) { + fflush(stdout); exit(-1); } } diff --git a/include/str.h b/include/str.h index 39477d0..934999c 100644 --- a/include/str.h +++ b/include/str.h @@ -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 */ diff --git a/include/types.h b/include/types.h index 26b7b04..77b3ddd 100644 --- a/include/types.h +++ b/include/types.h @@ -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); diff --git a/src/str.c b/src/str.c index 7264da5..10a2b17 100644 --- a/src/str.c +++ b/src/str.c @@ -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; +} diff --git a/src/types.c b/src/types.c index 00463f8..a9651d5 100644 --- a/src/types.c +++ b/src/types.c @@ -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; }