如何优化解析函数 [英] How Can I Optimize Parsing Function

查看:60
本文介绍了如何优化解析函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写这段代码并且它运行良好但是当我调用解析更多次时它的情况下堆栈溢出

我如何优化解析函数

i write this code and it works well but when i call parsing more times it cases stack overflow
how can i optimize parsing function

char raw_data2[] = "-------------------------------------------------------------------------------/"
"Testing NO.         : 1/"
"Beginning Time      : 2014-02-13 13:34:36/"
"Total Lines to test : 1/"
"Testing Result      :    1/"

"Status: finished/"

 "------------Accomplished testing lines and items------------/"
"--------  ------  -------  ------  --------  ------  -------  ------  -------/"
    "SLN(unit-sunit-index)           TESTITEM        RESULT/"
"--------  ------  -------  ------  --------  ------  -------  ------  -------/"

   "1-03-01  LineVolt  A-B   DC Vol:  019.11V  AC Vol: 0000.20V/"

   "1-03-01  LineVolt  A-GND DC Vol: -025.00V  AC Vol:0000.50V/"

   "1-03-01  LineVolt  B-GND DC Vol: -044.12V  AC Vol: 0000.56V/"

 "-----------No-accomplished testing lines and items-----------/"
"--------  ------  -------  ------  --------  ------  -------  ------  -------/"
    "SLN(unit-sunit-index)                 TESTITEM/"
"--------  ------  -------  ------  --------  ------  -------  ------  -------/"
"Total tested count  : 1 /"
"Total success count : 1 /"
"Total failure count : 0 /"
"---------------------------   END  ------------------------------------------";
        //char h [4];
        //delete []h;
        data = parsing(raw_data3,"A-B   DC Vol",":");
        data = parsing(raw_data3,"DC Vol",":",1);
        data = parsing(raw_data3,"A-B   DC Vo",":",2);
        float jk = atof(data.c_str());
        data = parsing(raw_data3,"Insulation capacitor  A-B",":");
        data = parsing(raw_data3,"Insulation resistence A-B",":");
        data = parsing(raw_data3,"A-B   DC Vol",":");
}
string parsing(string raw_data,string search_str,string delm , int offset)
{
	int  gg = raw_data.find(search_str);
	while(offset != 0) 
	{
	raw_data = raw_data.substr(gg+search_str.length());
	offset--;
	}
	trim(search_str);
	if(raw_data.length() == 0)
		return "";
	
	char *g;// = new char[50];//="";
	g = strtok((char*)raw_data.c_str(),delm.c_str());
	char value[50];
	memset(value,0,sizeof(value));
	int index = 0;
	bool trim_first =false;
	while(true)
	{
		if(!strstr(g,search_str.c_str())) 
		g =strtok(NULL,delm.c_str());
				if(g==NULL)
				               return "error5";
	
		
//		raw_data+=strlen(g);
		else if(strstr(g,search_str.c_str()))
		{ 
			g = strtok(NULL,delm.c_str());
			while (true)
			{
				while(!trim_first)
				{
							if(g==NULL)
				               return "error";
	
					// trim white space
					if(g[0]==' ')
					{
						g++;
				    }
					else
					{
						trim_first = true;
						break;
					}
				}
				if(g[0]==' ' || g[0]=='\n' || g[0]=='\r' ||g[0]==NULL)
					break;
				else 
				{
					value[index] = g[0];
					index++;
					g++;
				}
				
				
			}
	  	
			break;
		}
	}
	
	//delete[] g;
	return string(value);
}
  void trim(std::string& str)
{
	
str.erase(0, str.find_first_not_of(' '));       //prefixing spaces
str.erase(str.find_last_not_of(' ')+1);         //surfixing spaces
//return str;
}

推荐答案

value is声明为50个字符,但您的索引变量是无限制的。值在堆栈上声明,因此一旦索引达到50,您将开始删除堆栈中的其他数据。



作为旁注,您可以替换:



value is declared to be 50 characters but your index variable is unbounded. value is declared on the stack so once index reaches 50, you will start to erase other data on the stack.

As a side note, you can replace:

char value[50];
memset(value,0,sizeof(value));





...这个...





... with this ...

char value[50] = {0};





另外,我怀疑你的样本数据在每一行的末尾都缺少换行符(\ n)。



Also, I suspect your sample data is missing newlines ("\n") from the end of each line.


这篇关于如何优化解析函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