@@ -559,12 +559,12 @@ inline int parseIntAndAdvance(const char*& c)
559
559
560
560
// Parse width or precision `n` from format string pointer `c`, and advance it
561
561
// 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
564
564
// characters were read.
565
565
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 )
568
568
{
569
569
if (*c >= ' 0' && *c <= ' 9' ) {
570
570
n = parseIntAndAdvance (c);
@@ -576,15 +576,15 @@ inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
576
576
int pos = parseIntAndAdvance (c) - 1 ;
577
577
if (*c != ' $' )
578
578
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 ();
581
581
else
582
582
TINYFORMAT_ERROR (" tinyformat: Positional argument out of range" );
583
583
++c;
584
584
}
585
585
else {
586
- if (argIndex < numFormatters )
587
- n = formatters [argIndex++].toInt ();
586
+ if (argIndex < numArgs )
587
+ n = args [argIndex++].toInt ();
588
588
else
589
589
TINYFORMAT_ERROR (" tinyformat: Not enough arguments to read variable width or precision" );
590
590
}
@@ -595,29 +595,25 @@ inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
595
595
return true ;
596
596
}
597
597
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.
600
599
//
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.
604
603
inline const char * printFormatStringLiteral (std::ostream& out, const char * fmt)
605
604
{
606
605
const char * c = fmt;
607
606
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 ) != ' %' )
611
614
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;
621
617
}
622
618
}
623
619
}
@@ -659,8 +655,8 @@ inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt)
659
655
inline const char * streamStateFromFormat (std::ostream& out, bool & positionalMode,
660
656
bool & spacePadPositive,
661
657
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 )
664
660
{
665
661
if (*fmtStart != ' %' ) {
666
662
TINYFORMAT_ERROR (" tinyformat: Not enough conversion specifiers in format string" );
@@ -686,7 +682,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
686
682
int value = parseIntAndAdvance (c);
687
683
if (*c == ' $' ) {
688
684
// value is an argument index
689
- if (value > 0 && value <= numFormatters )
685
+ if (value > 0 && value <= numArgs )
690
686
argIndex = value - 1 ;
691
687
else
692
688
TINYFORMAT_ERROR (" tinyformat: Positional argument out of range" );
@@ -752,7 +748,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
752
748
// Parse width
753
749
int width = 0 ;
754
750
widthSet = parseWidthOrPrecision (width, c, positionalMode,
755
- formatters , argIndex, numFormatters );
751
+ args , argIndex, numArgs );
756
752
if (widthSet) {
757
753
if (width < 0 ) {
758
754
// negative widths correspond to '-' flag set
@@ -768,7 +764,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
768
764
++c;
769
765
int precision = 0 ;
770
766
parseWidthOrPrecision (precision, c, positionalMode,
771
- formatters , argIndex, numFormatters );
767
+ args , argIndex, numArgs );
772
768
// Presence of `.` indicates precision set, unless the inferred value
773
769
// was negative in which case the default is used.
774
770
precisionSet = precision >= 0 ;
@@ -868,8 +864,8 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
868
864
869
865
// ------------------------------------------------------------------------------
870
866
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 )
873
869
{
874
870
// Saved stream state
875
871
std::streamsize origWidth = out.width ();
@@ -878,21 +874,21 @@ inline void formatImpl(std::ostream& out, const char* fmt,
878
874
char origFill = out.fill ();
879
875
880
876
bool positionalMode = false ;
881
- for (int argIndex = 0 ; positionalMode || argIndex < numFormatters ; ++argIndex) {
877
+ for (int argIndex = 0 ; positionalMode || argIndex < numArgs ; ++argIndex) {
882
878
// Parse the format string
883
879
fmt = printFormatStringLiteral (out, fmt);
884
880
if (positionalMode && *fmt == ' \0 ' )
885
881
break ;
886
882
bool spacePadPositive = false ;
887
883
int ntrunc = -1 ;
888
884
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 ) {
891
887
// Check args remain after reading any variable width/precision
892
888
TINYFORMAT_ERROR (" tinyformat: Not enough format arguments" );
893
889
return ;
894
890
}
895
- const FormatArg& arg = formatters [argIndex];
891
+ const FormatArg& arg = args [argIndex];
896
892
// Format the arg into the stream.
897
893
if (!spacePadPositive) {
898
894
arg.format (out, fmt, fmtEnd, ntrunc);
@@ -940,14 +936,14 @@ inline void formatImpl(std::ostream& out, const char* fmt,
940
936
class FormatList
941
937
{
942
938
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) { }
945
941
946
942
friend void vformat (std::ostream& out, const char * fmt,
947
943
const FormatList& list);
948
944
949
945
private:
950
- const detail::FormatArg* m_formatters ;
946
+ const detail::FormatArg* m_args ;
951
947
int m_N;
952
948
};
953
949
@@ -1041,7 +1037,7 @@ TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_MAKEFORMATLIST)
1041
1037
// / list of format arguments is held in a single function argument.
1042
1038
inline void vformat (std::ostream& out, const char * fmt, FormatListRef list)
1043
1039
{
1044
- detail::formatImpl (out, fmt, list.m_formatters , list.m_N );
1040
+ detail::formatImpl (out, fmt, list.m_args , list.m_N );
1045
1041
}
1046
1042
1047
1043
0 commit comments