This commit is contained in:
Randy Jordan 2025-07-11 09:17:42 -05:00
parent 7e36a84f2f
commit 46a0b74e51
Signed by: Randy-Jordan
GPG Key ID: 153FF450FDC74D1A
3 changed files with 32 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#define SV_INCLUDED
#include <stddef.h>
#
// macros
#define SV(s) sv_str(s)
#define SV_NULL sv_strn(NULL, 0)
@ -21,5 +22,6 @@ 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);
int sv_cmp(const SV s1, const SV s2);
#endif

View File

@ -27,3 +27,17 @@ SV sv_trim_right(SV s){
while (s.len > 0 && is_space((int) *(s.buf + s.len - 1))) s.len--;
return s;
}
int sv_cmp(const SV s1, const SV s2){
size_t i = 0;
while (i < s1.len && i < s2.len) {
int c1 = s1.buf[i];
int c2 = s2.buf[i];
if (c1 < c2) return 0;
if (c1 > c2) return 1;
i++;
}
if (i < s1.len) return 1;
if (i < s2.len) return 0;
return 0;
}

16
tests/06_sv_cmp.c Normal file
View File

@ -0,0 +1,16 @@
#define DEBUG
#include "../include/sv.h"
#include "../include/except.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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_cmp(sv,sv1) == 0);
return EXIT_SUCCESS;
}