| 10 | |
| 11 | namespace NLastGetopt { |
| 12 | TString Wrap(ui32 width, TStringBuf text, TStringBuf indent, size_t* lastLineLen, bool* hasParagraphs) { |
| 13 | if (width == 0) { |
| 14 | return TString(text); |
| 15 | } |
| 16 | |
| 17 | if (width >= indent.size()) { |
| 18 | width -= indent.size(); |
| 19 | } |
| 20 | |
| 21 | if (hasParagraphs) { |
| 22 | *hasParagraphs = false; |
| 23 | } |
| 24 | |
| 25 | TString res; |
| 26 | auto os = TStringOutput(res); |
| 27 | |
| 28 | const char* spaceBegin = text.begin(); |
| 29 | const char* wordBegin = text.begin(); |
| 30 | const char* wordEnd = text.begin(); |
| 31 | const char* end = text.end(); |
| 32 | |
| 33 | size_t lenSoFar = 0; |
| 34 | |
| 35 | bool isPreParagraph = false; |
| 36 | |
| 37 | do { |
| 38 | spaceBegin = wordBegin = wordEnd; |
| 39 | |
| 40 | while (wordBegin < end && *wordBegin == ' ') { |
| 41 | wordBegin++; |
| 42 | } |
| 43 | |
| 44 | if (wordBegin == end) { |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | wordEnd = wordBegin; |
| 49 | |
| 50 | while (wordEnd < end && *wordEnd != ' ' && *wordEnd != '\n') { |
| 51 | wordEnd++; |
| 52 | } |
| 53 | |
| 54 | auto spaces = TStringBuf(spaceBegin, wordBegin); |
| 55 | auto word = TStringBuf(wordBegin, wordEnd); |
| 56 | |
| 57 | size_t spaceLen = spaces.size(); |
| 58 | |
| 59 | size_t wordLen = 0; |
| 60 | if (!GetNumberOfUTF8Chars(word.data(), word.size(), wordLen)) { |
| 61 | wordLen = word.size(); // not a utf8 string -- just use its binary size |
| 62 | } |
| 63 | wordLen -= NColorizer::TotalAnsiEscapeCodeLen(word); |
| 64 | |
| 65 | // Empty word means we've found a bunch of whitespaces followed by newline. |
| 66 | // We don't want to print trailing whitespaces. |
| 67 | if (word) { |
| 68 | // We can't fit this word into the line -- insert additional line break. |
| 69 | // We shouldn't insert line breaks if we're at the beginning of a line, hence `lenSoFar` check. |
no test coverage detected