Compare commits
5
Commits
646d438f5e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
871694a053 | ||
|
|
f397c1abf3 | ||
|
|
f3e416fa68 | ||
|
|
4ea742946a | ||
|
|
f3bdd6ce18 |
@@ -0,0 +1,19 @@
|
|||||||
|
---
|
||||||
|
BasedOnStyle: Google
|
||||||
|
Standard: Latest
|
||||||
|
UseTab: Always
|
||||||
|
TabWidth: 8
|
||||||
|
IndentWidth: 8
|
||||||
|
BreakBeforeBraces: Allman
|
||||||
|
SpaceBeforeCpp11BracedList: true
|
||||||
|
SeparateDefinitionBlocks: Always
|
||||||
|
Cpp11BracedListStyle: false
|
||||||
|
BreakAfterReturnType: TopLevel
|
||||||
|
AlignTrailingComments:
|
||||||
|
Kind: Always
|
||||||
|
ReflowComments: Always
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
|
||||||
|
AllowShortLoopsOnASingleLine: true
|
||||||
|
IndentAccessModifiers: false # Indent access specifiers by one level
|
||||||
|
AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
build/
|
||||||
|
.cache/
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
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)
|
||||||
|
|
||||||
|
add_library(libstr STATIC
|
||||||
|
src/string.c
|
||||||
|
# src/utils.c
|
||||||
|
# Add other modular string implementation units here as you expand:
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
#ifndef STRING_H
|
||||||
|
#define STRING_H
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
|
/* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define STR_INIT { .data = "", .len = 0 };
|
||||||
|
|
||||||
|
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
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
#include <libstr/string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
String hello = strvmk("Hello");
|
||||||
|
hello = strvreverse(hello);
|
||||||
|
printf("%s\n", strvget(hello));
|
||||||
|
strvfree(hello);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include <libstr/string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user