Refactoring: Combine replaceStr functions

This commit is contained in:
Daniel Marjamäki 2018-12-28 13:11:54 +01:00
parent 036fcf7827
commit ee3fd0af03
3 changed files with 28 additions and 35 deletions

View File

@ -360,30 +360,6 @@ static bool findPath(const CTU::FileInfo::FunctionCall &from,
return false; return false;
} }
static std::string replacestr(std::string s, const std::string &from, const std::string &to)
{
std::string::size_type pos1 = 0;
while (pos1 < s.size()) {
pos1 = s.find(from, pos1);
if (pos1 == std::string::npos)
return s;
if (pos1 > 0 && (s[pos1-1] == '_' || std::isalnum(s[pos1-1]))) {
pos1++;
continue;
}
const std::string::size_type pos2 = pos1 + from.size();
if (pos2 >= s.size())
return s.substr(0,pos1) + to;
if (s[pos2] == '_' || std::isalnum(s[pos2])) {
pos1++;
continue;
}
s = s.substr(0,pos1) + to + s.substr(pos2);
pos1 += to.size();
}
return s;
}
std::list<ErrorLogger::ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueType invalidValue, std::list<ErrorLogger::ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueType invalidValue,
const CTU::FileInfo::UnsafeUsage &unsafeUsage, const CTU::FileInfo::UnsafeUsage &unsafeUsage,
const std::map<std::string, std::list<CTU::FileInfo::NestedCall>> &nestedCallsMap, const std::map<std::string, std::list<CTU::FileInfo::NestedCall>> &nestedCallsMap,
@ -423,7 +399,7 @@ std::list<ErrorLogger::ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(I
ErrorLogger::ErrorMessage::FileLocation fileLoc2; ErrorLogger::ErrorMessage::FileLocation fileLoc2;
fileLoc2.setfile(unsafeUsage.location.fileName); fileLoc2.setfile(unsafeUsage.location.fileName);
fileLoc2.line = unsafeUsage.location.linenr; fileLoc2.line = unsafeUsage.location.linenr;
fileLoc2.setinfo(replacestr(info, "ARG", unsafeUsage.argumentName)); fileLoc2.setinfo(replaceStr(info, "ARG", unsafeUsage.argumentName));
locationList.push_back(fileLoc1); locationList.push_back(fileLoc1);
locationList.push_back(fileLoc2); locationList.push_back(fileLoc2);

View File

@ -178,16 +178,6 @@ ErrorLogger::ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errms
} }
} }
static std::string replaceStr(std::string s, const std::string &from, const std::string &to)
{
std::string::size_type pos = 0;
while (std::string::npos != (pos = s.find(from,pos))) {
s = s.substr(0, pos) + to + s.substr(pos + from.size());
pos += to.size();
}
return s;
}
void ErrorLogger::ErrorMessage::setmsg(const std::string &msg) void ErrorLogger::ErrorMessage::setmsg(const std::string &msg)
{ {
// If a message ends to a '\n' and contains only a one '\n' // If a message ends to a '\n' and contains only a one '\n'
@ -760,3 +750,27 @@ std::string ErrorLogger::plistData(const ErrorLogger::ErrorMessage &msg)
return plist.str(); return plist.str();
} }
std::string replaceStr(std::string s, const std::string &from, const std::string &to)
{
std::string::size_type pos1 = 0;
while (pos1 < s.size()) {
pos1 = s.find(from, pos1);
if (pos1 == std::string::npos)
return s;
if (pos1 > 0 && (s[pos1-1] == '_' || std::isalnum(s[pos1-1]))) {
pos1++;
continue;
}
const std::string::size_type pos2 = pos1 + from.size();
if (pos2 >= s.size())
return s.substr(0,pos1) + to;
if (s[pos2] == '_' || std::isalnum(s[pos2])) {
pos1++;
continue;
}
s = s.substr(0,pos1) + to + s.substr(pos2);
pos1 += to.size();
}
return s;
}

View File

@ -391,6 +391,9 @@ public:
} }
}; };
/** Replace substring. Example replaceStr("1,NR,3", "NR", "2") => "1,2,3" */
std::string replaceStr(std::string s, const std::string &from, const std::string &to);
/// @} /// @}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif // errorloggerH #endif // errorloggerH