sv cmp
This commit is contained in:
parent
7e36a84f2f
commit
46a0b74e51
@ -2,6 +2,7 @@
|
|||||||
#define SV_INCLUDED
|
#define SV_INCLUDED
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#
|
||||||
// macros
|
// macros
|
||||||
#define SV(s) sv_str(s)
|
#define SV(s) sv_str(s)
|
||||||
#define SV_NULL sv_strn(NULL, 0)
|
#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(SV s);
|
||||||
SV sv_trim_left(SV s);
|
SV sv_trim_left(SV s);
|
||||||
SV sv_trim_right(SV s);
|
SV sv_trim_right(SV s);
|
||||||
|
int sv_cmp(const SV s1, const SV s2);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
14
src/sv.c
14
src/sv.c
@ -27,3 +27,17 @@ SV sv_trim_right(SV s){
|
|||||||
while (s.len > 0 && is_space((int) *(s.buf + s.len - 1))) s.len--;
|
while (s.len > 0 && is_space((int) *(s.buf + s.len - 1))) s.len--;
|
||||||
return s;
|
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
16
tests/06_sv_cmp.c
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user