Added sv trims
This commit is contained in:
parent
5825c71eb6
commit
7e36a84f2f
@ -18,6 +18,8 @@ typedef struct String_View SV;
|
||||
|
||||
SV sv_str(const char *s);
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
19
src/sv.c
19
src/sv.c
@ -1,12 +1,29 @@
|
||||
#include "../include/sv.h"
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
static bool is_space(int c) {
|
||||
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f';
|
||||
}
|
||||
|
||||
SV sv_str(const char *s){
|
||||
SV str = {s == NULL ? 0 : strlen(s), (char *)s};
|
||||
return str;
|
||||
}
|
||||
|
||||
SV sv_strn(const char *s, size_t len){
|
||||
SV str = {len, (char *) s};
|
||||
return str;
|
||||
}
|
||||
SV sv_trim(SV s){
|
||||
while (s.len > 0 && is_space((int) *s.buf)) s.buf++, s.len--;
|
||||
while (s.len > 0 && is_space((int) *(s.buf + s.len - 1))) s.len--;
|
||||
return s;
|
||||
}
|
||||
SV sv_trim_left(SV s){
|
||||
while (s.len > 0 && is_space((int) *s.buf)) s.buf++, s.len--;
|
||||
return s;
|
||||
}
|
||||
SV sv_trim_right(SV s){
|
||||
while (s.len > 0 && is_space((int) *(s.buf + s.len - 1))) s.len--;
|
||||
return s;
|
||||
}
|
||||
|
17
tests/03_sv_trim.c
Normal file
17
tests/03_sv_trim.c
Normal file
@ -0,0 +1,17 @@
|
||||
#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));
|
||||
sv1 = sv_trim(sv1);
|
||||
ASSERTED(strncmp(sv.buf,sv1.buf,sv1.len) == 0);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
17
tests/04_sv_trim_left.c
Normal file
17
tests/04_sv_trim_left.c
Normal file
@ -0,0 +1,17 @@
|
||||
#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));
|
||||
sv1 = sv_trim_left(sv1);
|
||||
ASSERTED(strncmp(sv.buf,sv1.buf,sv1.len) == 0);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
17
tests/05_sv_trim_right.c
Normal file
17
tests/05_sv_trim_right.c
Normal file
@ -0,0 +1,17 @@
|
||||
#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));
|
||||
sv1 = sv_trim_right(sv1);
|
||||
ASSERTED(strncmp(sv.buf,sv1.buf,sv1.len) == 0);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user