From 2af19ecff5e59ff83e26d9fac8f88ad7314eeb26 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Mon, 2 Mar 2026 21:54:31 +0100 Subject: [PATCH] change replac string --- src/rename.c | 44 ++++++++++++++++++++++++++------------------ src/rename.h | 2 +- tests/rename_tests.c | 7 +++++-- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/rename.c b/src/rename.c index 35d6646..97544ad 100644 --- a/src/rename.c +++ b/src/rename.c @@ -123,19 +123,25 @@ void rename_lower(const char* src, char* dst) void rename_string(const char* src, char* dst) { - uint16_t dst_count = 0; - size_t length = strlen(src); - for (size_t i = 0; i < length; i++) { - switch (wide_char_length(src[i])) { - case 2: - dst_count += _replace_two_byte_wide_char(&src[i], &dst[dst_count]); - i++; - break; - default: - dst_count += _replace_char(src[i], &dst[dst_count]); - break; - } - } + size_t len = strlen(src) + 1; + char* out = malloc(len); + if (out == NULL) + return; + + memcpy(out, src, len); + + out = replace(&out, " ", "_"); + out = replace(&out, "ä", "ae"); + out = replace(&out, "Ä", "Ae"); + out = replace(&out, "ö", "oe"); + out = replace(&out, "Ö", "Oe"); + out = replace(&out, "ü", "ue"); + out = replace(&out, "Ü", "Ue"); + out = replace(&out, "ß", "ss"); + + memcpy(dst, out, strlen(out) + 1); + free(out); + out = NULL; } void rename_revert(const char* src, char* dst) @@ -179,14 +185,14 @@ void rename_revert(const char* src, char* dst) } } -char* replace(const char* src, const char* pattern, const char* replace_str) +char* replace(char** src, const char* pattern, const char* replace_str) { - const size_t len = strlen(src); + const size_t len = strlen(*src); const size_t plen = strlen(pattern); uint32_t count = 0; for (uint32_t i = 0; i < len; i++) { - if (strncmp(&src[i], pattern, plen) == 0) + if (strncmp(*src + i, pattern, plen) == 0) count++; } @@ -200,14 +206,16 @@ char* replace(const char* src, const char* pattern, const char* replace_str) uint32_t outi = 0; for (uint32_t i = 0; i < len; i++) { - if (strncmp(&src[i], pattern, plen) == 0) { + if (strncmp(*src + i, pattern, plen) == 0) { memcpy(&output[outi], replace_str, rlen); i += plen - 1; outi += rlen; } else { - output[outi++] = src[i]; + output[outi++] = (*src)[i]; } } + free(*src); + *src = NULL; return output; } \ No newline at end of file diff --git a/src/rename.h b/src/rename.h index 90c2f19..8bab7ff 100644 --- a/src/rename.h +++ b/src/rename.h @@ -40,6 +40,6 @@ void rename_lower(const char* src, char* dst); void rename_string(const char* src, char* dst); void rename_revert(const char* src, char* dst); -char* replace(const char* src, const char* pattern, const char* replace_str); +char* replace(char** src, const char* pattern, const char* replace_str); #endif \ No newline at end of file diff --git a/tests/rename_tests.c b/tests/rename_tests.c index a6e052c..4b12af2 100644 --- a/tests/rename_tests.c +++ b/tests/rename_tests.c @@ -269,9 +269,12 @@ void four_byte_wide_character() void replace_string() { - const char* input = "Hello Max!"; + char* input = malloc(sizeof(char) * 11); + if (input == NULL) + return; + memcpy(input, "Hello Max!", 11); - char* output = replace(input, "Max", "World"); + char* output = replace(&input, "Max", "World"); assert_string_equal(output, "Hello World!"); -- 2.47.3