Musa-Cpp-Lib-V2/lib/Base/String.h

127 lines
4.1 KiB
C

#pragma once
// #TODO: #strings:
// [ ] Always null-terminate strings!
// [ ] How do I accept variadic arguments of any type to my print function?
// [ ] Need to sort out how formatted strings and string builders are allocated
// [ ] Separate functions for temp alloc (tprint??)
// [ ] API needs to be completely overhauled
// [ ] I should also put path manipulation here or in a separate file.
struct string {
s64 count;
u8* data;
// Construct from a string literal or C-string
string () { // default constructor
count = 0;
data = nullptr;
}
string (char* cstr) {
count = strlen(cstr);
data = (u8*)cstr;
}
string (s64 _count, char* str) { count = _count; data = (u8*)str; }
string (s64 _count, u8* str) { count = _count; data = str; }
bool operator==(const string& other) const {
string first_string = *this;
string second_string = other;
// return strings_match(*this, other);
if (first_string.count != second_string.count) {
return false;
}
for (s64 i = 0; i < first_string.count; i += 1) {
if (first_string.data[i] != second_string.data[i]) {
return false;
}
}
return true;
}
bool operator ! () {
Assert(count >= 0);
return (data == nullptr || count == 0);
}
};
struct wstring {
s64 count;
u16* data;
wstring () { // default constructor
count = 0;
data = nullptr;
}
wstring (s32 length) {
data = NewArray<u16>(length + 1);
s32 length_bytes = (length + 1) * sizeof(u16);
count = length_bytes;
}
bool operator ! () {
Assert(count >= 0);
return (data == nullptr || count == 0);
}
};
// ~Keep these API
bool is_valid (string s);
bool is_c_string (string s);
u8* to_c_string (string s); // #allocates
string copy_string (string s); // #allocates, returned string is #null-terminated.
string copy_string (char* c_string); // #allocates, returned string is #null-terminated.
string to_string (ArrayView<u8> str);
ArrayView<u8> to_view (string s);
void free(string& s);
// String manipulation & comparison
force_inline string string_view (string s, s64 start_index, s64 view_count);
bool strings_match (string first_string, string second_string);
// #Unicode
// #TODO: Make a raw version that returns the raw pointer?
string wide_to_utf8 (u16* source, s32 length=-1);
wstring utf8_to_wide (string source);
string format_string (char* format, ...);
string format_string_no_context (char* format, ...);
string to_lower_copy (string s_orig);
// #TODO #Parsing stuff:
// is_white_space(char: u8)
// advance
// eat_spaces
// string to type or type to string conversions
// s64 string_to_int (string v, s32 base = 10, s64* remainder=nullptr);
// f64 string_to_f64
// f32 string_to_f32
// Need an API for inserting various types (ints, floats, etc.) into a String_Builder, and advancing
// the count.
// #string_builder
// #limitations This won't be as fast as Jon's String_Builder in jai because we're backing it with an
// Arena, which requires a variable number of cycles depending on if our process has
// memory available already. It also has a max capacity depending on what Arena_Reserve we choose.
// That being said, the implementation is much simpler.
typedef ArenaArray<u8> String_Builder; // struct String_Builder
force_inline String_Builder* new_string_builder (Arena_Reserve new_reserve=Arena_Reserve::Size_64K);
force_inline void append (String_Builder* sb, string s);
void append (String_Builder* sb, ArrayView<string> strings);
// This should probably be called append_but_do_not_increment_count
internal force_inline void append_no_add (String_Builder* sb, string s); // for appending null terminators, does not increment count.
void print_to_builder (String_Builder* sb, string format, ...);
void print_to_builder (String_Builder* sb, string format, va_list args);
string string_view (String_Builder* sb);
internal force_inline void reset_string_builder (String_Builder* sb, bool keep_memory=false);
force_inline string builder_to_string (String_Builder* sb); // returns string view
internal force_inline void free_string_builder (String_Builder* sb);