В данный момент вы читаете до '\n'
. Но вы должны читать до ';'
:
#include <iostream>
#include <sstream>
int main() {
std::stringstream file("result = f(a, b,\nc,\nd\n);\nresult = f2(a, b,\nc,\nd\n);");
std::string function;
while (std::getline(file, function, ';')) {
std::cout << "function: " << (function[0] == '\n' ? "" : "\n") << function << std::endl;
}
return 0;
}
Дополнительно вы можете уменьшить каждую функцию до одной строки:
#include <iostream>
#include <sstream>
std::string trim(const std::string& s) {
std::size_t b(0);
while (s[b] == '\n') {
++b;
}
std::size_t e(s.length() - 1);
while (s[e] == '\n') {
--e;
}
return s.substr(b, e - b + 1);
}
std::string& join(std::string& s) {
std::size_t pos(s.find('\n'));
while (pos != s.npos) {
s.replace(pos, 1, "");
pos = s.find('\n', pos); // continue looking from current location
}
return s;
}
int main() {
std::stringstream file("result = f(a, b,\nc,\nd\n);\nresult = f2(a, b,\nc,\nd\n);");
std::string function;
while (std::getline(file, function, ';')) {
function += ';';
std::cout << "function: " << trim(join(function)) << std::endl;
}
return 0;
}
trim
удаляет разрыв строки до и после каждой функции. join
удаляет каждый перевод строки внутри функции.
Введите:
result = f(a, b,
c,
d
);
result = f2(a, b,
c,
d
);
Выход:
function: result = f(a, b,c,d);
function: result = f2(a, b,c,d);