Magic_Game/core/Common/String.cpp

316 lines
4.8 KiB
C++
Raw Normal View History

2017-12-11 18:17:24 +08:00
#include "..\ecommon.h"
#include <iomanip>
2018-02-27 16:32:17 +08:00
e2d::String::String()
: m_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)
: m_str(str)
{
}
2018-02-27 16:32:17 +08:00
e2d::String::String(e2d::String && str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
m_str = std::move(str.m_str);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String::String(const e2d::String &str)
: m_str(str.m_str)
2017-12-11 18:17:24 +08:00
{
}
e2d::String::String(const std::wstring &str)
2018-02-27 16:32:17 +08:00
: m_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-02-27 16:32:17 +08:00
m_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-02-27 16:32:17 +08:00
m_str = str;
2017-12-11 18:17:24 +08:00
return *this;
}
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-02-27 16:32:17 +08:00
m_str = str.m_str;
2017-12-11 18:17:24 +08:00
return *this;
}
2018-02-27 16:32:17 +08:00
e2d::String & e2d::String::operator=(const std::wstring &str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
m_str = str;
2017-12-11 18:17:24 +08:00
return *this;
}
2018-02-27 16:32:17 +08:00
bool e2d::String::operator==(const wchar_t *str)
2017-12-11 18:17:24 +08:00
{
2018-02-03 22:04:43 +08:00
if (str)
{
2018-02-27 16:32:17 +08:00
return (m_str.compare(str) == 0);
2018-02-03 22:04:43 +08:00
}
else
{
return false;
}
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
bool e2d::String::operator ==(const e2d::String &str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
return m_str == str.m_str;
2017-12-11 18:17:24 +08:00
}
bool e2d::String::operator==(const std::wstring &str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
return m_str == str;
2017-12-11 18:17:24 +08:00
}
bool e2d::String::operator!=(const wchar_t *str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
if (str)
{
return (m_str.compare(str) != 0);
}
else
{
return true;
}
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
bool e2d::String::operator!=(const e2d::String &str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
return m_str != str.m_str;
2017-12-11 18:17:24 +08:00
}
bool e2d::String::operator!=(const std::wstring &str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
return m_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
{
2018-02-27 16:32:17 +08:00
return m_str[static_cast<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;
temp.m_str = m_str + str;
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 wchar_t x)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
String temp;
temp.m_str = m_str + x;
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;
temp.m_str = m_str + str.m_str;
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 std::wstring &str)
{
String temp;
temp.m_str = m_str + str;
return std::move(temp);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String e2d::operator+(const wchar_t ch, const e2d::String &str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
String temp;
temp.m_str = ch + str.m_str;
return std::move(temp);
}
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2)
{
String temp;
temp.m_str = str1 + str2.m_str;
return std::move(temp);
2017-12-11 18:17:24 +08:00
}
2018-02-27 16:32:17 +08:00
e2d::String e2d::operator+(const std::wstring &str1, const e2d::String &str2)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
String temp;
temp.m_str = str1 + str2.m_str;
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 wchar_t x)
{
m_str += x;
2017-12-11 18:17:24 +08:00
return *this;
}
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
m_str += str;
2017-12-11 18:17:24 +08:00
return *this;
}
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
m_str += str.m_str;
2017-12-11 18:17:24 +08:00
return *this;
}
2018-02-27 16:32:17 +08:00
e2d::String & e2d::String::operator+=(const std::wstring &str)
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
m_str += str;
2017-12-11 18:17:24 +08:00
return *this;
}
2018-02-27 16:32:17 +08:00
e2d::String::operator const wchar_t*() const
{
2018-02-27 16:32:17 +08:00
return m_str.c_str();
}
2018-02-27 16:32:17 +08:00
e2d::String::operator bool() const
{
2018-02-27 16:32:17 +08:00
return getLength() != 0;
}
2018-02-27 16:32:17 +08:00
bool e2d::String::isEmpty() const
{
2018-02-27 16:32:17 +08:00
return m_str.empty();
}
2018-02-27 16:32:17 +08:00
int e2d::String::getLength() const
{
2018-02-27 16:32:17 +08:00
return static_cast<int>(m_str.size());
}
2018-02-27 16:32:17 +08:00
unsigned int e2d::String::getHash() const
2017-12-11 18:17:24 +08:00
{
unsigned int hash = 0;
2018-02-27 16:32:17 +08:00
for (int i = 0; i < getLength(); i++)
2017-12-11 18:17:24 +08:00
{
hash *= 16777619;
2018-02-27 16:32:17 +08:00
hash ^= (unsigned int)towupper(m_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-02-27 16:32:17 +08:00
std::wistream & e2d::operator>>(std::wistream &cin, e2d::String &str)
{
2018-02-27 16:32:17 +08:00
cin >> str.m_str;
return cin;
}
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-02-27 16:32:17 +08:00
for (int i = 0; i < getLength(); i++)
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-02-27 16:32:17 +08:00
for (int i = 0; i < getLength(); i++)
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-02-27 16:32:17 +08:00
if (getLength() == 0)
{
return 0;
}
return _wtoi(m_str.c_str());
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-02-27 16:32:17 +08:00
if (getLength() == 0)
{
return 0.0;
}
return _wtof(m_str.c_str());
}
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
bool e2d::String::toBool() const
{
if (getLength() == 0)
{
return false;
}
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
if (m_str.compare(L"0") == 0 || m_str.compare(L"false") == 0)
{
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-02-27 16:32:17 +08:00
String temp;
int length = getLength();
2017-12-11 18:17:24 +08:00
2018-02-27 16:32:17 +08:00
if (length == 0 || offset >= length)
return std::move(temp);
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-02-27 16:32:17 +08:00
temp.m_str = m_str.substr(offset, count);
return std::move(temp);
2017-12-11 18:17:24 +08:00
}
int e2d::String::findFirstOf(const wchar_t ch) const
2017-12-11 18:17:24 +08:00
{
2018-02-27 16:32:17 +08:00
for (int i = 0; i < getLength(); i++)
if (m_str[i] == ch)
2017-12-11 18:17:24 +08:00
return i;
return -1;
}
int e2d::String::findLastOf(const wchar_t ch) const
2017-12-11 18:17:24 +08:00
{
int index = -1;
2018-02-27 16:32:17 +08:00
for (int i = 0; i < getLength(); i++)
if (m_str[i] == ch)
2017-12-11 18:17:24 +08:00
index = i;
return index;
}
2018-02-27 16:32:17 +08:00
e2d::String & e2d::String::append(const wchar_t ch)
2017-12-11 18:17:24 +08:00
{
return (*this) += ch;
}
2018-02-27 16:32:17 +08:00
e2d::String & e2d::String::append(const wchar_t * str)
2017-12-11 18:17:24 +08:00
{
return (*this) += str;
}
2018-02-27 16:32:17 +08:00
e2d::String & e2d::String::append(const e2d::String & str)
2017-12-11 18:17:24 +08:00
{
return (*this) += str;
}