79 lines
2.2 KiB
C
Executable File
79 lines
2.2 KiB
C
Executable File
#pragma once
|
|
|
|
#include "../std.h"
|
|
#include "char.h"
|
|
#include "cstr.h"
|
|
#include "../collections/Array.h"
|
|
|
|
typedef struct str {
|
|
char* data;
|
|
u32 size; // 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, .size = LEN, .isZeroTerminated = ZERO_TERMINATED })
|
|
|
|
static inline str str_from_cstr(cstr s_ptr){
|
|
return str_construct((void*)s_ptr, strlen(s_ptr), true);
|
|
}
|
|
|
|
static inline void str_destroy(str s){
|
|
free(s.data);
|
|
}
|
|
|
|
static inline Array_ str_castTo_Array(str s) {
|
|
return Array_construct_size(s.data, s.size);
|
|
}
|
|
|
|
static inline str Array_castTo_str(Array_ a, bool isZeroTerminated) {
|
|
return str_construct(a.data, a.size, 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.size - n, s.isZeroTerminated);
|
|
}
|