Blender  V3.3
string.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include <stdarg.h>
5 #include <stdio.h>
6 
7 #include <algorithm>
8 #include <cctype>
9 
10 #include "util/foreach.h"
11 #include "util/string.h"
12 #include "util/windows.h"
13 
14 #ifdef _WIN32
15 # ifndef vsnprintf
16 # define vsnprintf _vsnprintf
17 # endif
18 #endif /* _WIN32 */
19 
21 
22 string string_printf(const char *format, ...)
23 {
24  vector<char> str(128, 0);
25 
26  while (1) {
27  va_list args;
28  int result;
29 
30  va_start(args, format);
31  result = vsnprintf(&str[0], str.size(), format, args);
32  va_end(args);
33 
34  if (result == -1) {
35  /* not enough space or formatting error */
36  if (str.size() > 65536) {
37  assert(0);
38  return string("");
39  }
40 
41  str.resize(str.size() * 2, 0);
42  continue;
43  }
44  else if (result >= (int)str.size()) {
45  /* not enough space */
46  str.resize(result + 1, 0);
47  continue;
48  }
49 
50  return string(&str[0]);
51  }
52 }
53 
54 bool string_iequals(const string &a, const string &b)
55 {
56  if (a.size() == b.size()) {
57  for (size_t i = 0; i < a.size(); i++)
58  if (toupper(a[i]) != toupper(b[i]))
59  return false;
60 
61  return true;
62  }
63 
64  return false;
65 }
66 
68  const string &str,
69  const string &separators,
70  bool skip_empty_tokens)
71 {
72  size_t token_start = 0, token_length = 0;
73  for (size_t i = 0; i < str.size(); ++i) {
74  const char ch = str[i];
75  if (separators.find(ch) == string::npos) {
76  /* Current character is not a separator,
77  * append it to token by increasing token length.
78  */
79  ++token_length;
80  }
81  else {
82  /* Current character is a separator,
83  * append current token to the list.
84  */
85  if (!skip_empty_tokens || token_length > 0) {
86  string token = str.substr(token_start, token_length);
87  tokens.push_back(token);
88  }
89  token_start = i + 1;
90  token_length = 0;
91  }
92  }
93  /* Append token from the tail of the string if exists. */
94  if (token_length) {
95  string token = str.substr(token_start, token_length);
96  tokens.push_back(token);
97  }
98 }
99 
100 bool string_startswith(const string_view s, const string_view start)
101 {
102  const size_t len = start.size();
103 
104  if (len > s.size()) {
105  return false;
106  }
107 
108  return strncmp(s.c_str(), start.data(), len) == 0;
109 }
110 
111 bool string_endswith(const string_view s, const string_view end)
112 {
113  const size_t len = end.size();
114 
115  if (len > s.size()) {
116  return false;
117  }
118 
119  return strncmp(s.c_str() + s.size() - len, end.data(), len) == 0;
120 }
121 
122 string string_strip(const string &s)
123 {
124  string result = s;
125  result.erase(0, result.find_first_not_of(' '));
126  result.erase(result.find_last_not_of(' ') + 1);
127  return result;
128 }
129 
130 void string_replace(string &haystack, const string &needle, const string &other)
131 {
132  size_t i = 0, index;
133  while ((index = haystack.find(needle, i)) != string::npos) {
134  haystack.replace(index, needle.size(), other);
135  i = index + other.size();
136  }
137 }
138 
139 void string_replace_same_length(string &haystack, const string &needle, const string &other)
140 {
141  assert(needle.size() == other.size());
142  size_t pos = 0;
143  while (pos != string::npos) {
144  pos = haystack.find(needle, pos);
145  if (pos != string::npos) {
146  memcpy(haystack.data() + pos, other.data(), other.size());
147  pos += other.size();
148  }
149  }
150 }
151 
152 string string_remove_trademark(const string &s)
153 {
154  string result = s;
155 
156  /* Special case, so we don't leave sequential spaces behind. */
157  /* TODO(sergey): Consider using regex perhaps? */
158  string_replace(result, " (TM)", "");
159  string_replace(result, " (R)", "");
160 
161  string_replace(result, "(TM)", "");
162  string_replace(result, "(R)", "");
163 
164  return string_strip(result);
165 }
166 
167 string string_from_bool(bool var)
168 {
169  if (var)
170  return "True";
171  else
172  return "False";
173 }
174 
175 string to_string(const char *str)
176 {
177  return string(str);
178 }
179 
180 string to_string(const float4 &v)
181 {
182  return string_printf("%f,%f,%f,%f", v.x, v.y, v.z, v.w);
183 }
184 
185 string string_to_lower(const string &s)
186 {
187  string r = s;
188  std::transform(r.begin(), r.end(), r.begin(), [](char c) { return std::tolower(c); });
189  return r;
190 }
191 
192 /* Wide char strings helpers for Windows. */
193 
194 #ifdef _WIN32
195 
196 wstring string_to_wstring(const string &str)
197 {
198  const int length_wc = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
199  wstring str_wc(length_wc, 0);
200  MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &str_wc[0], length_wc);
201  return str_wc;
202 }
203 
204 string string_from_wstring(const wstring &str)
205 {
206  int length_mb = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), NULL, 0, NULL, NULL);
207  string str_mb(length_mb, 0);
208  WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), &str_mb[0], length_mb, NULL, NULL);
209  return str_mb;
210 }
211 
212 string string_to_ansi(const string &str)
213 {
214  const int length_wc = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
215  wstring str_wc(length_wc, 0);
216  MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &str_wc[0], length_wc);
217 
218  int length_mb = WideCharToMultiByte(
219  CP_ACP, 0, str_wc.c_str(), str_wc.size(), NULL, 0, NULL, NULL);
220 
221  string str_mb(length_mb, 0);
222  WideCharToMultiByte(CP_ACP, 0, str_wc.c_str(), str_wc.size(), &str_mb[0], length_mb, NULL, NULL);
223 
224  return str_mb;
225 }
226 
227 #endif /* _WIN32 */
228 
230 {
231  static const char suffixes[] = "BKMGTPEZY";
232 
233  const char *suffix = suffixes;
234  size_t r = 0;
235 
236  while (size >= 1024) {
237  r = size % 1024;
238  size /= 1024;
239  suffix++;
240  }
241 
242  if (*suffix != 'B')
243  return string_printf("%.2f%c", double(size * 1024 + r) / 1024.0, *suffix);
244  else
245  return string_printf("%zu", size);
246 }
247 
249 {
250  if (num == 0) {
251  return "0";
252  }
253 
254  /* Add thousands separators. */
255  char buf[32];
256 
257  char *p = buf + 31;
258  *p = '\0';
259 
260  int i = -1;
261  while (num) {
262  if (++i && i % 3 == 0)
263  *(--p) = ',';
264 
265  *(--p) = '0' + (num % 10);
266 
267  num /= 10;
268  }
269 
270  return p;
271 }
272 
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
float float4[4]
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
int len
Definition: draw_manager.c:108
#define str(s)
uint pos
format
Definition: logImageCore.h:38
static unsigned c
Definition: RandGen.cpp:83
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
string string_remove_trademark(const string &s)
Definition: string.cpp:152
string string_from_bool(bool var)
Definition: string.cpp:167
string string_human_readable_size(size_t size)
Definition: string.cpp:229
bool string_iequals(const string &a, const string &b)
Definition: string.cpp:54
string string_strip(const string &s)
Definition: string.cpp:122
string to_string(const char *str)
Definition: string.cpp:175
string string_human_readable_number(size_t num)
Definition: string.cpp:248
bool string_startswith(const string_view s, const string_view start)
Definition: string.cpp:100
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition: string.cpp:22
void string_replace_same_length(string &haystack, const string &needle, const string &other)
Definition: string.cpp:139
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition: string.cpp:67
void string_replace(string &haystack, const string &needle, const string &other)
Definition: string.cpp:130
bool string_endswith(const string_view s, const string_view end)
Definition: string.cpp:111
string string_to_lower(const string &s)
Definition: string.cpp:185