------------------------------------------------------------------------------
| 783 | |
| 784 | //------------------------------------------------------------------------------ |
| 785 | inline void formatImpl(std::ostream& out, const char* fmt, |
| 786 | const detail::FormatArg* formatters, |
| 787 | int numFormatters) |
| 788 | { |
| 789 | // Saved stream state |
| 790 | std::streamsize origWidth = out.width(); |
| 791 | std::streamsize origPrecision = out.precision(); |
| 792 | std::ios::fmtflags origFlags = out.flags(); |
| 793 | char origFill = out.fill(); |
| 794 | |
| 795 | for (int argIndex = 0; argIndex < numFormatters; ++argIndex) |
| 796 | { |
| 797 | // Parse the format string |
| 798 | fmt = printFormatStringLiteral(out, fmt); |
| 799 | bool spacePadPositive = false; |
| 800 | int ntrunc = -1; |
| 801 | const char* fmtEnd = streamStateFromFormat(out, spacePadPositive, ntrunc, fmt, |
| 802 | formatters, argIndex, numFormatters); |
| 803 | if (argIndex >= numFormatters) |
| 804 | { |
| 805 | // Check args remain after reading any variable width/precision |
| 806 | TINYFORMAT_ERROR("tinyformat: Not enough format arguments"); |
| 807 | return; |
| 808 | } |
| 809 | const FormatArg& arg = formatters[argIndex]; |
| 810 | // Format the arg into the stream. |
| 811 | if(!spacePadPositive) |
| 812 | arg.format(out, fmt, fmtEnd, ntrunc); |
| 813 | else |
| 814 | { |
| 815 | // The following is a special case with no direct correspondence |
| 816 | // between stream formatting and the printf() behaviour. Simulate |
| 817 | // it crudely by formatting into a temporary string stream and |
| 818 | // munging the resulting string. |
| 819 | std::ostringstream tmpStream; |
| 820 | tmpStream.copyfmt(out); |
| 821 | tmpStream.setf(std::ios::showpos); |
| 822 | arg.format(tmpStream, fmt, fmtEnd, ntrunc); |
| 823 | std::string result = tmpStream.str(); // allocates... yuck. |
| 824 | for(size_t i = 0, iend = result.size(); i < iend; ++i) |
| 825 | if(result[i] == '+') result[i] = ' '; |
| 826 | out << result; |
| 827 | } |
| 828 | fmt = fmtEnd; |
| 829 | } |
| 830 | |
| 831 | // Print remaining part of format string. |
| 832 | fmt = printFormatStringLiteral(out, fmt); |
| 833 | if(*fmt != '\0') |
| 834 | TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string"); |
| 835 | |
| 836 | // Restore stream state |
| 837 | out.width(origWidth); |
| 838 | out.precision(origPrecision); |
| 839 | out.flags(origFlags); |
| 840 | out.fill(origFill); |
| 841 | } |
| 842 |