refactored code from tcpu
This commit is contained in:
53
src/string/StringBuilder.c
Executable file
53
src/string/StringBuilder.c
Executable file
@@ -0,0 +1,53 @@
|
||||
#include "string/StringBuilder.h"
|
||||
|
||||
void StringBuilder_free(StringBuilder* b){
|
||||
free(b->buffer.data);
|
||||
b->buffer = List_construct_size(NULL, 0, 0);
|
||||
}
|
||||
|
||||
str StringBuilder_getStr(StringBuilder* b){
|
||||
List_push(&b->buffer, u8, '\0');
|
||||
str result = str_construct((char*)b->buffer.data, b->buffer.size - 1, true);
|
||||
return result;
|
||||
}
|
||||
|
||||
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count){
|
||||
if(count < b->buffer.size){
|
||||
b->buffer.size -= count;
|
||||
}
|
||||
else{
|
||||
b->buffer.size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void StringBuilder_append_char(StringBuilder* b, char c){
|
||||
List_push(&b->buffer, u8, c);
|
||||
}
|
||||
|
||||
|
||||
void StringBuilder_append_string(StringBuilder* b, str s){
|
||||
List_push_size(&b->buffer, s.data, s.size);
|
||||
}
|
||||
|
||||
void StringBuilder_append_cstr(StringBuilder* b, char* s){
|
||||
StringBuilder_append_string(b, str_construct(s, strlen(s), true));
|
||||
}
|
||||
|
||||
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
||||
char buf[32];
|
||||
sprintf(buf, IFWIN("%lli", "%li"), n);
|
||||
StringBuilder_append_cstr(b, buf);
|
||||
}
|
||||
|
||||
void StringBuilder_append_u64(StringBuilder* b, u64 n){
|
||||
char buf[32];
|
||||
sprintf(buf, IFWIN("%llu", "%lu"), n);
|
||||
StringBuilder_append_cstr(b, buf);
|
||||
}
|
||||
|
||||
void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
||||
char buf[32];
|
||||
sprintf(buf, "%lf", n);
|
||||
StringBuilder_append_cstr(b, buf);
|
||||
}
|
||||
53
src/string/cstr.c
Executable file
53
src/string/cstr.c
Executable file
@@ -0,0 +1,53 @@
|
||||
#include "string/cstr.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
char* _strcat_malloc(size_t n, cstr str0, ...){
|
||||
va_list argv;
|
||||
va_start(argv, str0);
|
||||
char* heap_ptr = _vstrcat_malloc(n, str0, argv);
|
||||
va_end(argv);
|
||||
return heap_ptr;
|
||||
}
|
||||
|
||||
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv){
|
||||
size_t str0_len = strlen(str0);
|
||||
size_t total_len = str0_len;
|
||||
cstr* const parts = malloc(sizeof(cstr) * n);
|
||||
size_t* const part_lengths = malloc(sizeof(size_t) * n);
|
||||
for(size_t i = 0; i < n; i++){
|
||||
cstr part = va_arg(argv, cstr);
|
||||
size_t length = strlen(part);
|
||||
parts[i] = part;
|
||||
part_lengths[i] = length;
|
||||
total_len += length;
|
||||
}
|
||||
char* const buf = malloc(total_len + 1);
|
||||
memcpy(buf, str0, str0_len);
|
||||
char* walking_ptr = buf + str0_len;
|
||||
for(size_t i = 0; i < n; i++){
|
||||
memcpy(walking_ptr, parts[i], part_lengths[i]);
|
||||
walking_ptr += part_lengths[i];
|
||||
}
|
||||
buf[total_len] = '\0';
|
||||
free(parts);
|
||||
free(part_lengths);
|
||||
return buf;
|
||||
}
|
||||
|
||||
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...){
|
||||
va_list argv;
|
||||
va_start(argv, format);
|
||||
char* NULLABLE(heap_ptr) = vsprintf_malloc(buffer_size, format, argv);
|
||||
va_end(argv);
|
||||
return heap_ptr;
|
||||
}
|
||||
|
||||
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
||||
char* buf = malloc(buffer_size);
|
||||
int r = vsprintf(buf, format, argv);
|
||||
if(r < 0){
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
125
src/string/str.c
Executable file
125
src/string/str.c
Executable file
@@ -0,0 +1,125 @@
|
||||
#include "string/str.h"
|
||||
|
||||
str str_copy(str src){
|
||||
if(src.data == NULL || src.size == 0)
|
||||
return src;
|
||||
|
||||
str nstr = str_construct((char*)malloc(src.size + 1), src.size, true);
|
||||
memcpy(nstr.data, src.data, src.size);
|
||||
nstr.data[nstr.size] = '\0';
|
||||
return nstr;
|
||||
}
|
||||
|
||||
bool str_equals(str s0, str s1){
|
||||
if(s0.size != s1.size)
|
||||
return false;
|
||||
|
||||
for(u32 i = 0; i < s0.size; i++)
|
||||
if(s0.data[i] != s1.data[i])
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
str str_reverse(str s){
|
||||
if(s.data == NULL || s.size == 0)
|
||||
return s;
|
||||
|
||||
str r = str_construct(malloc(s.size), s.size, s.isZeroTerminated);
|
||||
for(u32 i = 0; i < s.size; i++ )
|
||||
r.data[i] = s.data[s.size - i - 1];
|
||||
return r;
|
||||
}
|
||||
|
||||
i32 str_seek(str src, str fragment, u32 startIndex){
|
||||
if(src.size == 0 || fragment.size == 0)
|
||||
return -1;
|
||||
|
||||
for(u32 i = startIndex; i < src.size - fragment.size + 1; i++){
|
||||
for(u32 j = 0;; j++){
|
||||
if(j == fragment.size)
|
||||
return i;
|
||||
if(src.data[i + j] != fragment.data[j])
|
||||
break;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
i32 str_seekReverse(str src, str fragment, u32 startIndex){
|
||||
if(src.size == 0 || fragment.size == 0)
|
||||
return -1;
|
||||
|
||||
if(startIndex > src.size - 1)
|
||||
startIndex = src.size - 1;
|
||||
for(u32 i = startIndex; i >= fragment.size - 1; i--){
|
||||
for(u32 j = 0;; j++){
|
||||
if(j == fragment.size)
|
||||
return i - j + 1;
|
||||
if(src.data[i - j] != fragment.data[fragment.size - 1 - j])
|
||||
break;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
i32 str_seekChar(str src, char c, u32 startIndex){
|
||||
for(u32 i = startIndex; i < src.size; i++){
|
||||
if(src.data[i] == c)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
i32 str_seekCharReverse(str src, char c, u32 startIndex){
|
||||
if(startIndex > src.size - 1)
|
||||
startIndex = src.size - 1;
|
||||
for(u32 i = startIndex; i != (u32)-1; i--){
|
||||
if(src.data[i] == c)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool str_startsWith(str src, str fragment){
|
||||
if(src.size < fragment.size)
|
||||
return false;
|
||||
|
||||
src.size = fragment.size;
|
||||
return str_equals(src, fragment);
|
||||
}
|
||||
|
||||
bool str_endsWith(str src, str fragment){
|
||||
if(src.size < fragment.size)
|
||||
return false;
|
||||
|
||||
src.data = (char*)(src.data + src.size - fragment.size);
|
||||
src.size = fragment.size;
|
||||
return str_equals(src, fragment);
|
||||
}
|
||||
|
||||
u32 str_hash32(str s){
|
||||
u8* ubuf = (u8*)s.data;
|
||||
u32 hash=0;
|
||||
for (u32 i = 0; i < s.size; i++)
|
||||
hash = (hash<<6) + (hash<<16) - hash + ubuf[i];
|
||||
return hash;
|
||||
}
|
||||
|
||||
str str_toUpper(str src){
|
||||
str r = str_copy(src);
|
||||
for (u32 i = 0; i < r.size; i++){
|
||||
if(isAlphabeticalLower(r.data[i]))
|
||||
r.data[i] = r.data[i] - 'a' + 'A';
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
str str_toLower(str src){
|
||||
str r = str_copy(src);
|
||||
for (u32 i = 0; i < r.size; i++){
|
||||
if(isAlphabeticalUpper(r.data[i]))
|
||||
r.data[i] = r.data[i] - 'A' + 'a';
|
||||
}
|
||||
return r;
|
||||
}
|
||||
Reference in New Issue
Block a user