Blender  V2.93
util_string.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdarg.h>
18 #include <stdio.h>
19 
20 #include "util/util_foreach.h"
21 #include "util/util_string.h"
22 #include "util/util_windows.h"
23 
24 #ifdef _WIN32
25 # ifndef vsnprintf
26 # define vsnprintf _vsnprintf
27 # endif
28 #endif /* _WIN32 */
29 
31 
32 string string_printf(const char *format, ...)
33 {
34  vector<char> str(128, 0);
35 
36  while (1) {
37  va_list args;
38  int result;
39 
40  va_start(args, format);
41  result = vsnprintf(&str[0], str.size(), format, args);
42  va_end(args);
43 
44  if (result == -1) {
45  /* not enough space or formatting error */
46  if (str.size() > 65536) {
47  assert(0);
48  return string("");
49  }
50 
51  str.resize(str.size() * 2, 0);
52  continue;
53  }
54  else if (result >= (int)str.size()) {
55  /* not enough space */
56  str.resize(result + 1, 0);
57  continue;
58  }
59 
60  return string(&str[0]);
61  }
62 }
63 
64 bool string_iequals(const string &a, const string &b)
65 {
66  if (a.size() == b.size()) {
67  for (size_t i = 0; i < a.size(); i++)
68  if (toupper(a[i]) != toupper(b[i]))
69  return false;
70 
71  return true;
72  }
73 
74  return false;
75 }
76 
78  const string &str,
79  const string &separators,
80  bool skip_empty_tokens)
81 {
82  size_t token_start = 0, token_length = 0;
83  for (size_t i = 0; i < str.size(); ++i) {
84  const char ch = str[i];
85  if (separators.find(ch) == string::npos) {
86  /* Current character is not a separator,
87  * append it to token by increasing token length.
88  */
89  ++token_length;
90  }
91  else {
92  /* Current character is a separator,
93  * append current token to the list.
94  */
95  if (!skip_empty_tokens || token_length > 0) {
96  string token = str.substr(token_start, token_length);
97  tokens.push_back(token);
98  }
99  token_start = i + 1;
100  token_length = 0;
101  }
102  }
103  /* Append token from the tail of the string if exists. */
104  if (token_length) {
105  string token = str.substr(token_start, token_length);
106  tokens.push_back(token);
107  }
108 }
109 
110 bool string_startswith(const string &s, const char *start)
111 {
112  size_t len = strlen(start);
113 
114  if (len > s.size())
115  return 0;
116  else
117  return strncmp(s.c_str(), start, len) == 0;
118 }
119 
120 bool string_endswith(const string &s, const string &end)
121 {
122  size_t len = end.length();
123 
124  if (len > s.size())
125  return 0;
126  else
127  return s.compare(s.length() - len, len, end) == 0;
128 }
129 
130 string string_strip(const string &s)
131 {
132  string result = s;
133  result.erase(0, result.find_first_not_of(' '));
134  result.erase(result.find_last_not_of(' ') + 1);
135  return result;
136 }
137 
138 void string_replace(string &haystack, const string &needle, const string &other)
139 {
140  size_t i = 0, index;
141  while ((index = haystack.find(needle, i)) != string::npos) {
142  haystack.replace(index, needle.size(), other);
143  i = index + other.size();
144  }
145 }
146 
147 string string_remove_trademark(const string &s)
148 {
149  string result = s;
150 
151  /* Special case, so we don;t leave sequential spaces behind. */
152  /* TODO(sergey): Consider using regex perhaps? */
153  string_replace(result, " (TM)", "");
154  string_replace(result, " (R)", "");
155 
156  string_replace(result, "(TM)", "");
157  string_replace(result, "(R)", "");
158 
159  return string_strip(result);
160 }
161 
162 string string_from_bool(bool var)
163 {
164  if (var)
165  return "True";
166  else
167  return "False";
168 }
169 
170 string to_string(const char *str)
171 {
172  return string(str);
173 }
174 
175 /* Wide char strings helpers for Windows. */
176 
177 #ifdef _WIN32
178 
179 wstring string_to_wstring(const string &str)
180 {
181  const int length_wc = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
182  wstring str_wc(length_wc, 0);
183  MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &str_wc[0], length_wc);
184  return str_wc;
185 }
186 
187 string string_from_wstring(const wstring &str)
188 {
189  int length_mb = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), NULL, 0, NULL, NULL);
190  string str_mb(length_mb, 0);
191  WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), &str_mb[0], length_mb, NULL, NULL);
192  return str_mb;
193 }
194 
195 string string_to_ansi(const string &str)
196 {
197  const int length_wc = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
198  wstring str_wc(length_wc, 0);
199  MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &str_wc[0], length_wc);
200 
201  int length_mb = WideCharToMultiByte(
202  CP_ACP, 0, str_wc.c_str(), str_wc.size(), NULL, 0, NULL, NULL);
203 
204  string str_mb(length_mb, 0);
205  WideCharToMultiByte(CP_ACP, 0, str_wc.c_str(), str_wc.size(), &str_mb[0], length_mb, NULL, NULL);
206 
207  return str_mb;
208 }
209 
210 #endif /* _WIN32 */
211 
213 {
214  static const char suffixes[] = "BKMGTPEZY";
215 
216  const char *suffix = suffixes;
217  size_t r = 0;
218 
219  while (size >= 1024) {
220  r = size % 1024;
221  size /= 1024;
222  suffix++;
223  }
224 
225  if (*suffix != 'B')
226  return string_printf("%.2f%c", double(size * 1024 + r) / 1024.0, *suffix);
227  else
228  return string_printf("%zu", size);
229 }
230 
232 {
233  if (num == 0) {
234  return "0";
235  }
236 
237  /* Add thousands separators. */
238  char buf[32];
239 
240  char *p = buf + 31;
241  *p = '\0';
242 
243  int i = -1;
244  while (num) {
245  if (++i && i % 3 == 0)
246  *(--p) = ',';
247 
248  *(--p) = '0' + (num % 10);
249 
250  num /= 10;
251  }
252 
253  return p;
254 }
255 
_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
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define str(s)
#define CCL_NAMESPACE_END
format
Definition: logImageCore.h:47
static unsigned a[3]
Definition: RandGen.cpp:92
string string_remove_trademark(const string &s)
string string_from_bool(bool var)
string string_human_readable_size(size_t size)
bool string_iequals(const string &a, const string &b)
Definition: util_string.cpp:64
string string_strip(const string &s)
string to_string(const char *str)
string string_human_readable_number(size_t num)
bool string_startswith(const string &s, const char *start)
bool string_endswith(const string &s, const string &end)
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition: util_string.cpp:32
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition: util_string.cpp:77
void string_replace(string &haystack, const string &needle, const string &other)
uint len