------------------------------------------------------------------------------
| 753 | |
| 754 | //------------------------------------------------------------------------------ |
| 755 | inline void formatImpl(std::ostream &out, |
| 756 | const char *fmt, |
| 757 | const detail::FormatArg *formatters, |
| 758 | int numFormatters) { |
| 759 | // Saved stream state |
| 760 | std::streamsize origWidth = out.width(); |
| 761 | std::streamsize origPrecision = out.precision(); |
| 762 | std::ios::fmtflags origFlags = out.flags(); |
| 763 | char origFill = out.fill(); |
| 764 | |
| 765 | for (int argIndex = 0; argIndex < numFormatters; ++argIndex) { |
| 766 | // Parse the format string |
| 767 | fmt = printFormatStringLiteral(out, fmt); |
| 768 | bool spacePadPositive = false; |
| 769 | int ntrunc = -1; |
| 770 | const char *fmtEnd = streamStateFromFormat(out, |
| 771 | spacePadPositive, |
| 772 | ntrunc, |
| 773 | fmt, |
| 774 | formatters, |
| 775 | argIndex, |
| 776 | numFormatters); |
| 777 | if (argIndex >= numFormatters) { |
| 778 | // Check args remain after reading any variable width/precision |
| 779 | throw FormatError(); |
| 780 | return; |
| 781 | } |
| 782 | const FormatArg &arg = formatters[argIndex]; |
| 783 | // Format the arg into the stream. |
| 784 | if (!spacePadPositive) { |
| 785 | arg.format(out, fmt, fmtEnd, ntrunc); |
| 786 | } else { |
| 787 | // The following is a special case with no direct correspondence |
| 788 | // between stream formatting and the printf() behaviour. Simulate |
| 789 | // it crudely by formatting into a temporary string stream and |
| 790 | // munging the resulting string. |
| 791 | std::ostringstream tmpStream; |
| 792 | tmpStream.copyfmt(out); |
| 793 | tmpStream.setf(std::ios::showpos); |
| 794 | arg.format(tmpStream, fmt, fmtEnd, ntrunc); |
| 795 | std::string result = tmpStream.str(); // allocates... yuck. |
| 796 | for (size_t i = 0, iend = result.size(); i < iend; ++i) |
| 797 | if (result[i] == '+') result[i] = ' '; |
| 798 | out << result; |
| 799 | } |
| 800 | fmt = fmtEnd; |
| 801 | } |
| 802 | |
| 803 | // Print remaining part of format string. |
| 804 | fmt = printFormatStringLiteral(out, fmt); |
| 805 | if (fmt != nullptr && *fmt != '\0' && *fmt != 0) throw FormatError(); |
| 806 | |
| 807 | // Restore stream state |
| 808 | out.width(origWidth); |
| 809 | out.precision(origPrecision); |
| 810 | out.flags(origFlags); |
| 811 | out.fill(origFill); |
| 812 | } |