Added StringBuffer and StringBuilder from Pandemonium.

This commit is contained in:
Relintai 2024-07-21 20:28:19 +02:00
parent 4b2076ce7b
commit 6b6eb0846e
40 changed files with 910 additions and 6 deletions

View File

@ -35,6 +35,7 @@ ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/pool_vector.cpp -o sfw/core/p
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/pool_allocator.cpp -o sfw/core/pool_allocator.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/mutex.cpp -o sfw/core/mutex.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/sfw_time.cpp -o sfw/core/sfw_time.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/string_builder.cpp -o sfw/core/string_builder.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/thread.cpp -o sfw/core/thread.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/socket.cpp -o sfw/core/socket.o
@ -108,6 +109,7 @@ ccache g++ -Wall -D_REENTRANT -g sfw/core/aabb.o sfw/core/basis.o sfw/core/color
sfw/core/vector2.o sfw/core/vector2i.o sfw/core/vector3.o \
sfw/core/vector3i.o sfw/core/vector4.o sfw/core/vector4i.o \
sfw/core/pool_vector.o sfw/core/pool_allocator.o sfw/core/mutex.o sfw/core/sfw_time.o \
sfw/core/string_builder.o \
sfw/core/dir_access.o sfw/core/file_access.o sfw/core/thread.o \
sfw/core/socket.o sfw/core/inet_address.o \
sfw/core/sub_process.o \

View File

@ -22,6 +22,7 @@ ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/pool_vector.cpp -o sfwl/cor
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/pool_allocator.cpp -o sfwl/core/pool_allocator.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/mutex.cpp -o sfwl/core/mutex.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/sfw_time.cpp -o sfwl/core/sfw_time.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/string_builder.cpp -o sfwl/core/string_builder.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/thread.cpp -o sfwl/core/thread.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/socket.cpp -o sfwl/core/socket.o
@ -52,6 +53,7 @@ ccache g++ -Wall -D_REENTRANT -g sfwl/core/color.o sfwl/core/rect2i.o sfwl/core
sfwl/core/safe_refcount.o \
sfwl/core/ustring.o sfwl/core/string_name.o \
sfwl/core/pool_vector.o sfwl/core/pool_allocator.o sfwl/core/mutex.o sfwl/core/sfw_time.o \
sfwl/core/string_builder.o \
sfwl/core/dir_access.o sfwl/core/file_access.o sfwl/core/thread.o \
sfwl/core/socket.o sfwl/core/inet_address.o \
sfwl/core/sub_process.o \

View File

@ -46,6 +46,7 @@ clang++ $args -D_REENTRANT -g -Isfw -c sfw/core/pool_vector.cpp -o sfw/core/pool
clang++ $args -D_REENTRANT -g -Isfw -c sfw/core/pool_allocator.cpp -o sfw/core/pool_allocator.o
clang++ $args -D_REENTRANT -g -Isfw -c sfw/core/mutex.cpp -o sfw/core/mutex.o
clang++ $args -D_REENTRANT -g -Isfw -c sfw/core/sfw_time.cpp -o sfw/core/sfw_time.o
clang++ $args -D_REENTRANT -g -Isfw -c sfw/core/string_builder.cpp -o sfw/core/string_builder.o
clang++ $args -D_REENTRANT -g -Isfw -c sfw/core/thread.cpp -o sfw/core/thread.o
clang++ $args -D_REENTRANT -g -Isfw -c sfw/core/socket.cpp -o sfw/core/socket.o
@ -121,6 +122,7 @@ clang++ $args -D_REENTRANT -g sfw/core/aabb.o sfw/core/basis.o sfw/core/color.o
sfw/core/vector2.o sfw/core/vector2i.o sfw/core/vector3.o \
sfw/core/vector3i.o sfw/core/vector4.o sfw/core/vector4i.o \
sfw/core/pool_vector.o sfw/core/pool_allocator.o sfw/core/mutex.o sfw/core/sfw_time.o \
sfw/core/string_builder.o \
sfw/core/dir_access.o sfw/core/file_access.o sfw/core/thread.o \
sfw/core/socket.o sfw/core/inet_address.o \
sfw/core/sub_process.o \
@ -151,4 +153,4 @@ clang++ $args -D_REENTRANT -g sfw/core/aabb.o sfw/core/basis.o sfw/core/color.o
sfw/game_scene.o sfw/main.o \
-o game
#export args="-lm -ldl -lpthread -lX11 -w -Iengine/ $args"
#export args="-lm -ldl -lpthread -lX11 -w -Iengine/ $args"

