96 lines
2.8 KiB
C
Executable File
96 lines
2.8 KiB
C
Executable File
#pragma once
|
|
|
|
#include "../std.h"
|
|
#include "char.h"
|
|
#include "cstr.h"
|
|
#include "../collections/Array.h"
|
|
#include "../collections/Array_impl/Array_char.h"
|
|
#include "../collections/Array_impl/Array_u8.h"
|
|
|
|
typedef struct str {
|
|
char* data;
|
|
u32 len; // size of data in bytes without \0
|
|
bool isZeroTerminated;
|
|
} str;
|
|
|
|
#define FMT_str "%.*s"
|
|
|
|
/// creates str from a string literal
|
|
#define STR(LITERAL) str_construct(LITERAL, ARRAY_LEN(LITERAL) - 1, true)
|
|
|
|
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .len = LEN, .isZeroTerminated = ZERO_TERMINATED })
|
|
|
|
static inline str str_from_cstr(cstr s_ptr){
|
|
return str_construct((void*)s_ptr, strlen(s_ptr), true);
|
|
}
|
|
|
|
/// destroy str with .data allocated on heap
|
|
static inline void str_destroy(str self){
|
|
free(self.data);
|
|
}
|
|
|
|
/// destroy str allocated on heap with .data allocated on heap
|
|
static inline void str_free(str* self){
|
|
if(!self)
|
|
return;
|
|
free(self->data);
|
|
free(self);
|
|
}
|
|
|
|
static inline Array(char) str_castTo_Array_char(str s) {
|
|
return Array_char_construct(s.data, s.len);
|
|
}
|
|
static inline Array(u8) str_castTo_Array_u8(str s) {
|
|
return Array_u8_construct((void*)s.data, s.len);
|
|
}
|
|
|
|
static inline str Array_char_castTo_str(Array(char) a, bool isZeroTerminated) {
|
|
return str_construct(a.data, a.len, isZeroTerminated);
|
|
}
|
|
static inline str Array_u8_castTo_str(Array(u8) a, bool isZeroTerminated) {
|
|
return str_construct((void*)a.data, a.len, isZeroTerminated);
|
|
}
|
|
|
|
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(const str self);
|
|
|
|
/// compares two strings, NullPtr-friendly
|
|
bool str_equals(const str self, const str other);
|
|
|
|
/// allocates new string which is reversed variant of <s>
|
|
str str_reverse(str s);
|
|
|
|
i32 str_seek(const str src, const str fragment, u32 startIndex);
|
|
i32 str_seekReverse(const str src, const str fragment, u32 startIndex);
|
|
|
|
i32 str_seekChar(const str src, char c, u32 startIndex);
|
|
i32 str_seekCharReverse(const str src, char c, u32 startIndex);
|
|
|
|
bool str_startsWith(const str src, const str fragment);
|
|
bool str_endsWith(const str src, const str fragment);
|
|
|
|
/// @brief calculates string hash using sdbm32 algorythm (something like lightweight crc32)
|
|
/// @return non-cryptografic hash of the string
|
|
u32 str_hash32(const str s);
|
|
|
|
str str_toUpper(const str src);
|
|
str str_toLower(const str src);
|
|
|
|
str hex_to_str(Array(u8) buf, bool uppercase);
|
|
|
|
/// @brief removes blank characters from start and end of the line
|
|
void str_trim(str* line, bool set_zero_at_end);
|
|
|
|
|
|
///@return s[0..n]
|
|
static inline str str_sliceBefore(str s, u32 n){
|
|
return str_construct(s.data, n, false);
|
|
}
|
|
|
|
///@return s[n...]
|
|
static inline str str_sliceAfter(str s, u32 n){
|
|
return str_construct(s.data + n, s.len - n, s.isZeroTerminated);
|
|
}
|