From 7e36a84f2f3d19fa541ccd18642be9d0e1e9e9af Mon Sep 17 00:00:00 2001 From: Randy Jordan Date: Thu, 10 Jul 2025 19:19:52 -0500 Subject: [PATCH] Added sv trims --- include/sv.h | 4 +++- src/sv.c | 19 ++++++++++++++++++- tests/03_sv_trim.c | 17 +++++++++++++++++ tests/04_sv_trim_left.c | 17 +++++++++++++++++ tests/05_sv_trim_right.c | 17 +++++++++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 tests/03_sv_trim.c create mode 100644 tests/04_sv_trim_left.c create mode 100644 tests/05_sv_trim_right.c diff --git a/include/sv.h b/include/sv.h index 4be0f99..1b14d3f 100644 --- a/include/sv.h +++ b/include/sv.h @@ -18,6 +18,8 @@ typedef struct String_View SV; SV sv_str(const char *s); SV sv_strn(const char *s, size_t len); - +SV sv_trim(SV s); +SV sv_trim_left(SV s); +SV sv_trim_right(SV s); #endif diff --git a/src/sv.c b/src/sv.c index 99b6a3b..0040629 100644 --- a/src/sv.c +++ b/src/sv.c @@ -1,12 +1,29 @@ #include "../include/sv.h" #include +#include + +static bool is_space(int c) { + return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f'; +} SV sv_str(const char *s){ SV str = {s == NULL ? 0 : strlen(s), (char *)s}; return str; } - SV sv_strn(const char *s, size_t len){ SV str = {len, (char *) s}; return str; } +SV sv_trim(SV s){ + while (s.len > 0 && is_space((int) *s.buf)) s.buf++, s.len--; + while (s.len > 0 && is_space((int) *(s.buf + s.len - 1))) s.len--; + return s; +} +SV sv_trim_left(SV s){ + while (s.len > 0 && is_space((int) *s.buf)) s.buf++, s.len--; + return s; +} +SV sv_trim_right(SV s){ + while (s.len > 0 && is_space((int) *(s.buf + s.len - 1))) s.len--; + return s; +} diff --git a/tests/03_sv_trim.c b/tests/03_sv_trim.c new file mode 100644 index 0000000..b8e8d15 --- /dev/null +++ b/tests/03_sv_trim.c @@ -0,0 +1,17 @@ +#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 World "; + SV sv = sv_strn(s,strlen(s)); + SV sv1 = sv_strn(s1,strlen(s1)); + sv1 = sv_trim(sv1); + ASSERTED(strncmp(sv.buf,sv1.buf,sv1.len) == 0); + + return EXIT_SUCCESS; +} diff --git a/tests/04_sv_trim_left.c b/tests/04_sv_trim_left.c new file mode 100644 index 0000000..d2a42dc --- /dev/null +++ b/tests/04_sv_trim_left.c @@ -0,0 +1,17 @@ +#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 World"; + SV sv = sv_strn(s,strlen(s)); + SV sv1 = sv_strn(s1,strlen(s1)); + sv1 = sv_trim_left(sv1); + ASSERTED(strncmp(sv.buf,sv1.buf,sv1.len) == 0); + + return EXIT_SUCCESS; +} diff --git a/tests/05_sv_trim_right.c b/tests/05_sv_trim_right.c new file mode 100644 index 0000000..0ebf0ac --- /dev/null +++ b/tests/05_sv_trim_right.c @@ -0,0 +1,17 @@ +#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 World "; + SV sv = sv_strn(s,strlen(s)); + SV sv1 = sv_strn(s1,strlen(s1)); + sv1 = sv_trim_right(sv1); + ASSERTED(strncmp(sv.buf,sv1.buf,sv1.len) == 0); + + return EXIT_SUCCESS; +}