/**************************************************************************/1/* string_builder.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "string_builder.h"3132StringBuilder &StringBuilder::append(const String &p_string) {33if (p_string.is_empty()) {34return *this;35}3637strings.push_back(p_string);38appended_strings.push_back(-1);3940string_length += p_string.length();4142return *this;43}4445StringBuilder &StringBuilder::append(const char *p_cstring) {46int32_t len = strlen(p_cstring);4748c_strings.push_back(p_cstring);49appended_strings.push_back(len);5051string_length += len;5253return *this;54}5556String StringBuilder::as_string() const {57if (string_length == 0) {58return "";59}6061String string;62string.resize_uninitialized(string_length + 1);63char32_t *buffer = string.ptrw();6465int current_position = 0;6667int godot_string_elem = 0;68int c_string_elem = 0;6970for (uint32_t i = 0; i < appended_strings.size(); i++) {71const int32_t str_len = appended_strings[i];7273if (str_len == -1) {74// Godot string75const String &s = strings[godot_string_elem];7677memcpy(buffer + current_position, s.ptr(), s.length() * sizeof(char32_t));7879current_position += s.length();8081godot_string_elem++;82} else {83const char *s = c_strings[c_string_elem];8485for (int32_t j = 0; j < str_len; j++) {86buffer[current_position + j] = s[j];87}8889current_position += str_len;9091c_string_elem++;92}93}94buffer[current_position] = 0;9596return string;97}9899100