MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / formatImpl

Function formatImpl

src/tinyformat.h:888–951  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

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