17# define vsnprintf _vsnprintf
33 va_start(args, format);
34 result = vsnprintf(
str.data(),
str.size(), format, args);
39 if (
str.size() > 65536) {
44 str.resize(
str.size() * 2, 0);
47 if (result >= (
int)
str.size()) {
49 str.resize(result + 1, 0);
53 return string(
str.data());
59 if (a.size() ==
b.size()) {
60 for (
size_t i = 0; i < a.size(); i++) {
61 if (toupper(a[i]) != toupper(
b[i])) {
74 const string &separators,
75 bool skip_empty_tokens)
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) {
91 if (!skip_empty_tokens || token_length > 0) {
92 const string token =
str.substr(token_start, token_length);
93 tokens.push_back(token);
101 const string token =
str.substr(token_start, token_length);
102 tokens.push_back(token);
108 const size_t len = start.size();
109 if (
len > s.size()) {
113 for (
size_t i = 0; i <
len; i++) {
114 if (s[i] != start[i]) {
124 const size_t len = end.size();
125 if (
len > s.size()) {
129 const size_t offset = s.size() -
len;
130 for (
size_t i = 0; i <
len; i++) {
131 if (s[offset + i] != end[i]) {
142 result.erase(0, result.find_first_not_of(
' '));
143 result.erase(result.find_last_not_of(
' ') + 1);
151 while ((
index = haystack.find(needle, i)) != string::npos) {
152 haystack.replace(
index, needle.size(), other);
153 i =
index + other.size();
159 assert(needle.size() == other.size());
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());
196 for (
size_t i = 0; i <
size; i++) {
220 std::transform(r.begin(), r.end(), r.begin(), [](
char c) { return std::tolower(c); });
226 if (s.find(
"AMD") == std::string::npos) {
230 size_t pos = s.find(
"w/");
231 if (pos == std::string::npos) {
232 pos = s.find(
"with");
235 if (pos != std::string::npos) {
248 static const char *names[] = {
"en_US.UTF-8",
"C.UTF-8"};
250 for (
const char *name : names) {
251 locale_t locale = newlocale(LC_CTYPE_MASK, name, (locale_t)
nullptr);
257 return (locale_t)
nullptr;
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);
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);
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);
279 uselocale(old_locale);
280 freelocale(new_locale);
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);
293 CP_UTF8, 0,
str.c_str(),
str.size(), &str_mb[0], length_mb,
nullptr,
nullptr);
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);
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);
306 uselocale(old_locale);
307 freelocale(new_locale);
315string string_to_ansi(
const string &
str)
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);
321 int length_mb = WideCharToMultiByte(
322 CP_ACP, 0, str_wc.c_str(), str_wc.size(),
nullptr, 0,
nullptr,
nullptr);
324 string str_mb(length_mb, 0);
326 CP_ACP, 0, str_wc.c_str(), str_wc.size(), &str_mb[0], length_mb,
nullptr,
nullptr);
335 static const char suffixes[] =
"BKMGTPEZY";
337 const char *suffix = suffixes;
340 while (
size >= 1024) {
346 if (*suffix !=
'B') {
367 if (i && i % 3 == 0) {
371 *(--p) =
'0' + (num % 10);
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
#define CCL_NAMESPACE_END
#define assert(assertion)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
string string_from_wstring(const wstring &str)
static locale_t utf8_locale_new()
string string_remove_trademark(const string &s)
wstring string_to_wstring(const string &str)
string string_from_bool(bool var)
string string_human_readable_size(size_t size)
bool string_iequals(const string &a, const string &b)
string string_strip(const string &s)
string to_string(const char *str)
string string_hex(const uint8_t *data, const size_t size)
string string_human_readable_number(size_t num)
bool string_startswith(const string_view s, const string_view start)
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
void string_replace_same_length(string &haystack, const string &needle, const string &other)
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
void string_replace(string &haystack, const string &needle, const string &other)
string string_remove_gpu_from_cpu_name(const string &s)
bool string_endswith(const string_view s, const string_view end)
string string_to_lower(const string &s)