From 96e1a8e0d622606bd1786f82c24a47e8162b04dc Mon Sep 17 00:00:00 2001 From: lucielle Date: Fri, 10 Jul 2026 04:18:51 -0500 Subject: [PATCH] add string type --- .clangd | 2 +- Makefile | 1 + include/lstring.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++ test/string.c | 29 +++++++++++++++++++++++ test/test.c | 4 +++- 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 include/lstring.h create mode 100644 test/string.c diff --git a/.clangd b/.clangd index 2c0d843..0b1ac00 100644 --- a/.clangd +++ b/.clangd @@ -1,2 +1,2 @@ CompileFlags: - Add: [-xc, -DL_ARRAY_IMPLEMENTATION] \ No newline at end of file + Add: [-xc, -DL_ARRAY_IMPLEMENTATION, -DL_STRING_IMPLEMENTATION] \ No newline at end of file diff --git a/Makefile b/Makefile index a32d5ec..0fc811f 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ BUILD_DIR := build TEST_BUILD_DIR := $(BUILD_DIR)/test TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \ $(TEST_BUILD_DIR)/array.o \ + $(TEST_BUILD_DIR)/string.o \ INCLUDE_INSTALL_PATH := /usr/local/include/LTypes diff --git a/include/lstring.h b/include/lstring.h new file mode 100644 index 0000000..b0c2892 --- /dev/null +++ b/include/lstring.h @@ -0,0 +1,60 @@ +#ifndef LTYPES_STRING_H +#define LTYPES_STRING_H + +#include +#include +#include +#include + +#define MAX_STRING_LEN 1024 + +typedef struct { + char* buf; + size_t len; +} LString; + +LString* l_string_new(const char* fmt, ...); +void l_string_delete(LString* str); +int l_string_cmp(LString* strA, LString* strB); +LString* l_string_concat(LString* strA, LString* strB); + +#ifdef L_STRING_IMPLEMENTATION + +#include + +LString* l_string_new(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + char temp_string[MAX_STRING_LEN]; + vsnprintf(temp_string, MAX_STRING_LEN, fmt, args); + + va_end(args); + + LString* str = calloc(1, sizeof(LString)); + str->len = strlen(temp_string); + + str->buf = malloc(str->len + 1); + if (str->buf != NULL) { + strcpy(str->buf, temp_string); + } + + return str; +} + +void l_string_delete(LString* str) { + free(str); +} + +int l_string_cmp(LString* strA, LString* strB) { + int num_chars = (strA->len <= strB->len) ? strB->len : strA->len; + return strncmp(strA->buf, strB->buf, num_chars); +} + +LString* l_string_concat(LString* strA, LString* strB) { + return l_string_new("%.*s%.*s", strA->len, strA->buf, strB->len, strB->buf); +} + +#endif + +#endif \ No newline at end of file diff --git a/test/string.c b/test/string.c new file mode 100644 index 0000000..2b6beac --- /dev/null +++ b/test/string.c @@ -0,0 +1,29 @@ +#define L_STRING_IMPLEMENTATION +#include "../include/lstring.h" +#include + +int test_l_string() { + int RETURN_CODE = 0; + printf("test_l_string() : "); + + int integer_0 = 25; + int integer_1 = 15; + + LString* test_string_0 = l_string_new("I would like to purchase item id `%d`", integer_0); + LString* test_string_1 = l_string_new(" for `%d` coins.", integer_1); + + if (l_string_cmp(test_string_0, l_string_new("I would like to purchase item id `25`"))) RETURN_CODE++; + if (l_string_cmp(test_string_1, l_string_new(" for `15` coins."))) RETURN_CODE++; + + LString* test_string_2 = l_string_new("I would like to purchase item id `25` for `15` coins."); + LString* test_string_3 = l_string_concat(test_string_0, test_string_1); + if (l_string_cmp(test_string_2, test_string_3)) RETURN_CODE++; + + l_string_delete(test_string_0); + l_string_delete(test_string_1); + l_string_delete(test_string_2); + l_string_delete(test_string_3); + + printf("%d\n", RETURN_CODE); + return RETURN_CODE; +} \ No newline at end of file diff --git a/test/test.c b/test/test.c index 76de900..2fc4f10 100644 --- a/test/test.c +++ b/test/test.c @@ -2,10 +2,11 @@ #define L_TYPES_AUTHOR "Lucielle