reduce: simplify multiline function declarations better

This commit is contained in:
Daniel Marjamäki 2014-07-30 17:43:24 +02:00
parent e8451b5d91
commit 1144384242
1 changed files with 39 additions and 0 deletions

View File

@ -114,6 +114,11 @@ static void printstr(const std::vector<std::string> &filedata, int i1, int i2)
} }
#endif #endif
static char getEndChar(const std::string &line) {
std::size_t pos = line.find_last_not_of(" \t");
return (pos == std::string::npos) ? '\0' : line[pos];
}
static std::vector<std::string> readfile(const std::string &filename) static std::vector<std::string> readfile(const std::string &filename)
{ {
std::vector<std::string> filedata; std::vector<std::string> filedata;
@ -174,6 +179,40 @@ static std::vector<std::string> readfile(const std::string &filename)
filedata.push_back(line); filedata.push_back(line);
} }
// put declarations in a single line..
for (unsigned int linenr = 0U; linenr+1U < filedata.size(); ++linenr) {
// Does this look like start of a function declaration?
if (filedata[linenr].empty() ||
!std::isalpha(filedata[linenr][0U]) ||
getEndChar(filedata[linenr]) != ',' ||
filedata[linenr].find("(") == std::string::npos ||
filedata[linenr].find(")") != std::string::npos)
continue;
// Where does function declaration end?
unsigned int linenr2 = linenr + 1U;
while (linenr2 < filedata.size() &&
getEndChar(filedata[linenr2]) == ',' &&
filedata[linenr2].find("(") == std::string::npos &&
filedata[linenr2].find(")") == std::string::npos)
++linenr2;
// If function declaration looks correct.. simplify it
if (linenr2 < filedata.size() &&
getEndChar(filedata[linenr2]) == ';' &&
filedata[linenr2].find("(") == std::string::npos &&
filedata[linenr2].size() > 2U &&
filedata[linenr2].find(")") == filedata[linenr2].size() - 2U) {
std::string code;
for (unsigned int i = linenr; i <= linenr2; i++) {
code = code + filedata[i];
filedata[i].clear();
}
filedata[linenr] = code;
}
}
return filedata; return filedata;
} }