]> gitweb.hhaalo.de Git - mv_none_space.git/commitdiff
change replac string
authorBastian Dehn <hhaalo@arcor.de>
Mon, 2 Mar 2026 20:54:31 +0000 (21:54 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Mon, 2 Mar 2026 20:54:31 +0000 (21:54 +0100)
src/rename.c
src/rename.h
tests/rename_tests.c

index 35d6646e2c13542b975e0184e2bf0263505e0c71..97544adf1e1d734073573d9d0a1a334d342920ea 100644 (file)
@@ -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
index 90c2f194da3aa46fd417049a51b58ec4173a289f..8bab7ff5e74b66cc36e7da0c829f8e069bf283d7 100644 (file)
@@ -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
index a6e052c7ba8a6a6066d05e94681cffc398631290..4b12af226bf4e33f49fc35812ee0476c37841d21 100644 (file)
@@ -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!");