| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/string/print_string.h" |
| 34 |
#include "core/string/ustring.h" |
| 35 |
|
| 36 |
template <int SHORT_BUFFER_SIZE = 64> |
| 37 |
class StringBuffer { |
| 38 |
char32_t short_buffer[SHORT_BUFFER_SIZE]; |
| 39 |
String buffer; |
| 40 |
int string_length = 0; |
| 41 |
|
| 42 |
_FORCE_INLINE_ char32_t *current_buffer_ptr() { |
| 43 |
return static_cast<String &>(buffer).is_empty() ? short_buffer : buffer.ptrw(); |
| 44 |
} |
| 45 |
|
| 46 |
public: |
| 47 |
StringBuffer &append(char32_t p_char); |
| 48 |
StringBuffer &append(const String &p_string); |
| 49 |
StringBuffer &append(const char *p_str); |
| 50 |
StringBuffer &append(const char32_t *p_str, int p_clip_to_len = -1); |
| 51 |
|
| 52 |
_FORCE_INLINE_ void operator+=(char32_t p_char) { |
| 53 |
append(p_char); |
| 54 |
} |
| 55 |
|
| 56 |
_FORCE_INLINE_ void operator+=(const String &p_string) { |
| 57 |
append(p_string); |
| 58 |
} |
| 59 |
|
| 60 |
_FORCE_INLINE_ void operator+=(const char *p_str) { |
| 61 |
append(p_str); |
| 62 |
} |
| 63 |
|
| 64 |
_FORCE_INLINE_ void operator+=(const char32_t *p_str) { |
| 65 |
append(p_str); |
| 66 |
} |
| 67 |
|
| 68 |
StringBuffer &reserve(int p_size); |
| 69 |
|
| 70 |
int length() const; |
| 71 |
|
| 72 |
String as_string(); |
| 73 |
|
| 74 |
double as_double(); |
| 75 |
int64_t as_int(); |
| 76 |
|
| 77 |
_FORCE_INLINE_ operator String() { |
| 78 |
return as_string(); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
template <int SHORT_BUFFER_SIZE> |
| 83 |
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(char32_t p_char) { |
| 84 |
reserve(string_length + 2); |
| 85 |
current_buffer_ptr()[string_length++] = p_char; |
| 86 |
return *this; |
| 87 |
} |
| 88 |
|
| 89 |
template <int SHORT_BUFFER_SIZE> |
| 90 |
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const String &p_string) { |
| 91 |
return append(p_string.get_data()); |
| 92 |
} |
| 93 |
|
| 94 |
template <int SHORT_BUFFER_SIZE> |
| 95 |
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char *p_str) { |
| 96 |
int len = strlen(p_str); |
| 97 |
reserve(string_length + len + 1); |
| 98 |
|
| 99 |
char32_t *buf = current_buffer_ptr(); |
| 100 |
for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) { |
| 101 |
buf[string_length++] = *c_ptr; |
| 102 |
} |
| 103 |
return *this; |
| 104 |
} |
| 105 |
|
| 106 |
template <int SHORT_BUFFER_SIZE> |
| 107 |
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char32_t *p_str, int p_clip_to_len) { |
| 108 |
int len = 0; |
| 109 |
while ((p_clip_to_len < 0 || len < p_clip_to_len) && p_str[len]) { |
| 110 |
++len; |
| 111 |
} |
| 112 |
reserve(string_length + len + 1); |
| 113 |
memcpy(&(current_buffer_ptr()[string_length]), p_str, len * sizeof(char32_t)); |
| 114 |
string_length += len; |
| 115 |
|
| 116 |
return *this; |
| 117 |
} |
| 118 |
|
| 119 |
template <int SHORT_BUFFER_SIZE> |
| 120 |
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) { |
| 121 |
if (p_size <= SHORT_BUFFER_SIZE || p_size <= buffer.size()) { |
| 122 |
if (p_size < length()) { |
| 123 |
WARN_VERBOSE("reserve() called with a capacity smaller than the current size. This is likely a mistake."); |
| 124 |
} |
| 125 |
return *this; |
| 126 |
} |
| 127 |
|
| 128 |
bool need_copy = string_length > 0 && buffer.is_empty(); |
| 129 |
buffer.resize_uninitialized(next_power_of_2((uint32_t)p_size)); |
| 130 |
if (need_copy) { |
| 131 |
memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(char32_t)); |
| 132 |
} |
| 133 |
|
| 134 |
return *this; |
| 135 |
} |
| 136 |
|
| 137 |
template <int SHORT_BUFFER_SIZE> |
| 138 |
int StringBuffer<SHORT_BUFFER_SIZE>::length() const { |
| 139 |
return string_length; |
| 140 |
} |
| 141 |
|
| 142 |
template <int SHORT_BUFFER_SIZE> |
| 143 |
String StringBuffer<SHORT_BUFFER_SIZE>::as_string() { |
| 144 |
current_buffer_ptr()[string_length] = '\0'; |
| 145 |
if (buffer.is_empty()) { |
| 146 |
return String(short_buffer); |
| 147 |
} else { |
| 148 |
buffer.resize_uninitialized(string_length + 1); |
| 149 |
return buffer; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
template <int SHORT_BUFFER_SIZE> |
| 154 |
double StringBuffer<SHORT_BUFFER_SIZE>::as_double() { |
| 155 |
current_buffer_ptr()[string_length] = '\0'; |
| 156 |
return String::to_float(current_buffer_ptr()); |
| 157 |
} |
| 158 |
|
| 159 |
template <int SHORT_BUFFER_SIZE> |
| 160 |
int64_t StringBuffer<SHORT_BUFFER_SIZE>::as_int() { |
| 161 |
current_buffer_ptr()[string_length] = '\0'; |
| 162 |
return String::to_int(current_buffer_ptr()); |
| 163 |
} |