You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
3.7 KiB
152 lines
3.7 KiB
#ifndef LSFS_STRING_H |
|
#define LSFS_STRING_H |
|
|
|
#include <stdbool.h> |
|
|
|
typedef struct lsfs_string { |
|
bool dynamic; |
|
unsigned int length; |
|
char *chars; |
|
} lsfs_string; |
|
|
|
typedef struct lsfs_string_array { |
|
unsigned int length; |
|
lsfs_string *strings; |
|
} lsfs_string_array; |
|
|
|
|
|
static inline lsfs_string lsfs_make_id_string(uint64_t *id) { |
|
return (lsfs_string){ |
|
.dynamic = false, |
|
.length = sizeof(*id), |
|
.chars = (char *)id |
|
}; |
|
} |
|
|
|
static inline lsfs_string lsfs_create_id_string(uint64_t id) { |
|
uint64_t *id_ = malloc(sizeof(id)); |
|
*id_ = id; |
|
return (lsfs_string){ |
|
.dynamic = true, |
|
.length = sizeof(id), |
|
.chars = (char *)id_ |
|
}; |
|
} |
|
|
|
static inline lsfs_string lsfs_make_string(unsigned int length, const char *chars) { |
|
return (lsfs_string){ |
|
.dynamic = false, |
|
.length = length, |
|
.chars = (char *)chars |
|
}; |
|
} |
|
|
|
static inline lsfs_string lsfs_make_string_c(const char *cstring) { |
|
return lsfs_make_string(strlen(cstring), cstring); |
|
} |
|
|
|
static inline lsfs_string lsfs_create_string(unsigned int length, const char *chars) { |
|
|
|
char *copy = malloc(length + 1); // Space for null terminator |
|
memcpy(copy, chars, length); |
|
copy[length] = '\0'; |
|
|
|
return (lsfs_string){ |
|
.dynamic = true, |
|
.length = length, |
|
.chars = copy |
|
}; |
|
} |
|
|
|
static inline lsfs_string lsfs_clone_string(lsfs_string string) { |
|
return lsfs_create_string(string.length, string.chars); |
|
} |
|
|
|
static inline void lsfs_destroy_string(lsfs_string string) { |
|
if(string.dynamic) free(string.chars); |
|
} |
|
|
|
static inline bool lsfs_string_equal(lsfs_string a, lsfs_string b) { |
|
if (a.length != b.length) return false; |
|
return strncmp(a.chars, b.chars, b.length) == 0; |
|
} |
|
|
|
lsfs_string_array lsfs_create_string_array(size_t array_size) { |
|
lsfs_string_array result; |
|
result.length = array_size; |
|
result.strings = malloc(array_size * sizeof(lsfs_string)); |
|
return result; |
|
} |
|
|
|
static inline void lsfs_destroy_string_array(lsfs_string_array array) { |
|
for (unsigned int i = 0; i < array.length; ++i) { |
|
lsfs_destroy_string(array.strings[i]); |
|
} |
|
free(array.strings); |
|
} |
|
|
|
|
|
lsfs_string_array lsfs_string_split(lsfs_string string, char delim, bool keep_delim) { |
|
unsigned int i; |
|
unsigned int last; |
|
|
|
unsigned int count = 0; |
|
|
|
i = 0; |
|
last = 0; |
|
while(i < string.length) { |
|
if (string.chars[i] == delim) { |
|
if (i > last+1) { |
|
++count; |
|
} |
|
last = i; |
|
} |
|
++i; |
|
} |
|
if (i > last+1) { |
|
++count; |
|
} |
|
|
|
lsfs_string_array result = lsfs_create_string_array(count); |
|
|
|
unsigned int insert_index = 0; |
|
int k = keep_delim ? 0 : 1; |
|
|
|
i = 0; |
|
last = 0; |
|
while(i < string.length) { |
|
if (string.chars[i] == delim) { |
|
if (i > last+1) { |
|
result.strings[insert_index++] = lsfs_create_string(i-(last+k), string.chars+(last+k)); |
|
} |
|
last = i; |
|
} |
|
++i; |
|
} |
|
if (i > last+1) { |
|
result.strings[insert_index++] = lsfs_create_string(i-(last+k), string.chars+(last+k)); |
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
static inline lsfs_string_array lsfs_string_split_c(const char *string, char delim, bool keep_delim) { |
|
return lsfs_string_split(lsfs_make_string_c(string), delim, keep_delim); |
|
} |
|
|
|
char *dbg_strarr(lsfs_string_array strings) { |
|
static char temp[8192]; |
|
memset(temp, 0, sizeof(temp)); |
|
|
|
int where = 0; |
|
where += sprintf(temp+where, "{"); |
|
for (unsigned int i = 0; i < strings.length; ++i) { |
|
where += sprintf(temp+where, "<'%.*s'>", strings.strings[i].length, strings.strings[i].chars); |
|
} |
|
where += sprintf(temp+where, "}"); |
|
|
|
return temp; // @Leak |
|
} |
|
|
|
#endif |