Skip to content
Snippets Groups Projects
Commit 537032d2 authored by Norihiro Watanabe's avatar Norihiro Watanabe
Browse files

add format() in StringTools

parent 5c337872
No related branches found
No related tags found
No related merge requests found
...@@ -16,10 +16,11 @@ ...@@ -16,10 +16,11 @@
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cstdarg>
#include <cstdio>
#include <iomanip> #include <iomanip>
#include "logog/include/logog.hpp" #include <logog/include/logog.hpp>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
namespace BaseLib namespace BaseLib
...@@ -77,6 +78,23 @@ std::string const& tostring(std::string const& value) ...@@ -77,6 +78,23 @@ std::string const& tostring(std::string const& value)
return value; return value;
} }
std::string format(const char* format_str, ... )
{
va_list args;
va_start(args, format_str);
// get the number of chars to write
va_list args_tmp;
va_copy(args_tmp, args);
int char_length = std::vsnprintf(nullptr, 0, format_str, args_tmp);
va_end(args_tmp);
// allocate buffer and store formatted output there
std::vector<char> buffer(char_length + 1); // note +1 for null terminator
vsnprintf(buffer.data(), buffer.size(), format_str, args);
va_end(args);
return std::string(buffer.data());
}
} // end namespace BaseLib } // end namespace BaseLib
#ifdef MSVC #ifdef MSVC
......
...@@ -79,6 +79,9 @@ template<typename T> std::string tostring(T const& value) ...@@ -79,6 +79,9 @@ template<typename T> std::string tostring(T const& value)
//! \overload //! \overload
std::string const& tostring(std::string const& value); std::string const& tostring(std::string const& value);
//! returns printf-like formatted string
std::string format(const char* format_string, ... );
} // end namespace BaseLib } // end namespace BaseLib
#ifdef MSVC #ifdef MSVC
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment