Added trim functions and tests
This commit is contained in:
11
include/sv.h
11
include/sv.h
@@ -2,17 +2,24 @@
|
||||
#define SV_INCLUDED
|
||||
|
||||
#include <stddef.h>
|
||||
struct String_View{
|
||||
struct String_View {
|
||||
size_t len;
|
||||
const char *buf;
|
||||
};
|
||||
typedef struct String_View SV;
|
||||
|
||||
// printf macros for String_View
|
||||
// Macros for String_View printf
|
||||
#define SV_FMT "%.*s"
|
||||
#define SV_ARG(sv) (int) (sv).len, (sv).buf
|
||||
|
||||
// Null String
|
||||
#define SV_NULL sv_strn(NULL, 0)
|
||||
|
||||
extern struct String_View sv_str(const char *s);
|
||||
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);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
28
src/sv.c
28
src/sv.c
@@ -1,14 +1,36 @@
|
||||
#include "../include/sv.h"
|
||||
#include <string.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
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;
|
||||
//}
|
||||
|
||||
struct String_View sv_str(const char *s) {
|
||||
SV str = {s == NULL ? 0 : strlen(s), (char *) s };
|
||||
struct String_View str = {s == NULL ? 0 : strlen(s), (char *) s };
|
||||
return str;
|
||||
}
|
||||
|
||||
struct String_View sv_strn(const char *s, size_t n) {
|
||||
SV str = {n, (char *) s};
|
||||
struct String_View str = {n, (char *) s};
|
||||
return str;
|
||||
}
|
||||
|
||||
struct String_View sv_trim(struct String_View s){
|
||||
while (s.len > 0 && sv_is_space((int) *s.buf)) s.buf++, s.len--;
|
||||
while (s.len > 0 && sv_is_space((int) *(s.buf + s.len - 1))) s.len--;
|
||||
return s;
|
||||
}
|
||||
struct String_View sv_trim_left(struct String_View s){
|
||||
while (s.len > 0 && sv_is_space((int) *s.buf)) s.buf++, s.len--;
|
||||
return s;
|
||||
}
|
||||
struct String_View sv_trim_right(struct String_View s){
|
||||
|
||||
while (s.len > 0 && sv_is_space((int) *(s.buf + s.len - 1))) s.len--;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
24
tests/03_trim_left.c
Normal file
24
tests/03_trim_left.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#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);
|
||||
|
||||
assert(sv.buf != NULL);
|
||||
assert(strlen(s) == sv.len);
|
||||
assert( memcmp(sv.buf, s, strlen(s)) == 0);
|
||||
SV sv2 = sv_trim_left(sv);
|
||||
assert(sv2.len != strlen(s));
|
||||
assert( memcmp(sv2.buf, s, sv2.len) != 0);
|
||||
assert( memcmp(sv2.buf, s2, sv2.len) == 0);
|
||||
assert( sv2.len == strlen(s2));
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
21
tests/04_trim_right.c
Normal file
21
tests/04_trim_right.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#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);
|
||||
assert(sv.buf != NULL);
|
||||
assert(strlen(s) == sv.len);
|
||||
assert( memcmp(sv.buf, s, strlen(s)) == 0);
|
||||
SV sv2 = sv_trim_right(sv);
|
||||
assert(sv2.len == strlen(s2));
|
||||
assert(memcmp(s2, sv2.buf, sv2.len) == 0);
|
||||
assert(sv2.len != strlen(s));
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
22
tests/05_trim.c
Normal file
22
tests/05_trim.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#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);
|
||||
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);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user