2018-10-03 22:02:46 +08:00
|
|
|
// Copyright (c) 2016-2018 Easy2D - Nomango
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
2018-09-05 13:33:39 +08:00
|
|
|
#include "..\e2dutil.h"
|
2017-12-11 18:17:24 +08:00
|
|
|
#include <iomanip>
|
2018-05-24 17:19:54 +08:00
|
|
|
#include <cwctype>
|
2018-02-28 19:17:15 +08:00
|
|
|
#include <comutil.h>
|
|
|
|
|
#pragma comment(lib, "comsuppw.lib")
|
|
|
|
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::String()
|
2018-09-04 22:42:34 +08:00
|
|
|
: string_(L"")
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::String(const wchar_t *str)
|
2018-09-04 22:42:34 +08:00
|
|
|
: string_(str)
|
2017-12-12 12:18:28 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::String(const char *cstr)
|
2018-09-04 22:42:34 +08:00
|
|
|
: string_(static_cast<wchar_t*>(_bstr_t(cstr)))
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::String(easy2d::String && str)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ = std::move(str.string_);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::String(const easy2d::String &str)
|
2018-09-04 22:42:34 +08:00
|
|
|
: string_(str.string_)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::~String()
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_.clear();
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String &easy2d::String::operator=(const wchar_t *str)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ = str;
|
2018-03-12 15:55:41 +08:00
|
|
|
return (*this);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator=(const char *cstr)
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ = 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-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::Parse(int value)
|
2018-04-01 23:08:11 +08:00
|
|
|
{
|
|
|
|
|
String tmp;
|
2018-09-04 22:42:34 +08:00
|
|
|
tmp.string_ = std::to_wstring(value);
|
2018-04-01 23:08:11 +08:00
|
|
|
return std::move(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::Parse(unsigned int value)
|
2018-04-02 23:01:38 +08:00
|
|
|
{
|
|
|
|
|
String tmp;
|
2018-09-04 22:42:34 +08:00
|
|
|
tmp.string_ = std::to_wstring(value);
|
2018-04-02 23:01:38 +08:00
|
|
|
return std::move(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::Parse(float value)
|
2018-04-01 23:08:11 +08:00
|
|
|
{
|
|
|
|
|
String tmp;
|
2018-09-04 22:42:34 +08:00
|
|
|
tmp.string_ = std::to_wstring(value);
|
2018-04-01 23:08:11 +08:00
|
|
|
return std::move(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::Parse(double value)
|
2018-04-01 23:08:11 +08:00
|
|
|
{
|
|
|
|
|
String tmp;
|
2018-09-04 22:42:34 +08:00
|
|
|
tmp.string_ = std::to_wstring(value);
|
2018-04-01 23:08:11 +08:00
|
|
|
return std::move(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::Format(const char * format, ...)
|
2018-03-22 18:25:56 +08:00
|
|
|
{
|
|
|
|
|
std::string tmp;
|
|
|
|
|
|
2018-07-25 16:44:22 +08:00
|
|
|
va_list marker;
|
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-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::Format(const wchar_t * format, ...)
|
2018-03-22 18:25:56 +08:00
|
|
|
{
|
|
|
|
|
std::wstring tmp;
|
|
|
|
|
|
2018-07-25 16:44:22 +08:00
|
|
|
va_list marker;
|
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-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator=(const String &str)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ = str.string_;
|
2018-03-12 15:55:41 +08:00
|
|
|
return (*this);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::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-09-04 22:42:34 +08:00
|
|
|
return (string_.compare(str) == 0);
|
2018-02-03 22:04:43 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::operator==(const char *str) const
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
|
|
|
|
if (str)
|
|
|
|
|
{
|
|
|
|
|
String temp(str);
|
2018-09-04 22:42:34 +08:00
|
|
|
return (string_ == temp.string_);
|
2018-02-28 19:17:15 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::operator ==(const easy2d::String &str) const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_ == str.string_;
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::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-09-04 22:42:34 +08:00
|
|
|
return (string_.compare(str) != 0);
|
2018-02-27 16:32:17 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::operator!=(const char *str) const
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
|
|
|
|
if (str)
|
|
|
|
|
{
|
|
|
|
|
String temp(str);
|
2018-09-04 22:42:34 +08:00
|
|
|
return (string_ != temp.string_);
|
2018-02-28 19:17:15 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::operator!=(const easy2d::String &str) const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_ != str.string_;
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
wchar_t &easy2d::String::operator[](size_t index)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_[index];
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::operator+(const wchar_t *str) const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-02-27 16:32:17 +08:00
|
|
|
String temp;
|
2018-09-04 22:42:34 +08:00
|
|
|
temp.string_ = string_ + str;
|
2018-02-27 16:32:17 +08:00
|
|
|
return std::move(temp);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::operator+(const char *str) const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-03-11 23:56:40 +08:00
|
|
|
String temp;
|
2018-09-04 22:42:34 +08:00
|
|
|
temp.string_ = string_ + 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-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::operator+(const easy2d::String &str) const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-02-27 16:32:17 +08:00
|
|
|
String temp;
|
2018-09-04 22:42:34 +08:00
|
|
|
temp.string_ = string_ + str.string_;
|
2018-02-27 16:32:17 +08:00
|
|
|
return std::move(temp);
|
|
|
|
|
}
|
2017-12-11 18:17:24 +08:00
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::operator+(const wchar_t *str1, const easy2d::String &str2)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-02-27 16:32:17 +08:00
|
|
|
String temp;
|
2018-09-04 22:42:34 +08:00
|
|
|
temp.string_ = str1 + str2.string_;
|
2018-02-27 16:32:17 +08:00
|
|
|
return std::move(temp);
|
|
|
|
|
}
|
2017-12-11 18:17:24 +08:00
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::operator+(const char *str1, const String &str2)
|
2018-02-27 16:32:17 +08:00
|
|
|
{
|
2018-03-11 23:56:40 +08:00
|
|
|
String temp;
|
2018-09-04 22:42:34 +08:00
|
|
|
temp.string_ = static_cast<wchar_t*>(_bstr_t(str1)) + str2.string_;
|
2018-02-27 16:32:17 +08:00
|
|
|
return std::move(temp);
|
|
|
|
|
}
|
2017-12-11 18:17:24 +08:00
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator+=(const wchar_t *str)
|
2018-02-27 16:32:17 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ += str;
|
2018-02-28 19:17:15 +08:00
|
|
|
return (*this);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator+=(const char *str)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ += 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-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator+=(const String &str)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ += str.string_;
|
2018-02-28 19:17:15 +08:00
|
|
|
return (*this);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::operator>(const String &str) const
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_ > str.string_;
|
2018-02-28 19:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::operator>=(const String &str) const
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_ >= str.string_;
|
2018-02-28 19:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::operator<(const String &str) const
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_ < str.string_;
|
2018-02-28 19:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::operator<=(const String &str) const
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_ <= str.string_;
|
2018-02-28 19:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(const String &str)
|
2018-03-11 23:56:40 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ += str.string_;
|
2018-03-12 15:55:41 +08:00
|
|
|
return (*this);
|
2018-03-11 23:56:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(const wchar_t *str)
|
2018-03-11 23:56:40 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ += str;
|
2018-03-12 15:55:41 +08:00
|
|
|
return (*this);
|
2018-03-11 23:56:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(wchar_t *str)
|
2018-03-11 23:56:40 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ += str;
|
2018-03-12 15:55:41 +08:00
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(const char * cstr)
|
2018-03-12 15:55:41 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ += static_cast<wchar_t*>(_bstr_t(cstr));
|
2018-03-12 15:55:41 +08:00
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(char * cstr)
|
2018-03-12 15:55:41 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_ += static_cast<wchar_t*>(_bstr_t(cstr));
|
2018-03-12 15:55:41 +08:00
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(int value)
|
2018-03-12 15:55:41 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
(*this) += String::Parse(value);
|
2018-03-12 15:55:41 +08:00
|
|
|
return (*this);
|
2018-03-11 23:56:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(unsigned int value)
|
2018-04-02 23:01:38 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
(*this) += String::Parse(value);
|
2018-04-02 23:01:38 +08:00
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(float value)
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +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-10-16 14:13:15 +08:00
|
|
|
easy2d::String & easy2d::String::operator<<(double value)
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +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-10-16 14:13:15 +08:00
|
|
|
easy2d::String::operator const wchar_t*() const
|
2017-12-12 11:28:12 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_.c_str();
|
2017-12-12 11:28:12 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::operator wchar_t*() const
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return const_cast<wchar_t*>(string_.c_str());
|
2018-02-28 19:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::operator std::wstring() const
|
2018-08-28 00:06:10 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_;
|
2018-08-28 00:06:10 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String::operator std::string() const
|
2018-08-28 00:06:10 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
std::string str = static_cast<const char *>(_bstr_t(string_.c_str()));
|
2018-08-28 00:06:10 +08:00
|
|
|
return std::move(str);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::IsEmpty() const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_.empty();
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:57:28 +08:00
|
|
|
int easy2d::String::Length() const
|
2018-03-12 13:42:49 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return static_cast<int>(string_.size());
|
2018-03-12 13:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
size_t easy2d::String::GetHash() const
|
2018-03-12 13:42:49 +08:00
|
|
|
{
|
2018-08-23 16:37:44 +08:00
|
|
|
std::hash<std::wstring> hash;
|
2018-09-04 22:42:34 +08:00
|
|
|
return hash(string_);
|
2018-03-12 13:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
const wchar_t& easy2d::String::At(size_t index) const
|
2018-05-24 14:33:16 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_.at(index);
|
2018-05-24 14:33:16 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
int easy2d::String::Compare(const String & str) const
|
2018-05-01 17:19:55 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return string_.compare(str.string_);
|
2018-05-01 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::ToUpper() const
|
2017-12-12 12:18:28 +08:00
|
|
|
{
|
2018-02-27 16:32:17 +08:00
|
|
|
String str(*this);
|
2018-09-04 22:42:34 +08:00
|
|
|
std::transform(str.string_.begin(), str.string_.end(), str.string_.begin(), std::towupper);
|
2018-02-27 16:32:17 +08:00
|
|
|
return std::move(str);
|
2017-12-12 12:18:28 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::String::ToLower() const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-10-16 14:13:15 +08:00
|
|
|
easy2d::String str(*this);
|
2018-09-04 22:42:34 +08:00
|
|
|
std::transform(str.string_.begin(), str.string_.end(), str.string_.begin(), std::towlower);
|
2018-02-27 16:32:17 +08:00
|
|
|
return std::move(str);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
int easy2d::String::ToInt() const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
if (string_.empty())
|
2018-02-27 16:32:17 +08:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
return std::stoi(string_, 0, 10);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
float easy2d::String::ToFloat() const
|
2018-07-28 20:06:27 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
if (string_.empty())
|
2018-07-28 20:06:27 +08:00
|
|
|
{
|
|
|
|
|
return 0.f;
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
return std::stof(string_, 0);
|
2018-07-28 20:06:27 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
double easy2d::String::ToDouble() const
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
if (string_.empty())
|
2018-02-27 16:32:17 +08:00
|
|
|
{
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
return std::stod(string_, 0);
|
2018-02-27 16:32:17 +08:00
|
|
|
}
|
2017-12-11 18:17:24 +08:00
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
bool easy2d::String::ToBool() const
|
2018-02-27 16:32:17 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
if (string_.empty())
|
2018-02-27 16:32:17 +08:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-12-11 18:17:24 +08:00
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
if (string_.compare(L"0") == 0 || string_.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-10-16 14:13:15 +08:00
|
|
|
easy2d::String easy2d::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-09-04 22:42:34 +08:00
|
|
|
int length = static_cast<int>(string_.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-09-04 22:42:34 +08:00
|
|
|
tmp.string_ = string_.substr(offset, count);
|
2018-05-01 17:19:55 +08:00
|
|
|
return std::move(tmp);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::String::Insert(const String & str, int pos)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_.insert(size_t(pos), str.string_);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::String::Replace(const String & from, const String & to)
|
2017-12-11 18:17:24 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
if (from.string_.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;
|
2018-09-04 22:42:34 +08:00
|
|
|
while ((start_pos = string_.find((const wchar_t *)from, start_pos)) != std::wstring::npos)
|
2018-05-01 17:19:55 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_.replace(start_pos, from.string_.length(), (const wchar_t *)to);
|
|
|
|
|
start_pos += to.string_.length();
|
2018-05-01 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::String::Erase(int offset, int count)
|
2018-05-01 17:19:55 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_.erase(size_t(offset), size_t(count));
|
2018-05-01 17:19:55 +08:00
|
|
|
}
|
2017-12-11 18:17:24 +08:00
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
int easy2d::String::Find(const String & str, int offset) const
|
2018-05-01 17:19:55 +08:00
|
|
|
{
|
|
|
|
|
size_t index;
|
2018-09-04 22:42:34 +08:00
|
|
|
if ((index = string_.find(str.string_, size_t(offset))) == std::wstring::npos)
|
2018-05-01 17:19:55 +08:00
|
|
|
return -1;
|
2018-10-16 15:42:15 +08:00
|
|
|
return static_cast<int>(index);
|
2017-12-11 18:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
void easy2d::String::Clear()
|
2018-03-11 23:56:40 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
string_.clear();
|
2018-03-11 23:56:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
std::wostream & easy2d::operator<<(std::wostream &cout, const String &str)
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
cout << str.string_;
|
2018-02-28 19:17:15 +08:00
|
|
|
return cout;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
std::wistream & easy2d::operator>>(std::wistream &cin, String &str)
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
cin >> str.string_;
|
2018-02-28 19:17:15 +08:00
|
|
|
return cin;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
std::ostream & easy2d::operator<<(std::ostream &cout, const String &str)
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
std::string cstr = static_cast<char*>(_bstr_t(str.string_.c_str()));
|
2018-03-12 13:42:49 +08:00
|
|
|
cout << cstr;
|
2018-02-28 19:17:15 +08:00
|
|
|
return cout;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
std::istream & easy2d::operator>>(std::istream &cin, String &str)
|
2018-02-28 19:17:15 +08:00
|
|
|
{
|
|
|
|
|
std::string temp;
|
|
|
|
|
cin >> temp;
|
2018-09-04 22:42:34 +08:00
|
|
|
str.string_ = static_cast<wchar_t*>(_bstr_t(temp.c_str()));
|
2018-02-28 19:17:15 +08:00
|
|
|
return cin;
|
|
|
|
|
}
|