#include <iostream>
#include <vector>
#include <string>
using namespace std;
//自定义数据结构
struct ContentInfo
{
string desc;
int type;
int color;
int size;
int parm;
void reset(){
type = 1;
desc = "";
color = 1;
size = 22;
parm = 0;
}
};
//求最大值
int _max(int x, int y)
{
return (x>y?x:y);
}
//求最小值
int _min(int x, int y)
{
return (x<y?x:y);
}
//切割字符串
vector<string> separateString(const string &str, const char charac)
{
vector<string> vec;
string tmpStr;
for (auto c : str){
if (c == charac)
{
if (!tmpStr.empty())
{
vec.push_back(tmpStr);
tmpStr.clear();
}
}
else{
tmpStr += c;
}
}
if (!tmpStr.empty())
{
vec.push_back(tmpStr);
}
return vec;
}
string getValueByKey(const vector<string> &vec, const string &key)
{
for (auto aa : vec){
if (aa.find(key) != string::npos) {
auto pos = aa.find_first_of('=');
auto value = aa.substr(pos + 1, aa.size() - pos);
return value;
}
}
return "";
}
//解析自定义格式字符串
vector<ContentInfo> analyzeDecColor(const string &str)
{
std::string endStr = "end";
int end_len = endStr.size() + 2;
int err = 0;
std::vector<ContentInfo> infoVec;
int len = str.size();
ContentInfo info;
info.reset();
int begin_pos = err, assist_pos = err,last_cut_pos=err;
int format_pos1 = err,format_pos2=err;
bool own_start_format = false;
bool splited = true;
for (int i = 0; i < len; ++i) {
char c = str.at(i);
if (c == '{')
{
if (splited)
{
begin_pos = i;
splited = false;
}
assist_pos = i;
//记录'}'之前最后一个'{'的位置
}
else if (c == '}')
{
auto formatStr = str.substr(assist_pos + 1, i - assist_pos - 1);
//裁剪指定区间段的字符串
if (!formatStr.empty())
{
if (formatStr != endStr)
{
own_start_format = true;
//设置自定义格式开启标志位
format_pos1 = assist_pos;
format_pos2 = i;
//记录自定义格式有效数据的起始位置
info.reset();
auto strVec = separateString(formatStr, ',');
auto colorStr = getValueByKey(strVec, "color");
auto typeStr = getValueByKey(strVec, "type");
auto sizeStr = getValueByKey(strVec, "size");
auto parmStr = getValueByKey(strVec, "parm");
//解析自定义格式
if (!colorStr.empty()){
info.color = atoi(colorStr.c_str());
}
if (!typeStr.empty()){
info.type = atoi(typeStr.c_str());
}
if (!sizeStr.empty()){
info.size = atoi(sizeStr.c_str());
}
if (!parmStr.empty())
{
info.parm = atoi(parmStr.c_str());
}
}
else{
//检测为自定义格式 结束格式符
if (own_start_format)
{
//存在自定义格式起始标志位 切割有效数据
int cut_pos = _min(begin_pos, last_cut_pos);
int len = format_pos1 - cut_pos;
if (len > 0)
{
auto cpInfo = info;
cpInfo.reset();
cpInfo.desc = str.substr(cut_pos, format_pos1 - cut_pos);
infoVec.push_back(cpInfo);
}
info.desc = str.substr(format_pos2 + 1, i - format_pos2 - end_len);
}else{
//没有自定义格式起始标志位 继续....
continue;
}
infoVec.push_back(info);
last_cut_pos = i+1;
splited = true;
own_start_format = false;
//将切割的有效数据存储 并重置相关标志位
}
}
}
}
auto maxIndex = splited ? _max(_max(begin_pos, assist_pos), last_cut_pos) : _min(_min(begin_pos, assist_pos), last_cut_pos);
maxIndex = _max(0, maxIndex);
if (maxIndex < len - 1)
{
info.reset();
info.desc = str.substr(maxIndex, len - maxIndex);
infoVec.push_back(info);
}
return infoVec;
}
int main()
{
vector<ContentInfo> analyzeDecColor(const string &str);
string str = "{}{end}{color=6}滚滚长江东逝水,{end}{color=3}浪花淘尽英雄,{end}是非成败转头空,{color=9}青山依旧在,几度夕阳红.{end}aa{end}cdaba{color=5}{}{end}";
auto vec = analyzeDecColor(str);
std::string str2;
for (auto info : vec)
{
std::cout << "color=" << info.color <<" desc=" << info.desc << std::endl;
str2 += info.desc;
}
getchar();
return 0;
}