Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the use of StringBuilder with StringBuffer #77158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/io/config_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#include "core/io/file_access_encrypted.h"
#include "core/os/keyboard.h"
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"
#include "core/variant/variant_parser.h"

PackedStringArray ConfigFile::_get_sections() const {
Expand Down Expand Up @@ -132,7 +132,7 @@ void ConfigFile::erase_section_key(const String &p_section, const String &p_key)
}

String ConfigFile::encode_to_text() const {
StringBuilder sb;
StringBuffer<> sb;
bool first = true;
for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
if (first) {
Expand Down
4 changes: 2 additions & 2 deletions core/io/remote_filesystem_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
#include "core/io/stream_peer_tcp.h"
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"

#define FILESYSTEM_CACHE_VERSION 1
#define FILESYSTEM_PROTOCOL_VERSION 1
Expand Down Expand Up @@ -214,7 +214,7 @@ Error RemoteFilesystemClient::_synchronize_with_server(const String &p_host, int
// Encode file cache to send it via network.
Vector<uint8_t> file_cache_buffer;
if (file_cache.size()) {
StringBuilder sbuild;
StringBuffer<> sbuild;
for (int i = 0; i < file_cache.size(); i++) {
sbuild.append(file_cache[i].path);
sbuild.append("::");
Expand Down
33 changes: 29 additions & 4 deletions core/string/string_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class StringBuffer {
int length() const;

String as_string();
String as_string() const; // Const variant.

double as_double();
int64_t as_int();
Expand All @@ -88,18 +89,33 @@ StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(char32_

template <int SHORT_BUFFER_SIZE>
StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const String &p_string) {
return append(p_string.get_data());
const int rhs_len = p_string.length();
if (rhs_len == 0) {
return *this;
}

reserve(string_length + rhs_len + 1);

const char32_t *src = p_string.ptr();
char32_t *dst = current_buffer_ptr() + string_length;

memcpy(dst, src, rhs_len * sizeof(char32_t));
string_length += rhs_len;

return *this;
}

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);

char32_t *buf = current_buffer_ptr();
for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) {
buf[string_length++] = *c_ptr;
char32_t *buf = current_buffer_ptr() + string_length;
for (int i = 0; i < len; i++) {
buf[i] = p_str[i];
}

string_length += len;
return *this;
}

Expand Down Expand Up @@ -147,6 +163,15 @@ String StringBuffer<SHORT_BUFFER_SIZE>::as_string() {
}
}

template <int SHORT_BUFFER_SIZE>
String StringBuffer<SHORT_BUFFER_SIZE>::as_string() const {
if (buffer.is_empty()) {
return String(short_buffer, string_length);
} else {
return buffer.substr(0, string_length);
}
}

template <int SHORT_BUFFER_SIZE>
double StringBuffer<SHORT_BUFFER_SIZE>::as_double() {
current_buffer_ptr()[string_length] = '\0';
Expand Down
32 changes: 16 additions & 16 deletions drivers/gles3/shader_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void ShaderGLES3::_setup(const char *p_vertex_code, const char *p_fragment_code,
feedbacks = p_feedback;
feedback_count = p_feedback_count;

StringBuilder tohash;
StringBuffer<> tohash;
/*
tohash.append("[SpirvCacheKey]");
tohash.append(RenderingDevice::get_singleton()->shader_get_spirv_cache_key());
Expand Down Expand Up @@ -160,7 +160,7 @@ RID ShaderGLES3::version_create() {
return version_owner.make_rid(version);
}

void ShaderGLES3::_build_variant_code(StringBuilder &builder, uint32_t p_variant, const Version *p_version, StageType p_stage_type, uint64_t p_specialization) {
void ShaderGLES3::_build_variant_code(StringBuffer<> &builder, uint32_t p_variant, const Version *p_version, StageType p_stage_type, uint64_t p_specialization) {
if (RasterizerGLES3::is_gles_over_gl()) {
builder.append("#version 330\n");
builder.append("#define USE_GLES_OVER_GL\n");
Expand Down Expand Up @@ -315,11 +315,11 @@ void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_

//vertex stage
{
StringBuilder builder;
_build_variant_code(builder, p_variant, p_version, STAGE_TYPE_VERTEX, p_specialization);
StringBuffer<> buffer;
_build_variant_code(buffer, p_variant, p_version, STAGE_TYPE_VERTEX, p_specialization);

spec.vert_id = glCreateShader(GL_VERTEX_SHADER);
String builder_string = builder.as_string();
String builder_string = buffer.as_string();
CharString cs = builder_string.utf8();
const char *cstr = cs.ptr();
glShaderSource(spec.vert_id, 1, &cstr, nullptr);
Expand Down Expand Up @@ -363,11 +363,11 @@ void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_

//fragment stage
{
StringBuilder builder;
_build_variant_code(builder, p_variant, p_version, STAGE_TYPE_FRAGMENT, p_specialization);
StringBuffer<> buffer;
_build_variant_code(buffer, p_variant, p_version, STAGE_TYPE_FRAGMENT, p_specialization);

spec.frag_id = glCreateShader(GL_FRAGMENT_SHADER);
String builder_string = builder.as_string();
String builder_string = buffer.as_string();
CharString cs = builder_string.utf8();
const char *cstr = cs.ptr();
glShaderSource(spec.frag_id, 1, &cstr, nullptr);
Expand Down Expand Up @@ -484,24 +484,24 @@ RS::ShaderNativeSourceCode ShaderGLES3::version_get_native_source_code(RID p_ver
//vertex stage

{
StringBuilder builder;
_build_variant_code(builder, i, version, STAGE_TYPE_VERTEX, specialization_default_mask);
StringBuffer<> buffer;
_build_variant_code(buffer, i, version, STAGE_TYPE_VERTEX, specialization_default_mask);

RS::ShaderNativeSourceCode::Version::Stage stage;
stage.name = "vertex";
stage.code = builder.as_string();
stage.code = buffer.as_string();

source_code.versions.write[i].stages.push_back(stage);
}

//fragment stage
{
StringBuilder builder;
_build_variant_code(builder, i, version, STAGE_TYPE_FRAGMENT, specialization_default_mask);
StringBuffer<> buffer;
_build_variant_code(buffer, i, version, STAGE_TYPE_FRAGMENT, specialization_default_mask);

RS::ShaderNativeSourceCode::Version::Stage stage;
stage.name = "fragment";
stage.code = builder.as_string();
stage.code = buffer.as_string();

source_code.versions.write[i].stages.push_back(stage);
}
Expand All @@ -511,7 +511,7 @@ RS::ShaderNativeSourceCode ShaderGLES3::version_get_native_source_code(RID p_ver
}

String ShaderGLES3::_version_get_sha1(Version *p_version) const {
StringBuilder hash_build;
StringBuffer<> hash_build;

hash_build.append("[uniforms]");
hash_build.append(p_version->uniforms.get_data());
Expand Down Expand Up @@ -781,7 +781,7 @@ void ShaderGLES3::initialize(const String &p_general_defines, int p_base_texture
_init();

if (shader_cache_dir != String()) {
StringBuilder hash_build;
StringBuffer<> hash_build;

hash_build.append("[base_hash]");
hash_build.append(base_sha256);
Expand Down
4 changes: 2 additions & 2 deletions drivers/gles3/shader_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#include "core/math/projection.h"
#include "core/os/mutex.h"
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"
#include "core/templates/hash_map.h"
#include "core/templates/local_vector.h"
#include "core/templates/rb_map.h"
Expand Down Expand Up @@ -158,7 +158,7 @@ class ShaderGLES3 {

StageTemplate stage_templates[STAGE_TYPE_MAX];

void _build_variant_code(StringBuilder &p_builder, uint32_t p_variant, const Version *p_version, StageType p_stage_type, uint64_t p_specialization);
void _build_variant_code(StringBuffer<> &p_builder, uint32_t p_variant, const Version *p_version, StageType p_stage_type, uint64_t p_specialization);

void _add_stage(const char *p_code, StageType p_stage_type);

Expand Down
4 changes: 2 additions & 2 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#include "core/input/input.h"
#include "core/os/keyboard.h"
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
Expand Down Expand Up @@ -949,7 +949,7 @@ void CodeTextEditor::_line_col_changed() {
}
}

StringBuilder sb;
StringBuffer<> sb;
sb.append(itos(text_editor->get_caret_line() + 1).lpad(4));
sb.append(" : ");
sb.append(itos(positional_column + 1).lpad(3));
Expand Down
2 changes: 1 addition & 1 deletion editor/script_create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "core/config/project_settings.h"
#include "core/io/file_access.h"
#include "core/io/resource_saver.h"
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"
#include "editor/create_dialog.h"
#include "editor/editor_file_system.h"
#include "editor/editor_node.h"
Expand Down
2 changes: 1 addition & 1 deletion editor/shader_create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void ShaderCreateDialog::_create_new() {
text_shader.instantiate();
shader = text_shader;

StringBuilder code;
StringBuffer<> code;
code += vformat("shader_type %s;\n", mode_menu->get_text().to_snake_case());

if (current_template == 0) { // Default template.
Expand Down
6 changes: 3 additions & 3 deletions modules/gdscript/gdscript_disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "gdscript.h"
#include "gdscript_function.h"

#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"

static String _get_variant_string(const Variant &p_variant) {
String txt;
Expand Down Expand Up @@ -101,7 +101,7 @@ void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
#define DADDR(m_ip) (_disassemble_address(_script, *this, _code_ptr[ip + m_ip]))

for (int ip = 0; ip < _code_size;) {
StringBuilder text;
StringBuffer<> text;
int incr = 0;

text += " ";
Expand Down Expand Up @@ -1282,7 +1282,7 @@ void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const {
}

ip += incr;
if (text.get_string_length() > 0) {
if (text.length() > 0) {
print_line(text.as_string());
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

#ifdef DEBUG_ENABLED
#include "core/os/os.h"
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"
#include "servers/text_server.h"
#endif

Expand Down
4 changes: 2 additions & 2 deletions modules/gdscript/gdscript_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#include "core/variant/variant.h"

#ifdef DEBUG_ENABLED
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"
#endif

class GDScriptParser {
Expand Down Expand Up @@ -1610,7 +1610,7 @@ class GDScriptParser {
class TreePrinter {
int indent_level = 0;
String indent;
StringBuilder printed;
StringBuffer<> printed;
bool pending_indent = false;

void increase_indent();
Expand Down
6 changes: 3 additions & 3 deletions modules/gdscript/tests/gdscript_test_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "core/io/dir_access.h"
#include "core/io/file_access_pack.h"
#include "core/os/os.h"
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"
#include "scene/resources/packed_scene.h"

#include "tests/test_macros.h"
Expand Down Expand Up @@ -450,7 +450,7 @@ void GDScriptTest::error_handler(void *p_this, const char *p_function, const cha

result->status = GDTEST_RUNTIME_ERROR;

StringBuilder builder;
StringBuffer<> builder;
builder.append(">> ");
// Only include the function, file and line for script errors, otherwise the
// test outputs changes based on the platform/compiler.
Expand Down Expand Up @@ -600,7 +600,7 @@ GDScriptTest::TestResult GDScriptTest::execute_test_code(bool p_is_generating) {
}

#ifdef DEBUG_ENABLED
StringBuilder warning_string;
StringBuffer<> warning_string;
for (const GDScriptWarning &E : parser.get_warnings()) {
const GDScriptWarning warning = E;
warning_string.append(">> WARNING");
Expand Down
12 changes: 6 additions & 6 deletions modules/gdscript/tests/test_gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "core/io/file_access_pack.h"
#include "core/os/main_loop.h"
#include "core/os/os.h"
#include "core/string/string_builder.h"
#include "core/string/string_buffer.h"
#include "scene/resources/packed_scene.h"

#ifdef TOOLS_ENABLED
Expand All @@ -64,7 +64,7 @@ static void test_tokenizer(const String &p_code, const Vector<String> &p_lines)

GDScriptTokenizer::Token current = tokenizer.scan();
while (current.type != GDScriptTokenizer::Token::TK_EOF) {
StringBuilder token;
StringBuffer<> token;
token += " --> "; // Padding for line number.

for (int l = current.start_line; l <= current.end_line && l <= p_lines.size(); l++) {
Expand All @@ -73,7 +73,7 @@ static void test_tokenizer(const String &p_code, const Vector<String> &p_lines)

{
// Print carets to point at the token.
StringBuilder pointer;
StringBuffer<> pointer;
pointer += " "; // Padding for line number.
int rightmost_column = current.rightmost_column;
if (current.end_line > current.start_line) {
Expand All @@ -95,7 +95,7 @@ static void test_tokenizer(const String &p_code, const Vector<String> &p_lines)
token += "(";
token += Variant::get_type_name(current.literal.get_type());
token += ") ";
token += current.literal;
token += current.literal.operator String();
}

print_line(token.as_string());
Expand Down Expand Up @@ -129,7 +129,7 @@ static void test_tokenizer_buffer(const Vector<uint8_t> &p_buffer, const Vector<

GDScriptTokenizer::Token current = tokenizer.scan();
while (current.type != GDScriptTokenizer::Token::TK_EOF) {
StringBuilder token;
StringBuffer<> token;
token += " --> "; // Padding for line number.

for (int l = current.start_line; l <= current.end_line && l <= p_lines.size(); l++) {
Expand All @@ -142,7 +142,7 @@ static void test_tokenizer_buffer(const Vector<uint8_t> &p_buffer, const Vector<
token += "(";
token += Variant::get_type_name(current.literal.get_type());
token += ") ";
token += current.literal;
token += current.literal.operator String();
}

print_line(token.as_string());
Expand Down
Loading
Loading