xe::format_string utility.

This commit is contained in:
Ben Vanik
2015-11-09 11:13:34 -08:00
parent 65e0e907d8
commit 93708c0c1c
2 changed files with 33 additions and 0 deletions

View File

@@ -40,6 +40,29 @@ std::wstring to_wstring(const std::string& source) {
#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,
const std::string& search) {
const char* str = target.c_str();