Debian Codegraph @

modules/mono/utils/string_utils.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* string_utils.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_utils.h"
32
33 #include "core/io/file_access.h"
34
35 #include <cstdio>
36 #include <cstdlib>
37
38 namespace {
39
40 int sfind(const String &p_text, int p_from) {
41 if (p_from < 0) {
42 return -1;
43 }
44
45 int src_len = 2;
46 int len = p_text.length();
47
48 if (len == 0) {
49 return -1;
50 }
51
52 const char32_t *src = p_text.get_data();
53
54 for (int i = p_from; i <= (len - src_len); i++) {
55 bool found = true;
56
57 for (int j = 0; j < src_len; j++) {
58 int read_pos = i + j;
59
60 ERR_FAIL_COND_V(read_pos >= len, -1);
61
62 switch (j) {
63 case 0:
64 found = src[read_pos] == '%';
65 break;
66 case 1: {
67 char32_t c = src[read_pos];
68 found = src[read_pos] == 's' || (c >= '0' && c <= '5');
69 break;
70 }
71 default:
72 found = false;
73 }
74
75 if (!found) {
76 break;
77 }
78 }
79
80 if (found) {
81 return i;
82 }
83 }
84
85 return -1;
86 }
87 } // namespace
88
89 String sformat(const String &p_text, const String &p1, const String &p2,
90 const String &p3, const String &p4, const String &p5, const String &p6) {
91 if (p_text.length() < 2) {
92 return p_text;
93 }
94
95 String args[6] = { p1, p2, p3, p4, p5, p6 };
96
97 String new_string;
98
99 int findex = 0;
100 int search_from = 0;
101 int result = 0;
102
103 while ((result = sfind(p_text, search_from)) >= 0) {
104 char32_t c = p_text[result + 1];
105
106 int req_index = (c == 's' ? findex++ : c - '0');
107
108 new_string += p_text.substr(search_from, result - search_from);
109 new_string += args[req_index];
110 search_from = result + 2;
111 }
112
113 new_string += p_text.substr(search_from);
114
115 return new_string;
116 }
117
118 #ifdef TOOLS_ENABLED
119 bool is_csharp_keyword(const String &p_name) {
120 // Reserved keywords
121
122 return p_name == "abstract" || p_name == "as" || p_name == "base" || p_name == "bool" ||
123 p_name == "break" || p_name == "byte" || p_name == "case" || p_name == "catch" ||
124 p_name == "char" || p_name == "checked" || p_name == "class" || p_name == "const" ||
125 p_name == "continue" || p_name == "decimal" || p_name == "default" || p_name == "delegate" ||
126 p_name == "do" || p_name == "double" || p_name == "else" || p_name == "enum" ||
127 p_name == "event" || p_name == "explicit" || p_name == "extern" || p_name == "false" ||
128 p_name == "finally" || p_name == "fixed" || p_name == "float" || p_name == "for" ||
129 p_name == "foreach" || p_name == "goto" || p_name == "if" || p_name == "implicit" ||
130 p_name == "in" || p_name == "int" || p_name == "interface" || p_name == "internal" ||
131 p_name == "is" || p_name == "lock" || p_name == "long" || p_name == "namespace" ||
132 p_name == "new" || p_name == "null" || p_name == "object" || p_name == "operator" ||
133 p_name == "out" || p_name == "override" || p_name == "params" || p_name == "private" ||
134 p_name == "protected" || p_name == "public" || p_name == "readonly" || p_name == "ref" ||
135 p_name == "return" || p_name == "sbyte" || p_name == "sealed" || p_name == "short" ||
136 p_name == "sizeof" || p_name == "stackalloc" || p_name == "static" || p_name == "string" ||
137 p_name == "struct" || p_name == "switch" || p_name == "this" || p_name == "throw" ||
138 p_name == "true" || p_name == "try" || p_name == "typeof" || p_name == "uint" || p_name == "ulong" ||
139 p_name == "unchecked" || p_name == "unsafe" || p_name == "ushort" || p_name == "using" ||
140 p_name == "virtual" || p_name == "volatile" || p_name == "void" || p_name == "while";
141 }
142
143 String escape_csharp_keyword(const String &p_name) {
144 return is_csharp_keyword(p_name) ? "@" + p_name : p_name;
145 }
146 #endif
147
148 Error read_all_file_utf8(const String &p_path, String &r_content) {
149 Vector<uint8_t> sourcef;
150 Error err;
151 Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
152 ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
153
154 uint64_t len = f->get_length();
155 sourcef.resize(len + 1);
156 uint8_t *w = sourcef.ptrw();
157 uint64_t r = f->get_buffer(w, len);
158 ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
159 w[len] = 0;
160
161 String source;
162 if (source.append_utf8((const char *)w, len) != OK) {
163 ERR_FAIL_V(ERR_INVALID_DATA);
164 }
165
166 r_content = source;
167 return OK;
168 }
169
170 // TODO: Move to variadic templates once we upgrade to C++11
171
172 String str_format(const char *p_format, ...) {
173 va_list list;
174
175 va_start(list, p_format);
176 String res = str_format(p_format, list);
177 va_end(list);
178
179 return res;
180 }
181
182 #if defined(MINGW_ENABLED)
183 #define RSnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf_s(m_buffer, m_count, _TRUNCATE, m_format, m_args_copy)
184 #define RScprintf(m_format, m_args_copy) _vscprintf(m_format, m_args_copy)
185 #else
186 #define RSnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf(m_buffer, m_count, m_format, m_args_copy)
187 #define RScprintf(m_format, m_args_copy) vsnprintf(nullptr, 0, p_format, m_args_copy)
188 #endif
189
190 String str_format(const char *p_format, va_list p_list) {
191 char *buffer = str_format_new(p_format, p_list);
192
193 String res(buffer);
194 memdelete_arr(buffer);
195
196 return res;
197 }
198
199 char *str_format_new(const char *p_format, ...) {
200 va_list list;
201
202 va_start(list, p_format);
203 char *res = str_format_new(p_format, list);
204 va_end(list);
205
206 return res;
207 }
208
209 char *str_format_new(const char *p_format, va_list p_list) {
210 va_list list;
211
212 va_copy(list, p_list);
213 int len = RScprintf(p_format, list);
214 va_end(list);
215
216 len += 1; // for the trailing '/0'
217
218 char *buffer(memnew_arr(char, len));
219
220 va_copy(list, p_list);
221 RSnprintf(buffer, len, p_format, list);
222 va_end(list);
223
224 return buffer;
225 }