View File

@ -18,6 +18,7 @@ clang++ -std=c++14 -D_REENTRANT -g -Isfwl -c sfwl/core/pool_vector.cpp -o sfwl/c
clang++ -std=c++14 -D_REENTRANT -g -Isfwl -c sfwl/core/pool_allocator.cpp -o sfwl/core/pool_allocator.o
clang++ -std=c++14 -D_REENTRANT -g -Isfwl -c sfwl/core/mutex.cpp -o sfwl/core/mutex.o
clang++ -std=c++14 -D_REENTRANT -g -Isfwl -c sfwl/core/sfw_time.cpp -o sfwl/core/sfw_time.o
clang++ -std=c++14 -D_REENTRANT -g -Isfwl -c sfwl/core/string_builder.cpp -o sfwl/core/string_builder.o
clang++ -std=c++14 -D_REENTRANT -g -Isfwl -c sfwl/core/thread.cpp -o sfwl/core/thread.o
clang++ -std=c++14 -D_REENTRANT -g -Isfwl -c sfwl/core/socket.cpp -o sfwl/core/socket.o
@ -49,6 +50,7 @@ clang++ -std=c++14 -D_REENTRANT -g sfwl/core/color.o \
sfwl/core/ustring.o sfwl/core/string_name.o \
sfwl/core/vector2i.o \
sfwl/core/pool_vector.o sfwl/core/pool_allocator.o sfwl/core/mutex.o sfwl/core/sfw_time.o \
sfwl/core/string_builder.o \
sfwl/core/dir_access.o sfwl/core/file_access.o sfwl/core/thread.o \
sfwl/core/socket.o sfwl/core/inet_address.o \
sfwl/core/sub_process.o \

View File

@ -41,6 +41,7 @@ cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/pool_vector.cpp /Fo:sfw/core/pool_ve
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/pool_allocator.cpp /Fo:sfw/core/pool_allocator.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/mutex.cpp /Fo:sfw/core/mutex.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/sfw_time.cpp /Fo:sfw/core/sfw_time.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/string_builder.cpp /Fo:sfw/core/string_builder.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/thread.cpp /Fo:sfw/core/thread.obj
cl /D_REENTRANT /EHsc /Zi /Isfw /c sfw/core/socket.cpp /Fo:sfw/core/socket.obj
@ -118,6 +119,7 @@ cl /D_REENTRANT /Zi ^
sfw/core/vector2.obj sfw/core/vector2i.obj sfw/core/vector3.obj ^
sfw/core/vector3i.obj sfw/core/vector4.obj sfw/core/vector4i.obj ^
sfw/core/pool_vector.obj sfw/core/pool_allocator.obj sfw/core/mutex.obj sfw/core/sfw_time.obj ^
sfw/core/string_builder.obj ^
sfw/core/dir_access.obj sfw/core/file_access.obj sfw/core/thread.obj ^
sfw/core/socket.obj sfw/core/inet_address.obj ^
sfw/core/sub_process.obj ^

View File

@ -27,6 +27,7 @@ cl /D_REENTRANT /EHsc /Zi /Isfwl /c sfwl/core/pool_vector.cpp /Fo:sfwl/core/pool
cl /D_REENTRANT /EHsc /Zi /Isfwl /c sfwl/core/pool_allocator.cpp /Fo:sfwl/core/pool_allocator.obj
cl /D_REENTRANT /EHsc /Zi /Isfwl /c sfwl/core/mutex.cpp /Fo:sfwl/core/mutex.obj
cl /D_REENTRANT /EHsc /Zi /Isfwl /c sfwl/core/sfw_time.cpp /Fo:sfwl/core/sfw_time.obj
cl /D_REENTRANT /EHsc /Zi /Isfwl /c sfwl/core/string_builder.cpp /Fo:sfwl/core/string_builder.obj
cl /D_REENTRANT /EHsc /Zi /Isfwl /c sfwl/core/thread.cpp /Fo:sfwl/core/thread.obj
cl /D_REENTRANT /EHsc /Zi /Isfwl /c sfwl/core/socket.cpp /Fo:sfwl/core/socket.obj
@ -60,6 +61,7 @@ cl /D_REENTRANT /Zi ^
sfwl/core/ustring.obj sfwl/core/string_name.obj ^
sfwl/core/vector2i.obj ^
sfwl/core/pool_vector.obj sfwl/core/pool_allocator.obj sfwl/core/mutex.obj sfwl/core/sfw_time.obj ^
sfwl/core/string_builder.obj ^
sfwl/core/dir_access.obj sfwl/core/file_access.obj sfwl/core/thread.obj ^
sfwl/core/socket.obj sfwl/core/inet_address.obj ^
sfwl/core/sub_process.obj ^

