libcmo21/Unvirt/StringHelper.cpp

31 lines
836 B
C++
Raw Normal View History

2023-02-08 22:57:31 +08:00
#include "StringHelper.hpp"
#include <stdexcept>
namespace Unvirt::StringHelper {
2023-02-08 22:57:31 +08:00
void StdstringPrintf(std::string& strl, const char* format, ...) {
va_list argptr;
va_start(argptr, format);
StdstringVPrintf(strl, format, argptr);
va_end(argptr);
}
2023-02-08 22:57:31 +08:00
void StdstringVPrintf(std::string& strl, const char* format, va_list argptr) {
va_list args1;
va_copy(args1, argptr);
va_list args2;
va_copy(args2, argptr);
2023-02-08 22:57:31 +08:00
int count = std::vsnprintf(nullptr, 0, format, args1);
if (count < 0) throw new std::length_error("Invalid length returned by vsnprintf.");
va_end(args1);
2023-02-08 22:57:31 +08:00
2023-09-22 16:40:10 +08:00
strl.resize(count);
int write_result = std::vsnprintf(strl.data(), strl.size() + 1, format, args2);
va_end(args2);
if (write_result < 0 || write_result > count) throw new std::length_error("Invalid write result in vsnprintf.");
2023-02-08 22:57:31 +08:00
}
2023-02-08 22:57:31 +08:00
}