Debian Codegraph @

core/string/string_buffer.h

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