View File

@ -35,6 +35,7 @@ ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/pool_vector.cpp -o sfw/core/p
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/pool_allocator.cpp -o sfw/core/pool_allocator.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/mutex.cpp -o sfw/core/mutex.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/sfw_time.cpp -o sfw/core/sfw_time.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/string_builder.cpp -o sfw/core/string_builder.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/thread.cpp -o sfw/core/thread.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/socket.cpp -o sfw/core/socket.o
@ -109,6 +110,7 @@ ccache g++ -Wall \
sfw/core/vector2.o sfw/core/vector2i.o sfw/core/vector3.o \
sfw/core/vector3i.o sfw/core/vector4.o sfw/core/vector4i.o \
sfw/core/pool_vector.o sfw/core/pool_allocator.o sfw/core/mutex.o sfw/core/sfw_time.o \
sfw/core/string_builder.o \
sfw/core/dir_access.o sfw/core/file_access.o sfw/core/thread.o \
sfw/core/socket.o sfw/core/inet_address.o \
sfw/core/sub_process.o \
@ -139,4 +141,4 @@ ccache g++ -Wall \
-lgdi32 -lShlwapi -lws2_32 \
-o game
#export args="-lm -ldl -lpthread -lX11 -w -Iengine/ $args"
#export args="-lm -ldl -lpthread -lX11 -w -Iengine/ $args"

View File

@ -20,6 +20,7 @@ ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/pool_vector.cpp -o sfwl/cor
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/pool_allocator.cpp -o sfwl/core/pool_allocator.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/mutex.cpp -o sfwl/core/mutex.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/sfw_time.cpp -o sfwl/core/sfw_time.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/string_builder.cpp -o sfwl/core/string_builder.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/thread.cpp -o sfwl/core/thread.o
ccache g++ -Wall -D_REENTRANT -g -Isfwl -c sfwl/core/socket.cpp -o sfwl/core/socket.o
@ -52,6 +53,7 @@ ccache g++ -Wall \
sfwl/core/ustring.o sfwl/core/string_name.o \
sfwl/core/vector2i.o \
sfwl/core/pool_vector.o sfwl/core/pool_allocator.o sfwl/core/mutex.o sfwl/core/sfw_time.o \
sfwl/core/string_builder.o \
sfwl/core/dir_access.o sfwl/core/file_access.o sfwl/core/thread.o \
sfwl/core/socket.o sfwl/core/inet_address.o \
sfwl/core/sub_process.o \

View File

@ -110,7 +110,7 @@ public:
}
}
_ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) {
_ALWAYS_INLINE_ explicit SafeNumeric(T p_value = static_cast<T>(0)) {
set(p_value);
}
};
@ -236,7 +236,7 @@ public:
}
}
_ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) :
_ALWAYS_INLINE_ explicit SafeNumeric(T p_value = static_cast<T>(0)) :
value(p_value) {
}
};

147
sfw/core/string_buffer.h Normal file
View File

