| 419 | |
| 420 | template <class TStringBuilder> |
| 421 | void FormatString(TStringBuilder* builder, TStringBuf value, TStringBuf spec) |
| 422 | { |
| 423 | if (!spec) { |
| 424 | builder->AppendString(value); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | // Parse alignment. |
| 429 | bool alignLeft = false; |
| 430 | const char* current = spec.begin(); |
| 431 | if (*current == '-') { |
| 432 | alignLeft = true; |
| 433 | ++current; |
| 434 | } |
| 435 | |
| 436 | bool hasAlign = false; |
| 437 | int alignSize = 0; |
| 438 | while (*current >= '0' && *current <= '9') { |
| 439 | hasAlign = true; |
| 440 | alignSize = 10 * alignSize + (*current - '0'); |
| 441 | if (alignSize > 1000000) { |
| 442 | builder->AppendString(TStringBuf("<alignment overflow>")); |
| 443 | return; |
| 444 | } |
| 445 | ++current; |
| 446 | } |
| 447 | |
| 448 | int padding = 0; |
| 449 | bool padLeft = false; |
| 450 | bool padRight = false; |
| 451 | if (hasAlign) { |
| 452 | padding = alignSize - value.size(); |
| 453 | if (padding < 0) { |
| 454 | padding = 0; |
| 455 | } |
| 456 | padLeft = !alignLeft; |
| 457 | padRight = alignLeft; |
| 458 | } |
| 459 | |
| 460 | bool singleQuotes = false; |
| 461 | bool doubleQuotes = false; |
| 462 | bool escape = false; |
| 463 | while (current < spec.end()) { |
| 464 | switch (*current++) { |
| 465 | case 'q': |
| 466 | singleQuotes = true; |
| 467 | break; |
| 468 | case 'Q': |
| 469 | doubleQuotes = true; |
| 470 | break; |
| 471 | case 'h': |
| 472 | escape = true; |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (padLeft) { |
| 478 | builder->AppendChar(' ', padding); |
no test coverage detected