I was wondering could someone please help me implemenrt this method whereas I dont need to use std::string.
The method takes in tewo parameters, one for char array and the other one is the size of the char array.
The parameters passed will be an array of differernt values seperated by commas, e.g. “Rule1,Rule2,Rule3,Rule4,AT,T,Cat,Dog”.
The member variable “m_rulesSet” is a std::string constaining simular values. I want to compare the both to check if the “name” is within the std::string “m_ruleSet”
bool
Matche(const char *str, size_t strSize)
{
std::string target(str, strSize);
if(m_ruleSet.empty())
{
return true;
}
if((NULL == str) || (strSize <= 0))
{
return false;
}
const char * ptr =0;
const char * start = target.c_str();
while ((ptr = strchr(start, ',')) != 0)
{
std::string name(start, ptr - start);
if(name ==m_ruleSet)
{
return true;
}
start = ptr + 1;
}
if(*start)
{
std::string name(start);
if(name==m_ruleSet)
{
return true;
}
}
return false;
}
Please any help would be appreciated, thank you so much in advance
So you simply mean replacing
std::stringcomparison bystrcmporstrncmp(as this is the only thing you use thestd::stringfor, everything else is already using C-strings)? Well, if you really want it:Like Ben points out in his comment and answer, you need to use
memchrinstead ofstrchrwhen you got an additional size argument and your string is not neccessarily zero-terminated.You may also want to replace
m_ruleSetby a C-string (to get rid of the.c_str()), but I actually question the need for replacingstd::stringby C-strings in C++ code in the first place.