@ -0,0 +1,147 @@
//--STRIP
#ifndef STRING_BUFFER_H
#define STRING_BUFFER_H
//--STRIP
/*************************************************************************/
/* string_buffer.h */
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
/*************************************************************************/
//--STRIP
#include "core/ustring.h"
//--STRIP
template <int SHORT_BUFFER_SIZE = 64>
class StringBuffer {
CharType short_buffer[SHORT_BUFFER_SIZE];
String buffer;
int string_length;
_FORCE_INLINE_ CharType *current_buffer_ptr() {
return static_cast<String &>(buffer).empty() ? short_buffer : buffer.ptrw();
}
public:
StringBuffer &append(CharType p_char);
StringBuffer &append(const String &p_string);
StringBuffer &append(const char *p_str);
StringBuffer &append(const CharType *p_str, int p_clip_to_len = -1);
_FORCE_INLINE_ void operator+=(CharType p_char) {
append(p_char);
}
_FORCE_INLINE_ void operator+=(const String &p_string) {
append(p_string);
}
_FORCE_INLINE_ void operator+=(const char *p_str) {
append(p_str);
}
_FORCE_INLINE_ void operator+=(const CharType *p_str) {
append(p_str);
}
StringBuffer &reserve(int p_size);
int length() const;
String as_string();
double as_double();
int64_t as_int();
_FORCE_INLINE_ operator String() {
return as_string();
}
StringBuffer() {
string_length = 0;
}
};
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(CharType p_char) {
reserve(string_length + 2);
current_buffer_ptr()[string_length++] = p_char;
return *this;
}
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const String &p_string) {
return append(p_string.get_data());
}
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char *p_str) {
int len = strlen(p_str);
reserve(string_length + len + 1);
CharType *buf = current_buffer_ptr();
for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) {
buf[string_length++] = *c_ptr;
}
return *this;
}
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const CharType *p_str, int p_clip_to_len) {
int len = 0;
while ((p_clip_to_len < 0 || len < p_clip_to_len) && p_str[len]) {
++len;
}
reserve(string_length + len + 1);
memcpy(&(current_buffer_ptr()[string_length]), p_str, len * sizeof(CharType));
string_length += len;
return *this;
}
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) {
if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size()) {
return *this;
}
bool need_copy = string_length > 0 && buffer.empty();
buffer.resize(next_power_of_2(p_size));
if (need_copy) {
memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(CharType));
}
return *this;
}
template <int SHORT_BUFFER_SIZE>
int StringBuffer<SHORT_BUFFER_SIZE>::length() const {
return string_length;
}
template <int SHORT_BUFFER_SIZE>
String StringBuffer<SHORT_BUFFER_SIZE>::as_string() {
current_buffer_ptr()[string_length] = '\0';
if (buffer.empty()) {
return String(short_buffer);
} else {
buffer.resize(string_length + 1);
return buffer;
}
}
template <int SHORT_BUFFER_SIZE>
double StringBuffer<SHORT_BUFFER_SIZE>::as_double() {
current_buffer_ptr()[string_length] = '\0';
return String::to_double(current_buffer_ptr());
}
template <int SHORT_BUFFER_SIZE>
int64_t StringBuffer<SHORT_BUFFER_SIZE>::as_int() {
current_buffer_ptr()[string_length] = '\0';
return String::to_int(current_buffer_ptr());
}
//--STRIP
#endif
//--STRIP

View File

@ -0,0 +1,83 @@
/*************************************************************************/
/* string_builder.cpp */
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
/*************************************************************************/
//--STRIP
#include "string_builder.h"
#include <string.h>
//--STRIP
StringBuilder &StringBuilder::append(const String &p_string) {
if (p_string == String()) {
return *this;
}
strings.push_back(p_string);
appended_strings.push_back(-1);
string_length += p_string.length();
return *this;
}
StringBuilder &StringBuilder::append(const char *p_cstring) {
int32_t len = strlen(p_cstring);
c_strings.push_back(p_cstring);
appended_strings.push_back(len);
string_length += len;
return *this;
}
void StringBuilder::clear() {
string_length = 0;
strings.clear();
c_strings.clear();
appended_strings.clear();
}
String StringBuilder::as_string() const {
if (string_length == 0) {
return "";
}
CharType *buffer = memnew_arr(CharType, string_length);
int current_position = 0;
int pandemonium_string_elem = 0;
int c_string_elem = 0;
for (int i = 0; i < appended_strings.size(); i++) {
if (appended_strings[i] == -1) {
// Pandemonium string
const String &s = strings[pandemonium_string_elem];
memcpy(buffer + current_position, s.ptr(), s.length() * sizeof(CharType));
current_position += s.length();
pandemonium_string_elem++;
} else {
const char *s = c_strings[c_string_elem];
for (int32_t j = 0; j < appended_strings[i]; j++) {
buffer[current_position + j] = s[j];
}
current_position += appended_strings[i];
c_string_elem++;
}
}
String final_string = String(buffer, string_length);
memdelete_arr(buffer);
return final_string;
}

74
sfw/core/string_builder.h Normal file
View File

@ -0,0 +1,74 @@
//--STRIP
#ifndef STRING_BUILDER_H
#define STRING_BUILDER_H
//--STRIP
/*************************************************************************/
/* string_builder.h */
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
/*************************************************************************/
//--STRIP
#include "core/ustring.h"
#include "core/vector.h"
//--STRIP
class StringBuilder {
uint32_t string_length;
Vector<String> strings;
Vector<const char *> c_strings;
// -1 means it's a Pandemonium String
// a natural number means C string.
Vector<int32_t> appended_strings;
public:
StringBuilder &append(const String &p_string);
StringBuilder &append(const char *p_cstring);
void clear();
_FORCE_INLINE_ uint32_t length() {
return string_length;
}
_FORCE_INLINE_ StringBuilder &operator+(const String &p_string) {
return append(p_string);
}
_FORCE_INLINE_ StringBuilder &operator+(const char *p_cstring) {
return append(p_cstring);
}
_FORCE_INLINE_ void operator+=(const String &p_string) {
append(p_string);
}
_FORCE_INLINE_ void operator+=(const char *p_cstring) {
append(p_cstring);
}
_FORCE_INLINE_ int num_strings_appended() const {
return appended_strings.size();
}
_FORCE_INLINE_ uint32_t get_string_length() const {
return string_length;
}
String as_string() const;
_FORCE_INLINE_ operator String() const {
return as_string();
}
StringBuilder() {
string_length = 0;
}
};
//--STRIP
#endif // STRING_BUILDER_H
//--STRIP

