Added before/afer delims
This commit is contained in:
parent
0a1937ed62
commit
36038ef7ba
@ -24,4 +24,7 @@ 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);
|
int sv_cmp(const SV s1, const SV s2);
|
||||||
int sv_casecmp(const SV s1, const SV s2);
|
int sv_casecmp(const SV s1, const SV s2);
|
||||||
|
SV after_delim(const SV s, char *delims);
|
||||||
|
SV before_delim(const SV s, char *delims);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
44
src/sv.c
44
src/sv.c
@ -56,3 +56,47 @@ int sv_casecmp(const SV s1, const SV s2) {
|
|||||||
if (i < s2.len) return -1;
|
if (i < s2.len) return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
SV before_delim(const SV s, char *delims) {
|
||||||
|
// Handle NULL pointer or empty string view
|
||||||
|
if (s.buf == NULL || delims == NULL) {
|
||||||
|
return sv_strn(NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
while (i < s.len) {
|
||||||
|
// Check if the current character is a delimiter
|
||||||
|
for (size_t j = 0; delims[j] != '\0'; j++) {
|
||||||
|
if (s.buf[i] == delims[j]) {
|
||||||
|
SV result = sv_strn(s.buf, i);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the whole string if no delimiter was found
|
||||||
|
SV result = sv_strn(s.buf, i);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
SV after_delim(const SV s, char *delims) {
|
||||||
|
// Handle NULL pointer or empty string view
|
||||||
|
if (s.buf == NULL || delims == NULL) {
|
||||||
|
return sv_strn(NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
while (i < s.len) {
|
||||||
|
// Check if the current character is a delimiter
|
||||||
|
for (size_t j = 0; delims[j] != '\0'; j++) {
|
||||||
|
if (s.buf[i] == delims[j]) {
|
||||||
|
SV result = sv_strn(s.buf+i+1, s.len-i-1);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the whole string if no delimiter was found
|
||||||
|
SV result = sv_strn(s.buf, i);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
20
tests/08_sv_before_delim.c
Normal file
20
tests/08_sv_before_delim.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#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";
|
||||||
|
|
||||||
|
SV sv = sv_strn(s,strlen(s));
|
||||||
|
SV sv1 = sv_strn(s1,strlen(s1));
|
||||||
|
|
||||||
|
SV test = before_delim(sv,",");
|
||||||
|
ASSERTED(sv_cmp(sv1,test) == 0);
|
||||||
|
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
20
tests/09_sv_after_delim.c
Normal file
20
tests/09_sv_after_delim.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#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 = "World";
|
||||||
|
|
||||||
|
SV sv = sv_strn(s,strlen(s));
|
||||||
|
SV sv1 = sv_strn(s1,strlen(s1));
|
||||||
|
|
||||||
|
SV test = after_delim(sv,",");
|
||||||
|
ASSERTED(sv_cmp(sv1,test) == 0);
|
||||||
|
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user