Romasmi / cpp-practice

0 stars 0 forks source link

Замечания по программе Replace #4

Closed alexey-malov closed 6 years ago

alexey-malov commented 6 years ago
string StringReplace(string line, const string search, const string replace)
{
    string newLine = "";
    size_t currentPosition = 0;
    size_t searchLength = search.length();
    size_t lineLength = line.length();
alexey-malov commented 6 years ago
    while (currentPosition + searchLength < lineLength)
    {
        const string subString = line.substr(currentPosition, searchLength); // <-- тут создаётся новая строка
        if (subString == search)
        {
            newLine += replace;
            currentPosition += searchLength;
        }
        else
        {
            newLine += line[currentPosition];
            ++currentPosition;
        }
    }
alexey-malov commented 6 years ago
void CopyFileByReplacing(fstream& input, fstream& output, const string search,
    const string replace)
{
alexey-malov commented 6 years ago
    fstream input(argv[1], istream::in);
    fstream output(argv[2], ostream::out);
alexey-malov commented 6 years ago
alexey-malov commented 6 years ago
    if (search != "")
alexey-malov commented 6 years ago
    if (lineLength - currentPosition > 0)
    {
        newLine += line.substr(currentPosition, lineLength - currentPosition);
    }
alexey-malov commented 6 years ago
1>c:\teaching\ips\2018\oop\smirnov\cpp_oop_practice\lab_1\task_1\task_1.cpp(66): warning C4715: 'CopyFileByReplacing': not all control paths return a value
Romasmi commented 6 years ago

Fixed by commit https://github.com/Romasmi/cpp_oop_practice/blob/27cc9777c18b8b42ef8cb4f3cbc5ad77404f7eb8/lab_1/task_1/task_1.cpp