Blender V5.3
string.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <cstdarg>
6#include <cstdio>
7
8#include <algorithm>
9#include <cctype>
10
11#include "util/string.h"
12#include "util/types_float4.h"
13
14#ifdef _WIN32
15# include "util/windows.h"
16# ifndef vsnprintf
17# define vsnprintf _vsnprintf
18# endif
19#else
20# include <locale.h>
21#endif /* _WIN32 */
22
24
25string string_printf(const char *format, ...)
26{
27 vector<char> str(128, 0);
28
29 while (true) {
30 va_list args;
31 int result;
32
33 va_start(args, format);
34 result = vsnprintf(str.data(), str.size(), format, args);
35 va_end(args);
36
37 if (result == -1) {
38 /* not enough space or formatting error */
39 if (str.size() > 65536) {
40 assert(0);
41 return string("");
42 }
43
44 str.resize(str.size() * 2, 0);
45 continue;
46 }
47 if (result >= (int)str.size()) {
48 /* not enough space */
49 str.resize(result + 1, 0);
50 continue;
51 }
52
53 return string(str.data());
54 }
55}
56
57bool string_iequals(const string &a, const string &b)
58{
59 if (a.size() == b.size()) {
60 for (size_t i = 0; i < a.size(); i++) {
61 if (toupper(a[i]) != toupper(b[i])) {
62 return false;
63 }
64 }
65
66 return true;
67 }
68
69 return false;
70}
71
73 const string &str,
74 const string &separators,
75 bool skip_empty_tokens)
76{
77 size_t token_start = 0;
78 size_t token_length = 0;
79 for (size_t i = 0; i < str.size(); ++i) {
80 const char ch = str[i];
81 if (separators.find(ch) == string::npos) {
82 /* Current character is not a separator,
83 * append it to token by increasing token length.
84 */
85 ++token_length;
86 }
87 else {
88 /* Current character is a separator,
89 * append current token to the list.
90 */
91 if (!skip_empty_tokens || token_length > 0) {
92 const string token = str.substr(token_start, token_length);
93 tokens.push_back(token);
94 }
95 token_start = i + 1;
96 token_length = 0;
97 }
98 }
99 /* Append token from the tail of the string if exists. */
100 if (token_length) {
101 const string token = str.substr(token_start, token_length);
102 tokens.push_back(token);
103 }
104}
105
106bool string_startswith(const string_view s, const string_view start)
107{
108 const size_t len = start.size();
109 if (len > s.size()) {
110 return false;
111 }
112
113 for (size_t i = 0; i < len; i++) {
114 if (s[i] != start[i]) {
115 return false;
116 }
117 }
118
119 return true;
120}
121
122bool string_endswith(const string_view s, const string_view end)
123{
124 const size_t len = end.size();
125 if (len > s.size()) {
126 return false;
127 }
128
129 const size_t offset = s.size() - len;
130 for (size_t i = 0; i < len; i++) {
131 if (s[offset + i] != end[i]) {
132 return false;
133 }
134 }
135
136 return true;
137}
138
139string string_strip(const string &s)
140{
141 string result = s;
142 result.erase(0, result.find_first_not_of(' '));
143 result.erase(result.find_last_not_of(' ') + 1);
144 return result;
145}
146
147void string_replace(string &haystack, const string &needle, const string &other)
148{
149 size_t i = 0;
150 size_t index;
151 while ((index = haystack.find(needle, i)) != string::npos) {
152 haystack.replace(index, needle.size(), other);
153 i = index + other.size();
154 }
155}
156
157void string_replace_same_length(string &haystack, const string &needle, const string &other)
158{
159 assert(needle.size() == other.size());
160 size_t pos = 0;
161 while (pos != string::npos) {
162 pos = haystack.find(needle, pos);
163 if (pos != string::npos) {
164 memcpy(haystack.data() + pos, other.data(), other.size());
165 pos += other.size();
166 }
167 }
168}
169
170string string_remove_trademark(const string &s)
171{
172 string result = s;
173
174 /* Special case, so we don't leave sequential spaces behind. */
175 /* TODO(sergey): Consider using regex perhaps? */
176 string_replace(result, " (TM)", "");
177 string_replace(result, " (R)", "");
178
179 string_replace(result, "(TM)", "");
180 string_replace(result, "(R)", "");
181
182 return string_strip(result);
183}
184
185string string_from_bool(bool var)
186{
187 if (var) {
188 return "True";
189 }
190 return "False";
191}
192
193string string_hex(const uint8_t *data, const size_t size)
194{
195 string result;
196 for (size_t i = 0; i < size; i++) {
197 result += string_printf("%x", data[i]);
198 }
199 return result;
200}
201
202string to_string(const char *str)
203{
204 return string(str);
205}
206
207string to_string(const float2 &v)
208{
209 return string_printf("%f,%f", v.x, v.y);
210}
211
212string to_string(const float4 &v)
213{
214 return string_printf("%f,%f,%f,%f", v.x, v.y, v.z, v.w);
215}
216
217string string_to_lower(const string &s)
218{
219 string r = s;
220 std::transform(r.begin(), r.end(), r.begin(), [](char c) { return std::tolower(c); });
221 return r;
222}
223
224string string_remove_gpu_from_cpu_name(const string &s)
225{
226 if (s.find("AMD") == std::string::npos) {
227 return s;
228 }
229
230 size_t pos = s.find("w/");
231 if (pos == std::string::npos) {
232 pos = s.find("with");
233 }
234
235 if (pos != std::string::npos) {
236 return string_strip(s.substr(0, pos));
237 }
238
239 return s;
240}
241
242/* Wide char strings helpers for Windows. */
243
244#ifndef _WIN32
245static locale_t utf8_locale_new()
246{
247 /* Try common locales that the DLSS libraries understands. */
248 static const char *names[] = {"en_US.UTF-8", "C.UTF-8"};
249
250 for (const char *name : names) {
251 locale_t locale = newlocale(LC_CTYPE_MASK, name, (locale_t) nullptr);
252 if (locale) {
253 return locale;
254 }
255 }
256
257 return (locale_t) nullptr;
258}
259#endif
260
261wstring string_to_wstring(const string &str)
262{
263#ifdef _WIN32
264 const int length_wc = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), nullptr, 0);
265 wstring str_wc(length_wc, 0);
266 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &str_wc[0], length_wc);
267#else
268 locale_t new_locale = utf8_locale_new();
269 locale_t old_locale = new_locale ? uselocale(new_locale) : (locale_t) nullptr;
270 mbstate_t state = {};
271 const char *str_p = str.c_str();
272 const size_t length_wc = mbsrtowcs(nullptr, &str_p, 0, &state);
273 wstring str_wc;
274 if (length_wc != (size_t)-1) {
275 str_wc.resize(length_wc, 0);
276 mbsrtowcs(str_wc.data(), &str_p, str_wc.size(), &state);
277 }
278 if (new_locale) {
279 uselocale(old_locale);
280 freelocale(new_locale);
281 }
282#endif
283 return str_wc;
284}
285
286string string_from_wstring(const wstring &str)
287{
288#ifdef _WIN32
289 int length_mb = WideCharToMultiByte(
290 CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0, nullptr, nullptr);
291 string str_mb(length_mb, 0);
292 WideCharToMultiByte(
293 CP_UTF8, 0, str.c_str(), str.size(), &str_mb[0], length_mb, nullptr, nullptr);
294#else
295 locale_t new_locale = utf8_locale_new();
296 locale_t old_locale = new_locale ? uselocale(new_locale) : (locale_t) nullptr;
297 const wchar_t *str_p = str.c_str();
298 mbstate_t state = {};
299 const size_t length_mb = wcsrtombs(nullptr, &str_p, 0, &state);
300 string str_mb;
301 if (length_mb != (size_t)-1) {
302 str_mb.resize(length_mb, 0);
303 wcsrtombs(str_mb.data(), &str_p, str_mb.size(), &state);
304 }
305 if (new_locale) {
306 uselocale(old_locale);
307 freelocale(new_locale);
308 }
309#endif
310 return str_mb;
311}
312
313#ifdef _WIN32
314
315string string_to_ansi(const string &str)
316{
317 const int length_wc = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), nullptr, 0);
318 wstring str_wc(length_wc, 0);
319 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &str_wc[0], length_wc);
320
321 int length_mb = WideCharToMultiByte(
322 CP_ACP, 0, str_wc.c_str(), str_wc.size(), nullptr, 0, nullptr, nullptr);
323
324 string str_mb(length_mb, 0);
325 WideCharToMultiByte(
326 CP_ACP, 0, str_wc.c_str(), str_wc.size(), &str_mb[0], length_mb, nullptr, nullptr);
327
328 return str_mb;
329}
330
331#endif /* _WIN32 */
332
334{
335 static const char suffixes[] = "BKMGTPEZY";
336
337 const char *suffix = suffixes;
338 size_t r = 0;
339
340 while (size >= 1024) {
341 r = size % 1024;
342 size /= 1024;
343 suffix++;
344 }
345
346 if (*suffix != 'B') {
347 return string_printf("%.2f%c", double(size * 1024 + r) / 1024.0, *suffix);
348 }
349 return string_printf("%zuB", size);
350}
351
353{
354 if (num == 0) {
355 return "0";
356 }
357
358 /* Add thousands separators. */
359 char buf[32];
360
361 char *p = buf + 31;
362 *p = '\0';
363
364 int i = -1;
365 while (num) {
366 i++;
367 if (i && i % 3 == 0) {
368 *(--p) = ',';
369 }
370
371 *(--p) = '0' + (num % 10);
372
373 num /= 10;
374 }
375
376 return p;
377}
378
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define CCL_NAMESPACE_END
#define str(s)
#define assert(assertion)
#define index(i)
const int state
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
string string_from_wstring(const wstring &str)
Definition string.cpp:286
static locale_t utf8_locale_new()
Definition string.cpp:245
string string_remove_trademark(const string &s)
Definition string.cpp:170
wstring string_to_wstring(const string &str)
Definition string.cpp:261
string string_from_bool(bool var)
Definition string.cpp:185
string string_human_readable_size(size_t size)
Definition string.cpp:333
bool string_iequals(const string &a, const string &b)
Definition string.cpp:57
string string_strip(const string &s)
Definition string.cpp:139
string to_string(const char *str)
Definition string.cpp:202
string string_hex(const uint8_t *data, const size_t size)
Definition string.cpp:193
string string_human_readable_number(size_t num)
Definition string.cpp:352
bool string_startswith(const string_view s, const string_view start)
Definition string.cpp:106
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
void string_replace_same_length(string &haystack, const string &needle, const string &other)
Definition string.cpp:157
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition string.cpp:72
void string_replace(string &haystack, const string &needle, const string &other)
Definition string.cpp:147
string string_remove_gpu_from_cpu_name(const string &s)
Definition string.cpp:224
bool string_endswith(const string_view s, const string_view end)
Definition string.cpp:122
string string_to_lower(const string &s)
Definition string.cpp:217