StringFormat converted to move semantics

This commit is contained in:
Arthur Ice
2014-07-08 18:57:54 -07:00
committed by Arthur Ice
parent 9a634a2056
commit 6a59b83d43
6 changed files with 27 additions and 36 deletions
+2 -4
View File
@@ -893,8 +893,7 @@ void Inventory::dumpItemCollection(const std::map<int16, ItemInst*> &collection)
if (!inst || !inst->GetItem())
continue;
std::string slot;
StringFormat(slot, "Slot %d: %s (%d)", it->first, it->second->GetItem()->Name, (inst->GetCharges() <= 0) ? 1 : inst->GetCharges());
std::string slot = StringFormat("Slot %d: %s (%d)", it->first, it->second->GetItem()->Name, (inst->GetCharges() <= 0) ? 1 : inst->GetCharges());
std::cout << slot << std::endl;
dumpBagContents(inst, &it);
@@ -913,8 +912,7 @@ void Inventory::dumpBagContents(ItemInst *inst, iter_inst *it) {
if (!baginst || !baginst->GetItem())
continue;
std::string subSlot;
StringFormat(subSlot, " Slot %d: %s (%d)", Inventory::CalcSlotId((*it)->first, itb->first),
std::string subSlot = StringFormat(" Slot %d: %s (%d)", Inventory::CalcSlotId((*it)->first, itb->first),
baginst->GetItem()->Name, (baginst->GetCharges() <= 0) ? 1 : baginst->GetCharges());
std::cout << subSlot << std::endl;
}
+14 -19
View File
@@ -38,8 +38,9 @@
// original source:
// https://github.com/facebook/folly/blob/master/folly/String.cpp
//
void vStringFormat(std::string& output, const char* format, va_list args)
const std::string vStringFormat(const char* format, va_list args)
{
std::string output;
va_list tmpargs;
va_copy(tmpargs,args);
@@ -48,11 +49,8 @@ void vStringFormat(std::string& output, const char* format, va_list args)
if (characters_used < 0) {
// Looks like we have an invalid format string.
// error out.
std::string errorMessage("Invalid format string; snprintf returned negative with format string: ");
errorMessage.append(format);
throw std::runtime_error(errorMessage);
// return empty string.
return "";
}
else if ((unsigned int)characters_used > output.capacity()) {
output.resize(characters_used+1);
@@ -62,12 +60,10 @@ void vStringFormat(std::string& output, const char* format, va_list args)
if (characters_used < 0) {
// We shouldn't have a format error by this point, but I can't imagine what error we
// could have by this point. Still, error out and report it.
std::string errorMessage("Invalid format string or unknown vsnprintf error; vsnprintf returned negative with format string: ");
errorMessage.append(format);
throw std::runtime_error(errorMessage);
// could have by this point. Still, return empty string;
return "";
}
return std::move(output);
}
else {
output.resize(characters_used + 1);
@@ -78,24 +74,23 @@ void vStringFormat(std::string& output, const char* format, va_list args)
if (characters_used < 0) {
// We shouldn't have a format error by this point, but I can't imagine what error we
// could have by this point. still error out and report it.
std::string errorMessage("Invalid format string or unknown vsnprintf error; vsnprintf returned negative with format string: ");
errorMessage.append(format);
throw std::runtime_error(errorMessage);
// could have by this point. Still, return empty string;
return "";
}
return std::move(output);
}
}
void StringFormat(std::string& output, const char* format, ...)
const std::string StringFormat(const char* format, ...)
{
va_list args;
va_start(args, format);
vStringFormat(output,format,args);
std::string output = vStringFormat(format,args);
va_end(args);
return std::move(output);
}
// normal strncpy doesnt put a null term on copied strings, this one does
// ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_strncpy_wcsncpy.asp
char* strn0cpy(char* dest, const char* source, uint32 size) {
+3 -2
View File
@@ -19,11 +19,12 @@
#include <sstream>
#include <vector>
#include <cstdarg>
#include <string.h>
#include "types.h"
void vStringFormat(std::string& output, const char* format, va_list args);
void StringFormat(std::string& output, const char* format, ...);
const std::string vStringFormat(const char* format, va_list args);
const std::string StringFormat(const char* format, ...);
std::string EscapeString(const std::string &s);
const char *MakeLowerString(const char *source);
+1 -3
View File
@@ -33,9 +33,7 @@ void log_message(LogType type, const char *fmt, ...) {
}
void log_messageVA(LogType type, const char *fmt, va_list args) {
std::string prefix_buffer;
StringFormat(prefix_buffer, "[%s] ", log_type_info[type].name);
std::string prefix_buffer = StringFormat("[%s] ", log_type_info[type].name);
LogFile->writePVA(EQEMuLog::Debug, prefix_buffer.c_str(), fmt, args);
}