indent_text.cpp 476 B

12345678910111213141516171819202122232425
  1. #include "indent_text.h"
  2. #include <util/stream/str.h>
  3. TString IndentText(TStringBuf text, TStringBuf indent) {
  4. if (text.empty())
  5. return TString();
  6. TStringStream ss;
  7. ss.Reserve(text.size() + 20);
  8. char pc = 0;
  9. for (size_t i = 0; i < text.size(); ++i) {
  10. if (i == 0 || pc == '\n')
  11. ss << indent;
  12. char c = text.at(i);
  13. ss << c;
  14. pc = c;
  15. }
  16. if (pc != '\n')
  17. ss << '\n';
  18. return ss.Str();
  19. }