aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/string_utils/indent_text/indent_text.cpp
blob: 6c3c9cc9f442240c8cb17d7696e871bee200d1ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "indent_text.h"

#include <util/stream/str.h>

TString IndentText(TStringBuf text, TStringBuf indent) {
    if (text.empty())
        return TString();

    TStringStream ss;
    ss.Reserve(text.size() + 20); 

    char pc = 0;
    for (size_t i = 0; i < text.size(); ++i) {
        if (i == 0 || pc == '\n')
            ss << indent;

        char c = text.at(i);
        ss << c;
        pc = c;
    }
    if (pc != '\n')
        ss << '\n';

    return ss.Str();
}