casecmp
This commit is contained in:
parent
46a0b74e51
commit
0a1937ed62
@ -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
|
||||
|
17
src/sv.c
17
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;
|
||||
}
|
||||
|
16
tests/07_sv_casecmp.c
Normal file
16
tests/07_sv_casecmp.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_casecmp(sv,sv1) == 0);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user