Skip to content

Commit 656c570

Browse files
committed
Minor style update [NFC]
1 parent 0a48bdc commit 656c570

File tree

2 files changed

+57
-83
lines changed

2 files changed

+57
-83
lines changed

tinyformat.h

Lines changed: 54 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ struct formatZeroIntegerWorkaround<T,true>
246246
{
247247
static bool invoke(std::ostream& out, const T& value)
248248
{
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) {
251250
out << "+0";
252251
return true;
253252
}
@@ -288,7 +287,7 @@ inline void formatTruncated(std::ostream& out, const T& value, int ntrunc)
288287
inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \
289288
{ \
290289
std::streamsize len = 0; \
291-
while(len < ntrunc && value[len] != 0) \
290+
while (len < ntrunc && value[len] != 0) \
292291
++len; \
293292
out.write(value, len); \
294293
}
@@ -334,15 +333,14 @@ inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,
334333
// could otherwise lead to a crash when printing a dangling (const char*).
335334
const bool canConvertToChar = detail::is_convertible<T,char>::value;
336335
const bool canConvertToVoidPtr = detail::is_convertible<T, const void*>::value;
337-
if(canConvertToChar && *(fmtEnd-1) == 'c')
336+
if (canConvertToChar && *(fmtEnd-1) == 'c')
338337
detail::formatValueAsType<T, char>::invoke(out, value);
339-
else if(canConvertToVoidPtr && *(fmtEnd-1) == 'p')
338+
else if (canConvertToVoidPtr && *(fmtEnd-1) == 'p')
340339
detail::formatValueAsType<T, const void*>::invoke(out, value);
341340
#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
342-
else if(detail::formatZeroIntegerWorkaround<T>::invoke(out, value)) /**/;
341+
else if (detail::formatZeroIntegerWorkaround<T>::invoke(out, value)) /**/;
343342
#endif
344-
else if(ntrunc >= 0)
345-
{
343+
else if (ntrunc >= 0) {
346344
// Take care not to overread C strings in truncating conversions like
347345
// "%.4s" where at most 4 characters may be read.
348346
detail::formatTruncated(out, value, ntrunc);
@@ -357,8 +355,7 @@ inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,
357355
inline void formatValue(std::ostream& out, const char* /*fmtBegin*/, \
358356
const char* fmtEnd, int /**/, charType value) \
359357
{ \
360-
switch(*(fmtEnd-1)) \
361-
{ \
358+
switch (*(fmtEnd-1)) { \
362359
case 'u': case 'd': case 'i': case 'o': case 'X': case 'x': \
363360
out << static_cast<int>(value); break; \
364361
default: \
@@ -555,7 +552,7 @@ class FormatArg
555552
inline int parseIntAndAdvance(const char*& c)
556553
{
557554
int i = 0;
558-
for(;*c >= '0' && *c <= '9'; ++c)
555+
for (;*c >= '0' && *c <= '9'; ++c)
559556
i = 10*i + (*c - '0');
560557
return i;
561558
}
@@ -569,35 +566,30 @@ inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
569566
const detail::FormatArg* formatters,
570567
int& argIndex, int numFormatters)
571568
{
572-
if(*c >= '0' && *c <= '9')
573-
{
569+
if (*c >= '0' && *c <= '9') {
574570
n = parseIntAndAdvance(c);
575571
}
576-
else if(*c == '*')
577-
{
572+
else if (*c == '*') {
578573
++c;
579574
n = 0;
580-
if(positionalMode)
581-
{
575+
if (positionalMode) {
582576
int pos = parseIntAndAdvance(c) - 1;
583-
if(*c != '$')
577+
if (*c != '$')
584578
TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
585-
if(pos >= 0 && pos < numFormatters)
579+
if (pos >= 0 && pos < numFormatters)
586580
n = formatters[pos].toInt();
587581
else
588582
TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
589583
++c;
590584
}
591-
else
592-
{
593-
if(argIndex < numFormatters)
585+
else {
586+
if (argIndex < numFormatters)
594587
n = formatters[argIndex++].toInt();
595588
else
596589
TINYFORMAT_ERROR("tinyformat: Not enough arguments to read variable width or precision");
597590
}
598591
}
599-
else
600-
{
592+
else {
601593
return false;
602594
}
603595
return true;
@@ -612,16 +604,14 @@ inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
612604
inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt)
613605
{
614606
const char* c = fmt;
615-
for(;; ++c)
616-
{
617-
switch(*c)
618-
{
607+
for (;; ++c) {
608+
switch (*c) {
619609
case '\0':
620610
out.write(fmt, c - fmt);
621611
return c;
622612
case '%':
623613
out.write(fmt, c - fmt);
624-
if(*(c+1) != '%')
614+
if (*(c+1) != '%')
625615
return c;
626616
// for "%%", tack trailing % onto next literal section.
627617
fmt = ++c;
@@ -672,8 +662,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
672662
const detail::FormatArg* formatters,
673663
int& argIndex, int numFormatters)
674664
{
675-
if(*fmtStart != '%')
676-
{
665+
if (*fmtStart != '%') {
677666
TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string");
678667
return fmtStart;
679668
}
@@ -692,60 +681,49 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
692681

693682
// 1) Parse an argument index (if followed by '$') or a width possibly
694683
// preceded with '0' flag.
695-
if(*c >= '0' && *c <= '9')
696-
{
684+
if (*c >= '0' && *c <= '9') {
697685
const char tmpc = *c;
698686
int value = parseIntAndAdvance(c);
699-
if(*c == '$')
700-
{
687+
if (*c == '$') {
701688
// value is an argument index
702-
if(value > 0 && value <= numFormatters)
689+
if (value > 0 && value <= numFormatters)
703690
argIndex = value - 1;
704691
else
705692
TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
706693
++c;
707694
positionalMode = true;
708695
}
709-
else if(positionalMode)
710-
{
696+
else if (positionalMode) {
711697
TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
712698
}
713-
else
714-
{
715-
if(tmpc == '0')
716-
{
699+
else {
700+
if (tmpc == '0') {
717701
// Use internal padding so that numeric values are
718702
// formatted correctly, eg -00010 rather than 000-10
719703
out.fill('0');
720704
out.setf(std::ios::internal, std::ios::adjustfield);
721705
}
722-
if(value != 0)
723-
{
706+
if (value != 0) {
724707
// Nonzero value means that we parsed width.
725708
widthSet = true;
726709
out.width(value);
727710
}
728711
}
729712
}
730-
else if(positionalMode)
731-
{
713+
else if (positionalMode) {
732714
TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
733715
}
734716
// 2) Parse flags and width if we did not do it in previous step.
735-
if(!widthSet)
736-
{
717+
if (!widthSet) {
737718
// Parse flags
738-
for(;; ++c)
739-
{
740-
switch(*c)
741-
{
719+
for (;; ++c) {
720+
switch (*c) {
742721
case '#':
743722
out.setf(std::ios::showpoint | std::ios::showbase);
744723
continue;
745724
case '0':
746725
// overridden by left alignment ('-' flag)
747-
if(!(out.flags() & std::ios::left))
748-
{
726+
if (!(out.flags() & std::ios::left)) {
749727
// Use internal padding so that numeric values are
750728
// formatted correctly, eg -00010 rather than 000-10
751729
out.fill('0');
@@ -758,7 +736,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
758736
continue;
759737
case ' ':
760738
// overridden by show positive sign, '+' flag.
761-
if(!(out.flags() & std::ios::showpos))
739+
if (!(out.flags() & std::ios::showpos))
762740
spacePadPositive = true;
763741
continue;
764742
case '+':
@@ -775,10 +753,8 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
775753
int width = 0;
776754
widthSet = parseWidthOrPrecision(width, c, positionalMode,
777755
formatters, argIndex, numFormatters);
778-
if(widthSet)
779-
{
780-
if(width < 0)
781-
{
756+
if (widthSet) {
757+
if (width < 0) {
782758
// negative widths correspond to '-' flag set
783759
out.fill(' ');
784760
out.setf(std::ios::left, std::ios::adjustfield);
@@ -788,28 +764,27 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
788764
}
789765
}
790766
// 3) Parse precision
791-
if(*c == '.')
792-
{
767+
if (*c == '.') {
793768
++c;
794769
int precision = 0;
795770
parseWidthOrPrecision(precision, c, positionalMode,
796771
formatters, argIndex, numFormatters);
797772
// Presence of `.` indicates precision set, unless the inferred value
798773
// was negative in which case the default is used.
799774
precisionSet = precision >= 0;
800-
if(precisionSet)
775+
if (precisionSet)
801776
out.precision(precision);
802777
}
803778
// 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') {
806781
++c;
782+
}
807783
// 5) We're up to the conversion specifier character.
808784
// Set stream flags based on conversion specifier (thanks to the
809785
// boost::format class for forging the way here).
810786
bool intConversion = false;
811-
switch(*c)
812-
{
787+
switch (*c) {
813788
case 'u': case 'd': case 'i':
814789
out.setf(std::ios::dec, std::ios::basefield);
815790
intConversion = true;
@@ -862,7 +837,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
862837
// Handled as special case inside formatValue()
863838
break;
864839
case 's':
865-
if(precisionSet)
840+
if (precisionSet)
866841
ntrunc = static_cast<int>(out.precision());
867842
// Make %s print Booleans as "true" and "false"
868843
out.setf(std::ios::boolalpha);
@@ -878,8 +853,7 @@ inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode
878853
default:
879854
break;
880855
}
881-
if(intConversion && precisionSet && !widthSet)
882-
{
856+
if (intConversion && precisionSet && !widthSet) {
883857
// "precision" for integers gives the minimum number of digits (to be
884858
// padded with zeros on the left). This isn't really supported by the
885859
// iostreams, but we can approximately simulate it with the width if
@@ -904,28 +878,26 @@ inline void formatImpl(std::ostream& out, const char* fmt,
904878
char origFill = out.fill();
905879

906880
bool positionalMode = false;
907-
for(int argIndex = 0; positionalMode || argIndex < numFormatters; ++argIndex)
908-
{
881+
for (int argIndex = 0; positionalMode || argIndex < numFormatters; ++argIndex) {
909882
// Parse the format string
910883
fmt = printFormatStringLiteral(out, fmt);
911-
if(positionalMode && *fmt == '\0')
884+
if (positionalMode && *fmt == '\0')
912885
break;
913886
bool spacePadPositive = false;
914887
int ntrunc = -1;
915888
const char* fmtEnd = streamStateFromFormat(out, positionalMode, spacePadPositive, ntrunc, fmt,
916889
formatters, argIndex, numFormatters);
917-
if (argIndex >= numFormatters)
918-
{
890+
if (argIndex >= numFormatters) {
919891
// Check args remain after reading any variable width/precision
920892
TINYFORMAT_ERROR("tinyformat: Not enough format arguments");
921893
return;
922894
}
923895
const FormatArg& arg = formatters[argIndex];
924896
// Format the arg into the stream.
925-
if(!spacePadPositive)
897+
if (!spacePadPositive) {
926898
arg.format(out, fmt, fmtEnd, ntrunc);
927-
else
928-
{
899+
}
900+
else {
929901
// The following is a special case with no direct correspondence
930902
// between stream formatting and the printf() behaviour. Simulate
931903
// it crudely by formatting into a temporary string stream and
@@ -935,16 +907,18 @@ inline void formatImpl(std::ostream& out, const char* fmt,
935907
tmpStream.setf(std::ios::showpos);
936908
arg.format(tmpStream, fmt, fmtEnd, ntrunc);
937909
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+
}
940914
out << result;
941915
}
942916
fmt = fmtEnd;
943917
}
944918

945919
// Print remaining part of format string.
946920
fmt = printFormatStringLiteral(out, fmt);
947-
if(*fmt != '\0')
921+
if (*fmt != '\0')
948922
TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string");
949923

950924
// Restore stream state

tinyformat_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void compareSprintf(const Args&... args)
2929
std::string tfmResult = tfm::format(args...);
3030
char sprintfResult[200];
3131
sprintf(sprintfResult, args...);
32-
if(tfmResult != sprintfResult)
32+
if (tfmResult != sprintfResult)
3333
{
3434
std::cout << tfmResult << std::endl;
3535
std::cout << sprintfResult << std::endl;
@@ -46,10 +46,10 @@ try \
4646
std::cout << "expected exception in " #expression << "\n"; \
4747
++nfailed; \
4848
} \
49-
catch(std::runtime_error&) {} \
49+
catch (std::runtime_error&) {} \
5050

5151
#define CHECK_EQUAL(a, b) \
52-
if(!((a) == (b))) \
52+
if (!((a) == (b))) \
5353
{ \
5454
std::cout << "test failed, line " << __LINE__ << "\n"; \
5555
std::cout << (a) << " != " << (b) << "\n"; \

0 commit comments

Comments
 (0)