site

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

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

refactor: trim whitespace

Diffstat:
Msrc/generate_pages.c | 1-
Msrc/strings.c | 37+++++++++++++++++++++++++++++++++++++
Msrc/strings.h | 1+
Dsrc/trim_whitespace.c | 41-----------------------------------------
Dsrc/trim_whitespace.h | 9---------
5 files changed, 38 insertions(+), 51 deletions(-)

diff --git a/src/generate_pages.c b/src/generate_pages.c @@ -1,7 +1,6 @@ #include "create_subdirectories.h" #include "run_pandoc.h" #include "to_write_path.h" -#include "trim_whitespace.h" #include "strings.h" #include <stdio.h> #include <string.h> diff --git a/src/strings.c b/src/strings.c @@ -1,4 +1,5 @@ #include "strings.h" +#include <ctype.h> #include <stddef.h> #include <stdlib.h> #include <string.h> @@ -88,3 +89,39 @@ char *str_replace(const char *str, const char *old_sub, const char *new_sub) { return result; } + +char *trim_whitespace(const char *str) { + if (!str) + return NULL; + + size_t len = strlen(str); + + if (len == 0) + return strdup(""); + + size_t start = 0; + + while (start < len && isspace(str[start])) + start++; + + if (start == len) + return strdup(""); + + size_t end = len - 1; + + while (end > start && isspace(str[end])) + end--; + + size_t new_len = end - start + 1; + + char *result = malloc(new_len + 1); + + if (!result) + return NULL; + + memcpy(result, str + start, new_len); + + result[new_len] = '\0'; + + return result; +} diff --git a/src/strings.h b/src/strings.h @@ -12,5 +12,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); +char *trim_whitespace(const char *str); #endif diff --git a/src/trim_whitespace.c b/src/trim_whitespace.c @@ -1,40 +0,0 @@ -#include "trim_whitespace.h" -#include <ctype.h> -#include <stdlib.h> -#include <string.h> - -char *trim_whitespace(const char *str) { - if (!str) - return NULL; - - size_t len = strlen(str); - - if (len == 0) - return strdup(""); - - size_t start = 0; - - while (start < len && isspace(str[start])) - start++; - - if (start == len) - return strdup(""); - - size_t end = len - 1; - - while (end > start && isspace(str[end])) - end--; - - size_t new_len = end - start + 1; - - char *result = malloc(new_len + 1); - - if (!result) - return NULL; - - memcpy(result, str + start, new_len); - - result[new_len] = '\0'; - - return result; -} -\ No newline at end of file diff --git a/src/trim_whitespace.h b/src/trim_whitespace.h @@ -1,8 +0,0 @@ -#ifndef TRIM_WHITESPACE_H -#define TRIM_WHITESPACE_H - -#include <stddef.h> - -char *trim_whitespace(const char *str); - -#endif -\ No newline at end of file