Magic_Game/core/Common/String.cpp

512 lines
8.5 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dcommon.h"
2017-12-11 18:17:24 +08:00
#include <iomanip>
2018-02-28 19:17:15 +08:00
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")
2018-02-27 16:32:17 +08:00
e2d::String::String()
2018-05-08 17:40:36 +08:00
: _str(L"")
2017-12-11 18:17:24 +08:00
{
}
2018-02-27 16:32:17 +08:00
e2d::String::String(const wchar_t *str)
2018-05-08 17:40:36 +08:00
: _str(str)
{
}
2018-02-28 19:17:15 +08:00
e2d::String::String(const char *cstr)
2018-05-08 17:40:36 +08:00
: _str(static_cast<wchar_t*>(_bstr_t(cstr)))
2018-02-28 19:17:15 +08:00
{
}
2018-02-27 16:32:17 +08:00
e2d::String::String(e2d::String && str)
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
_str = std::move(str._str);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String::String(const e2d::String &str)
2018-05-08 17:40:36 +08:00
: _str(str._str)
2017-12-11 18:17:24 +08:00
{
}
2018-02-27 16:32:17 +08:00
e2d::String::~String()
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
_str.clear();
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String &e2d::String::operator=(const wchar_t *str)
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
_str = str;
2018-03-12 15:55:41 +08:00
return (*this);
2017-12-11 18:17:24 +08:00
}
2018-02-28 19:17:15 +08:00
e2d::String & e2d::String::operator=(const char *cstr)
{
2018-05-08 17:40:36 +08:00
_str = static_cast<wchar_t*>(_bstr_t(cstr));
2018-03-12 15:55:41 +08:00
return (*this);
2018-02-28 19:17:15 +08:00
}
2018-04-01 23:08:11 +08:00
e2d::String e2d::String::parse(int value)
{
String tmp;
2018-05-08 17:40:36 +08:00
tmp._str = std::to_wstring(value);
2018-04-01 23:08:11 +08:00
return std::move(tmp);
}
e2d::String e2d::String::parse(unsigned int value)
{
String tmp;
2018-05-08 17:40:36 +08:00
tmp._str = std::to_wstring(value);
return std::move(tmp);
}
2018-04-01 23:08:11 +08:00
e2d::String e2d::String::parse(float value)
{
String tmp;
2018-05-08 17:40:36 +08:00
tmp._str = std::to_wstring(value);
2018-04-01 23:08:11 +08:00
return std::move(tmp);
}
e2d::String e2d::String::parse(double value)
{
String tmp;
2018-05-08 17:40:36 +08:00
tmp._str = std::to_wstring(value);
2018-04-01 23:08:11 +08:00
return std::move(tmp);
}
2018-04-17 11:41:33 +08:00
e2d::String e2d::String::format(const char * format, ...)
2018-03-22 18:25:56 +08:00
{
std::string tmp;
va_list marker = nullptr;
2018-03-22 18:25:56 +08:00
va_start(marker, format);
2018-05-08 17:40:36 +08:00
size_t nu_of_chars = _vscprintf(format, marker);
2018-03-22 18:25:56 +08:00
2018-05-08 17:40:36 +08:00
if (nu_of_chars > tmp.capacity())
2018-03-22 18:25:56 +08:00
{
2018-05-08 17:40:36 +08:00
tmp.resize(nu_of_chars + 1);
2018-03-22 18:25:56 +08:00
}
vsprintf_s(const_cast<LPSTR>(tmp.data()), tmp.capacity(), format, marker);
va_end(marker);
2018-04-17 11:41:33 +08:00
String str = tmp.c_str();
return std::move(str);
2018-03-22 18:25:56 +08:00
}
2018-04-17 11:41:33 +08:00
e2d::String e2d::String::format(const wchar_t * format, ...)
2018-03-22 18:25:56 +08:00
{
std::wstring tmp;
va_list marker = nullptr;
2018-03-22 18:25:56 +08:00
va_start(marker, format);
2018-05-08 17:40:36 +08:00
size_t nu_of_chars = _vscwprintf(format, marker);
2018-03-22 18:25:56 +08:00
2018-05-08 17:40:36 +08:00
if (nu_of_chars > tmp.capacity())
2018-03-22 18:25:56 +08:00
{
2018-05-08 17:40:36 +08:00
tmp.resize(nu_of_chars + 1);
2018-03-22 18:25:56 +08:00
}
vswprintf_s(const_cast<LPWSTR>(tmp.data()), tmp.capacity(), format, marker);
va_end(marker);
2018-04-17 11:41:33 +08:00
String str = tmp.c_str();
return std::move(str);
2018-03-22 18:25:56 +08:00
}
2018-05-01 17:19:55 +08:00
void e2d::String::swap(String & str1, String & str2)
{
2018-05-08 17:40:36 +08:00
str1._str.swap(str2._str);
2018-05-01 17:19:55 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String & e2d::String::operator=(const String &str)
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
_str = str._str;
2018-03-12 15:55:41 +08:00
return (*this);
2017-12-11 18:17:24 +08:00
}
2018-05-22 22:55:06 +08:00
bool e2d::String::operator==(const wchar_t *str) const
2017-12-11 18:17:24 +08:00
{
2018-02-03 22:04:43 +08:00
if (str)
{
2018-05-08 17:40:36 +08:00
return (_str.compare(str) == 0);
2018-02-03 22:04:43 +08:00
}
else
{
return false;
}
2017-12-11 18:17:24 +08:00
}
2018-05-22 22:55:06 +08:00
bool e2d::String::operator==(const char *str) const
2018-02-28 19:17:15 +08:00
{
if (str)
{
String temp(str);
2018-05-08 17:40:36 +08:00
return (_str == temp._str);
2018-02-28 19:17:15 +08:00
}
else
{
return false;
}
}
2018-05-22 22:55:06 +08:00
bool e2d::String::operator ==(const e2d::String &str) const
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
return _str == str._str;
2017-12-11 18:17:24 +08:00
}
2018-05-22 22:55:06 +08:00
bool e2d::String::operator!=(const wchar_t *str) const
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
if (str)
{
2018-05-08 17:40:36 +08:00
return (_str.compare(str) != 0);
2018-02-27 16:32:17 +08:00
}
else
{
return true;
}
2017-12-11 18:17:24 +08:00
}
2018-05-22 22:55:06 +08:00
bool e2d::String::operator!=(const char *str) const
2018-02-28 19:17:15 +08:00
{
if (str)
{
String temp(str);
2018-05-08 17:40:36 +08:00
return (_str != temp._str);
2018-02-28 19:17:15 +08:00
}
else
{
return true;
}
}
2018-05-22 22:55:06 +08:00
bool e2d::String::operator!=(const e2d::String &str) const
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
return _str != str._str;
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
wchar_t &e2d::String::operator[](int index)
2017-12-11 18:17:24 +08:00
{
return _str[size_t(index)];
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String e2d::String::operator+(const wchar_t *str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
String temp;
2018-05-08 17:40:36 +08:00
temp._str = _str + str;
2018-02-27 16:32:17 +08:00
return std::move(temp);
2017-12-11 18:17:24 +08:00
}
2018-02-28 19:17:15 +08:00
e2d::String e2d::String::operator+(const char *str)
2017-12-11 18:17:24 +08:00
{
String temp;
2018-05-08 17:40:36 +08:00
temp._str = _str + static_cast<wchar_t*>(_bstr_t(str));
2018-02-27 16:32:17 +08:00
return std::move(temp);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String e2d::String::operator+(const e2d::String &str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
String temp;
2018-05-08 17:40:36 +08:00
temp._str = _str + str._str;
2018-02-27 16:32:17 +08:00
return std::move(temp);
}
2017-12-11 18:17:24 +08:00
2018-02-28 19:17:15 +08:00
e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
String temp;
2018-05-08 17:40:36 +08:00
temp._str = str1 + str2._str;
2018-02-27 16:32:17 +08:00
return std::move(temp);
}
2017-12-11 18:17:24 +08:00
2018-02-28 19:17:15 +08:00
e2d::String e2d::operator+(const char *str1, const String &str2)
2018-02-27 16:32:17 +08:00
{
String temp;
2018-05-08 17:40:36 +08:00
temp._str = static_cast<wchar_t*>(_bstr_t(str1)) + str2._str;
2018-02-27 16:32:17 +08:00
return std::move(temp);
}
2017-12-11 18:17:24 +08:00
2018-02-28 19:17:15 +08:00
e2d::String & e2d::String::operator+=(const wchar_t *str)
2018-02-27 16:32:17 +08:00
{
2018-05-08 17:40:36 +08:00
_str += str;
2018-02-28 19:17:15 +08:00
return (*this);
2017-12-11 18:17:24 +08:00
}
2018-02-28 19:17:15 +08:00
e2d::String & e2d::String::operator+=(const char *str)
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
_str += static_cast<wchar_t*>(_bstr_t(str));
2018-02-28 19:17:15 +08:00
return (*this);
2017-12-11 18:17:24 +08:00
}
2018-02-28 19:17:15 +08:00
e2d::String & e2d::String::operator+=(const String &str)
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
_str += str._str;
2018-02-28 19:17:15 +08:00
return (*this);
2017-12-11 18:17:24 +08:00
}
2018-02-28 19:17:15 +08:00
bool e2d::String::operator>(const String &str) const
{
2018-05-08 17:40:36 +08:00
return _str > str._str;
2018-02-28 19:17:15 +08:00
}
bool e2d::String::operator>=(const String &str) const
{
2018-05-08 17:40:36 +08:00
return _str >= str._str;
2018-02-28 19:17:15 +08:00
}
bool e2d::String::operator<(const String &str) const
{
2018-05-08 17:40:36 +08:00
return _str < str._str;
2018-02-28 19:17:15 +08:00
}
bool e2d::String::operator<=(const String &str) const
{
2018-05-08 17:40:36 +08:00
return _str <= str._str;
2018-02-28 19:17:15 +08:00
}
e2d::String & e2d::String::operator<<(const String &str)
{
2018-05-08 17:40:36 +08:00
_str += str._str;
2018-03-12 15:55:41 +08:00
return (*this);
}
e2d::String & e2d::String::operator<<(const wchar_t *str)
{
2018-05-08 17:40:36 +08:00
_str += str;
2018-03-12 15:55:41 +08:00
return (*this);
}
e2d::String & e2d::String::operator<<(wchar_t *str)
{
2018-05-08 17:40:36 +08:00
_str += str;
2018-03-12 15:55:41 +08:00
return (*this);
}
e2d::String & e2d::String::operator<<(const char * cstr)
{
2018-05-08 17:40:36 +08:00
_str += static_cast<wchar_t*>(_bstr_t(cstr));
2018-03-12 15:55:41 +08:00
return (*this);
}
e2d::String & e2d::String::operator<<(char * cstr)
{
2018-05-08 17:40:36 +08:00
_str += static_cast<wchar_t*>(_bstr_t(cstr));
2018-03-12 15:55:41 +08:00
return (*this);
}
e2d::String & e2d::String::operator<<(int value)
{
2018-04-01 23:08:11 +08:00
(*this) += String::parse(value);
2018-03-12 15:55:41 +08:00
return (*this);
}
e2d::String & e2d::String::operator<<(unsigned int value)
{
(*this) += String::parse(value);
return (*this);
}
2018-03-12 15:55:41 +08:00
e2d::String & e2d::String::operator<<(float value)
2018-02-28 19:17:15 +08:00
{
2018-04-01 23:08:11 +08:00
(*this) += String::parse(value);
2018-03-12 15:55:41 +08:00
return (*this);
2018-02-28 19:17:15 +08:00
}
2018-03-12 15:55:41 +08:00
e2d::String & e2d::String::operator<<(double value)
2018-02-28 19:17:15 +08:00
{
2018-04-01 23:08:11 +08:00
(*this) += String::parse(value);
2018-03-12 15:55:41 +08:00
return (*this);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String::operator const wchar_t*() const
{
2018-05-08 17:40:36 +08:00
return _str.c_str();
}
2018-02-28 19:17:15 +08:00
e2d::String::operator wchar_t*() const
{
2018-05-08 17:40:36 +08:00
return const_cast<wchar_t*>(_str.c_str());
2018-02-28 19:17:15 +08:00
}
2018-02-27 16:32:17 +08:00
bool e2d::String::isEmpty() const
{
2018-05-08 17:40:36 +08:00
return _str.empty();
}
2018-02-27 16:32:17 +08:00
int e2d::String::getLength() const
{
2018-05-08 17:40:36 +08:00
return static_cast<int>(_str.size());
}
unsigned int e2d::String::getHashCode() const
2017-12-11 18:17:24 +08:00
{
unsigned int hash = 0;
2018-05-14 22:51:40 +08:00
for (size_t i = 0, length = _str.size(); i < length; ++i)
2017-12-11 18:17:24 +08:00
{
hash *= 16777619;
2018-05-08 17:40:36 +08:00
hash ^= (unsigned int)towupper(_str[i]);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
return hash;
2017-12-11 18:17:24 +08:00
}
2018-03-12 13:42:49 +08:00
std::wstring e2d::String::getWString() const
{
2018-05-08 17:40:36 +08:00
return _str;
2018-03-12 13:42:49 +08:00
}
std::string e2d::String::getCString() const
{
2018-05-08 17:40:36 +08:00
std::string str = static_cast<const char *>(_bstr_t(_str.c_str()));
2018-03-12 13:42:49 +08:00
return std::move(str);
}
wchar_t e2d::String::at(int index) const
{
return _str[size_t(index)];
}
2018-05-01 17:19:55 +08:00
int e2d::String::compare(const String & str) const
{
2018-05-08 17:40:36 +08:00
return _str.compare(str._str);
2018-05-01 17:19:55 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String e2d::String::toUpper() const
{
2018-02-27 16:32:17 +08:00
String str(*this);
2018-05-14 22:51:40 +08:00
for (size_t i = 0, length = _str.size(); i < length; ++i)
2018-02-27 16:32:17 +08:00
str[i] = towupper(str[i]);
2018-02-27 16:32:17 +08:00
return std::move(str);
}
2018-02-27 16:32:17 +08:00
e2d::String e2d::String::toLower() const
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
e2d::String str(*this);
2017-12-11 18:17:24 +08:00
2018-05-14 22:51:40 +08:00
for (size_t i = 0, length = _str.size(); i < length; ++i)
2018-02-27 16:32:17 +08:00
str[i] = towlower(str[i]);
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
return std::move(str);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
int e2d::String::toInt() const
2017-12-11 18:17:24 +08:00
{
2018-05-14 22:51:40 +08:00
if (_str.empty())
2018-02-27 16:32:17 +08:00
{
return 0;
}
2018-05-08 17:40:36 +08:00
return std::stoi(_str, 0, 10);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
double e2d::String::toDouble() const
2017-12-11 18:17:24 +08:00
{
2018-05-14 22:51:40 +08:00
if (_str.empty())
2018-02-27 16:32:17 +08:00
{
return 0.0;
}
2018-05-08 17:40:36 +08:00
return std::stod(_str, 0);
2018-02-27 16:32:17 +08:00
}
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
bool e2d::String::toBool() const
{
2018-05-14 22:51:40 +08:00
if (_str.empty())
2018-02-27 16:32:17 +08:00
{
return false;
}
2017-12-11 18:17:24 +08:00
2018-05-08 17:40:36 +08:00
if (_str.compare(L"0") == 0 || _str.compare(L"false") == 0)
2018-02-27 16:32:17 +08:00
{
return false;
}
return true;
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String e2d::String::subtract(int offset, int count) const
2017-12-11 18:17:24 +08:00
{
2018-05-01 17:19:55 +08:00
String tmp;
2018-05-14 22:51:40 +08:00
int length = static_cast<int>(_str.size());
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
if (length == 0 || offset >= length)
2018-05-01 17:19:55 +08:00
return std::move(tmp);
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
offset = offset >= 0 ? offset : 0;
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
if (count < 0 || (offset + count) > length)
count = length - offset;
2017-12-11 18:17:24 +08:00
2018-05-08 17:40:36 +08:00
tmp._str = _str.substr(offset, count);
2018-05-01 17:19:55 +08:00
return std::move(tmp);
2017-12-11 18:17:24 +08:00
}
2018-05-01 17:19:55 +08:00
void e2d::String::insert(const String & str, int pos)
2017-12-11 18:17:24 +08:00
{
_str.insert(size_t(pos), str._str);
2017-12-11 18:17:24 +08:00
}
2018-05-01 17:19:55 +08:00
void e2d::String::replace(const String & from, const String & to)
2017-12-11 18:17:24 +08:00
{
2018-05-08 17:40:36 +08:00
if (from._str.empty())
2018-05-01 17:19:55 +08:00
return;
2017-12-11 18:17:24 +08:00
2018-05-01 17:19:55 +08:00
size_t start_pos = 0;
while ((start_pos = _str.find((const wchar_t *)from, start_pos)) != std::string::npos)
2018-05-01 17:19:55 +08:00
{
_str.replace(start_pos, from._str.length(), (const wchar_t *)to);
2018-05-08 17:40:36 +08:00
start_pos += to._str.length();
2018-05-01 17:19:55 +08:00
}
}
void e2d::String::erase(int offset, int count)
{
_str.erase(size_t(offset), size_t(count));
2018-05-01 17:19:55 +08:00
}
2017-12-11 18:17:24 +08:00
2018-05-01 17:19:55 +08:00
int e2d::String::find(const String & str, int offset) const
{
size_t index;
if ((index = _str.find(str._str, size_t(offset))) == std::wstring::npos)
2018-05-01 17:19:55 +08:00
{
return -1;
}
else
{
return static_cast<int>(index);
}
2017-12-11 18:17:24 +08:00
}
void e2d::String::clear()
{
2018-05-08 17:40:36 +08:00
_str.clear();
}
2018-03-12 13:42:49 +08:00
std::wostream & e2d::operator<<(std::wostream &cout, const String &str)
2018-02-28 19:17:15 +08:00
{
2018-05-08 17:40:36 +08:00
cout << str._str;
2018-02-28 19:17:15 +08:00
return cout;
}
2018-03-12 13:42:49 +08:00
std::wistream & e2d::operator>>(std::wistream &cin, String &str)
2018-02-28 19:17:15 +08:00
{
2018-05-08 17:40:36 +08:00
cin >> str._str;
2018-02-28 19:17:15 +08:00
return cin;
}
2018-03-12 13:42:49 +08:00
std::ostream & e2d::operator<<(std::ostream &cout, const String &str)
2018-02-28 19:17:15 +08:00
{
2018-05-08 17:40:36 +08:00
std::string cstr = static_cast<char*>(_bstr_t(str._str.c_str()));
2018-03-12 13:42:49 +08:00
cout << cstr;
2018-02-28 19:17:15 +08:00
return cout;
}
2018-03-12 13:42:49 +08:00
std::istream & e2d::operator>>(std::istream &cin, String &str)
2018-02-28 19:17:15 +08:00
{
std::string temp;
cin >> temp;
2018-05-08 17:40:36 +08:00
str._str = static_cast<wchar_t*>(_bstr_t(temp.c_str()));
2018-02-28 19:17:15 +08:00
return cin;
}