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