refactored code from tcpu
This commit is contained in:
25
include/string/StringBuilder.h
Executable file
25
include/string/StringBuilder.h
Executable file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "../collections/List.h"
|
||||
#include "str.h"
|
||||
|
||||
typedef struct StringBuilder {
|
||||
List buffer;
|
||||
} StringBuilder;
|
||||
|
||||
static inline StringBuilder StringBuilder_alloc(u32 initial_size) {
|
||||
return (StringBuilder){ .buffer = List_alloc_size(initial_size) };
|
||||
}
|
||||
void StringBuilder_free(StringBuilder* b);
|
||||
|
||||
/// @param count set to -1 to clear StringBuilder
|
||||
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
|
||||
void StringBuilder_append_char(StringBuilder* b, char c);
|
||||
void StringBuilder_append_cstr(StringBuilder* b, char* s);
|
||||
void StringBuilder_append_string(StringBuilder* b, str s);
|
||||
void StringBuilder_append_i64(StringBuilder* b, i64 a);
|
||||
void StringBuilder_append_u64(StringBuilder* b, u64 a);
|
||||
void StringBuilder_append_f64(StringBuilder* b, f64 a);
|
||||
|
||||
// adds '\0' to the buffer and returns pointer to buffer content
|
||||
str StringBuilder_getStr(StringBuilder* b);
|
||||
6
include/string/char.h
Executable file
6
include/string/char.h
Executable file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
static inline bool isAlphabeticalLower(char c) { return 'a' <= c && c <= 'z'; }
|
||||
static inline bool isAlphabeticalUpper(char c) { return 'A' <= c && c <= 'Z'; }
|
||||
static inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
|
||||
9
include/string/cstr.h
Executable file
9
include/string/cstr.h
Executable file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
#define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__)
|
||||
char* _strcat_malloc(size_t n, cstr str0, ...);
|
||||
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv);
|
||||
|
||||
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...) __attribute__((__format__(__printf__, 2, 3)));
|
||||
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv);
|
||||
43
include/string/str.h
Executable file
43
include/string/str.h
Executable file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "../std.h"
|
||||
#include "char.h"
|
||||
#include "cstr.h"
|
||||
|
||||
typedef struct str {
|
||||
char* data;
|
||||
u32 size;
|
||||
bool isZeroTerminated;
|
||||
} str;
|
||||
|
||||
/// creates str from a string literal
|
||||
#define STR(LITERAL) str_construct(LITERAL, ARRAY_SIZE(LITERAL) - 1, true)
|
||||
|
||||
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = LEN, .isZeroTerminated = ZERO_TERMINATED })
|
||||
|
||||
static const str str_null = str_construct(NULL, 0, 0);
|
||||
|
||||
/// copies src content to new string and adds \0 at the end
|
||||
str str_copy(str src);
|
||||
|
||||
/// compares two strings, NullPtr-friendly
|
||||
bool str_equals(str str0, str str1);
|
||||
|
||||
/// allocates new string which is reversed variant of <s>
|
||||
str str_reverse(str s);
|
||||
|
||||
i32 str_seek(str src, str fragment, u32 startIndex);
|
||||
i32 str_seekReverse(str src, str fragment, u32 startIndex);
|
||||
|
||||
i32 str_seekChar(str src, char c, u32 startIndex);
|
||||
i32 str_seekCharReverse(str src, char c, u32 startIndex);
|
||||
|
||||
bool str_startsWith(str src, str fragment);
|
||||
bool str_endsWith(str src, str fragment);
|
||||
|
||||
/// @brief calculates string hash using sdbm32 algorythm (something like lightweight crc32)
|
||||
/// @return non-cryptografic hash of the string
|
||||
u32 str_hash32(str s);
|
||||
|
||||
str str_toUpper(str src);
|
||||
str str_toLower(str src);
|
||||
Reference in New Issue
Block a user