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

Function streamStateFromFormat

source/extern/tinyformat.h:674–878  ·  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

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

Callers 1

formatImplFunction · 0.85

Calls 3

flagsMethod · 0.80
widthMethod · 0.45
fillMethod · 0.45

Tested by

no test coverage detected