From 0a1937ed625804ebc413419979a45d855c6abcce Mon Sep 17 00:00:00 2001 From: Randy Jordan Date: Fri, 11 Jul 2025 09:38:59 -0500 Subject: [PATCH] casecmp --- include/sv.h | 2 +- src/sv.c | 17 ++++++++++++++++- tests/07_sv_casecmp.c | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/07_sv_casecmp.c diff --git a/include/sv.h b/include/sv.h index 7c7056a..84dd79d 100644 --- a/include/sv.h +++ b/include/sv.h @@ -23,5 +23,5 @@ SV sv_trim(SV s); 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); #endif diff --git a/src/sv.c b/src/sv.c index 5c92636..f25f1a6 100644 --- a/src/sv.c +++ b/src/sv.c @@ -5,6 +5,9 @@ static bool is_space(int c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f'; } +static int to_lower(char c) { + return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c; +} SV sv_str(const char *s){ SV str = {s == NULL ? 0 : strlen(s), (char *)s}; @@ -40,4 +43,16 @@ int sv_cmp(const SV s1, const SV s2){ if (i < s2.len) return 0; return 0; } - +int sv_casecmp(const SV s1, const SV s2) { + size_t i = 0; + while (i < s1.len && i < s2.len){ + int c1 = to_lower(s1.buf[i]); + int c2 = to_lower(s2.buf[i]); + if (c1 < c2) return -1; + if (c1 > c2) return 1; + i++; + } + if (i < s1.len) return 1; + if (i < s2.len) return -1; + return 0; +} diff --git a/tests/07_sv_casecmp.c b/tests/07_sv_casecmp.c new file mode 100644 index 0000000..1da0c71 --- /dev/null +++ b/tests/07_sv_casecmp.c @@ -0,0 +1,16 @@ +#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)); + ASSERTED(sv_casecmp(sv,sv1) == 0); + + return EXIT_SUCCESS; +}