MCPcopy Create free account
hub / github.com/c42f/tinyformat / formatImpl

Function formatImpl

tinyformat.h:863–926  ·  view source on GitHub ↗

------------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

861
862//------------------------------------------------------------------------------
863inline void formatImpl(std::ostream& out, const char* fmt,
864 const detail::FormatArg* args,
865 int numArgs)
866{
867 // Saved stream state
868 std::streamsize origWidth = out.width();
869 std::streamsize origPrecision = out.precision();
870 std::ios::fmtflags origFlags = out.flags();
871 char origFill = out.fill();
872
873 // "Positional mode" means all format specs should be of the form "%n$..."
874 // with `n` an integer. We detect this in `streamStateFromFormat`.
875 bool positionalMode = false;
876 int argIndex = 0;
877 while (true) {
878 fmt = printFormatStringLiteral(out, fmt);
879 if (*fmt == '\0') {
880 if (!positionalMode && argIndex < numArgs) {
881 TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string");
882 }
883 break;
884 }
885 bool spacePadPositive = false;
886 int ntrunc = -1;
887 const char* fmtEnd = streamStateFromFormat(out, positionalMode, spacePadPositive, ntrunc, fmt,
888 args, argIndex, numArgs);
889 // NB: argIndex may be incremented by reading variable width/precision
890 // in `streamStateFromFormat`, so do the bounds check here.
891 if (argIndex >= numArgs) {
892 TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string");
893 return;
894 }
895 const FormatArg& arg = args[argIndex];
896 // Format the arg into the stream.
897 if (!spacePadPositive) {
898 arg.format(out, fmt, fmtEnd, ntrunc);
899 }
900 else {
901 // The following is a special case with no direct correspondence
902 // between stream formatting and the printf() behaviour. Simulate
903 // it crudely by formatting into a temporary string stream and
904 // munging the resulting string.
905 std::ostringstream tmpStream;
906 tmpStream.copyfmt(out);
907 tmpStream.setf(std::ios::showpos);
908 arg.format(tmpStream, fmt, fmtEnd, ntrunc);
909 std::string result = tmpStream.str(); // allocates... yuck.
910 for (size_t i = 0, iend = result.size(); i < iend; ++i) {
911 if (result[i] == '+')
912 result[i] = ' ';
913 }
914 out << result;
915 }
916 if (!positionalMode)
917 ++argIndex;
918 fmt = fmtEnd;
919 }
920

Callers 1

vformatFunction · 0.85

Calls 2

printFormatStringLiteralFunction · 0.85
streamStateFromFormatFunction · 0.85

Tested by

no test coverage detected