Move some StdString.h functions to .cpp

Update StdString.h
Update CMakeData-globals.cmake

This is just meant to hold us over until std::string is implemented and tested to be release-stable.
This commit is contained in:
sukibaby
2024-08-26 08:32:53 -07:00
committed by teejusb
parent e80380cc12
commit 77bf60f6e1
3 changed files with 134 additions and 41 deletions
+1
View File
@@ -2,6 +2,7 @@ list(APPEND SMDATA_GLOBAL_FILES_SRC
"GameLoop.cpp" "GameLoop.cpp"
"global.cpp" "global.cpp"
"SpecialFiles.cpp" "SpecialFiles.cpp"
"StdString.cpp"
"StepMania.cpp" # TODO: Refactor into separate main project. "StepMania.cpp" # TODO: Refactor into separate main project.
"${SM_GENERATED_SRC_DIR}/verstub.cpp") "${SM_GENERATED_SRC_DIR}/verstub.cpp")
+118
View File
@@ -0,0 +1,118 @@
// I've reorganized StdString.h to be optimized for the ITGmania engine.
//
// A huge amount of code bloat was traditionally due to these functions being
// inlined. Performance issues with RStrings are largely mitigated by migrating
// frequently used functions here. Aside from the added declaration of 'noexcept',
// everything here is the same as it was when it was in the original header file.
//
// Testing showed a 100-250FPS gain on all platforms with these functions moved in here.
//
// A couple things, like ssasn, were updated to be more idiomatic for C++ but remain
// functionally identical.
//
// This is just meant to hold us over until we get std::string fully worked into
// the engine, tested with Simply Love, etc. (unless this ends up better than
// std::string for some reason).
//
// --sukibaby
#include "global.h"
#include "StdString.h"
namespace StdString
{
void ssasn(std::string& sDst, const std::string& sSrc) noexcept
{
if (sDst.c_str() != sSrc.c_str())
{
sDst = sSrc;
}
}
void ssasn(std::wstring& sDst, const std::wstring& sSrc) noexcept
{
if (sDst.c_str() != sSrc.c_str())
{
sDst = sSrc;
}
}
void ssasn(std::string& sDst, PCSTR pA) noexcept
{
sDst = pA ? pA : "";
}
void ssasn(std::wstring& sDst, PCWSTR pA) noexcept
{
sDst = pA ? pA : L"";
}
void ssadd(std::string& sDst, const std::string& sSrc) noexcept
{
sDst += sSrc;
}
void ssadd(std::wstring& sDst, const std::wstring& sSrc) noexcept
{
sDst += sSrc;
}
void ssadd(std::string& sDst, PCSTR pA) noexcept
{
if (pA)
{
if (pA >= sDst.c_str() && pA <= sDst.c_str() + sDst.length())
{
if (sDst.capacity() <= sDst.size() + strlen(pA))
sDst.append(std::string(pA));
else
sDst.append(pA);
}
else
{
sDst.append(pA);
}
}
}
void ssadd(std::wstring& sDst, PCWSTR pA) noexcept
{
if (pA)
{
if (pA >= sDst.c_str() && pA <= sDst.c_str() + sDst.length())
{
if (sDst.capacity() <= sDst.size() + wcslen(pA))
sDst.append(std::wstring(pA));
else
sDst.append(pA);
}
else
{
sDst.append(pA);
}
}
}
char sstoupper(char ch) noexcept
{
return (ch >= 'a' && ch <= 'z') ? char(ch + 'A' - 'a') : ch;
}
char sstolower(char ch) noexcept
{
return (ch >= 'A' && ch <= 'Z') ? char(ch + 'a' - 'A') : ch;
}
wchar_t sstoupper(wchar_t ch) noexcept
{
return (ch >= L'a' && ch <= L'z') ? wchar_t(ch + L'A' - L'a') : ch;
}
wchar_t sstolower(wchar_t ch) noexcept
{
return (ch >= L'A' && ch <= L'Z') ? wchar_t(ch + L'a' - L'A') : ch;
}
}
// see StdString.h for license
+15 -41
View File
@@ -88,15 +88,13 @@
typedef const char* PCSTR; typedef const char* PCSTR;
typedef char* PSTR; typedef char* PSTR;
typedef const wchar_t* PCWSTR;
typedef wchar_t* PWSTR;
// Standard headers needed // Standard headers needed
#include <string> // basic_string #include <string> // basic_string
#include <algorithm> // for_each, etc. #include <algorithm> // for_each, etc.
#if defined(WIN32)
#include <malloc.h> // _alloca
#endif
#include <cctype> #include <cctype>
#include <cstdarg> #include <cstdarg>
#include <cstddef> #include <cstddef>
@@ -148,13 +146,15 @@ namespace StdString
* @param ch the character to convert. * @param ch the character to convert.
* @return the converted character. * @return the converted character.
*/ */
inline char sstoupper(char ch) { return (ch >= 'a' && ch <= 'z')? char(ch + 'A' - 'a'): ch; } char sstoupper(char ch) noexcept;
wchar_t sstoupper(wchar_t ch) noexcept;
/** /**
* @brief Turn the character into its lowercase equivalent. * @brief Turn the character into its lowercase equivalent.
* @param ch the character to convert. * @param ch the character to convert.
* @return the converted character. * @return the converted character.
*/ */
inline char sstolower(char ch) { return (ch >= 'A' && ch <= 'Z')? char(ch + 'a' - 'A'): ch; } char sstolower(char ch) noexcept;
wchar_t sstolower(wchar_t ch) noexcept;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// ssasn: assignment functions -- assign "sSrc" to "sDst" // ssasn: assignment functions -- assign "sSrc" to "sDst"
@@ -167,23 +167,16 @@ typedef std::string::pointer SS_PTRTYPE;
* @param sDst the destination string. * @param sDst the destination string.
* @param sSrc the source string. * @param sSrc the source string.
*/ */
inline void ssasn(std::string& sDst, const std::string& sSrc) void ssasn(std::string& sDst, const std::string& sSrc) noexcept;
{ void ssasn(std::wstring& sDst, const std::wstring& sSrc) noexcept;
if ( sDst.c_str() != sSrc.c_str() )
{
sDst.erase();
sDst.assign(sSrc);
}
}
/** /**
* @brief Assign one string to another. * @brief Assign one string to another.
* @param sDst the destination string. * @param sDst the destination string.
* @param pA the source string. * @param pA the source string.
*/ */
inline void ssasn(std::string& sDst, PCSTR pA) void ssasn(std::string& sDst, PCSTR pA) noexcept;
{ void ssasn(std::wstring& sDst, PCWSTR pA) noexcept;
sDst.assign(pA);
}
#undef StrSizeType #undef StrSizeType
@@ -195,34 +188,15 @@ inline void ssasn(std::string& sDst, PCSTR pA)
* @param sDst the original string. * @param sDst the original string.
* @param sSrc the string being added. * @param sSrc the string being added.
*/ */
inline void ssadd(std::string& sDst, const std::string& sSrc) void ssadd(std::string& sDst, const std::string& sSrc) noexcept;
{ void ssadd(std::wstring& sDst, const std::wstring& sSrc) noexcept;
sDst += sSrc;
}
/** /**
* @brief Concatenate one string with another. * @brief Concatenate one string with another.
* @param sDst the original string. * @param sDst the original string.
* @param pA the string being added. * @param pA the string being added.
*/ */
inline void ssadd(std::string& sDst, PCSTR pA) void ssadd(std::string& sDst, PCSTR pA) noexcept;
{ void ssadd(std::wstring& sDst, PCWSTR pA) noexcept;
// If the string being added is our internal string or a part of our
// internal string, then we must NOT do any reallocation without
// first copying that string to another object (since we're using a
// direct pointer)
if ( pA >= sDst.c_str() && pA <= sDst.c_str()+sDst.length())
{
if ( sDst.capacity() <= sDst.size()+strlen(pA) )
sDst.append(std::string(pA));
else
sDst.append(pA);
}
else
{
sDst.append(pA);
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// ssicmp: comparison (case insensitive ) // ssicmp: comparison (case insensitive )