xe::format_string utility.
This commit is contained in:
parent
65e0e907d8
commit
93708c0c1c
|
@ -40,6 +40,29 @@ std::wstring to_wstring(const std::string& source) {
|
||||||
#endif // XE_PLATFORM_LINUX
|
#endif // XE_PLATFORM_LINUX
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string format_string(const char* format, va_list args) {
|
||||||
|
if (!format) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
size_t max_len = 64;
|
||||||
|
std::string new_s;
|
||||||
|
while (true) {
|
||||||
|
new_s.resize(max_len);
|
||||||
|
int ret =
|
||||||
|
std::vsnprintf(const_cast<char*>(new_s.data()), max_len, format, args);
|
||||||
|
if (ret > max_len) {
|
||||||
|
// Needed size is known (+2 for termination and avoid ambiguity).
|
||||||
|
max_len = ret + 2;
|
||||||
|
} else if (ret == -1 || ret >= max_len - 1) {
|
||||||
|
// Handle some buggy vsnprintf implementations.
|
||||||
|
max_len *= 2;
|
||||||
|
} else {
|
||||||
|
// Everything fit for sure.
|
||||||
|
return new_s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::string::size_type find_first_of_case(const std::string& target,
|
std::string::size_type find_first_of_case(const std::string& target,
|
||||||
const std::string& search) {
|
const std::string& search) {
|
||||||
const char* str = target.c_str();
|
const char* str = target.c_str();
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#ifndef XENIA_BASE_STRING_H_
|
#ifndef XENIA_BASE_STRING_H_
|
||||||
#define XENIA_BASE_STRING_H_
|
#define XENIA_BASE_STRING_H_
|
||||||
|
|
||||||
|
#include <cstdarg>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
@ -22,6 +23,15 @@ namespace xe {
|
||||||
std::string to_string(const std::wstring& source);
|
std::string to_string(const std::wstring& source);
|
||||||
std::wstring to_wstring(const std::string& source);
|
std::wstring to_wstring(const std::string& source);
|
||||||
|
|
||||||
|
std::string format_string(const char* format, va_list args);
|
||||||
|
inline std::string format_string(const char* format, ...) {
|
||||||
|
va_list va;
|
||||||
|
va_start(va, format);
|
||||||
|
auto result = format_string(format, va);
|
||||||
|
va_end(va);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// find_first_of string, case insensitive.
|
// find_first_of string, case insensitive.
|
||||||
std::string::size_type find_first_of_case(const std::string& target,
|
std::string::size_type find_first_of_case(const std::string& target,
|
||||||
const std::string& search);
|
const std::string& search);
|
||||||
|
|
Loading…
Reference in New Issue