View File

@ -110,7 +110,7 @@ public:
}
}
_ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) {
_ALWAYS_INLINE_ explicit SafeNumeric(T p_value = static_cast<T>(0)) {
set(p_value);
}
};
@ -236,7 +236,7 @@ public:
}
}
_ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) :
_ALWAYS_INLINE_ explicit SafeNumeric(T p_value = static_cast<T>(0)) :
value(p_value) {
}
};

147
sfwl/core/string_buffer.h Normal file
View File

@ -0,0 +1,147 @@
//--STRIP
#ifndef STRING_BUFFER_H
#define STRING_BUFFER_H
//--STRIP
/*************************************************************************/
/* string_buffer.h */
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
/*************************************************************************/
//--STRIP
#include "core/ustring.h"
//--STRIP
template <int SHORT_BUFFER_SIZE = 64>
class StringBuffer {
CharType short_buffer[SHORT_BUFFER_SIZE];
String buffer;
int string_length;
_FORCE_INLINE_ CharType *current_buffer_ptr() {
return static_cast<String &>(buffer).empty() ? short_buffer : buffer.ptrw();
}
public:
StringBuffer &append(CharType p_char);
StringBuffer &append(const String &p_string);
StringBuffer &append(const char *p_str);
StringBuffer &append(const CharType *p_str, int p_clip_to_len = -1);
_FORCE_INLINE_ void operator+=(CharType p_char) {
append(p_char);
}
_FORCE_INLINE_ void operator+=(const String &p_string) {
append(p_string);
}
_FORCE_INLINE_ void operator+=(const char *p_str) {
append(p_str);
}
_FORCE_INLINE_ void operator+=(const CharType *p_str) {
append(p_str);
}
StringBuffer &reserve(int p_size);
int length() const;
String as_string();
double as_double();
int64_t as_int();
_FORCE_INLINE_ operator String() {
return as_string();
}
StringBuffer() {
string_length = 0;
}
};
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(CharType p_char) {
reserve(string_length + 2);
current_buffer_ptr()[string_length++] = p_char;
return *this;
}
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const String &p_string) {
return append(p_string.get_data());
}
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char *p_str) {
int len = strlen(p_str);
reserve(string_length + len + 1);
CharType *buf = current_buffer_ptr();
for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) {
buf[string_length++] = *c_ptr;
}
return *this;
}
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const CharType *p_str, int p_clip_to_len) {
int len = 0;
while ((p_clip_to_len < 0 || len < p_clip_to_len) && p_str[len]) {
++len;
}
reserve(string_length + len + 1);
memcpy(&(current_buffer_ptr()[string_length]), p_str, len * sizeof(CharType));
string_length += len;
return *this;
}
template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) {
if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size()) {
return *this;
}
bool need_copy = string_length > 0 && buffer.empty();
buffer.resize(next_power_of_2(p_size));
if (need_copy) {
memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(CharType));
}
return *this;
}
template <int SHORT_BUFFER_SIZE>
int StringBuffer<SHORT_BUFFER_SIZE>::length() const {
return string_length;
}
template <int SHORT_BUFFER_SIZE>
String StringBuffer<SHORT_BUFFER_SIZE>::as_string() {
current_buffer_ptr()[string_length] = '\0';
if (buffer.empty()) {
return String(short_buffer);
} else {
buffer.resize(string_length + 1);
return buffer;
}
}
template <int SHORT_BUFFER_SIZE>
double StringBuffer<SHORT_BUFFER_SIZE>::as_double() {
current_buffer_ptr()[string_length] = '\0';
return String::to_double(current_buffer_ptr());
}
template <int SHORT_BUFFER_SIZE>
int64_t StringBuffer<SHORT_BUFFER_SIZE>::as_int() {
current_buffer_ptr()[string_length] = '\0';
return String::to_int(current_buffer_ptr());
}
//--STRIP
#endif
//--STRIP

