site

A custom static site generator written in C.
git clone https://unitehenry.com/git/site.git
Log | Files | Refs | README

commit 0c49c3d6389f3de828b0bf4f6f9d79997daa2781
parent a5704f3f9e91a7c19088e305ca874ca4dad58663
Author: Henry Unite <unitehenry@gmail.com>
Date:   Sat, 11 Apr 2026 23:36:17 +0000

refactor: string replace

Diffstat:
Dsrc/string_replace.c | 57---------------------------------------------------------
Dsrc/string_replace.h | 4----
Msrc/strings.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/strings.h | 1+
Msrc/to_write_path.c | 2+-
Msrc/to_write_path.h | 5++---
6 files changed, 57 insertions(+), 65 deletions(-)

diff --git a/src/string_replace.c b/src/string_replace.c @@ -1,57 +0,0 @@ -#include "string_replace.h" -#include <stddef.h> -#include <stdlib.h> -#include <string.h> - -char *str_replace(const char *str, const char *old_sub, const char *new_sub) { - size_t old_len = strlen(old_sub); - - if (old_len == 0) { - return strdup(str); - } - - size_t new_len = strlen(new_sub); - - size_t str_len = strlen(str); - - size_t count = 0; - - const char *p = str; - - while ((p = strstr(p, old_sub)) != NULL) { - count++; - p += old_len; - } - - if (count == 0) { - return strdup(str); - } - - size_t new_str_len = str_len + count * (new_len - old_len) + 1; - - char *result = malloc(new_str_len); - - if (!result) { - return NULL; - } - - char *dest = result; - - const char *src = str; - - while (*src) { - if (strstr(src, old_sub) == src) { - strcpy(dest, new_sub); - - dest += new_len; - - src += old_len; - } else { - *dest++ = *src++; - } - } - - *dest = '\0'; - - return result; -} diff --git a/src/string_replace.h b/src/string_replace.h @@ -1,3 +0,0 @@ -#include <stddef.h> - -char *str_replace(const char *str, const char *old_sub, const char *new_sub); -\ No newline at end of file diff --git a/src/strings.c b/src/strings.c @@ -35,3 +35,56 @@ void string_list_append(StringList *string_list, char *string) { string_list->count++; string_list->strings[string_list->count] = NULL; } + +char *str_replace(const char *str, const char *old_sub, const char *new_sub) { + size_t old_len = strlen(old_sub); + + if (old_len == 0) { + return strdup(str); + } + + size_t new_len = strlen(new_sub); + + size_t str_len = strlen(str); + + size_t count = 0; + + const char *p = str; + + while ((p = strstr(p, old_sub)) != NULL) { + count++; + p += old_len; + } + + if (count == 0) { + return strdup(str); + } + + size_t new_str_len = str_len + count * (new_len - old_len) + 1; + + char *result = malloc(new_str_len); + + if (!result) { + return NULL; + } + + char *dest = result; + + const char *src = str; + + while (*src) { + if (strstr(src, old_sub) == src) { + strcpy(dest, new_sub); + + dest += new_len; + + src += old_len; + } else { + *dest++ = *src++; + } + } + + *dest = '\0'; + + return result; +} diff --git a/src/strings.h b/src/strings.h @@ -11,5 +11,6 @@ typedef struct { StringList *init_string_list(size_t size); void string_list_append(StringList *string_list, char *string); +char *str_replace(const char *str, const char *old_sub, const char *new_sub); #endif diff --git a/src/to_write_path.c b/src/to_write_path.c @@ -1,5 +1,5 @@ #include "path_root.h" -#include "string_replace.h" +#include "strings.h" #include <stdio.h> #include <string.h> diff --git a/src/to_write_path.h b/src/to_write_path.h @@ -2,10 +2,10 @@ #define TO_WRITE_PATH_H #include "path_root.h" -#include "string_replace.h" +#include "strings.h" extern char *BUILD_DIRECTORY; void to_write_path(char **write_path, char *read_path); -#endif -\ No newline at end of file +#endif