QUOTE: Kindness is always fashionable.

feat: Switched to single header file - libstr - A util library for dealing with strings in C

libstr

A util library for dealing with strings in C
git clone git@soophie.de:/srv/git/libstr
log | files | refs | readme

commit bb8f1fe579ecf6a4916151eb65848b7263218d0d
parent fd80a2a08462fc3f9b0bbbbed96fb53bd73e11da
Author: Sophie <info@soophie.de>
Date:   Sat, 14 Dec 2024 22:40:37 +0000

feat: Switched to single header file

Diffstat:
MMakefile | 17+++--------------
Dlibstr.c | 46----------------------------------------------
Mlibstr.h | 53++++++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 55 insertions(+), 61 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,18 +1,7 @@ -default: build - -build: - cc -o libstr.so -shared -fPIC -lpthread -lssl -Wall -Wextra libstr.c +default: install install: - cc -o libstr.so -shared -fPIC libstr.c - sudo cp libstr.h /usr/local/include/ - sudo cp libstr.so /usr/local/lib/ - sudo ldconfig - rm libstr.so + cp libstr.h /usr/local/include/ uninstall: - sudo rm /usr/local/include/libstr.h - sudo rm /usr/local/lib/libstr.so - -clean: - rm libstr.so + rm /usr/local/include/libstr.h diff --git a/libstr.c b/libstr.c @@ -1,46 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <stdarg.h> - -#include "libstr.h" - -void str_append_len(char **buffer, char *value, int len) { - int buffer_len = 0; - if (*buffer != NULL) { - buffer_len = strlen(*buffer); - } - *buffer = realloc(*buffer, sizeof(char) * (buffer_len + len + 1)); - memcpy(*buffer + buffer_len, value, len); - (*buffer)[buffer_len + len] = '\0'; -} - -void str_append(char **buffer, char *value) { - int len = strlen(value); - str_append_len(buffer, value, len); -} - -void str_appendf(char **buffer, const char *format, ...) { - va_list args; - va_start(args, format); - char str[1000]; - vsnprintf(str, sizeof(str), format, args); - str_append(buffer, str); - va_end(args); -} - -char *str_replace_all(char *buffer, char *str, char *replacement) { - char *result = NULL; - int str_len = strlen(str); - for (int i = 0; i < (int) strlen(buffer); i++) { - char *ptr = &buffer[i]; - if (strncmp(ptr, str, str_len) == 0) { - str_append(&result, replacement); - i += str_len - 1; - } - else { - str_append_len(&result, &buffer[i], 1); - } - } - return result; -} diff --git a/libstr.h b/libstr.h @@ -1,6 +1,57 @@ -#pragma once +#ifndef LIB_STR_H +#define LIB_STR_H void str_append_len(char **buffer, char *value, int len); void str_append(char **buffer, char *value); void str_appendf(char **buffer, const char *format, ...); char *str_replace_all(char *buffer, char *str, char *replacement); + +#ifdef LIB_STR_IMPL + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> + +void str_append_len(char **buffer, char *value, int len) { + int buffer_len = 0; + if (*buffer != NULL) { + buffer_len = strlen(*buffer); + } + *buffer = realloc(*buffer, sizeof(char) * (buffer_len + len + 1)); + memcpy(*buffer + buffer_len, value, len); + (*buffer)[buffer_len + len] = '\0'; +} + +void str_append(char **buffer, char *value) { + int len = strlen(value); + str_append_len(buffer, value, len); +} + +void str_appendf(char **buffer, const char *format, ...) { + va_list args; + va_start(args, format); + char str[1000]; + vsnprintf(str, sizeof(str), format, args); + str_append(buffer, str); + va_end(args); +} + +char *str_replace_all(char *buffer, char *str, char *replacement) { + char *result = NULL; + int str_len = strlen(str); + for (int i = 0; i < (int) strlen(buffer); i++) { + char *ptr = &buffer[i]; + if (strncmp(ptr, str, str_len) == 0) { + str_append(&result, replacement); + i += str_len - 1; + } + else { + str_append_len(&result, &buffer[i], 1); + } + } + return result; +} + +#endif /* LIB_STR_IMPL */ +#endif /* LIB_STR_H */