2023-08-28 20:46:50 -07:00
|
|
|
#ifdef GEN_INTELLISENSE_DIRECTIVES
|
|
|
|
# pragma once
|
|
|
|
# include "hashing.hpp"
|
|
|
|
#endif
|
2023-08-21 17:30:13 -07:00
|
|
|
|
2023-08-09 15:47:59 -07:00
|
|
|
#pragma region Strings
|
2023-07-25 20:00:57 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
struct Str;
|
2024-12-03 15:47:12 -08:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
Str to_str_from_c_str (char const* bad_string);
|
|
|
|
bool str_are_equal (Str lhs, Str rhs);
|
|
|
|
char const* str_back (Str str);
|
|
|
|
bool str_contains (Str str, Str substring);
|
|
|
|
Str str_duplicate (Str str, AllocatorInfo allocator);
|
|
|
|
b32 str_starts_with (Str str, Str substring);
|
|
|
|
Str str_visualize_whitespace(Str str, AllocatorInfo allocator);
|
2024-12-04 08:01:53 -08:00
|
|
|
|
2023-07-24 15:19:37 -07:00
|
|
|
// Constant string with length.
|
2024-12-12 09:55:15 -08:00
|
|
|
struct Str
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-12-01 00:06:30 -08:00
|
|
|
ssize Len;
|
2023-07-24 15:19:37 -07:00
|
|
|
char const* Ptr;
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
#if GEN_COMPILER_CPP
|
|
|
|
forceinline operator char const* () const { return Ptr; }
|
|
|
|
forceinline char const& operator[]( ssize index ) const { return Ptr[index]; }
|
2024-12-03 15:47:12 -08:00
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
#if ! GEN_C_LIKE_CPP
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline bool is_equal (Str rhs) const { return str_are_equal(* this, rhs); }
|
|
|
|
forceinline char const* back () const { return str_back(* this); }
|
|
|
|
forceinline bool contains (Str substring) const { return str_contains(* this, substring); }
|
|
|
|
forceinline Str duplicate (AllocatorInfo allocator) const { return str_duplicate(* this, allocator); }
|
|
|
|
forceinline b32 starts_with (Str substring) const { return str_starts_with(* this, substring); }
|
|
|
|
forceinline Str visualize_whitespace(AllocatorInfo allocator) const { return str_visualize_whitespace(* this, allocator); }
|
2024-12-03 17:42:35 -08:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-07-24 15:19:37 -07:00
|
|
|
};
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
#define cast_to_str( str ) * rcast( Str*, (str) - sizeof(ssize) )
|
2024-12-04 23:53:14 -08:00
|
|
|
|
|
|
|
#ifndef txt
|
|
|
|
# if GEN_COMPILER_CPP
|
2024-12-12 09:55:15 -08:00
|
|
|
# define txt( text ) Str { sizeof( text ) - 1, ( text ) }
|
2024-12-04 23:53:14 -08:00
|
|
|
# else
|
2024-12-12 09:55:15 -08:00
|
|
|
# define txt( text ) (Str){ sizeof( text ) - 1, ( text ) }
|
2024-12-04 23:53:14 -08:00
|
|
|
# endif
|
|
|
|
#endif
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
GEN_API_C_BEGIN
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline char const* str_begin(Str str) { return str.Ptr; }
|
|
|
|
forceinline char const* str_end (Str str) { return str.Ptr + str.Len; }
|
|
|
|
forceinline char const* str_next (Str str, char const* iter) { return iter + 1; }
|
2024-12-04 08:01:53 -08:00
|
|
|
GEN_API_C_END
|
|
|
|
|
2024-12-04 12:00:37 -08:00
|
|
|
#if GEN_COMPILER_CPP
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline char const* begin(Str str) { return str.Ptr; }
|
|
|
|
forceinline char const* end (Str str) { return str.Ptr + str.Len; }
|
|
|
|
forceinline char const* next (Str str, char const* iter) { return iter + 1; }
|
2024-12-04 08:01:53 -08:00
|
|
|
#endif
|
2024-12-03 15:47:12 -08:00
|
|
|
|
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool str_are_equal(Str lhs, Str rhs)
|
2024-12-03 15:47:12 -08:00
|
|
|
{
|
|
|
|
if (lhs.Len != rhs.Len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (ssize idx = 0; idx < lhs.Len; ++idx)
|
2024-12-04 23:53:14 -08:00
|
|
|
if (lhs.Ptr[idx] != rhs.Ptr[idx])
|
2024-12-03 15:47:12 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
char const* str_back(Str str) {
|
2024-12-03 15:47:12 -08:00
|
|
|
return & str.Ptr[str.Len - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool str_contains(Str str, Str substring)
|
2024-12-03 15:47:12 -08:00
|
|
|
{
|
|
|
|
if (substring.Len > str.Len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ssize main_len = str.Len;
|
|
|
|
ssize sub_len = substring.Len;
|
|
|
|
for (ssize idx = 0; idx <= main_len - sub_len; ++idx)
|
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
if (c_str_compare_len(str.Ptr + idx, substring.Ptr, sub_len) == 0)
|
2024-12-03 15:47:12 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-25 02:01:37 -07:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
b32 str_starts_with(Str str, Str substring) {
|
2024-12-03 15:47:12 -08:00
|
|
|
if (substring.Len > str.Len)
|
|
|
|
return false;
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
b32 result = c_str_compare_len(str.Ptr, substring.Ptr, substring.Len) == 0;
|
2024-12-03 15:47:12 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
Str to_str_from_c_str( char const* bad_str ) {
|
|
|
|
Str result = { c_str_len( bad_str ), bad_str };
|
2024-12-04 23:53:14 -08:00
|
|
|
return result;
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
// Dynamic StrBuilder
|
2023-07-24 15:19:37 -07:00
|
|
|
// This is directly based off the ZPL string api.
|
|
|
|
// They used a header pattern
|
|
|
|
// I kept it for simplicty of porting but its not necessary to keep it that way.
|
2024-12-12 09:55:15 -08:00
|
|
|
#pragma region StrBuilder
|
|
|
|
struct StrBuilderHeader;
|
2024-12-03 15:47:12 -08:00
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
#if GEN_COMPILER_C
|
2024-12-12 09:55:15 -08:00
|
|
|
typedef char* StrBuilder;
|
2024-12-03 15:47:12 -08:00
|
|
|
#else
|
2024-12-12 09:55:15 -08:00
|
|
|
struct StrBuilder;
|
2024-12-03 15:47:12 -08:00
|
|
|
#endif
|
2024-12-01 00:06:30 -08:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline usize strbuilder_grow_formula(usize value);
|
|
|
|
|
|
|
|
StrBuilder strbuilder_make_c_str (AllocatorInfo allocator, char const* str);
|
|
|
|
StrBuilder strbuilder_make_str (AllocatorInfo allocator, Str str);
|
|
|
|
StrBuilder strbuilder_make_reserve (AllocatorInfo allocator, ssize capacity);
|
|
|
|
StrBuilder strbuilder_make_length (AllocatorInfo allocator, char const* str, ssize length);
|
|
|
|
StrBuilder strbuilder_fmt (AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...);
|
|
|
|
StrBuilder strbuilder_fmt_buf (AllocatorInfo allocator, char const* fmt, ...);
|
|
|
|
StrBuilder strbuilder_join (AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue);
|
|
|
|
bool strbuilder_are_equal (StrBuilder const lhs, StrBuilder const rhs);
|
|
|
|
bool strbuilder_are_equal_str (StrBuilder const lhs, Str rhs);
|
|
|
|
bool strbuilder_make_space_for (StrBuilder* str, char const* to_append, ssize add_len);
|
|
|
|
bool strbuilder_append_char (StrBuilder* str, char c);
|
|
|
|
bool strbuilder_append_c_str (StrBuilder* str, char const* c_str_to_append);
|
|
|
|
bool strbuilder_append_c_str_len (StrBuilder* str, char const* c_str_to_append, ssize length);
|
|
|
|
bool strbuilder_append_str (StrBuilder* str, Str c_str_to_append);
|
|
|
|
bool strbuilder_append_string (StrBuilder* str, StrBuilder const other);
|
|
|
|
bool strbuilder_append_fmt (StrBuilder* str, char const* fmt, ...);
|
|
|
|
ssize strbuilder_avail_space (StrBuilder const str);
|
|
|
|
char* strbuilder_back (StrBuilder str);
|
|
|
|
bool strbuilder_contains_str (StrBuilder const str, Str substring);
|
|
|
|
bool strbuilder_contains_string (StrBuilder const str, StrBuilder const substring);
|
|
|
|
ssize strbuilder_capacity (StrBuilder const str);
|
|
|
|
void strbuilder_clear (StrBuilder str);
|
|
|
|
StrBuilder strbuilder_duplicate (StrBuilder const str, AllocatorInfo allocator);
|
|
|
|
void strbuilder_free (StrBuilder* str);
|
|
|
|
StrBuilderHeader* strbuilder_get_header (StrBuilder str);
|
|
|
|
ssize strbuilder_length (StrBuilder const str);
|
|
|
|
b32 strbuilder_starts_with_str (StrBuilder const str, Str substring);
|
|
|
|
b32 strbuilder_starts_with_string (StrBuilder const str, StrBuilder substring);
|
|
|
|
void strbuilder_skip_line (StrBuilder str);
|
|
|
|
void strbuilder_strip_space (StrBuilder str);
|
|
|
|
Str strbuilder_to_str (StrBuilder str);
|
|
|
|
void strbuilder_trim (StrBuilder str, char const* cut_set);
|
|
|
|
void strbuilder_trim_space (StrBuilder str);
|
|
|
|
StrBuilder strbuilder_visualize_whitespace(StrBuilder const str);
|
|
|
|
|
|
|
|
struct StrBuilderHeader {
|
2024-11-30 11:13:30 -08:00
|
|
|
AllocatorInfo Allocator;
|
|
|
|
ssize Capacity;
|
|
|
|
ssize Length;
|
|
|
|
};
|
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
#if GEN_COMPILER_CPP
|
2024-12-12 09:55:15 -08:00
|
|
|
struct StrBuilder
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-11-30 11:13:30 -08:00
|
|
|
char* Data;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 00:06:30 -08:00
|
|
|
forceinline operator char*() { return Data; }
|
|
|
|
forceinline operator char const*() const { return Data; }
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline operator Str() const { return { strbuilder_length(* this), Data }; }
|
2024-12-01 00:06:30 -08:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder const& operator=(StrBuilder const& other) const {
|
2024-12-01 00:06:30 -08:00
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder* this_ = ccast(StrBuilder*, this);
|
2024-12-01 00:06:30 -08:00
|
|
|
this_->Data = other.Data;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
forceinline char& operator[](ssize index) { return Data[index]; }
|
2024-12-01 00:06:30 -08:00
|
|
|
forceinline char const& operator[](ssize index) const { return Data[index]; }
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline bool operator==(std::nullptr_t) const { return Data == nullptr; }
|
|
|
|
forceinline bool operator!=(std::nullptr_t) const { return Data != nullptr; }
|
|
|
|
friend forceinline bool operator==(std::nullptr_t, const StrBuilder str) { return str.Data == nullptr; }
|
|
|
|
friend forceinline bool operator!=(std::nullptr_t, const StrBuilder str) { return str.Data != nullptr; }
|
2024-12-03 15:47:12 -08:00
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
#if ! GEN_C_LIKE_CPP
|
2024-12-01 00:06:30 -08:00
|
|
|
forceinline char* begin() const { return Data; }
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline char* end() const { return Data + strbuilder_length(* this); }
|
2024-12-01 00:06:30 -08:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
#pragma region Member Mapping
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline static StrBuilder make(AllocatorInfo allocator, char const* str) { return strbuilder_make_c_str(allocator, str); }
|
|
|
|
forceinline static StrBuilder make(AllocatorInfo allocator, Str str) { return strbuilder_make_str(allocator, str); }
|
|
|
|
forceinline static StrBuilder make_reserve(AllocatorInfo allocator, ssize cap) { return strbuilder_make_reserve(allocator, cap); }
|
|
|
|
forceinline static StrBuilder make_length(AllocatorInfo a, char const* s, ssize l) { return strbuilder_make_length(a, s, l); }
|
|
|
|
forceinline static StrBuilder join(AllocatorInfo a, char const** p, ssize n, char const* g) { return strbuilder_join(a, p, n, g); }
|
|
|
|
forceinline static usize grow_formula(usize value) { return strbuilder_grow_formula(value); }
|
2023-07-24 15:19:37 -07:00
|
|
|
|
|
|
|
static
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) {
|
2024-11-30 11:13:30 -08:00
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize res = c_str_fmt_va(buf, buf_size, fmt, va) - 1;
|
2024-11-30 11:13:30 -08:00
|
|
|
va_end(va);
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_make_length(allocator, buf, res);
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder fmt_buf(AllocatorInfo allocator, char const* fmt, ...) {
|
2024-11-30 11:13:30 -08:00
|
|
|
local_persist thread_local
|
|
|
|
char buf[GEN_PRINTF_MAXLEN] = { 0 };
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize res = c_str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va) - 1;
|
2024-11-30 11:13:30 -08:00
|
|
|
va_end(va);
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_make_length(allocator, buf, res);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline bool make_space_for(char const* str, ssize add_len) { return strbuilder_make_space_for(this, str, add_len); }
|
|
|
|
forceinline bool append(char c) { return strbuilder_append_char(this, c); }
|
|
|
|
forceinline bool append(char const* str) { return strbuilder_append_c_str(this, str); }
|
|
|
|
forceinline bool append(char const* str, ssize length) { return strbuilder_append_c_str_len(this, str, length); }
|
|
|
|
forceinline bool append(Str str) { return strbuilder_append_str(this, str); }
|
|
|
|
forceinline bool append(const StrBuilder other) { return strbuilder_append_string(this, other); }
|
|
|
|
forceinline ssize avail_space() const { return strbuilder_avail_space(* this); }
|
|
|
|
forceinline char* back() { return strbuilder_back(* this); }
|
|
|
|
forceinline bool contains(Str substring) const { return strbuilder_contains_str(* this, substring); }
|
|
|
|
forceinline bool contains(StrBuilder const& substring) const { return strbuilder_contains_string(* this, substring); }
|
|
|
|
forceinline ssize capacity() const { return strbuilder_capacity(* this); }
|
|
|
|
forceinline void clear() { strbuilder_clear(* this); }
|
|
|
|
forceinline StrBuilder duplicate(AllocatorInfo allocator) const { return strbuilder_duplicate(* this, allocator); }
|
|
|
|
forceinline void free() { strbuilder_free(this); }
|
|
|
|
forceinline bool is_equal(StrBuilder const& other) const { return strbuilder_are_equal(* this, other); }
|
|
|
|
forceinline bool is_equal(Str other) const { return strbuilder_are_equal_str(* this, other); }
|
|
|
|
forceinline ssize length() const { return strbuilder_length(* this); }
|
|
|
|
forceinline b32 starts_with(Str substring) const { return strbuilder_starts_with_str(* this, substring); }
|
|
|
|
forceinline b32 starts_with(StrBuilder substring) const { return strbuilder_starts_with_string(* this, substring); }
|
|
|
|
forceinline void skip_line() { strbuilder_skip_line(* this); }
|
|
|
|
forceinline void strip_space() { strbuilder_strip_space(* this); }
|
|
|
|
forceinline Str to_str() { return { strbuilder_length(*this), Data}; }
|
|
|
|
forceinline void trim(char const* cut_set) { strbuilder_trim(* this, cut_set); }
|
|
|
|
forceinline void trim_space() { strbuilder_trim_space(* this); }
|
|
|
|
forceinline StrBuilder visualize_whitespace() const { return strbuilder_visualize_whitespace(* this); }
|
|
|
|
forceinline StrBuilderHeader& get_header() { return * strbuilder_get_header(* this); }
|
2024-11-30 11:13:30 -08:00
|
|
|
|
|
|
|
bool append_fmt(char const* fmt, ...) {
|
|
|
|
ssize res;
|
|
|
|
char buf[GEN_PRINTF_MAXLEN] = { 0 };
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-12 09:55:15 -08:00
|
|
|
res = c_str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1;
|
2024-11-30 11:13:30 -08:00
|
|
|
va_end(va);
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_append_c_str_len(this, buf, res);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
|
|
|
#pragma endregion Member Mapping
|
2024-12-06 21:21:09 -08:00
|
|
|
#endif
|
2024-11-30 11:13:30 -08:00
|
|
|
};
|
2024-12-01 00:06:30 -08:00
|
|
|
#endif
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline char* strbuilder_begin(StrBuilder str) { return ((char*) str); }
|
|
|
|
forceinline char* strbuilder_end (StrBuilder str) { return ((char*) str + strbuilder_length(str)); }
|
|
|
|
forceinline char* strbuilder_next (StrBuilder str, char const* iter) { return ((char*) iter + 1); }
|
2024-12-04 08:01:53 -08:00
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline char* begin(StrBuilder str) { return ((char*) str); }
|
|
|
|
forceinline char* end (StrBuilder str) { return ((char*) str + strbuilder_length(str)); }
|
|
|
|
forceinline char* next (StrBuilder str, char* iter) { return ((char*) iter + 1); }
|
2024-12-04 08:01:53 -08:00
|
|
|
#endif
|
2024-12-03 15:47:12 -08:00
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP
|
2024-12-12 09:55:15 -08:00
|
|
|
forceinline bool make_space_for(StrBuilder& str, char const* to_append, ssize add_len);
|
|
|
|
forceinline bool append(StrBuilder& str, char c);
|
|
|
|
forceinline bool append(StrBuilder& str, char const* c_str_to_append);
|
|
|
|
forceinline bool append(StrBuilder& str, char const* c_str_to_append, ssize length);
|
|
|
|
forceinline bool append(StrBuilder& str, Str c_str_to_append);
|
|
|
|
forceinline bool append(StrBuilder& str, const StrBuilder other);
|
|
|
|
forceinline bool append_fmt(StrBuilder& str, char const* fmt, ...);
|
|
|
|
forceinline char& back(StrBuilder& str);
|
|
|
|
forceinline void clear(StrBuilder& str);
|
|
|
|
forceinline void free(StrBuilder& str);
|
2024-12-01 20:35:58 -08:00
|
|
|
#endif
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
usize strbuilder_grow_formula(usize value) {
|
2024-11-30 11:13:30 -08:00
|
|
|
// Using a very aggressive growth formula to reduce time mem_copying with recursive calls to append in this library.
|
|
|
|
return 4 * value + 8;
|
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder strbuilder_make_c_str(AllocatorInfo allocator, char const* str) {
|
|
|
|
ssize length = str ? c_str_len(str) : 0;
|
|
|
|
return strbuilder_make_length(allocator, str, length);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder strbuilder_make_str(AllocatorInfo allocator, Str str) {
|
|
|
|
return strbuilder_make_length(allocator, str.Ptr, str.Len);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder strbuilder_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) {
|
2024-11-30 11:13:30 -08:00
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize res = c_str_fmt_va(buf, buf_size, fmt, va) - 1;
|
2024-11-30 11:13:30 -08:00
|
|
|
va_end(va);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_make_length(allocator, buf, res);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder strbuilder_fmt_buf(AllocatorInfo allocator, char const* fmt, ...)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
|
|
|
local_persist thread_local
|
2024-12-04 23:53:14 -08:00
|
|
|
PrintF_Buffer buf = struct_init(PrintF_Buffer, {0});
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize res = c_str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va) -1;
|
2024-11-30 11:13:30 -08:00
|
|
|
va_end(va);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_make_length(allocator, buf, res);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder strbuilder_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder result = strbuilder_make_c_str(allocator, "");
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
for (ssize idx = 0; idx < num_parts; ++idx)
|
2024-05-05 18:53:22 -07:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_c_str(& result, parts[idx]);
|
2024-05-05 18:53:22 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
if (idx < num_parts - 1)
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_c_str(& result, glue);
|
2024-05-05 18:53:22 -07:00
|
|
|
}
|
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
return result;
|
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_append_char(StrBuilder* str, char c) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_append_c_str_len( str, (char const*)& c, (ssize)1);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-09-03 17:29:45 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_append_c_str(StrBuilder* str, char const* c_str_to_append) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_append_c_str_len(str, c_str_to_append, c_str_len(c_str_to_append));
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_append_c_str_len(StrBuilder* str, char const* c_str_to_append, ssize append_length)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-12 09:55:15 -08:00
|
|
|
if ( rcast(sptr, c_str_to_append) > 0)
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize curr_len = strbuilder_length(* str);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
if ( ! strbuilder_make_space_for(str, c_str_to_append, append_length))
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilderHeader* header = strbuilder_get_header(* str);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 20:35:58 -08:00
|
|
|
char* Data = * str;
|
2024-12-12 09:55:15 -08:00
|
|
|
mem_copy( Data + curr_len, c_str_to_append, append_length);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 20:35:58 -08:00
|
|
|
Data[curr_len + append_length] = '\0';
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 20:35:58 -08:00
|
|
|
header->Length = curr_len + append_length;
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
2024-12-12 09:55:15 -08:00
|
|
|
return c_str_to_append != nullptr;
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_append_str(StrBuilder* str, Str c_str_to_append) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_append_c_str_len(str, c_str_to_append.Ptr, c_str_to_append.Len);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_append_string(StrBuilder* str, StrBuilder const other) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_append_c_str_len(str, (char const*)other, strbuilder_length(other));
|
2024-12-01 00:06:30 -08:00
|
|
|
}
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_append_fmt(StrBuilder* str, char const* fmt, ...) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-01 00:06:30 -08:00
|
|
|
ssize res;
|
|
|
|
char buf[GEN_PRINTF_MAXLEN] = { 0 };
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-12 09:55:15 -08:00
|
|
|
res = c_str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1;
|
2024-12-01 00:06:30 -08:00
|
|
|
va_end(va);
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_append_c_str_len(str, (char const*)buf, res);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-08-08 19:14:58 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_are_equal_string(StrBuilder const lhs, StrBuilder const rhs)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
if (strbuilder_length(lhs) != strbuilder_length(rhs))
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2024-11-29 11:50:54 -08:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
for (ssize idx = 0; idx < strbuilder_length(lhs); ++idx)
|
2024-11-30 11:13:30 -08:00
|
|
|
if (lhs[idx] != rhs[idx])
|
2024-11-29 11:50:54 -08:00
|
|
|
return false;
|
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
return true;
|
|
|
|
}
|
2024-11-29 11:50:54 -08:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_are_equal_str(StrBuilder const lhs, Str rhs)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
if (strbuilder_length(lhs) != (rhs.Len))
|
2024-11-29 11:50:54 -08:00
|
|
|
return false;
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
for (ssize idx = 0; idx < strbuilder_length(lhs); ++idx)
|
2024-12-01 15:50:37 -08:00
|
|
|
if (lhs[idx] != rhs.Ptr[idx])
|
2024-11-29 11:50:54 -08:00
|
|
|
return false;
|
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
return true;
|
|
|
|
}
|
2024-11-29 11:50:54 -08:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize strbuilder_avail_space(StrBuilder const str) {
|
|
|
|
StrBuilderHeader const* header = rcast(StrBuilderHeader const*, scast(char const*, str) - sizeof(StrBuilderHeader));
|
2024-12-01 20:35:58 -08:00
|
|
|
return header->Capacity - header->Length;
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2024-11-29 11:50:54 -08:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
char* strbuilder_back(StrBuilder str) {
|
|
|
|
return & (str)[strbuilder_length(str) - 1];
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2024-11-29 11:50:54 -08:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_contains_StrC(StrBuilder const str, Str substring)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilderHeader const* header = rcast(StrBuilderHeader const*, scast(char const*, str) - sizeof(StrBuilderHeader));
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 20:35:58 -08:00
|
|
|
if (substring.Len > header->Length)
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 20:35:58 -08:00
|
|
|
ssize main_len = header->Length;
|
|
|
|
ssize sub_len = substring.Len;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
for (ssize idx = 0; idx <= main_len - sub_len; ++idx)
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
if (c_str_compare_len(str + idx, substring.Ptr, sub_len) == 0)
|
2024-11-30 11:13:30 -08:00
|
|
|
return true;
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_contains_string(StrBuilder const str, StrBuilder const substring)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilderHeader const* header = rcast(StrBuilderHeader const*, scast(char const*, str) - sizeof(StrBuilderHeader));
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
if (strbuilder_length(substring) > header->Length)
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 20:35:58 -08:00
|
|
|
ssize main_len = header->Length;
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize sub_len = strbuilder_length(substring);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
for (ssize idx = 0; idx <= main_len - sub_len; ++idx)
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
if (c_str_compare_len(str + idx, substring, sub_len) == 0)
|
2024-11-30 11:13:30 -08:00
|
|
|
return true;
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
|
|
|
}
|
2024-05-05 18:53:22 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize strbuilder_capacity(StrBuilder const str) {
|
|
|
|
StrBuilderHeader const* header = rcast(StrBuilderHeader const*, scast(char const*, str) - sizeof(StrBuilderHeader));
|
2024-12-07 14:17:02 -08:00
|
|
|
return header->Capacity;
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2024-05-05 18:53:22 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
void strbuilder_clear(StrBuilder str) {
|
|
|
|
strbuilder_get_header(str)->Length = 0;
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2024-05-05 18:53:22 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder strbuilder_duplicate(StrBuilder const str, AllocatorInfo allocator) {
|
|
|
|
return strbuilder_make_length(allocator, str, strbuilder_length(str));
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2024-05-05 18:53:22 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
void strbuilder_free(StrBuilder* str) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
|
|
|
if (! (* str))
|
|
|
|
return;
|
2023-08-21 17:30:13 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilderHeader* header = strbuilder_get_header(* str);
|
2024-12-04 08:01:53 -08:00
|
|
|
allocator_free(header->Allocator, header);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-08-21 17:30:13 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilderHeader* strbuilder_get_header(StrBuilder str) {
|
|
|
|
return (StrBuilderHeader*)(scast(char*, str) - sizeof(StrBuilderHeader));
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-08-21 17:30:13 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize strbuilder_length(StrBuilder const str)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilderHeader const* header = rcast(StrBuilderHeader const*, scast(char const*, str) - sizeof(StrBuilderHeader));
|
2024-12-07 14:17:02 -08:00
|
|
|
return header->Length;
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-08-21 17:30:13 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
bool strbuilder_make_space_for(StrBuilder* str, char const* to_append, ssize add_len)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
ssize available = strbuilder_avail_space(* str);
|
2023-08-21 17:30:13 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
if (available >= add_len) {
|
|
|
|
return true;
|
2023-08-26 08:55:05 -07:00
|
|
|
}
|
2024-11-30 11:13:30 -08:00
|
|
|
else
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-11-30 11:13:30 -08:00
|
|
|
ssize new_len, old_size, new_size;
|
|
|
|
void* ptr;
|
|
|
|
void* new_ptr;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
AllocatorInfo allocator = strbuilder_get_header(* str)->Allocator;
|
|
|
|
StrBuilderHeader* header = nullptr;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
new_len = strbuilder_grow_formula(strbuilder_length(* str) + add_len);
|
|
|
|
ptr = strbuilder_get_header(* str);
|
|
|
|
old_size = size_of(StrBuilderHeader) + strbuilder_length(* str) + 1;
|
|
|
|
new_size = size_of(StrBuilderHeader) + new_len + 1;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
new_ptr = resize(allocator, ptr, old_size, new_size);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
if (new_ptr == nullptr)
|
|
|
|
return false;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
header = rcast(StrBuilderHeader*, new_ptr);
|
2024-11-30 11:13:30 -08:00
|
|
|
header->Allocator = allocator;
|
2024-12-01 20:35:58 -08:00
|
|
|
header->Capacity = new_len;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 20:35:58 -08:00
|
|
|
char** Data = rcast(char**, str);
|
|
|
|
* Data = rcast(char*, header + 1);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
return true;
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
b32 strbuilder_starts_with_str(StrBuilder const str, Str substring) {
|
|
|
|
if (substring.Len > strbuilder_length(str))
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
b32 result = c_str_compare_len(str, substring.Ptr, substring.Len) == 0;
|
2024-11-30 11:13:30 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
b32 strbuilder_starts_with_string(StrBuilder const str, StrBuilder substring) {
|
|
|
|
if (strbuilder_length(substring) > strbuilder_length(str))
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2023-09-03 17:29:45 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
b32 result = c_str_compare_len(str, substring, strbuilder_length(substring) - 1) == 0;
|
2024-11-30 11:13:30 -08:00
|
|
|
return result;
|
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
void strbuilder_skip_line(StrBuilder str)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
|
|
|
#define current (*scanner)
|
2024-12-03 15:47:12 -08:00
|
|
|
char* scanner = str;
|
2024-11-30 11:13:30 -08:00
|
|
|
while (current != '\r' && current != '\n') {
|
2024-12-07 14:17:02 -08:00
|
|
|
++scanner;
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
s32 new_length = scanner - str;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
if (current == '\r') {
|
|
|
|
new_length += 1;
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
mem_move((char*)str, scanner, new_length);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilderHeader* header = strbuilder_get_header(str);
|
2024-11-30 11:13:30 -08:00
|
|
|
header->Length = new_length;
|
|
|
|
#undef current
|
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
void strip_space(StrBuilder str)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-07 14:17:02 -08:00
|
|
|
char* write_pos = str;
|
|
|
|
char* read_pos = str;
|
2024-11-30 11:13:30 -08:00
|
|
|
|
2024-12-07 14:17:02 -08:00
|
|
|
while (* read_pos)
|
|
|
|
{
|
|
|
|
if (! char_is_space(* read_pos))
|
|
|
|
{
|
|
|
|
* write_pos = * read_pos;
|
|
|
|
write_pos++;
|
|
|
|
}
|
|
|
|
read_pos++;
|
|
|
|
}
|
2024-11-30 11:13:30 -08:00
|
|
|
write_pos[0] = '\0'; // Null-terminate the modified string
|
|
|
|
|
2024-12-07 14:17:02 -08:00
|
|
|
// Update the length if needed
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_get_header(str)->Length = write_pos - str;
|
2024-12-03 15:47:12 -08:00
|
|
|
}
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
Str strbuilder_to_str(StrBuilder str) {
|
|
|
|
Str result = { strbuilder_length(str), (char const*)str };
|
2024-12-04 23:53:14 -08:00
|
|
|
return result;
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
void strbuilder_trim(StrBuilder str, char const* cut_set)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-03 15:47:12 -08:00
|
|
|
ssize len = 0;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
char* start_pos = str;
|
2024-12-12 09:55:15 -08:00
|
|
|
char* end_pos = scast(char*, str) + strbuilder_length(str) - 1;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
while (start_pos <= end_pos && char_first_occurence(cut_set, *start_pos))
|
|
|
|
start_pos++;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
while (end_pos > start_pos && char_first_occurence(cut_set, *end_pos))
|
|
|
|
end_pos--;
|
2023-07-25 20:00:57 -07:00
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
len = scast(ssize, (start_pos > end_pos) ? 0 : ((end_pos - start_pos) + 1));
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
if (str != start_pos)
|
|
|
|
mem_move(str, start_pos, len);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
str[len] = '\0';
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_get_header(str)->Length = len;
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
void strbuilder_trim_space(StrBuilder str) {
|
|
|
|
strbuilder_trim(str, " \t\r\n\v\f");
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder strbuilder_visualize_whitespace(StrBuilder const str)
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilderHeader* header = (StrBuilderHeader*)(scast(char const*, str) - sizeof(StrBuilderHeader));
|
|
|
|
StrBuilder result = strbuilder_make_reserve(header->Allocator, strbuilder_length(str) * 2); // Assume worst case for space requirements.
|
2024-11-30 11:13:30 -08:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
for (char const* c = strbuilder_begin(str); c != strbuilder_end(str); c = strbuilder_next(str, c))
|
2024-12-04 08:01:53 -08:00
|
|
|
switch ( * c )
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
|
|
|
case ' ':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("·"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\t':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("→"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\n':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("↵"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\r':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("⏎"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\v':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("⇕"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\f':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("⌂"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
default:
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_char(& result, * c);
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2024-12-12 09:55:15 -08:00
|
|
|
#pragma endregion StrBuilder
|
2024-11-30 11:13:30 -08:00
|
|
|
|
2024-12-04 23:53:14 -08:00
|
|
|
#if GEN_COMPILER_CPP
|
2024-12-12 09:55:15 -08:00
|
|
|
struct StrBuilder_POD {
|
2023-07-24 15:19:37 -07:00
|
|
|
char* Data;
|
|
|
|
};
|
2024-12-12 09:55:15 -08:00
|
|
|
static_assert( sizeof( StrBuilder_POD ) == sizeof( StrBuilder ), "StrBuilder is not a POD" );
|
2024-12-04 23:53:14 -08:00
|
|
|
#endif
|
2023-07-25 20:00:57 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-12 09:55:15 -08:00
|
|
|
Str str_duplicate(Str str, AllocatorInfo allocator) {
|
|
|
|
Str result = strbuilder_to_str( strbuilder_make_length(allocator, str.Ptr, str.Len));
|
2024-12-04 23:53:14 -08:00
|
|
|
return result;
|
2024-12-03 15:47:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-12 09:55:15 -08:00
|
|
|
Str str_visualize_whitespace(Str str, AllocatorInfo allocator)
|
2024-12-03 15:47:12 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
StrBuilder result = strbuilder_make_reserve(allocator, str.Len * 2); // Assume worst case for space requirements.
|
|
|
|
for (char const* c = str_begin(str); c != str_end(str); c = str_next(str, c))
|
2024-12-04 08:01:53 -08:00
|
|
|
switch ( * c )
|
2024-12-03 15:47:12 -08:00
|
|
|
{
|
|
|
|
case ' ':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("·"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\t':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("→"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\n':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("↵"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\r':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("⏎"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\v':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("⇕"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\f':
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str(& result, txt("⌂"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
default:
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_char(& result, * c);
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
2024-12-04 08:01:53 -08:00
|
|
|
}
|
2024-12-12 09:55:15 -08:00
|
|
|
return strbuilder_to_str(result);
|
2024-12-03 15:47:12 -08:00
|
|
|
}
|
2023-08-09 15:47:59 -07:00
|
|
|
|
|
|
|
// Represents strings cached with the string table.
|
|
|
|
// Should never be modified, if changed string is desired, cache_string( str ) another.
|
2024-12-12 09:55:15 -08:00
|
|
|
typedef Str StringCached;
|
2023-08-09 15:47:59 -07:00
|
|
|
|
2024-12-03 15:47:12 -08:00
|
|
|
// Implements basic string interning. Data structure is based off the ZPL Hashtable.
|
2024-12-04 23:53:14 -08:00
|
|
|
typedef HashTable(StringCached) StringTable;
|
2023-08-09 15:47:59 -07:00
|
|
|
#pragma endregion Strings
|