Skip to content

Commit 765601b

Browse files
committed
Some variable renaming for clarity [NFC]
1 parent 656c570 commit 765601b

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

tinyformat.h

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,12 @@ inline int parseIntAndAdvance(const char*& c)
559559

560560
// Parse width or precision `n` from format string pointer `c`, and advance it
561561
// to the next character. If an indirection is requested with `*`, the argument
562-
// is read from `formatters[argIndex]` and `argIndex` is incremented (or read
563-
// from `formatters[n]` in positional mode). Returns true if one or more
562+
// is read from `args[argIndex]` and `argIndex` is incremented (or read
563+
// from `args[n]` in positional mode). Returns true if one or more
564564
// characters were read.
565565
inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
566-
const detail::FormatArg* formatters,
567-
int& argIndex, int numFormatters)
566+
const detail::FormatArg* args,
567+
int& argIndex, int numArgs)
568568
{
569569
if (*c >= '0' && *c <= '9') {
570570
n = parseIntAndAdvance(c);
@@ -576,15 +576,15 @@ inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
576576
int pos = parseIntAndAdvance(c) - 1;
577577
if (*c != '$')
578578
TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
579-
if (pos >= 0 && pos < numFormatters)
580-
n = formatters[pos].toInt();
579+
if (pos >= 0 && pos < numArgs)
580+
n = args[pos].toInt();
581581
else
582582
TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
583583
++c;
584584
}
585585
else {
586-
if (argIndex < numFormatters)
587-
n = formatters[argIndex++].toInt();
586+
if (argIndex < numArgs)
587+
n = args[argIndex++].toInt();
588588
else
589589
TINYFORMAT_ERROR("tinyformat: Not enough arguments to read variable width or precision");
590590
}
@@ -595,29 +595,25 @@ inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
595595
return true;
596596
}
597597

598-
// Print literal part of format string and return next format spec
599-
// position.
598+
// Print literal part of format string and return next format spec position.
600599
//
601-
// Skips over any occurrences of '%%', printing a literal '%' to the
602-
// output. The position of the first % character of the next
603-
// nontrivial format spec is returned, or the end of string.
600+
// Skips over any occurrences of '%%', printing a literal '%' to the output.
601+
// The position of the first % character of the next nontrivial format spec is
602+
// returned, or the end of string.
604603
inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt)
605604
{
606605
const char* c = fmt;
607606
for (;; ++c) {
608-
switch (*c) {
609-
case '\0':
610-
out.write(fmt, c - fmt);
607+
if (*c == '\0') {
608+
out.write(fmt, c - fmt);
609+
return c;
610+
}
611+
else if (*c == '%') {
612+
out.write(fmt, c - fmt);
613+
if (*(c+1) != '%')
611614
return c;
612-
case '%':
613-
out.write(fmt, c - fmt);
614-
if (*(c+1) != '%')
615-
return c;
616-
// for "%%", tack trailing % onto next literal section.
617-
fmt = ++c;
618-
break;
619-
default:
620-
break;
615+
// for "%%", tack trailing % onto next literal section.
616+
fmt = ++c;
621617
}
622618
}
623619
}
@@ -659,8 +655,8 @@ inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt)
659655
inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode,
660656
bool& spacePadPositive,
661657
int& ntrunc, const char* fmtStart,
662-
const detail::FormatArg* formatters,
663-
int& argIndex, int numFormatters)
658+
const detail::FormatArg* args,
659+
int& argIndex, int numArgs)
664660
{
665661
if (*fmtStart != '%') {
666662
TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string");
@@ -686,7 +682,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
686682
int value = parseIntAndAdvance(c);
687683
if (*c == '$') {
688684
// value is an argument index
689-
if (value > 0 && value <= numFormatters)
685+
if (value > 0 && value <= numArgs)
690686
argIndex = value - 1;
691687
else
692688
TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
@@ -752,7 +748,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
752748
// Parse width
753749
int width = 0;
754750
widthSet = parseWidthOrPrecision(width, c, positionalMode,
755-
formatters, argIndex, numFormatters);
751+
args, argIndex, numArgs);
756752
if (widthSet) {
757753
if (width < 0) {
758754
// negative widths correspond to '-' flag set
@@ -768,7 +764,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
768764
++c;
769765
int precision = 0;
770766
parseWidthOrPrecision(precision, c, positionalMode,
771-
formatters, argIndex, numFormatters);
767+
args, argIndex, numArgs);
772768
// Presence of `.` indicates precision set, unless the inferred value
773769
// was negative in which case the default is used.
774770
precisionSet = precision >= 0;
@@ -868,8 +864,8 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
868864

869865
//------------------------------------------------------------------------------
870866
inline void formatImpl(std::ostream& out, const char* fmt,
871-
const detail::FormatArg* formatters,
872-
int numFormatters)
867+
const detail::FormatArg* args,
868+
int numArgs)
873869
{
874870
// Saved stream state
875871
std::streamsize origWidth = out.width();
@@ -878,21 +874,21 @@ inline void formatImpl(std::ostream& out, const char* fmt,
878874
char origFill = out.fill();
879875

880876
bool positionalMode = false;
881-
for (int argIndex = 0; positionalMode || argIndex < numFormatters; ++argIndex) {
877+
for (int argIndex = 0; positionalMode || argIndex < numArgs; ++argIndex) {
882878
// Parse the format string
883879
fmt = printFormatStringLiteral(out, fmt);
884880
if (positionalMode && *fmt == '\0')
885881
break;
886882
bool spacePadPositive = false;
887883
int ntrunc = -1;
888884
const char* fmtEnd = streamStateFromFormat(out, positionalMode, spacePadPositive, ntrunc, fmt,
889-
formatters, argIndex, numFormatters);
890-
if (argIndex >= numFormatters) {
885+
args, argIndex, numArgs);
886+
if (argIndex >= numArgs) {
891887
// Check args remain after reading any variable width/precision
892888
TINYFORMAT_ERROR("tinyformat: Not enough format arguments");
893889
return;
894890
}
895-
const FormatArg& arg = formatters[argIndex];
891+
const FormatArg& arg = args[argIndex];
896892
// Format the arg into the stream.
897893
if (!spacePadPositive) {
898894
arg.format(out, fmt, fmtEnd, ntrunc);
@@ -940,14 +936,14 @@ inline void formatImpl(std::ostream& out, const char* fmt,
940936
class FormatList
941937
{
942938
public:
943-
FormatList(detail::FormatArg* formatters, int N)
944-
: m_formatters(formatters), m_N(N) { }
939+
FormatList(detail::FormatArg* args, int N)
940+
: m_args(args), m_N(N) { }
945941

946942
friend void vformat(std::ostream& out, const char* fmt,
947943
const FormatList& list);
948944

949945
private:
950-
const detail::FormatArg* m_formatters;
946+
const detail::FormatArg* m_args;
951947
int m_N;
952948
};
953949

@@ -1041,7 +1037,7 @@ TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_MAKEFORMATLIST)
10411037
/// list of format arguments is held in a single function argument.
10421038
inline void vformat(std::ostream& out, const char* fmt, FormatListRef list)
10431039
{
1044-
detail::formatImpl(out, fmt, list.m_formatters, list.m_N);
1040+
detail::formatImpl(out, fmt, list.m_args, list.m_N);
10451041
}
10461042

10471043

0 commit comments

Comments
 (0)