124 lines
4.3 KiB
C
124 lines
4.3 KiB
C
#ifndef STRING_H
|
|
#define STRING_H
|
|
|
|
struct string_array {
|
|
u64 count;
|
|
struct string *strings;
|
|
};
|
|
|
|
/* ========================== *
|
|
* Conversion
|
|
* ========================== */
|
|
|
|
struct string string_from_char(struct arena *arena, char c);
|
|
struct string string_from_uint(struct arena *arena, u64 n, u32 base);
|
|
struct string string_from_int(struct arena *arena, i64 n, u32 base);
|
|
struct string string_from_ptr(struct arena *arena, void *ptr);
|
|
struct string string_from_float(struct arena *arena, f64 f, u32 precision);
|
|
|
|
/* ========================== *
|
|
* String operations
|
|
* ========================== */
|
|
|
|
struct string string_copy(struct arena *arena, struct string src);
|
|
struct string string_copy_buff(struct buffer dest_buff, struct string src);
|
|
struct string string_repeat(struct arena *arena, struct string src, u64 count);
|
|
struct string string_cat(struct arena *arena, struct string str1, struct string str2);
|
|
struct string_array string_split(struct arena *arena, struct string str, struct string delim);
|
|
struct string string_indent(struct arena *arena, struct string str, u32 indent);
|
|
b32 string_eq(struct string str1, struct string str2);
|
|
b32 string_contains(struct string str, struct string substring);
|
|
b32 string_starts_with(struct string str, struct string substring);
|
|
b32 string_ends_with(struct string str, struct string substring);
|
|
|
|
/* ========================== *
|
|
* Format
|
|
* ========================== */
|
|
|
|
#define DEFAULT_FMT_PRECISION 5
|
|
|
|
enum fmt_type {
|
|
FMT_TYPE_NONE,
|
|
|
|
/* Arbitrary magic numbers to avoid accidental non-fmt arguments */
|
|
FMT_TYPE_CHAR = 0xC6ECD1,
|
|
FMT_TYPE_STR = 0xC6ECD2,
|
|
FMT_TYPE_UINT = 0xC6ECD3,
|
|
FMT_TYPE_SINT = 0xC6ECD4,
|
|
FMT_TYPE_HEX = 0xC6ECD5,
|
|
FMT_TYPE_PTR = 0xC6ECD6,
|
|
FMT_TYPE_FLOAT = 0xC6ECD7,
|
|
|
|
FMT_TYPE_END = 0xAD32F3
|
|
};
|
|
|
|
struct fmt_arg {
|
|
enum fmt_type type;
|
|
union {
|
|
u8 c;
|
|
struct string string;
|
|
u64 uint;
|
|
i64 sint;
|
|
void *ptr;
|
|
struct {
|
|
f64 val;
|
|
u32 precision;
|
|
} f;
|
|
} value;
|
|
};
|
|
|
|
#define FMT_END (struct fmt_arg) {.type = FMT_TYPE_END}
|
|
|
|
#define FMT_CHAR(v) (struct fmt_arg) {.type = FMT_TYPE_CHAR, .value.c = (v)}
|
|
#define FMT_STR(v) (struct fmt_arg) {.type = FMT_TYPE_STR, .value.string = (v)}
|
|
#define FMT_UINT(v) (struct fmt_arg) {.type = FMT_TYPE_UINT, .value.uint = (v)}
|
|
#define FMT_SINT(v) (struct fmt_arg) {.type = FMT_TYPE_SINT, .value.sint = (v)}
|
|
#define FMT_HEX(v) (struct fmt_arg) {.type = FMT_TYPE_HEX, .value.uint = (v)}
|
|
#define FMT_PTR(v) (struct fmt_arg) {.type = FMT_TYPE_PTR, .value.ptr = (v)}
|
|
#define FMT_FLOAT(v) FMT_FLOAT_P(v, DEFAULT_FMT_PRECISION)
|
|
#define FMT_FLOAT_P(v, p) (struct fmt_arg) {.type = FMT_TYPE_FLOAT, .value.f.val = (v), .value.f.precision = p}
|
|
|
|
#define string_format(arena, fmt, ...) _string_format(arena, fmt, __VA_ARGS__, FMT_END)
|
|
struct string _string_format(struct arena *arena, struct string fmt, ...);
|
|
struct string string_formatv(struct arena *arena, struct string fmt, va_list args);
|
|
|
|
/* ========================== *
|
|
* Unicode
|
|
* ========================== */
|
|
|
|
struct string_codepoint_iter {
|
|
u32 codepoint;
|
|
|
|
/* Internal */
|
|
struct string src;
|
|
u64 pos;
|
|
};
|
|
|
|
struct string_codepoint_iter string_codepoint_iter_begin(struct string str);
|
|
b32 string_codepoint_iter_next(struct string_codepoint_iter *iter);
|
|
void string_codepoint_iter_end(struct string_codepoint_iter *iter);
|
|
|
|
struct string string_from_string16(struct arena *arena, struct string16 str16);
|
|
struct string string_from_string32(struct arena *arena, struct string32 str32);
|
|
|
|
struct string16 string16_from_string(struct arena *arena, struct string str8);
|
|
struct string32 string32_from_string(struct arena *arena, struct string str8);
|
|
|
|
/* ========================== *
|
|
* Legacy strings
|
|
* ========================== */
|
|
|
|
u64 cstr_len(char *cstr);
|
|
char *cstr_from_string(struct arena *arena, struct string src);
|
|
char *cstr_buff_from_string(struct buffer dest_buff, struct string src);
|
|
struct string string_from_cstr(char *cstr);
|
|
struct string string_from_cstr_len(char *cstr, u64 len);
|
|
|
|
u64 wstr_len(wchar_t *wstr);
|
|
wchar_t *wstr_from_string(struct arena *arena, struct string src);
|
|
wchar_t *wstr_from_string16(struct arena *arena, struct string16 src);
|
|
struct string string_from_wstr(struct arena *arena, wchar_t *wstr);
|
|
struct string16 string16_from_wstr(wchar_t *wstr);
|
|
|
|
#endif
|