Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/string/string_builder.cpp
9903 views
1
/**************************************************************************/
2
/* string_builder.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "string_builder.h"
32
33
StringBuilder &StringBuilder::append(const String &p_string) {
34
if (p_string.is_empty()) {
35
return *this;
36
}
37
38
strings.push_back(p_string);
39
appended_strings.push_back(-1);
40
41
string_length += p_string.length();
42
43
return *this;
44
}
45
46
StringBuilder &StringBuilder::append(const char *p_cstring) {
47
int32_t len = strlen(p_cstring);
48
49
c_strings.push_back(p_cstring);
50
appended_strings.push_back(len);
51
52
string_length += len;
53
54
return *this;
55
}
56
57
String StringBuilder::as_string() const {
58
if (string_length == 0) {
59
return "";
60
}
61
62
String string;
63
string.resize_uninitialized(string_length + 1);
64
char32_t *buffer = string.ptrw();
65
66
int current_position = 0;
67
68
int godot_string_elem = 0;
69
int c_string_elem = 0;
70
71
for (uint32_t i = 0; i < appended_strings.size(); i++) {
72
const int32_t str_len = appended_strings[i];
73
74
if (str_len == -1) {
75
// Godot string
76
const String &s = strings[godot_string_elem];
77
78
memcpy(buffer + current_position, s.ptr(), s.length() * sizeof(char32_t));
79
80
current_position += s.length();
81
82
godot_string_elem++;
83
} else {
84
const char *s = c_strings[c_string_elem];
85
86
for (int32_t j = 0; j < str_len; j++) {
87
buffer[current_position + j] = s[j];
88
}
89
90
current_position += str_len;
91
92
c_string_elem++;
93
}
94
}
95
buffer[current_position] = 0;
96
97
return string;
98
}
99
100