add:添加字符串分割。

This commit is contained in:
taynpg 2024-12-12 22:42:44 +08:00
parent 0f7a8d3c92
commit 1427bf4bca
5 changed files with 51 additions and 37 deletions

View File

@ -11,15 +11,14 @@ public:
~COfPath(); ~COfPath();
public: public:
static bool isSamePath(const ofString& pa, const ofString& pb); static bool is_same_path(const ofString& pa, const ofString& pb);
static ofString normalizePath(const ofString& path); static ofString normalize(const ofString& path);
static ofString replaceStr(const ofString& str, const ofString& from, const ofString& to); static ofString get_full_path();
static ofString getFullRunPath(); static ofString get_home();
static ofString getHome(); static ofString get_config_dir(const ofString& sub_dir, bool create = false);
static ofString getConfigDir(const ofString& sub_dir, bool create = false); static ofString get_full(const ofString& path, const ofString& sub_file_path);
static ofString getFull(const ofString& path, const ofString& sub_file_path); static bool exist(const ofString& path);
static bool isExist(const ofString& path); static bool write(const ofString& path, const char* data, int len);
static bool writeBin(const ofString& path, const char* data, int len);
}; };
}; // namespace ofen }; // namespace ofen
#endif #endif

View File

@ -2,6 +2,7 @@
#define OFEN_STRING_HEADER #define OFEN_STRING_HEADER
#include "of_def.hpp" #include "of_def.hpp"
#include <vector>
namespace ofen { namespace ofen {
class COfStr class COfStr
@ -9,6 +10,9 @@ class COfStr
public: public:
COfStr(); COfStr();
~COfStr(); ~COfStr();
public:
static ofString replace(const ofString& str, const ofString& from, const ofString& to);
static std::vector<ofString> split(const ofString& input, const ofString& delimiter);
}; };
}; // namespace ofen }; // namespace ofen

View File

@ -1,6 +1,7 @@
#include "of_path.h" #include "of_path.h"
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include "of_str.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
@ -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('/')) { if (!normalized.empty() && normalized.back() == ofT('/')) {
normalized.pop_back(); normalized.pop_back();
} }
return normalized; return normalized;
} }
ofString COfPath::get_full_path()
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 path; ofString path;
#ifdef _WIN32 #ifdef _WIN32
@ -81,7 +68,7 @@ ofString COfPath::getFullRunPath()
#endif #endif
} }
ofString COfPath::getHome() ofString COfPath::get_home()
{ {
#if defined(_WIN32) #if defined(_WIN32)
ofChar* value = nullptr; ofChar* value = nullptr;
@ -107,9 +94,9 @@ ofString COfPath::getHome()
#endif #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(".config");
userHome.append(sub_dir); userHome.append(sub_dir);
if (create) { if (create) {
@ -124,7 +111,7 @@ ofString COfPath::getConfigDir(const ofString& sub_dir, bool create)
#endif #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); fs::path p(path);
p.append(sub_file_path); p.append(sub_file_path);
@ -135,13 +122,13 @@ ofString COfPath::getFull(const ofString& path, const ofString& sub_file_path)
#endif #endif
} }
bool COfPath::isExist(const ofString& path) bool COfPath::exist(const ofString& path)
{ {
fs::path p(path); fs::path p(path);
return fs::exists(p); 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); std::ofstream file(path, std::ios::binary);
if (!file.is_open()) { if (!file.is_open()) {

View File

@ -7,4 +7,28 @@ COfStr::COfStr()
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<ofString> COfStr::split(const ofString& input, const ofString& delimiter)
{
std::vector<ofString> 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 } // namespace ofen

View File

@ -6,15 +6,15 @@ using namespace ofen;
void testA() void testA()
{ {
assert(COfPath::isSamePath(ofT("D:/Java"), ofT("D:\\Java\\"))); assert(COfPath::is_same_path(ofT("D:/Java"), ofT("D:\\Java\\")));
} }
void testB() void testB()
{ {
auto home = COfPath::getHome(); auto home = COfPath::get_home();
assert(!home.empty()); 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")); assert(rp == ofT("cpNiz"));
} }