实现功能
主要实现了类似js的Split功能,类似py的format功能,以及Replace字符串替换功能。
代码
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <filesystem>
using namespace std;
// update: 20240512
// #include "String_Utils.hpp"
// 作者:撷星镌镜
namespace String_Utils {
#define _S(c) std::to_string(c)
struct Divide_Info {
size_t pos;
size_t len;
Divide_Info(const size_t pos, const size_t len) {
this->len = len;
this->pos = pos;
}
};
static vector<size_t> Split_Id(string str, string sp) {
vector<size_t> id_list;
size_t start = 0;
while (true) {
size_t id = (size_t)str.find(sp, start);
if (id == SIZE_MAX) {
break;
}
id_list.push_back(id);
start = id + (size_t)sp.size();
}
return id_list;
}
static vector<string> Split(string str, vector<string> sp_list) {
vector<Divide_Info> list;
for (string& sp : sp_list) {
vector<size_t> res = Split_Id(str, sp);
size_t len = (size_t)sp.size();
for (size_t p : res) {
list.push_back({ p,len });
}
}
auto sort_fun = [](const Divide_Info& v1, const Divide_Info& v2) {
return v1.pos < v2.pos;
};
sort(list.begin(), list.end(), sort_fun);
vector<string> res_list;
size_t chk = 0;
for (size_t i = 0; i < list.size(); i++) {
string sub = str.substr(chk, list[i].pos - chk);
if (sub != "") {
res_list.push_back(sub);
}
chk = list[i].pos + list[i].len;
}
string sub = str.substr(chk, str.size() - chk);
if (sub != "") {
res_list.push_back(sub);
}
return res_list;
}
static string Replace(string str,string from, string to)
{
vector<size_t> list = Split_Id(str, from);
size_t str_from_len = (size_t)from.size();
for (size_t i = (size_t)list.size() - 1; i != SIZE_MAX;i--)
{
str.replace(list[i],str_from_len ,to);
}
return str;
}
static string fmt(string str, vector<string> str_list) {
vector<size_t> list = Split_Id(str, "{}");
size_t times = (size_t)min(list.size(), list.size());
for (size_t i = times - 1; i != SIZE_MAX; i--) {
str.replace(list[i], 2, str_list[i]);
}
return str;
}
string Read_All(const string path) {
if (!filesystem::directory_entry(path).exists()) {
cout << "file not found" << endl;
return "";
}
size_t size = filesystem::directory_entry(path).file_size();
char* buf = new char[size + 4];
memset(buf,0,size + 4);
ifstream fin(path,ios::in|ios::binary);
fin.read(buf,size);
string res = buf;
fin.close();
delete(buf);
return res;
}
bool Has_Str_or(string str,const vector<string> sub_list) {
for (const string& sub : sub_list) {
size_t pos = str.find(sub);
if (pos != SIZE_MAX) {
return true;
}
}
return false;
}
bool Has_Str_and(string str,const vector<string> sub_list) {
for (const string& sub : sub_list) {
size_t pos = str.find(sub);
if (pos == SIZE_MAX) {
return false;
}
}
return true;
}
};
功能调用样例
string str = "12345678888889123456789ab\ncde88888fghijklmnopq88888rstuvwxyz";
//Split参数1为待处理的字符串,参数二类型是vector<string> 传入要将哪些字符串作为分割的标志
vector<string> list = su::Split(str, {"888","777","x","z"});
int i = 100;
//类似python的format功能,参数一为待处理的字符串,
//字符串中“{}”作为待填入参数的位置,
//参数二为字符串列表,同Split函数,
//“_S”宏定义就是to_string函数,可以快速将任意类型变量变成字符串。
//字符串列表中的参数先后对应到“{}”标记中
string str2 = su::fmt("hello_{}_{}_{}.sh\n___{}", {_S(1),_S(i+10),_S(i),"123456"});
//字符串替换,自动查找要替换的字符串,然后替换成目标字符串,字符串长度可以任意
//参数一为待处理的字符串
//参数二为要替换的字符串
//参数三为替换的目标字符串
string str3 = su::Replace(str,"888","___");
运行结果:
运行条件
c++ 17以上,若较低版本的c++则某些函数会出问题