From f3e416fa6844bed60e8f0ad924c4d644afe5e1ae Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Wed, 14 Oct 2026 10:31:31 -0400 Subject: [PATCH] CMAKELISTS: A pullable api now --- .DS_Store | Bin 0 -> 6148 bytes CMakeLists.txt | 20 ++++++++++---- include/string/string.h | 60 ++++++++++++++++++++++++++++++++++++++++ src/main.c | 10 +++++-- src/string.c | 58 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 .DS_Store create mode 100644 include/string/string.h create mode 100644 src/string.c diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9cfe24668a985b84bb6dc2688d9c338eb4d46dfe GIT binary patch literal 6148 zcmeHKu};G<5Ir{mg6PnZ(JK;5{~%OhV(h|*k|01zNd?MK#E_MNv0uQ(%1`h&`~vTM zR$4byY!yOx()l^(JD>C7&n*88O%TR{mY<1%9&pV{~migDRtx6YkR zDyXe8pbUfzL~=Re`hULt{2vDCl`^0V{3`}boK3R{Zpqiy-pz5Xwa_al3;VT#T?jg^ g6vLNG@iEj1?3O#g)MG0M3q*ee91Yqi13P8l8*Rp8_W%F@ literal 0 HcmV?d00001 diff --git a/CMakeLists.txt b/CMakeLists.txt index db242c7..d37bfb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,20 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 4.4.3) +project(libstr LANGUAGES C) set(CMAKE_C_STANDARD 23) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -include_directories(include) -project(libstr LANGUAGES C) -set(SOURCES - src/main.c +add_library(libstr STATIC + src/string.c + # src/utils.c + # Add other modular string implementation units here as you expand: ) -add_executable(libstr ${SOURCES}) +target_include_directories(libstr + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/include +) + +add_executable(libstr_bin src/main.c) +set_target_properties(libstr_bin PROPERTIES OUTPUT_NAME "libstr") +target_link_libraries(libstr_bin PRIVATE libstr) diff --git a/include/string/string.h b/include/string/string.h new file mode 100644 index 0000000..7e5b5c0 --- /dev/null +++ b/include/string/string.h @@ -0,0 +1,60 @@ +#ifndef STRING_H +#define STRING_H +#include +#include +/* Functions to implement: +charAt + +substring / slice + +concat + +equals / compareTo + +find / indexOf + +contains / includes + +isEmpty / isBlank + +replace + +split + +trim + +toLower / toLowerCase + +toUpper / toUpperCase + +startsWith + +endsWith + +replaceAll + +join + */ + +typedef struct +{ + char* data; + size_t len; +} String; + +String +strvmk(char* s); + +size_t +strvlen(String s); + +char* +strvget(String s); + +String +strvreverse(String s); + +void +strvfree(String s); + +#endif diff --git a/src/main.c b/src/main.c index c154a7b..7fa9b1a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,12 @@ #include +#include -int main() { - printf("%s", "Hello World"); +int +main() +{ + String hello = strvmk("Hello"); + hello = strvreverse(hello); + printf("%s\n", strvget(hello)); + strvfree(hello); return 0; } diff --git a/src/string.c b/src/string.c new file mode 100644 index 0000000..b837901 --- /dev/null +++ b/src/string.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +struct String +{ + char* data; + size_t len; +}; + +String +strvmk(char* s) +{ + size_t len = strlen(s); + String data = { .data = strdup(s), .len = len }; + return data; +} + +size_t +strvlen(String s) +{ + return s.len; +} + +char* +strvget(String s) +{ + return s.data; +} + +String +strvreverse(String s) +{ + size_t n = strvlen(s); + char* data = malloc(n + 1); + memset(data, 0, strvlen(s)); + + int j = 0; + for (int i = n - 1; i >= 0; i--) + { + data[j] = s.data[i]; + j++; + } + strvfree(s); + + return strvmk(data); +} + +void +strvfree(String s) +{ + if (s.data) + { + free(s.data); + s.data = NULL; + } +}