Merge remote-tracking branch 'suokko/stl_vsnprintf' into develop

develop
lethosor 2018-06-20 11:23:57 -04:00
commit 2f1d3c68c6
1 changed files with 22 additions and 15 deletions

@ -27,6 +27,8 @@ distribution.
#include "MiscUtils.h"
#ifndef LINUX_BUILD
// We don't want min and max macros
#define NOMINMAX
#include <Windows.h>
#else
#include <sys/time.h>
@ -40,6 +42,7 @@ distribution.
#include <sstream>
#include <map>
#include <array>
std::string stl_sprintf(const char *fmt, ...) {
va_list lst;
@ -50,21 +53,25 @@ std::string stl_sprintf(const char *fmt, ...) {
}
std::string stl_vsprintf(const char *fmt, va_list args) {
std::vector<char> buf;
buf.resize(4096);
for (;;) {
va_list args2;
va_copy(args2, args);
int rsz = vsnprintf(&buf[0], buf.size(), fmt, args2);
va_end(args2);
if (rsz < 0)
buf.resize(buf.size()*2);
else if (unsigned(rsz) >= buf.size())
buf.resize(rsz+1);
else
return std::string(&buf[0], rsz);
}
/* Allow small (about single line) strings to be printed into stack memory
* with a call to vsnprintf.
*/
std::array<char,128> buf;
va_list args2;
va_copy(args2, args);
int rsz = vsnprintf(&buf[0], buf.size(), fmt, args2);
va_end(args2);
if (rsz < 0)
return std::string(); /* Error occurred */
if (static_cast<unsigned>(rsz) < buf.size())
return std::string(&buf[0], rsz); /* Whole string fits to a single line buffer */
std::string rv;
// Allocate enough memory for the output and null termination
rv.resize(rsz+1);
rsz = vsnprintf(&rv[0], rv.size(), fmt, args);
if (rsz + 1 < static_cast<int>(rv.size()))
rv.resize(std::max(rsz+1,0));
return rv;
}
bool split_string(std::vector<std::string> *out,