mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-25 15: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:
parent
f6d6138f46
commit
477e0ee912
2 changed files with 43 additions and 4 deletions
|
@ -3,6 +3,8 @@
|
|||
#include <components/misc/stringops.hpp>
|
||||
#include <components/esm/loadcell.hpp>
|
||||
|
||||
#include <libs/platform/strings.h>
|
||||
|
||||
#include "ref.hpp"
|
||||
#include "cell.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
|
||||
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
|
||||
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()
|
||||
{
|
||||
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
|
||||
|
|
|
@ -10,11 +10,38 @@
|
|||
# pragma warning(disable: 4996)
|
||||
# define strcasecmp stricmp
|
||||
# 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
|
||||
#else
|
||||
# warning "Unable to determine your compiler, you should probably take a look here."
|
||||
# include <strings.h> // Just take a guess
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif /* _STRINGS_WRAPPER_H */
|
||||
|
|
Loading…
Reference in a new issue