1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-25 22:39:42 +00:00

Convert std::to_string() calls to snprintf() for cell references.

- Profiling indicates snprintf() is more efficient when using MSVC (not tested with linux)
This commit is contained in:
cc9cii 2015-12-18 19:39:40 +11:00
parent f6d6138f46
commit 477e0ee912
2 changed files with 43 additions and 4 deletions

View file

@ -3,6 +3,8 @@
#include <components/misc/stringops.hpp> #include <components/misc/stringops.hpp>
#include <components/esm/loadcell.hpp> #include <components/esm/loadcell.hpp>
#include <libs/platform/strings.h>
#include "ref.hpp" #include "ref.hpp"
#include "cell.hpp" #include "cell.hpp"
#include "universalid.hpp" #include "universalid.hpp"
@ -60,7 +62,12 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool
// ignoring moved references sub-record; instead calculate cell from coordinates // ignoring moved references sub-record; instead calculate cell from coordinates
std::pair<int, int> index = ref.getCellIndex(); std::pair<int, int> index = ref.getCellIndex();
ref.mCell = "#" + std::to_string(index.first) + " " + std::to_string(index.second); char buf[100];
int res = snprintf(buf, 100, "#%d %d", index.first, index.second);
if (res > 0 && res < 100)
ref.mCell = std::string(buf);
else
throw std::runtime_error("getNewId possible buffer overflow");
if (!base && // don't try to update base records if (!base && // don't try to update base records
mref.mRefNum.mIndex != 0) // MVRF tag found mref.mRefNum.mIndex != 0) // MVRF tag found
@ -181,7 +188,12 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool
std::string CSMWorld::RefCollection::getNewId() std::string CSMWorld::RefCollection::getNewId()
{ {
return "ref#" + std::to_string(mNextId++); char buf[100];
int res = snprintf(buf, 100, "ref#%d", mNextId++);
if (res > 0 && res < 100)
return std::string(buf);
else
throw std::runtime_error("getNewId possible buffer overflow");
} }
unsigned int CSMWorld::RefCollection::extractIdNum (const std::string& id) const unsigned int CSMWorld::RefCollection::extractIdNum (const std::string& id) const

View file

@ -10,7 +10,34 @@
# pragma warning(disable: 4996) # pragma warning(disable: 4996)
# define strcasecmp stricmp # define strcasecmp stricmp
# if (_MSC_VER < 1900) # if (_MSC_VER < 1900)
# define snprintf _snprintf # include <stdio.h>
# include <stdarg.h>
# define snprintf c99_snprintf
# define vsnprintf c99_vsnprintf
/* see http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 */
inline int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap)
{
int count = -1;
if (size != 0)
count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
if (count == -1)
count = _vscprintf(format, ap);
return count;
}
inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...)
{
int count;
va_list ap;
va_start(ap, format);
count = c99_vsnprintf(outBuf, size, format, ap);
va_end(ap);
return count;
}
# endif # endif
#else #else
# warning "Unable to determine your compiler, you should probably take a look here." # warning "Unable to determine your compiler, you should probably take a look here."