View File

@ -0,0 +1,83 @@
/*************************************************************************/
/* string_builder.cpp */
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
/*************************************************************************/
//--STRIP
#include "string_builder.h"
#include <string.h>
//--STRIP
StringBuilder &StringBuilder::append(const String &p_string) {
if (p_string == String()) {
return *this;
}
strings.push_back(p_string);
appended_strings.push_back(-1);
string_length += p_string.length();
return *this;
}
StringBuilder &StringBuilder::append(const char *p_cstring) {
int32_t len = strlen(p_cstring);
c_strings.push_back(p_cstring);
appended_strings.push_back(len);
string_length += len;
return *this;
}
void StringBuilder::clear() {
string_length = 0;
strings.clear();
c_strings.clear();
appended_strings.clear();
}
String StringBuilder::as_string() const {
if (string_length == 0) {
return "";
}
CharType *buffer = memnew_arr(CharType, string_length);
int current_position = 0;
int pandemonium_string_elem = 0;
int c_string_elem = 0;
for (int i = 0; i < appended_strings.size(); i++) {
if (appended_strings[i] == -1) {
// Pandemonium string
const String &s = strings[pandemonium_string_elem];
memcpy(buffer + current_position, s.ptr(), s.length() * sizeof(CharType));
current_position += s.length();
pandemonium_string_elem++;
} else {
const char *s = c_strings[c_string_elem];
for (int32_t j = 0; j < appended_strings[i]; j++) {
buffer[current_position + j] = s[j];
}
current_position += appended_strings[i];
c_string_elem++;
}
}
String final_string = String(buffer, string_length);
memdelete_arr(buffer);
return final_string;
}

View File

@ -0,0 +1,74 @@
//--STRIP
#ifndef STRING_BUILDER_H
#define STRING_BUILDER_H
//--STRIP
/*************************************************************************/
/* string_builder.h */
/* From https://github.com/Relintai/pandemonium_engine (MIT) */
/*************************************************************************/
//--STRIP
#include "core/ustring.h"
#include "core/vector.h"
//--STRIP
class StringBuilder {
uint32_t string_length;
Vector<String> strings;
Vector<const char *> c_strings;
// -1 means it's a Pandemonium String
// a natural number means C string.
Vector<int32_t> appended_strings;
public:
StringBuilder &append(const String &p_string);
StringBuilder &append(const char *p_cstring);
void clear();
_FORCE_INLINE_ uint32_t length() {
return string_length;
}
_FORCE_INLINE_ StringBuilder &operator+(const String &p_string) {
return append(p_string);
}
_FORCE_INLINE_ StringBuilder &operator+(const char *p_cstring) {
return append(p_cstring);
}
_FORCE_INLINE_ void operator+=(const String &p_string) {
append(p_string);
}
_FORCE_INLINE_ void operator+=(const char *p_cstring) {
append(p_cstring);
}
_FORCE_INLINE_ int num_strings_appended() const {
return appended_strings.size();
}
_FORCE_INLINE_ uint32_t get_string_length() const {
return string_length;
}
String as_string() const;
_FORCE_INLINE_ operator String() const {
return as_string();
}
StringBuilder() {
string_length = 0;
}
};
//--STRIP
#endif // STRING_BUILDER_H
//--STRIP

View File

@ -412,6 +412,24 @@ Main
</details>
<a name="CLASS_StringBuffer"></a>
<details><summary>StringName</summary>
A class that makes it easy to copy Strings into a buffer.
|||CLASS_StringBuffer|||
</details>
<a name="CLASS_StringBuilder"></a>
<details><summary>StringName</summary>
A class that makes it easy and fast to concatenate lots of Strings.
|||CLASS_StringBuilder|||
</details>
Containers
------------------------------------------------------------------

View File

@ -437,6 +437,24 @@ Main
</details>
<a name="CLASS_StringBuffer"></a>
<details><summary>StringName</summary>
A class that makes it easy to copy Strings into a buffer.
|||CLASS_StringBuffer|||
</details>
<a name="CLASS_StringBuilder"></a>
<details><summary>StringName</summary>
A class that makes it easy and fast to concatenate lots of Strings.
|||CLASS_StringBuilder|||
</details>
Containers
------------------------------------------------------------------

View File

@ -412,6 +412,24 @@ Main
</details>
<a name="CLASS_StringBuffer"></a>
<details><summary>StringName</summary>
A class that makes it easy to copy Strings into a buffer.
|||CLASS_StringBuffer|||
</details>
<a name="CLASS_StringBuilder"></a>
<details><summary>StringName</summary>
A class that makes it easy and fast to concatenate lots of Strings.
|||CLASS_StringBuilder|||
</details>
Containers
------------------------------------------------------------------

