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