From 6797570015eca66610c3b7edd7a44a9586336b04 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 1 May 2018 17:19:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Common/String.cpp | 64 +++++++++++++++++++++++++++++------------- core/Tool/Path.cpp | 2 +- core/e2dcommon.h | 40 ++++++++++++++++++++------ 3 files changed, 77 insertions(+), 29 deletions(-) diff --git a/core/Common/String.cpp b/core/Common/String.cpp index 2daf7a3c..cf27c3fb 100644 --- a/core/Common/String.cpp +++ b/core/Common/String.cpp @@ -83,7 +83,7 @@ e2d::String e2d::String::format(const char * format, ...) size_t num_of_chars = _vscprintf(format, marker); - if (num_of_chars > tmp.capacity()) + if (num_of_chars > tmp.capacity()) { tmp.resize(num_of_chars + 1); } @@ -118,6 +118,11 @@ e2d::String e2d::String::format(const wchar_t * format, ...) return std::move(str); } +void e2d::String::swap(String & str1, String & str2) +{ + str1.m_str.swap(str2.m_str); +} + e2d::String & e2d::String::operator=(const String &str) { m_str = str.m_str; @@ -359,6 +364,11 @@ std::string e2d::String::getCString() const return std::move(str); } +int e2d::String::compare(const String & str) const +{ + return m_str.compare(str.m_str); +} + e2d::String e2d::String::toUpper() const { String str(*this); @@ -385,7 +395,7 @@ int e2d::String::toInt() const { return 0; } - return _wtoi(m_str.c_str()); + return std::stoi(m_str, 0, 10); } double e2d::String::toDouble() const @@ -394,7 +404,7 @@ double e2d::String::toDouble() const { return 0.0; } - return _wtof(m_str.c_str()); + return std::stod(m_str, 0); } bool e2d::String::toBool() const @@ -413,39 +423,55 @@ bool e2d::String::toBool() const e2d::String e2d::String::subtract(int offset, int count) const { - String temp; + String tmp; int length = getLength(); if (length == 0 || offset >= length) - return std::move(temp); + return std::move(tmp); offset = offset >= 0 ? offset : 0; if (count < 0 || (offset + count) > length) count = length - offset; - temp.m_str = m_str.substr(offset, count); - return std::move(temp); + tmp.m_str = m_str.substr(offset, count); + return std::move(tmp); } -int e2d::String::findFirstOf(const wchar_t ch) const +void e2d::String::insert(const String & str, int pos) { - for (int i = 0; i < getLength(); i++) - if (m_str[i] == ch) - return i; - - return -1; + m_str.insert(static_cast(pos), str.m_str); } -int e2d::String::findLastOf(const wchar_t ch) const +void e2d::String::replace(const String & from, const String & to) { - int index = -1; + if (from.m_str.empty()) + return; - for (int i = 0; i < getLength(); i++) - if (m_str[i] == ch) - index = i; + size_t start_pos = 0; + while ((start_pos = m_str.find(from, start_pos)) != std::string::npos) + { + m_str.replace(start_pos, from.m_str.length(), to); + start_pos += to.m_str.length(); + } +} - return index; +void e2d::String::erase(int offset, int count) +{ + m_str.erase(static_cast(offset), static_cast(count)); +} + +int e2d::String::find(const String & str, int offset) const +{ + size_t index; + if ((index = m_str.find(str.m_str, static_cast(offset))) == std::wstring::npos) + { + return -1; + } + else + { + return static_cast(index); + } } void e2d::String::clear() diff --git a/core/Tool/Path.cpp b/core/Tool/Path.cpp index 79484d1b..6fdbadf6 100644 --- a/core/Tool/Path.cpp +++ b/core/Tool/Path.cpp @@ -133,7 +133,7 @@ e2d::String e2d::Path::getFileExtension(String filePath) { String fileExtension; // 找到文件名中的最后一个 '.' 的位置 - int pos = filePath.findLastOf(L'.'); + int pos = filePath.getWString().find_last_of(L'.'); // 判断 pos 是否是个有效位置 if (pos != -1) { diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 3f3f4340..afd38134 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -93,20 +93,39 @@ public: // 获取 ANSI 字符串 std::string getCString() const; - // 获取裁剪字符串 - String subtract( + // 比较字符串 + int compare( + const String & str + ) const; + + // 截取字符串 + e2d::String subtract( int offset, /* 偏移量 */ int count = -1 /* 截取字符数量 */ ) const; - // 获取字符串中第一个特定字符的下标 - int findFirstOf( - const wchar_t ch - ) const; + // 插入字符串 + void insert( + const String & str, + int pos + ); - // 获取字符串中最后一个特定字符的下标 - int findLastOf( - const wchar_t ch + // 替换字符串中的指定内容 + void replace( + const String & from, /* 需替换内容 */ + const String & to /* 替换成内容 */ + ); + + // 删除字符串中的指定内容 + void erase( + int offset, /* 偏移量 */ + int count /* 删除字符数量 */ + ); + + // 搜索字符串 + int find( + const String & str, /* 查找内容 */ + int offset = 0 /* 偏移量 */ ) const; // 清空字符串 @@ -137,6 +156,9 @@ public: static String format(const char * format, ...); static String format(const wchar_t * format, ...); + // 交换两字符串 + static void swap(String &str1, String &str2); + // 赋值运算符 String& operator= (const String &); String& operator= (const char *);