From 1427bf4bca2c0f52429fe751c4c205acfd78b326 Mon Sep 17 00:00:00 2001 From: taynpg Date: Thu, 12 Dec 2024 22:42:44 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E5=88=86=E5=89=B2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/of_path.h | 17 ++++++++--------- include/of_str.h | 4 ++++ src/of_path.cpp | 37 ++++++++++++------------------------- src/of_str.cpp | 24 ++++++++++++++++++++++++ test/main.cpp | 6 +++--- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/include/of_path.h b/include/of_path.h index 9249c7f..2a012c8 100644 --- a/include/of_path.h +++ b/include/of_path.h @@ -11,15 +11,14 @@ public: ~COfPath(); public: - static bool isSamePath(const ofString& pa, const ofString& pb); - static ofString normalizePath(const ofString& path); - static ofString replaceStr(const ofString& str, const ofString& from, const ofString& to); - static ofString getFullRunPath(); - static ofString getHome(); - static ofString getConfigDir(const ofString& sub_dir, bool create = false); - static ofString getFull(const ofString& path, const ofString& sub_file_path); - static bool isExist(const ofString& path); - static bool writeBin(const ofString& path, const char* data, int len); + static bool is_same_path(const ofString& pa, const ofString& pb); + static ofString normalize(const ofString& path); + static ofString get_full_path(); + static ofString get_home(); + static ofString get_config_dir(const ofString& sub_dir, bool create = false); + static ofString get_full(const ofString& path, const ofString& sub_file_path); + static bool exist(const ofString& path); + static bool write(const ofString& path, const char* data, int len); }; }; // namespace ofen #endif \ No newline at end of file diff --git a/include/of_str.h b/include/of_str.h index a684329..f31b6f1 100644 --- a/include/of_str.h +++ b/include/of_str.h @@ -2,6 +2,7 @@ #define OFEN_STRING_HEADER #include "of_def.hpp" +#include namespace ofen { class COfStr @@ -9,6 +10,9 @@ class COfStr public: COfStr(); ~COfStr(); +public: + static ofString replace(const ofString& str, const ofString& from, const ofString& to); + static std::vector split(const ofString& input, const ofString& delimiter); }; }; // namespace ofen diff --git a/src/of_path.cpp b/src/of_path.cpp index a23dd34..55d3298 100644 --- a/src/of_path.cpp +++ b/src/of_path.cpp @@ -1,6 +1,7 @@ #include "of_path.h" #include #include +#include "of_str.h" #ifdef _WIN32 #include @@ -25,34 +26,20 @@ COfPath::~COfPath() { } -bool COfPath::isSamePath(const ofString& pa, const ofString& pb) +bool COfPath::is_same_path(const ofString& pa, const ofString& pb) { - return normalizePath(pa) == normalizePath(pb); + return normalize(pa) == normalize(pb); } -ofString COfPath::normalizePath(const ofString& path) +ofString COfPath::normalize(const ofString& path) { - ofString normalized = replaceStr(path, ofT("\\"), ofT("/")); + ofString normalized = COfStr::replace(path, ofT("\\"), ofT("/")); if (!normalized.empty() && normalized.back() == ofT('/')) { normalized.pop_back(); } return normalized; } - -ofString COfPath::replaceStr(const ofString& str, const ofString& from, const ofString& to) -{ - if (from.empty()) { - return str; - } - ofString result = str; - size_t startPos = 0; - while ((startPos = result.find(from, startPos)) != ofString::npos) { - result.replace(startPos, from.length(), to); - startPos += to.length(); - } - return result; -} -ofString COfPath::getFullRunPath() +ofString COfPath::get_full_path() { ofString path; #ifdef _WIN32 @@ -81,7 +68,7 @@ ofString COfPath::getFullRunPath() #endif } -ofString COfPath::getHome() +ofString COfPath::get_home() { #if defined(_WIN32) ofChar* value = nullptr; @@ -107,9 +94,9 @@ ofString COfPath::getHome() #endif } -ofString COfPath::getConfigDir(const ofString& sub_dir, bool create) +ofString COfPath::get_config_dir(const ofString& sub_dir, bool create) { - fs::path userHome = fs::path(getHome()); + fs::path userHome = fs::path(get_home()); userHome.append(".config"); userHome.append(sub_dir); if (create) { @@ -124,7 +111,7 @@ ofString COfPath::getConfigDir(const ofString& sub_dir, bool create) #endif } -ofString COfPath::getFull(const ofString& path, const ofString& sub_file_path) +ofString COfPath::get_full(const ofString& path, const ofString& sub_file_path) { fs::path p(path); p.append(sub_file_path); @@ -135,13 +122,13 @@ ofString COfPath::getFull(const ofString& path, const ofString& sub_file_path) #endif } -bool COfPath::isExist(const ofString& path) +bool COfPath::exist(const ofString& path) { fs::path p(path); return fs::exists(p); } -bool COfPath::writeBin(const ofString& path, const char* data, int len) +bool COfPath::write(const ofString& path, const char* data, int len) { std::ofstream file(path, std::ios::binary); if (!file.is_open()) { diff --git a/src/of_str.cpp b/src/of_str.cpp index c127240..fea283e 100644 --- a/src/of_str.cpp +++ b/src/of_str.cpp @@ -7,4 +7,28 @@ COfStr::COfStr() COfStr::~COfStr() { } +ofString COfStr::replace(const ofString& str, const ofString& from, const ofString& to) +{ + if (from.empty()) { + return str; + } + ofString result = str; + size_t startPos = 0; + while ((startPos = result.find(from, startPos)) != ofString::npos) { + result.replace(startPos, from.length(), to); + startPos += to.length(); + } + return result; +} +std::vector COfStr::split(const ofString& input, const ofString& delimiter) +{ + std::vector result; + size_t pos = 0, prev = 0; + while ((pos = input.find(delimiter, prev)) != ofString::npos) { + result.push_back(input.substr(prev, pos - prev)); + prev = pos + delimiter.size(); // Move past the delimiter + } + result.push_back(input.substr(prev)); + return result; +} } // namespace ofen diff --git a/test/main.cpp b/test/main.cpp index 21096e2..7146355 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -6,15 +6,15 @@ using namespace ofen; void testA() { - assert(COfPath::isSamePath(ofT("D:/Java"), ofT("D:\\Java\\"))); + assert(COfPath::is_same_path(ofT("D:/Java"), ofT("D:\\Java\\"))); } void testB() { - auto home = COfPath::getHome(); + auto home = COfPath::get_home(); assert(!home.empty()); - auto rp = COfPath::replaceStr(ofT("cpp/z"), ofT("p/"), ofT("Ni")); + auto rp = COfPath::replace(ofT("cpp/z"), ofT("p/"), ofT("Ni")); assert(rp == ofT("cpNiz")); }