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-07 14:17:02 -08:00
|
|
|
struct StrC;
|
2024-12-03 15:47:12 -08:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
bool strc_are_equal (StrC lhs, StrC rhs);
|
|
|
|
char const* strc_back (StrC str);
|
|
|
|
bool strc_contains (StrC str, StrC substring);
|
|
|
|
StrC strc_duplicate (StrC str, AllocatorInfo allocator);
|
|
|
|
b32 strc_starts_with (StrC str, StrC substring);
|
|
|
|
StrC strc_to_str (char const* bad_string);
|
|
|
|
StrC strc_visualize_whitespace(StrC str, AllocatorInfo allocator);
|
|
|
|
|
2023-07-24 15:19:37 -07:00
|
|
|
// Constant string with length.
|
2024-12-07 14:17:02 -08:00
|
|
|
struct StrC
|
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-07 14:17:02 -08:00
|
|
|
forceinline bool is_equal (StrC rhs) const { return strc_are_equal(* this, rhs); }
|
|
|
|
forceinline char const* back () const { return strc_back(* this); }
|
|
|
|
forceinline bool contains (StrC substring) const { return strc_contains(* this, substring); }
|
|
|
|
forceinline StrC duplicate (AllocatorInfo allocator) const { return strc_duplicate(* this, allocator); }
|
|
|
|
forceinline b32 starts_with (StrC substring) const { return strc_starts_with(* this, substring); }
|
|
|
|
forceinline StrC visualize_whitespace(AllocatorInfo allocator) const { return strc_visualize_whitespace(* this, allocator); }
|
2024-12-03 17:42:35 -08:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-07-24 15:19:37 -07:00
|
|
|
};
|
|
|
|
|
2024-10-27 15:58:37 -07:00
|
|
|
#define cast_to_strc( str ) * rcast( StrC*, (str) - sizeof(ssize) )
|
2024-12-04 23:53:14 -08:00
|
|
|
|
|
|
|
#ifndef txt
|
|
|
|
# if GEN_COMPILER_CPP
|
|
|
|
# define txt( text ) StrC { sizeof( text ) - 1, ( text ) }
|
|
|
|
# else
|
|
|
|
# define txt( text ) (StrC){ sizeof( text ) - 1, ( text ) }
|
|
|
|
# endif
|
|
|
|
#endif
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
GEN_API_C_BEGIN
|
|
|
|
forceinline char const* strc_begin(StrC str) { return str.Ptr; }
|
|
|
|
forceinline char const* strc_end (StrC str) { return str.Ptr + str.Len; }
|
|
|
|
forceinline char const* strc_next (StrC str, char const* iter) { return iter + 1; }
|
|
|
|
GEN_API_C_END
|
|
|
|
|
2024-12-04 12:00:37 -08:00
|
|
|
#if GEN_COMPILER_CPP
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline char const* begin(StrC str) { return str.Ptr; }
|
|
|
|
forceinline char const* end (StrC str) { return str.Ptr + str.Len; }
|
|
|
|
forceinline char const* next (StrC str, char const* iter) { return iter + 1; }
|
|
|
|
#endif
|
2024-12-03 15:47:12 -08:00
|
|
|
|
|
|
|
inline
|
2024-12-04 08:01:53 -08:00
|
|
|
bool strc_are_equal(StrC lhs, StrC 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-04 08:01:53 -08:00
|
|
|
char const* strc_back(StrC str) {
|
2024-12-03 15:47:12 -08:00
|
|
|
return & str.Ptr[str.Len - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-04 08:01:53 -08:00
|
|
|
bool strc_contains(StrC str, StrC 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-04 08:01:53 -08:00
|
|
|
if (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-04 08:01:53 -08:00
|
|
|
b32 strc_starts_with(StrC str, StrC substring) {
|
2024-12-03 15:47:12 -08:00
|
|
|
if (substring.Len > str.Len)
|
|
|
|
return false;
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
b32 result = str_compare_len(str.Ptr, substring.Ptr, substring.Len) == 0;
|
2024-12-03 15:47:12 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-04 08:01:53 -08:00
|
|
|
StrC to_strc_from_c_str( char const* bad_str ) {
|
2024-12-04 23:53:14 -08:00
|
|
|
StrC result = { str_len( bad_str ), bad_str };
|
|
|
|
return result;
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamic String
|
|
|
|
// 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-11-30 11:13:30 -08:00
|
|
|
#pragma region String
|
2024-12-07 14:17:02 -08:00
|
|
|
struct StringHeader;
|
2024-12-03 15:47:12 -08:00
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
#if GEN_COMPILER_C
|
2024-12-03 15:47:12 -08:00
|
|
|
typedef char* String;
|
|
|
|
#else
|
2024-12-01 00:06:30 -08:00
|
|
|
struct String;
|
2024-12-03 15:47:12 -08:00
|
|
|
#endif
|
2024-12-01 00:06:30 -08:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline usize string_grow_formula(usize value);
|
|
|
|
|
|
|
|
String string_make_c_str (AllocatorInfo allocator, char const* str);
|
|
|
|
String string_make_strc (AllocatorInfo allocator, StrC str);
|
|
|
|
String string_make_reserve (AllocatorInfo allocator, ssize capacity);
|
|
|
|
String string_make_length (AllocatorInfo allocator, char const* str, ssize length);
|
|
|
|
String string_fmt (AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...);
|
|
|
|
String string_fmt_buf (AllocatorInfo allocator, char const* fmt, ...);
|
|
|
|
String string_join (AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue);
|
|
|
|
bool string_are_equal (String const lhs, String const rhs);
|
|
|
|
bool string_are_equal_strc (String const lhs, StrC rhs);
|
|
|
|
bool string_make_space_for (String* str, char const* to_append, ssize add_len);
|
|
|
|
bool string_append_char (String* str, char c);
|
|
|
|
bool string_append_c_str (String* str, char const* str_to_append);
|
|
|
|
bool string_append_c_str_len (String* str, char const* str_to_append, ssize length);
|
|
|
|
bool string_append_strc (String* str, StrC str_to_append);
|
|
|
|
bool string_append_string (String* str, String const other);
|
|
|
|
bool string_append_fmt (String* str, char const* fmt, ...);
|
|
|
|
ssize string_avail_space (String const str);
|
|
|
|
char* string_back (String str);
|
|
|
|
bool string_contains_strc (String const str, StrC substring);
|
|
|
|
bool string_contains_string (String const str, String const substring);
|
|
|
|
ssize string_capacity (String const str);
|
|
|
|
void string_clear (String str);
|
|
|
|
String string_duplicate (String const str, AllocatorInfo allocator);
|
|
|
|
void string_free (String* str);
|
|
|
|
StringHeader* string_get_header (String str);
|
|
|
|
ssize string_length (String const str);
|
|
|
|
b32 string_starts_with_strc (String const str, StrC substring);
|
|
|
|
b32 string_starts_with_string (String const str, String substring);
|
|
|
|
void string_skip_line (String str);
|
|
|
|
void string_strip_space (String str);
|
|
|
|
StrC string_to_strc (String str);
|
|
|
|
void string_trim (String str, char const* cut_set);
|
|
|
|
void string_trim_space (String str);
|
|
|
|
String string_visualize_whitespace(String const str);
|
2024-12-04 12:00:37 -08:00
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
struct StringHeader {
|
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
|
2023-07-24 15:19:37 -07:00
|
|
|
struct String
|
|
|
|
{
|
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-04 08:30:54 -08:00
|
|
|
forceinline operator StrC() const { return { string_length(* this), Data }; }
|
2024-12-01 00:06:30 -08:00
|
|
|
|
|
|
|
String const& operator=(String const& other) const {
|
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
String* this_ = ccast(String*, this);
|
|
|
|
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-04 08:01:53 -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 String str) { return str.Data == nullptr; }
|
|
|
|
friend forceinline bool operator!=(std::nullptr_t, const String 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-04 08:30:54 -08:00
|
|
|
forceinline char* end() const { return Data + string_length(* this); }
|
2024-12-01 00:06:30 -08:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
#pragma region Member Mapping
|
2024-12-04 08:30:54 -08:00
|
|
|
forceinline static String make(AllocatorInfo allocator, char const* str) { return string_make_c_str(allocator, str); }
|
|
|
|
forceinline static String make(AllocatorInfo allocator, StrC str) { return string_make_strc(allocator, str); }
|
|
|
|
forceinline static String make_reserve(AllocatorInfo allocator, ssize cap) { return string_make_reserve(allocator, cap); }
|
|
|
|
forceinline static String make_length(AllocatorInfo a, char const* s, ssize l) { return string_make_length(a, s, l); }
|
|
|
|
forceinline static String join(AllocatorInfo a, char const** p, ssize n, char const* g) { return string_join(a, p, n, g); }
|
|
|
|
forceinline static usize grow_formula(usize value) { return string_grow_formula(value); }
|
2023-07-24 15:19:37 -07:00
|
|
|
|
|
|
|
static
|
2024-11-30 11:13:30 -08:00
|
|
|
String fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) {
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-04 21:40:51 -08:00
|
|
|
ssize res = str_fmt_va(buf, buf_size, fmt, va) - 1;
|
2024-11-30 11:13:30 -08:00
|
|
|
va_end(va);
|
2024-12-04 08:30:54 -08:00
|
|
|
return string_make_length(allocator, buf, res);
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2024-11-30 11:13:30 -08:00
|
|
|
String fmt_buf(AllocatorInfo allocator, char const* fmt, ...) {
|
|
|
|
local_persist thread_local
|
|
|
|
char buf[GEN_PRINTF_MAXLEN] = { 0 };
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-04 21:40:51 -08:00
|
|
|
ssize res = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va) - 1;
|
2024-11-30 11:13:30 -08:00
|
|
|
va_end(va);
|
2024-12-04 08:30:54 -08:00
|
|
|
return string_make_length(allocator, buf, res);
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
|
|
|
|
2024-12-04 08:30:54 -08:00
|
|
|
forceinline bool make_space_for(char const* str, ssize add_len) { return string_make_space_for(this, str, add_len); }
|
|
|
|
forceinline bool append(char c) { return string_append_char(this, c); }
|
|
|
|
forceinline bool append(char const* str) { return string_append_c_str(this, str); }
|
|
|
|
forceinline bool append(char const* str, ssize length) { return string_append_c_str_len(this, str, length); }
|
|
|
|
forceinline bool append(StrC str) { return string_append_strc(this, str); }
|
|
|
|
forceinline bool append(const String other) { return string_append_string(this, other); }
|
|
|
|
forceinline ssize avail_space() const { return string_avail_space(* this); }
|
|
|
|
forceinline char* back() { return string_back(* this); }
|
|
|
|
forceinline bool contains(StrC substring) const { return string_contains_strc(* this, substring); }
|
|
|
|
forceinline bool contains(String const& substring) const { return string_contains_string(* this, substring); }
|
|
|
|
forceinline ssize capacity() const { return string_capacity(* this); }
|
|
|
|
forceinline void clear() { string_clear(* this); }
|
|
|
|
forceinline String duplicate(AllocatorInfo allocator) const { return string_duplicate(* this, allocator); }
|
|
|
|
forceinline void free() { string_free(this); }
|
|
|
|
forceinline bool is_equal(String const& other) const { return string_are_equal(* this, other); }
|
|
|
|
forceinline bool is_equal(StrC other) const { return string_are_equal_strc(* this, other); }
|
|
|
|
forceinline ssize length() const { return string_length(* this); }
|
|
|
|
forceinline b32 starts_with(StrC substring) const { return string_starts_with_strc(* this, substring); }
|
|
|
|
forceinline b32 starts_with(String substring) const { return string_starts_with_string(* this, substring); }
|
|
|
|
forceinline void skip_line() { string_skip_line(* this); }
|
|
|
|
forceinline void strip_space() { string_strip_space(* this); }
|
|
|
|
forceinline StrC to_strc() { return { string_length(*this), Data}; }
|
|
|
|
forceinline void trim(char const* cut_set) { string_trim(* this, cut_set); }
|
|
|
|
forceinline void trim_space() { string_trim_space(* this); }
|
|
|
|
forceinline String visualize_whitespace() const { return string_visualize_whitespace(* this); }
|
|
|
|
forceinline StringHeader& get_header() { return * string_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);
|
|
|
|
res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1;
|
|
|
|
va_end(va);
|
|
|
|
|
2024-12-04 08:30:54 -08:00
|
|
|
return string_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-04 08:01:53 -08:00
|
|
|
forceinline char* string_begin(String str) { return ((char*) str); }
|
|
|
|
forceinline char* string_end (String str) { return ((char*) str + string_length(str)); }
|
|
|
|
forceinline char* string_next (String str, char const* iter) { return ((char*) iter + 1); }
|
|
|
|
|
2024-12-06 21:21:09 -08:00
|
|
|
#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline char* begin(String str) { return ((char*) str); }
|
|
|
|
forceinline char* end (String str) { return ((char*) str + string_length(str)); }
|
|
|
|
forceinline char* next (String str, char* iter) { return ((char*) iter + 1); }
|
|
|
|
#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-04 08:01:53 -08:00
|
|
|
forceinline bool make_space_for(String& str, char const* to_append, ssize add_len);
|
|
|
|
forceinline bool append(String& str, char c);
|
|
|
|
forceinline bool append(String& str, char const* str_to_append);
|
|
|
|
forceinline bool append(String& str, char const* str_to_append, ssize length);
|
|
|
|
forceinline bool append(String& str, StrC str_to_append);
|
|
|
|
forceinline bool append(String& str, const String other);
|
|
|
|
forceinline bool append_fmt(String& str, char const* fmt, ...);
|
|
|
|
forceinline char& back(String& str);
|
|
|
|
forceinline void clear(String& str);
|
|
|
|
forceinline void free(String& str);
|
2024-12-01 20:35:58 -08:00
|
|
|
#endif
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-11-30 11:13:30 -08:00
|
|
|
usize string_grow_formula(usize value) {
|
|
|
|
// 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
|
|
|
|
String string_make_c_str(AllocatorInfo allocator, char const* str) {
|
2024-11-30 11:13:30 -08:00
|
|
|
ssize length = str ? str_len(str) : 0;
|
|
|
|
return string_make_length(allocator, str, length);
|
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
|
|
|
String string_make_strc(AllocatorInfo allocator, StrC str) {
|
2024-11-30 11:13:30 -08:00
|
|
|
return string_make_length(allocator, str.Ptr, str.Len);
|
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
inline
|
|
|
|
String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) {
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2024-12-04 21:40:51 -08:00
|
|
|
ssize res = 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-04 08:01:53 -08:00
|
|
|
return string_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
|
|
|
|
String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...)
|
|
|
|
{
|
|
|
|
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-04 21:40:51 -08:00
|
|
|
ssize res = 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-04 08:01:53 -08:00
|
|
|
return string_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
|
|
|
|
String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue)
|
|
|
|
{
|
2024-12-04 08:01:53 -08:00
|
|
|
String result = string_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-04 08:01:53 -08:00
|
|
|
string_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-04 08:01:53 -08:00
|
|
|
string_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
|
|
|
|
bool string_append_char(String* str, char c) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-04 08:01:53 -08:00
|
|
|
return string_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
|
|
|
|
bool string_append_c_str(String* str, char const* str_to_append) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-04 08:01:53 -08:00
|
|
|
return string_append_c_str_len(str, str_to_append, str_len(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-04 08:30:54 -08:00
|
|
|
bool string_append_c_str_len(String* str, char const* 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-04 23:53:14 -08:00
|
|
|
if ( rcast(sptr, str_to_append) > 0)
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-12-04 08:01:53 -08:00
|
|
|
ssize curr_len = string_length(* str);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
if ( ! string_make_space_for(str, 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-04 08:01:53 -08:00
|
|
|
StringHeader* header = string_get_header(* str);
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-01 20:35:58 -08:00
|
|
|
char* Data = * str;
|
|
|
|
mem_copy( Data + curr_len, 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-11-30 11:13:30 -08:00
|
|
|
return str_to_append != nullptr;
|
|
|
|
}
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
|
|
|
bool string_append_strc(String* str, StrC str_to_append) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-04 08:01:53 -08:00
|
|
|
return string_append_c_str_len(str, str_to_append.Ptr, 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
|
|
|
|
bool string_append_string(String* str, String const other) {
|
2024-12-01 20:35:58 -08:00
|
|
|
GEN_ASSERT(str != nullptr);
|
2024-12-04 08:01:53 -08:00
|
|
|
return string_append_c_str_len(str, (char const*)other, string_length(other));
|
2024-12-01 00:06:30 -08:00
|
|
|
}
|
|
|
|
|
2024-12-04 08:30:54 -08:00
|
|
|
bool string_append_fmt(String* 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);
|
|
|
|
res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1;
|
|
|
|
va_end(va);
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
return string_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-04 08:01:53 -08:00
|
|
|
bool string_are_equal_string(String const lhs, String const rhs)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-04 08:01:53 -08:00
|
|
|
if (string_length(lhs) != string_length(rhs))
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2024-11-29 11:50:54 -08:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
for (ssize idx = 0; idx < string_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-04 08:01:53 -08:00
|
|
|
bool string_are_equal_strc(String const lhs, StrC rhs)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-04 08:01:53 -08:00
|
|
|
if (string_length(lhs) != (rhs.Len))
|
2024-11-29 11:50:54 -08:00
|
|
|
return false;
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
for (ssize idx = 0; idx < string_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-04 08:30:54 -08:00
|
|
|
ssize string_avail_space(String const str) {
|
2024-12-01 20:35:58 -08:00
|
|
|
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
|
|
|
|
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-04 08:30:54 -08:00
|
|
|
char* string_back(String str) {
|
|
|
|
return & (str)[string_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-04 08:30:54 -08:00
|
|
|
bool string_contains_StrC(String const str, StrC substring)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-01 20:35:58 -08:00
|
|
|
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
|
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-04 08:01:53 -08:00
|
|
|
if (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-04 08:01:53 -08:00
|
|
|
bool string_contains_string(String const str, String const substring)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-01 20:35:58 -08:00
|
|
|
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
if (string_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-04 08:01:53 -08:00
|
|
|
ssize sub_len = string_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-04 08:01:53 -08:00
|
|
|
if (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
|
|
|
|
ssize string_capacity(String const str) {
|
2024-12-07 14:17:02 -08:00
|
|
|
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
|
|
|
|
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
|
|
|
|
void string_clear(String str) {
|
2024-12-07 14:17:02 -08:00
|
|
|
string_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
|
|
|
|
String string_duplicate(String const str, AllocatorInfo allocator) {
|
2024-12-07 14:17:02 -08:00
|
|
|
return string_make_length(allocator, str, string_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
|
|
|
|
void string_free(String* 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-04 08:01:53 -08:00
|
|
|
StringHeader* header = string_get_header(* str);
|
|
|
|
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
|
|
|
|
StringHeader* string_get_header(String str) {
|
2024-12-07 14:17:02 -08:00
|
|
|
return (StringHeader*)(scast(char*, str) - sizeof(StringHeader));
|
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
|
|
|
|
ssize string_length(String const str)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-07 14:17:02 -08:00
|
|
|
StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
|
|
|
|
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-04 08:01:53 -08:00
|
|
|
bool string_make_space_for(String* str, char const* to_append, ssize add_len)
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
2024-12-04 08:01:53 -08:00
|
|
|
ssize available = string_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-04 08:01:53 -08:00
|
|
|
AllocatorInfo allocator = string_get_header(* str)->Allocator;
|
2024-12-01 20:35:58 -08:00
|
|
|
StringHeader* header = nullptr;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
new_len = string_grow_formula(string_length(* str) + add_len);
|
|
|
|
ptr = string_get_header(* str);
|
|
|
|
old_size = size_of(StringHeader) + string_length(* str) + 1;
|
2024-11-30 11:13:30 -08:00
|
|
|
new_size = size_of(StringHeader) + 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-11-30 11:13:30 -08:00
|
|
|
header = rcast(StringHeader*, new_ptr);
|
|
|
|
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
|
|
|
|
b32 string_starts_with_strc(String const str, StrC substring) {
|
|
|
|
if (substring.Len > string_length(str))
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
b32 result = 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
|
|
|
|
b32 string_starts_with_string(String const str, String substring) {
|
|
|
|
if (string_length(substring) > string_length(str))
|
2024-11-30 11:13:30 -08:00
|
|
|
return false;
|
2023-09-03 17:29:45 -07:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
b32 result = str_compare_len(str, substring, string_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-04 08:01:53 -08:00
|
|
|
void string_skip_line(String 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-04 08:01:53 -08:00
|
|
|
StringHeader* header = string_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-01 20:35:58 -08:00
|
|
|
void strip_space(String 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
|
|
|
|
string_get_header(str)->Length = write_pos - str;
|
2024-12-03 15:47:12 -08:00
|
|
|
}
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
|
|
|
StrC string_to_strc(String str) {
|
2024-12-04 23:53:14 -08:00
|
|
|
StrC result = { string_length(str), (char const*)str };
|
|
|
|
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-01 20:35:58 -08:00
|
|
|
void trim(String 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-04 08:01:53 -08:00
|
|
|
char* end_pos = scast(char*, str) + string_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-07 14:17:02 -08:00
|
|
|
string_get_header(str)->Length = len;
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
forceinline
|
2024-12-01 20:35:58 -08:00
|
|
|
void trim_space(String str) {
|
2024-12-07 14:17:02 -08:00
|
|
|
trim(str, " \t\r\n\v\f");
|
2024-11-30 11:13:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-01 20:35:58 -08:00
|
|
|
String visualize_whitespace(String const str)
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
2024-12-01 00:06:30 -08:00
|
|
|
StringHeader* header = (StringHeader*)(scast(char const*, str) - sizeof(StringHeader));
|
2024-12-04 08:01:53 -08:00
|
|
|
String result = string_make_reserve(header->Allocator, string_length(str) * 2); // Assume worst case for space requirements.
|
2024-11-30 11:13:30 -08:00
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
for (char const* c = string_begin(str); c != string_end(str); c = string_next(str, c))
|
|
|
|
switch ( * c )
|
2024-11-30 11:13:30 -08:00
|
|
|
{
|
|
|
|
case ' ':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("·"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\t':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("→"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\n':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("↵"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\r':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("⏎"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\v':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("⇕"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
case '\f':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("⌂"));
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
default:
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_char(& result, * c);
|
2024-11-30 11:13:30 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#pragma endregion String
|
|
|
|
|
2024-12-04 23:53:14 -08:00
|
|
|
#if GEN_COMPILER_CPP
|
2024-11-30 11:13:30 -08:00
|
|
|
struct String_POD {
|
2023-07-24 15:19:37 -07:00
|
|
|
char* Data;
|
|
|
|
};
|
|
|
|
static_assert( sizeof( String_POD ) == sizeof( String ), "String 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
|
|
|
|
StrC strc_duplicate(StrC str, AllocatorInfo allocator) {
|
2024-12-04 23:53:14 -08:00
|
|
|
StrC result = string_to_strc( string_make_length(allocator, str.Ptr, str.Len));
|
|
|
|
return result;
|
2024-12-03 15:47:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2024-12-04 08:01:53 -08:00
|
|
|
StrC strc_visualize_whitespace(StrC str, AllocatorInfo allocator)
|
2024-12-03 15:47:12 -08:00
|
|
|
{
|
|
|
|
String result = string_make_reserve(allocator, str.Len * 2); // Assume worst case for space requirements.
|
2024-12-07 14:17:02 -08:00
|
|
|
for (char const* c = strc_begin(str); c != strc_end(str); c = strc_next(str, c))
|
2024-12-04 08:01:53 -08:00
|
|
|
switch ( * c )
|
2024-12-03 15:47:12 -08:00
|
|
|
{
|
|
|
|
case ' ':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("·"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\t':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("→"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\n':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("↵"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\r':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("⏎"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\v':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("⇕"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
case '\f':
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_strc(& result, txt("⌂"));
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
|
|
|
default:
|
2024-12-04 08:01:53 -08:00
|
|
|
string_append_char(& result, * c);
|
2024-12-03 15:47:12 -08:00
|
|
|
break;
|
2024-12-04 08:01:53 -08:00
|
|
|
}
|
|
|
|
return string_to_strc(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-03 15:47:12 -08:00
|
|
|
typedef StrC 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
|