MCPcopy Create free account
hub / github.com/ElementsProject/elements / formatImpl

Function formatImpl

src/tinyformat.h:872–935  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers 1

vformatFunction · 0.85

Calls 7

printFormatStringLiteralFunction · 0.85
streamStateFromFormatFunction · 0.85
fillMethod · 0.80
formatMethod · 0.80
strMethod · 0.80
flagsMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected