Added before/afer delims

This commit is contained in:
Randy Jordan 2025-07-11 10:43:46 -05:00
parent 0a1937ed62
commit 36038ef7ba
Signed by: Randy-Jordan
GPG Key ID: 153FF450FDC74D1A
4 changed files with 87 additions and 0 deletions

View File

@ -24,4 +24,7 @@ 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);
SV after_delim(const SV s, char *delims);
SV before_delim(const SV s, char *delims);
#endif

View File

@ -56,3 +56,47 @@ int sv_casecmp(const SV s1, const SV s2) {
if (i < s2.len) return -1;
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;
}

View 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
View 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;
}