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

Function streamStateFromFormat

src/tinyformat.h:680–884  ·  view source on GitHub ↗

Parse a format string and set the stream state accordingly. The format mini-language recognized here is meant to be the one from C99, with the form "%[flags][width][.precision][length]type" with POSIX positional arguments extension. POSIX positional arguments extension: Conversions can be applied to the nth argument after the format in the argument list, rather than to the next unused argument.

Source from the content-addressed store, hash-verified

678// necessary to pull out variable width and precision. The function returns a
679// pointer to the character after the end of the current format spec.
680inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode,
681 bool& spacePadPositive,
682 int& ntrunc, const char* fmtStart,
683 const detail::FormatArg* args,
684 int& argIndex, int numArgs)
685{
686 TINYFORMAT_ASSERT(*fmtStart == '%');
687 // Reset stream state to defaults.
688 out.width(0);
689 out.precision(6);
690 out.fill(' ');
691 // Reset most flags; ignore irrelevant unitbuf & skipws.
692 out.unsetf(std::ios::adjustfield | std::ios::basefield |
693 std::ios::floatfield | std::ios::showbase | std::ios::boolalpha |
694 std::ios::showpoint | std::ios::showpos | std::ios::uppercase);
695 bool precisionSet = false;
696 bool widthSet = false;
697 int widthExtra = 0;
698 const char* c = fmtStart + 1;
699
700 // 1) Parse an argument index (if followed by '$') or a width possibly
701 // preceded with '0' flag.
702 if (*c >= '0' && *c <= '9') {
703 const char tmpc = *c;
704 int value = parseIntAndAdvance(c);
705 if (*c == '$') {
706 // value is an argument index
707 if (value > 0 && value <= numArgs)
708 argIndex = value - 1;
709 else
710 TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
711 ++c;
712 positionalMode = true;
713 }
714 else if (positionalMode) {
715 TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
716 }
717 else {
718 if (tmpc == '0') {
719 // Use internal padding so that numeric values are
720 // formatted correctly, eg -00010 rather than 000-10
721 out.fill('0');
722 out.setf(std::ios::internal, std::ios::adjustfield);
723 }
724 if (value != 0) {
725 // Nonzero value means that we parsed width.
726 widthSet = true;
727 out.width(value);
728 }
729 }
730 }
731 else if (positionalMode) {
732 TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
733 }
734 // 2) Parse flags and width if we did not do it in previous step.
735 if (!widthSet) {
736 // Parse flags
737 for (;; ++c) {

Callers 1

formatImplFunction · 0.85

Calls 2

fillMethod · 0.80
flagsMethod · 0.45

Tested by

no test coverage detected