From 93708c0c1c450fa1a3b81197504ade4e6cd55ed0 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Mon, 9 Nov 2015 11:13:34 -0800 Subject: [PATCH] xe::format_string utility. --- src/xenia/base/string.cc | 23 +++++++++++++++++++++++ src/xenia/base/string.h | 10 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/xenia/base/string.cc b/src/xenia/base/string.cc index 129323cf7..e6cf5b651 100644 --- a/src/xenia/base/string.cc +++ b/src/xenia/base/string.cc @@ -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(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(); diff --git a/src/xenia/base/string.h b/src/xenia/base/string.h index a23951798..9cab4645f 100644 --- a/src/xenia/base/string.h +++ b/src/xenia/base/string.h @@ -10,6 +10,7 @@ #ifndef XENIA_BASE_STRING_H_ #define XENIA_BASE_STRING_H_ +#include #include #include #include @@ -22,6 +23,15 @@ namespace xe { std::string to_string(const std::wstring& 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. std::string::size_type find_first_of_case(const std::string& target, const std::string& search);