String replace

FossilOrigin-Name: 63773000f0554657fd047c0416a22def13bb02da3eb18bfb87fcd6ba60fa55ae
This commit is contained in:
me@ow.nekobit.net 2022-04-19 03:24:41 +00:00
parent b2ce413a13
commit 392ef8cb81
5 changed files with 55 additions and 18 deletions

View file

@ -55,21 +55,45 @@ char* strnstr(const char* haystack, const char* needle, size_t s)
char* strrepl(char* source, char* find, char* repl)
{
size_t repl_len = strlen(repl);
const size_t find_len = strlen(find);
const size_t repl_len = strlen(repl);
char* result = NULL;
char* n;
char* needle = source;
char* curr;
size_t is_last = 0;
char* last = source;
size_t str_size = 0;
size_t last_str_size;
do
{
n = strstr(needle, find);
if (!n) break;
result = realloc(result, n - source + repl_len + 1);
result[n - source + repl_len] = '\0';
// Copy initial string up to here
strncpy(result, find, n - source - 1);
if (*last == '\0') break;
curr = strstr(last, find);
if (last == source && !curr) break;
// Move to end
else if (!curr)
{
curr = last + strlen(last);
is_last = 1;
}
} while (n);
// Increase to distance
last_str_size = str_size;
str_size += curr - last;
// Create and copy
result = realloc(result, str_size + (!is_last ? repl_len : 0) + 1);
strncpy(result + last_str_size, last, curr - last);
if (!is_last)
{
strncpy(result + str_size, repl, repl_len);
// Bump past replace size and null term
str_size += repl_len;
}
result[str_size] = '\0';
// If is_last is true, this value doesn't matter
last = curr + find_len;
} while (curr && !is_last);
// Return source string if no replacements
return result ? result : source;

View file

@ -4,7 +4,7 @@ MASTODONT = $(MASTODONT_DIR)libmastodont.a
CFLAGS += -g -Wall -I ../$(MASTODONT_DIR)include/ -Wno-unused-variable -Wno-discarded-qualifiers -I/usr/include/ $(shell pkg-config --cflags libcurl libcjson libpcre)
LDFLAGS = -L./../$(MASTODONT_DIR) -lmastodont $(shell pkg-config --libs libcjson libcurl libpcre) -lfcgi
TARGET = tests
SRC = unit/main.c ../src/mime.c
SRC = unit/main.c ../src/mime.c ../src/string.c
OBJ = $(patsubst %.c,%.o,$(SRC))
all: $(TARGET)

View file

@ -31,10 +31,10 @@ int iterate_tests(struct test* tests, size_t tests_len)
for (size_t i = 0; i < tests_len; ++i)
{
printf("[ Test ] %04ld %s\n", i, tests->test_name);
status = tests->callback();
printf("[ Test \"%s\" %s! ]\n", tests->test_name, !status ? "passed" : "failed");
if (!status)
printf("[ Test ] %04ld %s\n", i, tests[i].test_name);
status = tests[i].callback();
printf("[ Test \"%s\" %s! ]\n", tests[i].test_name, !status ? "passed" : "failed");
if (status)
return 1;
}

View file

@ -42,14 +42,14 @@ void form_check(void)
// Copy and test
strcpy(multipart, MULTIPART_TEST);
pos = read_form_data(BOUNDARY_RES_T, multipart, &info);
pos = read_form_data(BOUNDARY_RES_T, multipart, &info, sizeof(MULTIPART_TEST)-1);
assert(pos != NULL);
assert(strcmp(info.name, "text") == 0);
assert(strcmp(info.value, "text default") == 0);
// test next value
pos = read_form_data(BOUNDARY_RES_T, pos, &info);
pos = read_form_data(BOUNDARY_RES_T, pos, &info, sizeof(MULTIPART_TEST) - (pos - multipart));
assert(pos != NULL);
assert(strcmp(info.name, "file1") == 0);

View file

@ -1,6 +1,19 @@
#include <string.h>
#include "../../src/string.h"
int string_replace_test(void)
{
strrepl
char* res1 = strrepl("hello world", "wo", "swi");
assert(strcmp(res1, "hello swirld") == 0);
char* res2 = strrepl("hello world itswo the world wo", "wo", "swi");
assert(strcmp(res2, "hello swirld itsswi the swirld swi") == 0);
char* res3 = strrepl(">implying\nhuh? <><", ">", "&gt;");
assert(strcmp(res3, "&gt;implying\nhuh? <&gt;<") == 0);
char* res4 = strrepl(">lol >hi", ">", ">>>");
assert(strcmp(res4, ">>>lol >>>hi") == 0);
free(res1);
free(res2);
free(res3);
free(res4);
return 0;
}