Compare commits

...

2 Commits

Author SHA1 Message Date
095d98d0ba Added strstr 2025-10-18 15:18:30 -05:00
c1e7ca3bca Added before/after delims 2025-10-18 15:05:22 -05:00
5 changed files with 121 additions and 0 deletions

View File

@@ -22,5 +22,11 @@ extern struct String_View sv_trim_right(struct String_View sv);
extern struct String_View sv_trim(struct String_View sv);
extern int sv_eq(const struct String_View sv1, const struct String_View sv2);
extern int sv_casecmp( const struct String_View sv1, const struct String_View sv2);
extern struct String_View sv_before_delim(const struct String_View sv,
const char *delims);
extern struct String_View sv_after_delim(const struct String_View sv,
const char *delims);
extern struct String_View sv_str_str(const struct String_View haystack,
const struct String_View needle);
#endif

View File

@@ -56,3 +56,62 @@ int sv_casecmp(const struct String_View sv1, const struct String_View sv2){
if (i < sv2.len) return -1;
return 0;
}
struct String_View sv_before_delim(const struct String_View sv,const char *delims){
// Handle NULL pointer or empty string view
if (sv.buf == NULL || delims == NULL) {
return sv_strn(NULL, 0);
}
size_t i = 0;
while (i < sv.len) {
// Check if the current character is a delimiter
for (size_t j = 0; delims[j] != '\0'; j++) {
if (sv.buf[i] == delims[j]) {
struct String_View result = sv_strn(sv.buf, i);
return result;
}
}
i++;
}
// Return the whole string if no delimiter was found
struct String_View result = sv_strn(sv.buf, i);
return result;
}
struct String_View sv_after_delim(const struct String_View sv, const char *delims){
// Handle NULL pointer or empty string view
if (sv.buf == NULL || delims == NULL) {
return sv_strn(NULL, 0);
}
size_t i = 0;
while (i < sv.len) {
// Check if the current character is a delimiter
for (size_t j = 0; delims[j] != '\0'; j++) {
if (sv.buf[i] == delims[j]) {
struct String_View result = sv_strn(sv.buf+i+1, sv.len-i-1);
return result;
}
}
i++;
}
// Return the whole string if no delimiter was found
struct String_View result = sv_strn(sv.buf, i);
return result;
}
struct String_View sv_str_str(const struct String_View haystack, const struct String_View needle){
size_t i;
if (needle.len > haystack.len) return sv_strn(NULL,0);
if (needle.len == 0) return haystack;
for (i = 0; i <= haystack.len - needle.len; i++) {
if (memcmp(haystack.buf + i, needle.buf, needle.len) == 0) {
return sv_strn(haystack.buf + i, haystack.len -i);
}
}
return sv_strn(NULL,0);
}

View File

@@ -0,0 +1,18 @@
#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!,Goodbye World!";
const char *s3 = "Hello World!";
struct String_View sv = sv_strn(s, strlen(s));
struct String_View sv2 = sv_before_delim(sv,",");
assert(sv2.len == strlen(s3));
assert( memcmp(sv2.buf, s3, sv2.len) == 0);
return EXIT_SUCCESS;
}

18
tests/09_after_delims.c Normal file
View File

@@ -0,0 +1,18 @@
#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!,Goodbye World!";
const char *s3 = "Goodbye World!";
struct String_View sv = sv_strn(s, strlen(s));
struct String_View sv2 = sv_after_delim(sv,",");
assert(sv2.len == strlen(s3));
assert( memcmp(sv2.buf, s3, sv2.len) == 0);
return EXIT_SUCCESS;
}

20
tests/10_strstr.c Normal file
View File

@@ -0,0 +1,20 @@
#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!,Goodbye World!";
const char *n = "Goodbye";
const char *test = "Goodbye World!";
struct String_View sv = sv_strn(s, strlen(s));
struct String_View sv2 = sv_strn(n, strlen(n));
struct String_View found = sv_str_str(sv, sv2);
assert(found.len == strlen(test));
assert( memcmp(found.buf, test, found.len) == 0);
return EXIT_SUCCESS;
}