View File

@ -437,6 +437,24 @@ Main
</details>
<a name="CLASS_StringBuffer"></a>
<details><summary>StringName</summary>
A class that makes it easy to copy Strings into a buffer.
|||CLASS_StringBuffer|||
</details>
<a name="CLASS_StringBuilder"></a>
<details><summary>StringName</summary>
A class that makes it easy and fast to concatenate lots of Strings.
|||CLASS_StringBuilder|||
</details>
Containers
------------------------------------------------------------------

View File

@ -437,6 +437,24 @@ Main
</details>
<a name="CLASS_StringBuffer"></a>
<details><summary>StringName</summary>
A class that makes it easy to copy Strings into a buffer.
|||CLASS_StringBuffer|||
</details>
<a name="CLASS_StringBuilder"></a>
<details><summary>StringName</summary>
A class that makes it easy and fast to concatenate lots of Strings.
|||CLASS_StringBuilder|||
</details>
Containers
------------------------------------------------------------------

View File

@ -437,6 +437,24 @@ Main
</details>
<a name="CLASS_StringBuffer"></a>
<details><summary>StringName</summary>
A class that makes it easy to copy Strings into a buffer.
|||CLASS_StringBuffer|||
</details>
<a name="CLASS_StringBuilder"></a>
<details><summary>StringName</summary>
A class that makes it easy and fast to concatenate lots of Strings.
|||CLASS_StringBuilder|||
</details>
Containers
------------------------------------------------------------------

View File

@ -314,6 +314,24 @@ Main
</details>
<a name="CLASS_StringBuffer"></a>
<details><summary>StringName</summary>
A class that makes it easy to copy Strings into a buffer.
|||CLASS_StringBuffer|||
</details>
<a name="CLASS_StringBuilder"></a>
<details><summary>StringName</summary>
A class that makes it easy and fast to concatenate lots of Strings.
|||CLASS_StringBuilder|||
</details>
Containers
------------------------------------------------------------------

View File

@ -314,6 +314,24 @@ Main
</details>
<a name="CLASS_StringBuffer"></a>
<details><summary>StringName</summary>
A class that makes it easy to copy Strings into a buffer.
|||CLASS_StringBuffer|||
</details>
<a name="CLASS_StringBuilder"></a>
<details><summary>StringName</summary>
A class that makes it easy and fast to concatenate lots of Strings.
|||CLASS_StringBuilder|||
</details>
Containers
------------------------------------------------------------------

View File

@ -192,6 +192,12 @@
//--STRIP
{{FILE:sfw/core/vector4.cpp}}
//--STRIP
//#include "string_builder.h"
//#include <string.h>
//--STRIP
{{FILE:sfw/core/string_builder.cpp}}
//--STRIP
//#include "file_access.h"
//--STRIP

View File

@ -413,6 +413,17 @@
//--STRIP
{{FILE:sfw/core/hash_set.h}}
//--STRIP
//#include "core/ustring.h"
//--STRIP
{{FILE:sfw/core/string_buffer.h}}
//--STRIP
//#include "core/ustring.h"
//#include "core/vector.h"
//--STRIP
{{FILE:sfw/core/string_builder.h}}
//--STRIP
//#include "core/error_list.h"
//#include "core/ustring.h"

View File

@ -398,6 +398,12 @@
//--STRIP
{{FILE:sfw/core/vector4.cpp}}
//--STRIP
//#include "string_builder.h"
//#include <string.h>
//--STRIP
{{FILE:sfw/core/string_builder.cpp}}
//--STRIP
//#include "file_access.h"
//--STRIP

View File

@ -414,6 +414,17 @@
//--STRIP
{{FILE:sfw/core/hash_set.h}}
//--STRIP
//#include "core/ustring.h"
//--STRIP
{{FILE:sfw/core/string_buffer.h}}
//--STRIP
//#include "core/ustring.h"
//#include "core/vector.h"
//--STRIP
{{FILE:sfw/core/string_builder.h}}
//--STRIP
//#include "core/error_list.h"
//#include "core/ustring.h"

View File

@ -200,6 +200,12 @@
//--STRIP
{{FILE:sfw/core/vector4.cpp}}
//--STRIP
//#include "string_builder.h"
//#include <string.h>
//--STRIP
{{FILE:sfw/core/string_builder.cpp}}
//--STRIP
//#include "file_access.h"
//--STRIP

