| 199 | } |
| 200 | |
| 201 | std::string FormatParagraph(std::string_view in, size_t width, size_t indent) |
| 202 | { |
| 203 | assert(width >= indent); |
| 204 | std::stringstream out; |
| 205 | size_t ptr = 0; |
| 206 | size_t indented = 0; |
| 207 | while (ptr < in.size()) |
| 208 | { |
| 209 | size_t lineend = in.find_first_of('\n', ptr); |
| 210 | if (lineend == std::string::npos) { |
| 211 | lineend = in.size(); |
| 212 | } |
| 213 | const size_t linelen = lineend - ptr; |
| 214 | const size_t rem_width = width - indented; |
| 215 | if (linelen <= rem_width) { |
| 216 | out << in.substr(ptr, linelen + 1); |
| 217 | ptr = lineend + 1; |
| 218 | indented = 0; |
| 219 | } else { |
| 220 | size_t finalspace = in.find_last_of(" \n", ptr + rem_width); |
| 221 | if (finalspace == std::string::npos || finalspace < ptr) { |
| 222 | // No place to break; just include the entire word and move on |
| 223 | finalspace = in.find_first_of("\n ", ptr); |
| 224 | if (finalspace == std::string::npos) { |
| 225 | // End of the string, just add it and break |
| 226 | out << in.substr(ptr); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | out << in.substr(ptr, finalspace - ptr) << "\n"; |
| 231 | if (in[finalspace] == '\n') { |
| 232 | indented = 0; |
| 233 | } else if (indent) { |
| 234 | out << std::string(indent, ' '); |
| 235 | indented = indent; |
| 236 | } |
| 237 | ptr = finalspace + 1; |
| 238 | } |
| 239 | } |
| 240 | return out.str(); |
| 241 | } |
| 242 | |
| 243 | /** Upper bound for mantissa. |
| 244 | * 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer. |