c++解析自定义格式字符串
来源:互联网 发布:php工作流引擎原理 编辑:程序博客网 时间:2024/06/11 11:39
最近在开发功能模块时,需要支持自定义格式化字符串,便自己写了一个(效率不高,还请见谅)
直接上代码:
//自定义数据struct DescribeInfo{string desc;int type;int color;int size;int parm;void reset(){type = 1;desc = "";color = 1;size = 22;parm = 0;}};//根据分隔符切割字符串std::vector<string> getSubStrings(const string &str, char seperater){std::vector<string> strVec;string str1;auto it = str.begin();while (it != str.end()){if(*it != seperater){str1.push_back(*it);}else{strVec.push_back(str1);str1.clear();}++ it;}if(str1.length() != 0){strVec.push_back(str1);}return strVec;}std::string getValueByKey(const vector<std::string> &vec,const std::string &key){for (auto aa : vec){if (aa.find(key) != std::string::npos) {auto pos = aa.find_first_of('=');auto value = aa.substr(pos + 1, aa.size() - pos);return value;}}return "";}vector<DescribeInfo> analyzeDecColor(const string &str){string endStr = "end";int err = -1;vector<DescribeInfo> infoVec;///midint len = str.size();DescribeInfo info;info.reset();int pos1 = err, pos2 = err;for (int i = 0; i<len; ++i) {char c = str.at(i);if (c=='{'){pos1 = i;}else if (c=='}'){if (pos1>pos2){//cut last pos2 to last pos1int start = pos2;if (start != err){start += 1;//from '{' next pos} else{start = 0;}auto dis = pos1 - start;if (dis>0){info.desc = str.substr(start, dis);infoVec.push_back(info);}}if (i>pos1){if (pos2<pos1){auto formatStr = str.substr(pos1 + 1, i-pos1-1);if (formatStr != endStr){info.reset();auto strVec = getSubStrings(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{info.reset();}} else{//is contentinfo.desc = str.substr(pos2+1, i - pos2);infoVec.push_back(info);}}pos2 = i;}}auto maxIndex = pos1>pos2?pos1:pos2;if (maxIndex < len){info.desc = str.substr(maxIndex, len - maxIndex);infoVec.push_back(info);}return infoVec;}int main(int argc, char* argv[]){std::string str = "{{{color=6}滚滚长江东逝水,{{{{{type=6}浪花淘尽英雄,{end}是非成败转头空,{size=18}青山依旧在,几度夕阳红}{}{";auto vec = analyzeDecColor(str);for (auto info:vec){std::cout << "type=" << info.type << " color=" << info.color << " size=" << info.size << " desc=" << info.desc << std::endl;}getchar();return 0;}
测试结果:
阅读全文
0 0
- c++解析自定义格式字符串
- 自定义 DateTime 格式字符串
- C# 自定义格式字符串
- 自定义数字格式字符串
- 类型自定义格式字符串
- _snscanf_s解析格式字符串
- 解析json格式字符串
- C格式字符串
- C语言格式字符串
- C#自定义 DateTime 格式字符串
- [00802]自定义字符串输出格式
- iphone objective-c解析xml字符串(接收格式参照上篇 webservie xml字符串传值)
- C# 解析json格式字符串
- java解析xml格式字符串
- JAVA解析XML格式字符串
- c# 解析xml格式字符串
- JAVA解析XML格式字符串
- JAVA解析XML格式字符串
- 通过URL下载文件
- Shell编程中select用法
- mysql建表出现Error Code : 1005;Can't create table 'table' (errno: 121)
- Java进程CPU使用率高排查
- POJ
- c++解析自定义格式字符串
- python 虚拟环境
- 字符串堆空间释放报错bug
- Elasticsearch系列(六)----JAVA客户端之Jest操作详解
- Android查询任务栈Task
- 字符串反转-基于Python的6种方法比较(最全总结版本!)
- There are inconsistent line endings in the 'Assets/Scripts/Test.cs' script. Some are Mac OS X (UNIX)
- Linux下安装jdk
- 解决Package Control:There are no packages available for installation (sublime Text3)