MCPcopy Create free account
hub / github.com/bupticybee/TexasSolver / formatImpl

Function formatImpl

include/tools/tinyformat.h:876–939  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers 1

vformatFunction · 0.85

Calls 4

printFormatStringLiteralFunction · 0.85
streamStateFromFormatFunction · 0.85
flagsMethod · 0.80
sizeMethod · 0.80

Tested by

no test coverage detected