From 36038ef7bafab76b1280ff9aad408bce96d122db Mon Sep 17 00:00:00 2001 From: Randy Jordan Date: Fri, 11 Jul 2025 10:43:46 -0500 Subject: [PATCH] Added before/afer delims --- include/sv.h | 3 +++ src/sv.c | 44 ++++++++++++++++++++++++++++++++++++++ tests/08_sv_before_delim.c | 20 +++++++++++++++++ tests/09_sv_after_delim.c | 20 +++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 tests/08_sv_before_delim.c create mode 100644 tests/09_sv_after_delim.c diff --git a/include/sv.h b/include/sv.h index 84dd79d..0130a33 100644 --- a/include/sv.h +++ b/include/sv.h @@ -24,4 +24,7 @@ SV sv_trim_left(SV s); SV sv_trim_right(SV s); int sv_cmp(const SV s1, const SV s2); int sv_casecmp(const SV s1, const SV s2); +SV after_delim(const SV s, char *delims); +SV before_delim(const SV s, char *delims); + #endif diff --git a/src/sv.c b/src/sv.c index f25f1a6..6736250 100644 --- a/src/sv.c +++ b/src/sv.c @@ -56,3 +56,47 @@ int sv_casecmp(const SV s1, const SV s2) { if (i < s2.len) return -1; return 0; } +SV before_delim(const SV s, char *delims) { + // Handle NULL pointer or empty string view + if (s.buf == NULL || delims == NULL) { + return sv_strn(NULL, 0); + } + + size_t i = 0; + while (i < s.len) { + // Check if the current character is a delimiter + for (size_t j = 0; delims[j] != '\0'; j++) { + if (s.buf[i] == delims[j]) { + SV result = sv_strn(s.buf, i); + return result; + } + } + i++; + } + + // Return the whole string if no delimiter was found + SV result = sv_strn(s.buf, i); + return result; +} +SV after_delim(const SV s, char *delims) { + // Handle NULL pointer or empty string view + if (s.buf == NULL || delims == NULL) { + return sv_strn(NULL, 0); + } + + size_t i = 0; + while (i < s.len) { + // Check if the current character is a delimiter + for (size_t j = 0; delims[j] != '\0'; j++) { + if (s.buf[i] == delims[j]) { + SV result = sv_strn(s.buf+i+1, s.len-i-1); + return result; + } + } + i++; + } + + // Return the whole string if no delimiter was found + SV result = sv_strn(s.buf, i); + return result; +} diff --git a/tests/08_sv_before_delim.c b/tests/08_sv_before_delim.c new file mode 100644 index 0000000..ed27c7e --- /dev/null +++ b/tests/08_sv_before_delim.c @@ -0,0 +1,20 @@ +#define DEBUG +#include "../include/sv.h" +#include "../include/except.h" +#include +#include +#include + +int main(void){ + const char *s = "Hello,World"; + const char *s1 = "Hello"; + + SV sv = sv_strn(s,strlen(s)); + SV sv1 = sv_strn(s1,strlen(s1)); + + SV test = before_delim(sv,","); + ASSERTED(sv_cmp(sv1,test) == 0); + + + return EXIT_SUCCESS; +} diff --git a/tests/09_sv_after_delim.c b/tests/09_sv_after_delim.c new file mode 100644 index 0000000..43f7592 --- /dev/null +++ b/tests/09_sv_after_delim.c @@ -0,0 +1,20 @@ +#define DEBUG +#include "../include/sv.h" +#include "../include/except.h" +#include +#include +#include + +int main(void){ + const char *s = "Hello,World"; + const char *s1 = "World"; + + SV sv = sv_strn(s,strlen(s)); + SV sv1 = sv_strn(s1,strlen(s1)); + + SV test = after_delim(sv,","); + ASSERTED(sv_cmp(sv1,test) == 0); + + + return EXIT_SUCCESS; +}