Added eq and casecmp

This commit is contained in:
2025-10-18 14:48:45 -05:00
parent ec02b84777
commit f9e963d7e3
4 changed files with 85 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ extern struct String_View sv_strn(const char *s, size_t n);
extern struct String_View sv_trim_left(struct String_View sv);
extern struct String_View sv_trim_right(struct String_View sv);
extern struct String_View sv_trim(struct String_View sv);
extern int sv_eq(const struct String_View sv1, const struct String_View sv2);
extern int sv_casecmp( const struct String_View sv1, const struct String_View sv2);
#endif

View File

@@ -6,17 +6,17 @@
static bool sv_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;
//}
static int sv_tolc(char c) {
return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;
}
struct String_View sv_str(const char *s) {
struct String_View str = {s == NULL ? 0 : strlen(s), (char *) s };
return str;
struct String_View str = {s == NULL ? 0 : strlen(s), (char *) s };
return str;
}
struct String_View sv_strn(const char *s, size_t n) {
struct String_View str = {n, (char *) s};
return str;
struct String_View str = {n, (char *) s};
return str;
}
struct String_View sv_trim(struct String_View s){
@@ -34,3 +34,25 @@ struct String_View sv_trim_right(struct String_View s){
return s;
}
int sv_eq( const struct String_View sv1, const struct String_View sv2){
if (sv1.len != sv2.len) {
return 0;
} else {
return memcmp(sv1.buf, sv2.buf, sv1.len) == 0;
}
return 0;
}
int sv_casecmp(const struct String_View sv1, const struct String_View sv2){
size_t i = 0;
while (i < sv1.len && i < sv2.len) {
int c1 = sv_tolc(sv1.buf[i]);
int c2 = sv_tolc(sv2.buf[i]);
if (c1 < c2) return -1;
if (c1 > c2) return 1;
i++;
}
if (i < sv1.len) return 1;
if (i < sv2.len) return -1;
return 0;
}

25
tests/06_sv_eq.c Normal file
View File

@@ -0,0 +1,25 @@
#define DEBUG
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "../include/sv.h"
int main(void){
const char *s = " Hello World! ";
const char *s2 = "Hello World!";
SV sv = sv_str(s);
SV sv3 = sv_str(s2);
assert(sv.buf != NULL);
assert(strlen(s) == sv.len);
assert( memcmp(sv.buf, s, strlen(s)) == 0);
SV sv2 = sv_trim(sv);
assert(sv2.len == strlen(s2));
assert(memcmp(s2, sv2.buf, sv2.len) == 0);
assert(sv2.len != strlen(s));
assert( memcmp(s, sv2.buf, sv2.len) != 0);
assert( !sv_eq(sv, sv2));
assert( sv_eq(sv2,sv3));
return EXIT_SUCCESS;
}

29
tests/07_case_cmp.c Normal file
View File

@@ -0,0 +1,29 @@
#define DEBUG
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "../include/sv.h"
int main(void){
const char *s = " Hello World! ";
const char *s2 = "Hello World!";
const char *s3 = "hElLo wOrLD!";
const char *s4 = "Goodbye World!";
SV sv = sv_str(s);
SV sv3 = sv_str(s3);
SV sv4 = sv_str(s4);
assert(sv.buf != NULL);
assert(strlen(s) == sv.len);
assert( memcmp(sv.buf, s, strlen(s)) == 0);
SV sv2 = sv_trim(sv);
assert(sv2.len == strlen(s2));
assert(memcmp(s2, sv2.buf, sv2.len) == 0);
assert(sv2.len != strlen(s));
assert( memcmp(s, sv2.buf, sv2.len) != 0);
assert( !sv_eq(sv, sv2));
assert( sv_casecmp(sv2,sv3) == 0);
assert( sv_casecmp(sv3,sv4) == 1);
return EXIT_SUCCESS;
}