View File

@ -413,6 +413,17 @@
//--STRIP
{{FILE:sfw/core/hash_set.h}}
//--STRIP
//#include "core/ustring.h"
//--STRIP
{{FILE:sfw/core/string_buffer.h}}
//--STRIP
//#include "core/ustring.h"
//#include "core/vector.h"
//--STRIP
{{FILE:sfw/core/string_builder.h}}
//--STRIP
//#include "core/error_list.h"
//#include "core/ustring.h"

View File

@ -398,6 +398,12 @@
//--STRIP
{{FILE:sfw/core/vector4.cpp}}
//--STRIP
//#include "string_builder.h"
//#include <string.h>
//--STRIP
{{FILE:sfw/core/string_builder.cpp}}
//--STRIP
//#include "file_access.h"
//--STRIP

View File

@ -414,6 +414,17 @@
//--STRIP
{{FILE:sfw/core/hash_set.h}}
//--STRIP
//#include "core/ustring.h"
//--STRIP
{{FILE:sfw/core/string_buffer.h}}
//--STRIP
//#include "core/ustring.h"
//#include "core/vector.h"
//--STRIP
{{FILE:sfw/core/string_builder.h}}
//--STRIP
//#include "core/error_list.h"
//#include "core/ustring.h"

View File

@ -398,6 +398,12 @@
//--STRIP
{{FILE:sfw/core/vector4.cpp}}
//--STRIP
//#include "string_builder.h"
//#include <string.h>
//--STRIP
{{FILE:sfw/core/string_builder.cpp}}
//--STRIP
//#include "file_access.h"
//--STRIP

View File

@ -414,6 +414,17 @@
//--STRIP
{{FILE:sfw/core/hash_set.h}}
//--STRIP
//#include "core/ustring.h"
//--STRIP
{{FILE:sfw/core/string_buffer.h}}
//--STRIP
//#include "core/ustring.h"
//#include "core/vector.h"
//--STRIP
{{FILE:sfw/core/string_builder.h}}
//--STRIP
//#include "core/error_list.h"
//#include "core/ustring.h"

View File

@ -398,6 +398,12 @@
//--STRIP
{{FILE:sfw/core/vector4.cpp}}
//--STRIP
//#include "string_builder.h"
//#include <string.h>
//--STRIP
{{FILE:sfw/core/string_builder.cpp}}
//--STRIP
//#include "file_access.h"
//--STRIP

View File

@ -414,6 +414,17 @@
//--STRIP
{{FILE:sfw/core/hash_set.h}}
//--STRIP
//#include "core/ustring.h"
//--STRIP
{{FILE:sfw/core/string_buffer.h}}
//--STRIP
//#include "core/ustring.h"
//#include "core/vector.h"
//--STRIP
{{FILE:sfw/core/string_builder.h}}
//--STRIP
//#include "core/error_list.h"
//#include "core/ustring.h"

View File

@ -119,6 +119,12 @@
//--STRIP
{{FILE:sfwl/core/pcg.cpp}}
//--STRIP
//#include "string_builder.h"
//#include <string.h>
//--STRIP
{{FILE:sfw/core/string_builder.cpp}}
//--STRIP
//#include "file_access.h"
//--STRIP

View File

@ -326,6 +326,17 @@
//--STRIP
{{FILE:sfwl/core/hash_set.h}}
//--STRIP
//#include "core/ustring.h"
//--STRIP
{{FILE:sfw/core/string_buffer.h}}
//--STRIP
//#include "core/ustring.h"
//#include "core/vector.h"
//--STRIP
{{FILE:sfw/core/string_builder.h}}
//--STRIP
//#include "core/error_list.h"
//#include "core/ustring.h"

View File

@ -119,6 +119,12 @@
//--STRIP
{{FILE:sfwl/core/pcg.cpp}}
//--STRIP
//#include "string_builder.h"
//#include <string.h>
//--STRIP
{{FILE:sfw/core/string_builder.cpp}}
//--STRIP
//#include "file_access.h"
//--STRIP

View File

@ -326,6 +326,17 @@
//--STRIP
{{FILE:sfwl/core/hash_set.h}}
//--STRIP
//#include "core/ustring.h"
//--STRIP
{{FILE:sfw/core/string_buffer.h}}
//--STRIP
//#include "core/ustring.h"
//#include "core/vector.h"
//--STRIP
{{FILE:sfw/core/string_builder.h}}
//--STRIP
//#include "core/error_list.h"
//#include "core/ustring.h"