@@ -246,8 +246,7 @@ struct formatZeroIntegerWorkaround<T,true>
246
246
{
247
247
static bool invoke (std::ostream& out, const T& value)
248
248
{
249
- if (static_cast <int >(value) == 0 && out.flags () & std::ios::showpos)
250
- {
249
+ if (static_cast <int >(value) == 0 && out.flags () & std::ios::showpos) {
251
250
out << " +0" ;
252
251
return true ;
253
252
}
@@ -288,7 +287,7 @@ inline void formatTruncated(std::ostream& out, const T& value, int ntrunc)
288
287
inline void formatTruncated (std::ostream& out, type* value, int ntrunc) \
289
288
{ \
290
289
std::streamsize len = 0 ; \
291
- while (len < ntrunc && value[len] != 0 ) \
290
+ while (len < ntrunc && value[len] != 0 ) \
292
291
++len; \
293
292
out.write (value, len); \
294
293
}
@@ -334,15 +333,14 @@ inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,
334
333
// could otherwise lead to a crash when printing a dangling (const char*).
335
334
const bool canConvertToChar = detail::is_convertible<T,char >::value;
336
335
const bool canConvertToVoidPtr = detail::is_convertible<T, const void *>::value;
337
- if (canConvertToChar && *(fmtEnd-1 ) == ' c' )
336
+ if (canConvertToChar && *(fmtEnd-1 ) == ' c' )
338
337
detail::formatValueAsType<T, char >::invoke (out, value);
339
- else if (canConvertToVoidPtr && *(fmtEnd-1 ) == ' p' )
338
+ else if (canConvertToVoidPtr && *(fmtEnd-1 ) == ' p' )
340
339
detail::formatValueAsType<T, const void *>::invoke (out, value);
341
340
#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
342
- else if (detail::formatZeroIntegerWorkaround<T>::invoke (out, value)) /* */ ;
341
+ else if (detail::formatZeroIntegerWorkaround<T>::invoke (out, value)) /* */ ;
343
342
#endif
344
- else if (ntrunc >= 0 )
345
- {
343
+ else if (ntrunc >= 0 ) {
346
344
// Take care not to overread C strings in truncating conversions like
347
345
// "%.4s" where at most 4 characters may be read.
348
346
detail::formatTruncated (out, value, ntrunc);
@@ -357,8 +355,7 @@ inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,
357
355
inline void formatValue (std::ostream& out, const char * /* fmtBegin*/ , \
358
356
const char * fmtEnd, int /* */ , charType value) \
359
357
{ \
360
- switch (*(fmtEnd-1 )) \
361
- { \
358
+ switch (*(fmtEnd-1 )) { \
362
359
case ' u' : case ' d' : case ' i' : case ' o' : case ' X' : case ' x' : \
363
360
out << static_cast <int >(value); break ; \
364
361
default : \
@@ -555,7 +552,7 @@ class FormatArg
555
552
inline int parseIntAndAdvance (const char *& c)
556
553
{
557
554
int i = 0 ;
558
- for (;*c >= ' 0' && *c <= ' 9' ; ++c)
555
+ for (;*c >= ' 0' && *c <= ' 9' ; ++c)
559
556
i = 10 *i + (*c - ' 0' );
560
557
return i;
561
558
}
@@ -569,35 +566,30 @@ inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
569
566
const detail::FormatArg* formatters,
570
567
int & argIndex, int numFormatters)
571
568
{
572
- if (*c >= ' 0' && *c <= ' 9' )
573
- {
569
+ if (*c >= ' 0' && *c <= ' 9' ) {
574
570
n = parseIntAndAdvance (c);
575
571
}
576
- else if (*c == ' *' )
577
- {
572
+ else if (*c == ' *' ) {
578
573
++c;
579
574
n = 0 ;
580
- if (positionalMode)
581
- {
575
+ if (positionalMode) {
582
576
int pos = parseIntAndAdvance (c) - 1 ;
583
- if (*c != ' $' )
577
+ if (*c != ' $' )
584
578
TINYFORMAT_ERROR (" tinyformat: Non-positional argument used after a positional one" );
585
- if (pos >= 0 && pos < numFormatters)
579
+ if (pos >= 0 && pos < numFormatters)
586
580
n = formatters[pos].toInt ();
587
581
else
588
582
TINYFORMAT_ERROR (" tinyformat: Positional argument out of range" );
589
583
++c;
590
584
}
591
- else
592
- {
593
- if (argIndex < numFormatters)
585
+ else {
586
+ if (argIndex < numFormatters)
594
587
n = formatters[argIndex++].toInt ();
595
588
else
596
589
TINYFORMAT_ERROR (" tinyformat: Not enough arguments to read variable width or precision" );
597
590
}
598
591
}
599
- else
600
- {
592
+ else {
601
593
return false ;
602
594
}
603
595
return true ;
@@ -612,16 +604,14 @@ inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
612
604
inline const char * printFormatStringLiteral (std::ostream& out, const char * fmt)
613
605
{
614
606
const char * c = fmt;
615
- for (;; ++c)
616
- {
617
- switch (*c)
618
- {
607
+ for (;; ++c) {
608
+ switch (*c) {
619
609
case ' \0 ' :
620
610
out.write (fmt, c - fmt);
621
611
return c;
622
612
case ' %' :
623
613
out.write (fmt, c - fmt);
624
- if (*(c+1 ) != ' %' )
614
+ if (*(c+1 ) != ' %' )
625
615
return c;
626
616
// for "%%", tack trailing % onto next literal section.
627
617
fmt = ++c;
@@ -672,8 +662,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
672
662
const detail::FormatArg* formatters,
673
663
int & argIndex, int numFormatters)
674
664
{
675
- if (*fmtStart != ' %' )
676
- {
665
+ if (*fmtStart != ' %' ) {
677
666
TINYFORMAT_ERROR (" tinyformat: Not enough conversion specifiers in format string" );
678
667
return fmtStart;
679
668
}
@@ -692,60 +681,49 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
692
681
693
682
// 1) Parse an argument index (if followed by '$') or a width possibly
694
683
// preceded with '0' flag.
695
- if (*c >= ' 0' && *c <= ' 9' )
696
- {
684
+ if (*c >= ' 0' && *c <= ' 9' ) {
697
685
const char tmpc = *c;
698
686
int value = parseIntAndAdvance (c);
699
- if (*c == ' $' )
700
- {
687
+ if (*c == ' $' ) {
701
688
// value is an argument index
702
- if (value > 0 && value <= numFormatters)
689
+ if (value > 0 && value <= numFormatters)
703
690
argIndex = value - 1 ;
704
691
else
705
692
TINYFORMAT_ERROR (" tinyformat: Positional argument out of range" );
706
693
++c;
707
694
positionalMode = true ;
708
695
}
709
- else if (positionalMode)
710
- {
696
+ else if (positionalMode) {
711
697
TINYFORMAT_ERROR (" tinyformat: Non-positional argument used after a positional one" );
712
698
}
713
- else
714
- {
715
- if (tmpc == ' 0' )
716
- {
699
+ else {
700
+ if (tmpc == ' 0' ) {
717
701
// Use internal padding so that numeric values are
718
702
// formatted correctly, eg -00010 rather than 000-10
719
703
out.fill (' 0' );
720
704
out.setf (std::ios::internal, std::ios::adjustfield);
721
705
}
722
- if (value != 0 )
723
- {
706
+ if (value != 0 ) {
724
707
// Nonzero value means that we parsed width.
725
708
widthSet = true ;
726
709
out.width (value);
727
710
}
728
711
}
729
712
}
730
- else if (positionalMode)
731
- {
713
+ else if (positionalMode) {
732
714
TINYFORMAT_ERROR (" tinyformat: Non-positional argument used after a positional one" );
733
715
}
734
716
// 2) Parse flags and width if we did not do it in previous step.
735
- if (!widthSet)
736
- {
717
+ if (!widthSet) {
737
718
// Parse flags
738
- for (;; ++c)
739
- {
740
- switch (*c)
741
- {
719
+ for (;; ++c) {
720
+ switch (*c) {
742
721
case ' #' :
743
722
out.setf (std::ios::showpoint | std::ios::showbase);
744
723
continue ;
745
724
case ' 0' :
746
725
// overridden by left alignment ('-' flag)
747
- if (!(out.flags () & std::ios::left))
748
- {
726
+ if (!(out.flags () & std::ios::left)) {
749
727
// Use internal padding so that numeric values are
750
728
// formatted correctly, eg -00010 rather than 000-10
751
729
out.fill (' 0' );
@@ -758,7 +736,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
758
736
continue ;
759
737
case ' ' :
760
738
// overridden by show positive sign, '+' flag.
761
- if (!(out.flags () & std::ios::showpos))
739
+ if (!(out.flags () & std::ios::showpos))
762
740
spacePadPositive = true ;
763
741
continue ;
764
742
case ' +' :
@@ -775,10 +753,8 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
775
753
int width = 0 ;
776
754
widthSet = parseWidthOrPrecision (width, c, positionalMode,
777
755
formatters, argIndex, numFormatters);
778
- if (widthSet)
779
- {
780
- if (width < 0 )
781
- {
756
+ if (widthSet) {
757
+ if (width < 0 ) {
782
758
// negative widths correspond to '-' flag set
783
759
out.fill (' ' );
784
760
out.setf (std::ios::left, std::ios::adjustfield);
@@ -788,28 +764,27 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
788
764
}
789
765
}
790
766
// 3) Parse precision
791
- if (*c == ' .' )
792
- {
767
+ if (*c == ' .' ) {
793
768
++c;
794
769
int precision = 0 ;
795
770
parseWidthOrPrecision (precision, c, positionalMode,
796
771
formatters, argIndex, numFormatters);
797
772
// Presence of `.` indicates precision set, unless the inferred value
798
773
// was negative in which case the default is used.
799
774
precisionSet = precision >= 0 ;
800
- if (precisionSet)
775
+ if (precisionSet)
801
776
out.precision (precision);
802
777
}
803
778
// 4) Ignore any C99 length modifier
804
- while (*c == ' l' || *c == ' h' || *c == ' L' ||
805
- *c == ' j' || *c == ' z' || *c == ' t' )
779
+ while (*c == ' l' || *c == ' h' || *c == ' L' ||
780
+ *c == ' j' || *c == ' z' || *c == ' t' ) {
806
781
++c;
782
+ }
807
783
// 5) We're up to the conversion specifier character.
808
784
// Set stream flags based on conversion specifier (thanks to the
809
785
// boost::format class for forging the way here).
810
786
bool intConversion = false ;
811
- switch (*c)
812
- {
787
+ switch (*c) {
813
788
case ' u' : case ' d' : case ' i' :
814
789
out.setf (std::ios::dec, std::ios::basefield);
815
790
intConversion = true ;
@@ -862,7 +837,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
862
837
// Handled as special case inside formatValue()
863
838
break ;
864
839
case ' s' :
865
- if (precisionSet)
840
+ if (precisionSet)
866
841
ntrunc = static_cast <int >(out.precision ());
867
842
// Make %s print Booleans as "true" and "false"
868
843
out.setf (std::ios::boolalpha);
@@ -878,8 +853,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
878
853
default :
879
854
break ;
880
855
}
881
- if (intConversion && precisionSet && !widthSet)
882
- {
856
+ if (intConversion && precisionSet && !widthSet) {
883
857
// "precision" for integers gives the minimum number of digits (to be
884
858
// padded with zeros on the left). This isn't really supported by the
885
859
// iostreams, but we can approximately simulate it with the width if
@@ -904,28 +878,26 @@ inline void formatImpl(std::ostream& out, const char* fmt,
904
878
char origFill = out.fill ();
905
879
906
880
bool positionalMode = false ;
907
- for (int argIndex = 0 ; positionalMode || argIndex < numFormatters; ++argIndex)
908
- {
881
+ for (int argIndex = 0 ; positionalMode || argIndex < numFormatters; ++argIndex) {
909
882
// Parse the format string
910
883
fmt = printFormatStringLiteral (out, fmt);
911
- if (positionalMode && *fmt == ' \0 ' )
884
+ if (positionalMode && *fmt == ' \0 ' )
912
885
break ;
913
886
bool spacePadPositive = false ;
914
887
int ntrunc = -1 ;
915
888
const char * fmtEnd = streamStateFromFormat (out, positionalMode, spacePadPositive, ntrunc, fmt,
916
889
formatters, argIndex, numFormatters);
917
- if (argIndex >= numFormatters)
918
- {
890
+ if (argIndex >= numFormatters) {
919
891
// Check args remain after reading any variable width/precision
920
892
TINYFORMAT_ERROR (" tinyformat: Not enough format arguments" );
921
893
return ;
922
894
}
923
895
const FormatArg& arg = formatters[argIndex];
924
896
// Format the arg into the stream.
925
- if (!spacePadPositive)
897
+ if (!spacePadPositive) {
926
898
arg.format (out, fmt, fmtEnd, ntrunc);
927
- else
928
- {
899
+ }
900
+ else {
929
901
// The following is a special case with no direct correspondence
930
902
// between stream formatting and the printf() behaviour. Simulate
931
903
// it crudely by formatting into a temporary string stream and
@@ -935,16 +907,18 @@ inline void formatImpl(std::ostream& out, const char* fmt,
935
907
tmpStream.setf (std::ios::showpos);
936
908
arg.format (tmpStream, fmt, fmtEnd, ntrunc);
937
909
std::string result = tmpStream.str (); // allocates... yuck.
938
- for (size_t i = 0 , iend = result.size (); i < iend; ++i)
939
- if (result[i] == ' +' ) result[i] = ' ' ;
910
+ for (size_t i = 0 , iend = result.size (); i < iend; ++i) {
911
+ if (result[i] == ' +' )
912
+ result[i] = ' ' ;
913
+ }
940
914
out << result;
941
915
}
942
916
fmt = fmtEnd;
943
917
}
944
918
945
919
// Print remaining part of format string.
946
920
fmt = printFormatStringLiteral (out, fmt);
947
- if (*fmt != ' \0 ' )
921
+ if (*fmt != ' \0 ' )
948
922
TINYFORMAT_ERROR (" tinyformat: Too many conversion specifiers in format string" );
949
923
950
924
// Restore stream state
0 commit comments