2021-04-12 09:41:28 +00:00
|
|
|
#include "StdString.h"
|
2021-06-28 01:57:35 +00:00
|
|
|
#include "Platform.h"
|
2013-04-17 10:29:48 +00:00
|
|
|
#include <algorithm>
|
2022-10-03 08:04:42 +00:00
|
|
|
#include <malloc.h>
|
2016-01-12 19:48:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
2016-01-18 08:48:52 +00:00
|
|
|
|
2013-04-17 10:29:48 +00:00
|
|
|
stdstr::stdstr()
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2013-04-17 10:29:48 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-11-30 10:14:14 +00:00
|
|
|
stdstr::stdstr(const std::string & str) :
|
2016-01-18 08:48:52 +00:00
|
|
|
std::string(str)
|
2013-04-17 10:29:48 +00:00
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 10:14:14 +00:00
|
|
|
stdstr::stdstr(const stdstr & str) :
|
2016-01-18 08:48:52 +00:00
|
|
|
std::string((const std::string &)str)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2013-04-17 10:29:48 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-11-30 10:14:14 +00:00
|
|
|
stdstr::stdstr(const char * str) :
|
2016-01-18 08:48:52 +00:00
|
|
|
std::string(str ? str : "")
|
2013-04-17 10:29:48 +00:00
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 10:29:48 +00:00
|
|
|
strvector stdstr::Tokenize(const char * delimiter) const
|
|
|
|
{
|
2015-11-30 10:14:14 +00:00
|
|
|
strvector tokens;
|
2013-04-17 10:29:48 +00:00
|
|
|
|
2015-11-30 10:14:14 +00:00
|
|
|
stdstr::size_type lastPos = find_first_not_of(delimiter, 0);
|
|
|
|
stdstr::size_type pos = find_first_of(delimiter, lastPos);
|
2020-12-29 22:44:04 +00:00
|
|
|
size_t DelLen = strlen(delimiter);
|
2017-11-15 09:02:44 +00:00
|
|
|
while (stdstr::npos != pos)
|
2015-11-30 10:14:14 +00:00
|
|
|
{
|
|
|
|
tokens.push_back(substr(lastPos, pos - lastPos));
|
2020-12-29 22:44:04 +00:00
|
|
|
lastPos = pos + DelLen;
|
2015-11-30 10:14:14 +00:00
|
|
|
pos = find_first_of(delimiter, lastPos);
|
|
|
|
}
|
2017-11-15 09:02:44 +00:00
|
|
|
if (stdstr::npos != lastPos)
|
|
|
|
{
|
|
|
|
tokens.push_back(substr(lastPos));
|
|
|
|
}
|
2015-11-30 10:14:14 +00:00
|
|
|
return tokens;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 10:29:48 +00:00
|
|
|
strvector stdstr::Tokenize(char delimiter) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-11-30 10:14:14 +00:00
|
|
|
strvector tokens;
|
2013-04-17 10:29:48 +00:00
|
|
|
|
2015-11-30 10:14:14 +00:00
|
|
|
stdstr::size_type lastPos = find_first_not_of(delimiter, 0);
|
|
|
|
stdstr::size_type pos = find_first_of(delimiter, lastPos);
|
2017-11-15 09:02:44 +00:00
|
|
|
while (stdstr::npos != pos)
|
2015-11-30 10:14:14 +00:00
|
|
|
{
|
|
|
|
tokens.push_back(substr(lastPos, pos - lastPos));
|
2017-11-15 09:02:44 +00:00
|
|
|
lastPos = pos + 1;
|
2015-11-30 10:14:14 +00:00
|
|
|
pos = find_first_of(delimiter, lastPos);
|
|
|
|
}
|
2017-11-15 09:02:44 +00:00
|
|
|
if (stdstr::npos != lastPos)
|
|
|
|
{
|
|
|
|
tokens.push_back(substr(lastPos));
|
|
|
|
}
|
2015-11-30 10:14:14 +00:00
|
|
|
return tokens;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 10:29:48 +00:00
|
|
|
void stdstr::ArgFormat(const char * strFormat, va_list & args)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2013-04-17 10:29:48 +00:00
|
|
|
#pragma warning(push)
|
2022-10-03 08:04:42 +00:00
|
|
|
#pragma warning(disable : 4996)
|
2013-04-17 10:29:48 +00:00
|
|
|
|
2015-11-30 10:14:14 +00:00
|
|
|
size_t nlen = _vscprintf(strFormat, args) + 1;
|
|
|
|
char * buffer = (char *)alloca(nlen * sizeof(char));
|
|
|
|
buffer[nlen - 1] = 0;
|
2021-04-12 11:35:39 +00:00
|
|
|
if (buffer != nullptr)
|
2015-11-30 10:14:14 +00:00
|
|
|
{
|
|
|
|
vsprintf(buffer, strFormat, args);
|
|
|
|
*this = buffer;
|
|
|
|
}
|
2013-04-17 10:29:48 +00:00
|
|
|
#pragma warning(pop)
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 10:29:48 +00:00
|
|
|
void stdstr::Format(const char * strFormat, ...)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-11-30 10:14:14 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, strFormat);
|
|
|
|
ArgFormat(strFormat, args);
|
|
|
|
va_end(args);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
stdstr & stdstr::ToLower(void)
|
2013-04-17 10:29:48 +00:00
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
std::transform(begin(), end(), begin(), (char (*)(int))tolower);
|
2015-11-30 10:14:14 +00:00
|
|
|
return *this;
|
2013-04-17 10:29:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
stdstr & stdstr::ToUpper(void)
|
2013-04-17 10:29:48 +00:00
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
std::transform(begin(), end(), begin(), (char (*)(int))toupper);
|
2015-11-30 10:14:14 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stdstr::Replace(const char search, const char replace)
|
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
std::string & str = *this;
|
2015-11-30 10:14:14 +00:00
|
|
|
std::string::size_type pos = str.find(search);
|
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
str.replace(pos, 1, &replace);
|
|
|
|
pos = str.find(search, pos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stdstr::Replace(const char * search, const char replace)
|
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
std::string & str = *this;
|
2015-11-30 10:14:14 +00:00
|
|
|
std::string::size_type pos = str.find(search);
|
|
|
|
size_t SearchSize = strlen(search);
|
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
str.replace(pos, SearchSize, &replace);
|
|
|
|
pos = str.find(search, pos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
void stdstr::Replace(const std::string & search, const std::string & replace)
|
2015-11-30 10:14:14 +00:00
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
std::string & str = *this;
|
2015-11-30 10:14:14 +00:00
|
|
|
std::string::size_type pos = str.find(search);
|
|
|
|
size_t SearchSize = search.size();
|
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
str.replace(pos, SearchSize, replace);
|
|
|
|
pos = str.find(search, pos + replace.length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stdstr & stdstr::TrimLeft(const char * chars2remove)
|
|
|
|
{
|
|
|
|
if (!empty())
|
|
|
|
{
|
|
|
|
std::string::size_type pos = find_first_not_of(chars2remove);
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
erase(0, pos);
|
|
|
|
}
|
2016-01-05 10:32:18 +00:00
|
|
|
else
|
|
|
|
{
|
2021-03-16 22:59:14 +00:00
|
|
|
erase(begin(), end()); // Make empty
|
2015-11-30 10:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
stdstr & stdstr::TrimRight(const char * chars2remove)
|
|
|
|
{
|
|
|
|
if (!empty())
|
|
|
|
{
|
|
|
|
std::string::size_type pos = find_last_not_of(chars2remove);
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
erase(pos + 1);
|
|
|
|
}
|
2016-01-05 10:32:18 +00:00
|
|
|
else
|
|
|
|
{
|
2021-03-16 22:59:14 +00:00
|
|
|
erase(begin(), end()); // Make empty
|
2015-11-30 10:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
stdstr & stdstr::Trim(const char * chars2remove)
|
|
|
|
{
|
|
|
|
if (!empty())
|
|
|
|
{
|
|
|
|
std::string::size_type pos = find_first_not_of(chars2remove);
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
erase(0, pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-16 22:59:14 +00:00
|
|
|
erase(begin(), end()); // Make empty
|
2015-11-30 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = find_last_not_of(chars2remove);
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
erase(pos + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-16 22:59:14 +00:00
|
|
|
erase(begin(), end()); // Make empty
|
2015-11-30 10:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-01-05 10:32:18 +00:00
|
|
|
#ifdef _WIN32
|
2015-11-30 10:14:14 +00:00
|
|
|
stdstr & stdstr::FromUTF16(const wchar_t * UTF16Source, bool * bSuccess)
|
|
|
|
{
|
|
|
|
bool bConverted = false;
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (UTF16Source == nullptr)
|
2015-11-30 10:14:14 +00:00
|
|
|
{
|
|
|
|
*this = "";
|
|
|
|
bConverted = true;
|
|
|
|
}
|
|
|
|
else if (wcslen(UTF16Source) > 0)
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
uint32_t nNeeded = WideCharToMultiByte(CODEPAGE_UTF8, 0, UTF16Source, -1, nullptr, 0, nullptr, nullptr);
|
2015-11-30 10:14:14 +00:00
|
|
|
if (nNeeded > 0)
|
|
|
|
{
|
|
|
|
char * buf = (char *)alloca(nNeeded + 1);
|
2021-04-12 11:35:39 +00:00
|
|
|
if (buf != nullptr)
|
2015-11-30 10:14:14 +00:00
|
|
|
{
|
|
|
|
memset(buf, 0, nNeeded + 1);
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
nNeeded = WideCharToMultiByte(CODEPAGE_UTF8, 0, UTF16Source, -1, buf, nNeeded, nullptr, nullptr);
|
2015-11-30 10:14:14 +00:00
|
|
|
if (nNeeded)
|
|
|
|
{
|
|
|
|
*this = buf;
|
|
|
|
bConverted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bSuccess)
|
|
|
|
{
|
|
|
|
*bSuccess = bConverted;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-05-12 12:19:05 +00:00
|
|
|
std::wstring stdstr::ToUTF16(unsigned int CodePage, bool * bSuccess) const
|
2015-11-30 10:14:14 +00:00
|
|
|
{
|
|
|
|
bool bConverted = false;
|
|
|
|
std::wstring res;
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
DWORD nNeeded = MultiByteToWideChar(CodePage, 0, this->c_str(), (int)this->length(), nullptr, 0);
|
2015-11-30 10:14:14 +00:00
|
|
|
if (nNeeded > 0)
|
|
|
|
{
|
|
|
|
wchar_t * buf = (wchar_t *)alloca((nNeeded + 1) * sizeof(wchar_t));
|
2021-04-12 11:35:39 +00:00
|
|
|
if (buf != nullptr)
|
2015-11-30 10:14:14 +00:00
|
|
|
{
|
|
|
|
memset(buf, 0, (nNeeded + 1) * sizeof(wchar_t));
|
|
|
|
|
|
|
|
nNeeded = MultiByteToWideChar(CodePage, 0, this->c_str(), (int)this->length(), buf, nNeeded);
|
|
|
|
if (nNeeded)
|
|
|
|
{
|
|
|
|
res = buf;
|
|
|
|
bConverted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bSuccess)
|
|
|
|
{
|
|
|
|
*bSuccess = bConverted;
|
|
|
|
}
|
|
|
|
return res;
|
2016-01-05 10:32:18 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
stdstr_f::stdstr_f(const char * strFormat, ...)
|
2016-01-12 19:01:13 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, strFormat);
|
|
|
|
ArgFormat(strFormat, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
stdwstr_f::stdwstr_f(const wchar_t * strFormat, ...)
|
|
|
|
{
|
2016-01-12 19:48:07 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, strFormat);
|
2016-01-12 19:01:13 +00:00
|
|
|
|
2016-01-12 19:48:07 +00:00
|
|
|
wchar_t Msg[1000];
|
|
|
|
_vsnwprintf(Msg, sizeof(Msg) - 1, strFormat, args);
|
2016-01-12 19:01:13 +00:00
|
|
|
|
2016-01-12 19:48:07 +00:00
|
|
|
va_end(args);
|
2016-01-12 19:01:13 +00:00
|
|
|
|
2016-01-12 19:48:07 +00:00
|
|
|
this->assign(Msg);
|
2016-01-05 10:32:18 +00:00
|
|
|
}
|
2021-03-16 22:59:14 +00:00
|
|
|
#endif
|