1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-11-09 21:32:02 +05:30

str_replace.c: dedicate a parameter for backward search

Improve the test
This commit is contained in:
Intel A80486DX2-66 2024-06-28 14:01:30 +03:00
parent 1ad67ca96c
commit 32050ac6a8
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 58 additions and 4 deletions

View File

@ -13,7 +13,8 @@ char* str_replace(
const char* str, const char* str,
const char* substr, const char* substr,
const char* replacement, const char* replacement,
ssize_t max_count size_t max_count,
bool direction
) { ) {
/* /*
* input arguments: * input arguments:
@ -24,6 +25,15 @@ char* str_replace(
size_t substr_len = strlen(substr), size_t substr_len = strlen(substr),
replacement_len = strlen(replacement), replacement_len = strlen(replacement),
count = 0; count = 0;
if (direction == STR_REPLACE_DIR_BACKWARD) {
// TODO: implement replacing from the end
fprintf(stderr, "str_replace, STR_REPLACE_DIR_BACKWARD: Work in "
"progress\n");
return str_replace(str, substr, replacement, max_count,
STR_REPLACE_DIR_FORWARD);
}
const char* p = str; const char* p = str;
// count the number of occurrences of the substring // count the number of occurrences of the substring
@ -58,16 +68,59 @@ char* str_replace(
} }
#ifdef TEST #ifdef TEST
#include <stdio.h> # include <stdint.h>
# include <stdio.h>
uintmax_t tests_failed = 0;
static void func_expect(
const char* str,
const char* substr,
const char* replacement,
size_t max_count,
bool direction,
const char* expected_output
);
static void func_expect(
const char* str,
const char* substr,
const char* replacement,
size_t max_count,
bool direction,
const char* expected_output
) {
char* result = str_replace(str, substr, replacement, max_count, direction);
if (result == NULL) {
perror("str_replace");
exit(EXIT_FAILURE);
}
if (strcmp(result, expected_output)) {
puts("Failed!\n");
tests_failed++;
return;
}
puts("Passed\n");
}
int main(void) { int main(void) {
func_expect("Hello; peace! This is a peaceful test.",
"; peace",
", universe",
STR_REPLACE_ALL,
STR_REPLACE_DIR_FORWARD,
"Hello, universe! This is a peaceful test.");
const char* str = "Hello; peace! This is a peaceful test.", const char* str = "Hello; peace! This is a peaceful test.",
* substr = "peace", * substr = "peace",
* replacement1 = "universe", * replacement1 = "universe",
* replacement2 = "_____"; * replacement2 = "_____";
char* result1 = str_replace(str, substr, replacement1, STR_REPLACE_ALL), char* result1 = str_replace(str, substr, replacement1, STR_REPLACE_ALL,
* result2 = str_replace(str, substr, replacement2, 1); STR_REPLACE_DIR_FORWARD),
* result2 = str_replace(str, substr, replacement2, 1,
STR_REPLACE_DIR_FORWARD);
puts(result1); free(result1); puts(result1); free(result1);
puts(result2); free(result2); puts(result2); free(result2);

View File

@ -8,6 +8,7 @@
#ifndef _STR_REPLACE_H #ifndef _STR_REPLACE_H
#define _STR_REPLACE_H #define _STR_REPLACE_H
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>