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

Function formatImpl

source/extern/tinyformat.h:882–945  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers 1

vformatFunction · 0.85

Calls 8

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

Tested by

no test coverage detected