Skip to content

Commit 11c2a8d

Browse files
ikegami-tigaw
authored andcommitted
util: use switch case statements instead of repeated else if
This changes as same with other switch case statements. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent 2c3e873 commit 11c2a8d

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

util/table.c

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,23 @@ static void table_print_centered(struct value *val, int width, enum fmt_type typ
4646
int i, len, left_pad, right_pad;
4747
char buf[64];
4848

49-
if (type == FMT_STRING) {
49+
switch (type) {
50+
case FMT_STRING:
5051
len = strlen(val->s);
51-
} else if (type == FMT_INT) {
52+
break;
53+
case FMT_INT:
5254
len = snprintf(buf, sizeof(buf), "%d", val->i);
53-
} else if (type == FMT_UNSIGNED) {
55+
break;
56+
case FMT_UNSIGNED:
5457
len = snprintf(buf, sizeof(buf), "%u", val->u);
55-
} else if (type == FMT_LONG) {
58+
break;
59+
case FMT_LONG:
5660
len = snprintf(buf, sizeof(buf), "%ld", val->ld);
57-
} else if (type == FMT_UNSIGNED_LONG) {
61+
break;
62+
case FMT_UNSIGNED_LONG:
5863
len = snprintf(buf, sizeof(buf), "%lu", val->lu);
59-
} else {
64+
break;
65+
default:
6066
fprintf(stderr, "Invalid format!\n");
6167
return;
6268
}
@@ -69,16 +75,25 @@ static void table_print_centered(struct value *val, int width, enum fmt_type typ
6975
putchar(' ');
7076

7177
/* print value */
72-
if (type == FMT_STRING)
78+
switch (type) {
79+
case FMT_STRING:
7380
printf("%s%s", val->s, add_pad ? "" : " ");
74-
else if (type == FMT_INT)
81+
break;
82+
case FMT_INT:
7583
printf("%d%s", val->i, add_pad ? "" : " ");
76-
else if (type == FMT_UNSIGNED)
84+
break;
85+
case FMT_UNSIGNED:
7786
printf("%u%s", val->u, add_pad ? "" : " ");
78-
else if (type == FMT_LONG)
87+
break;
88+
case FMT_LONG:
7989
printf("%ld%s", val->ld, add_pad ? "" : " ");
80-
else if (type == FMT_UNSIGNED_LONG)
90+
break;
91+
case FMT_UNSIGNED_LONG:
8192
printf("%lu%s", val->lu, add_pad ? "" : " ");
93+
break;
94+
default:
95+
break;
96+
}
8297

8398
/* add right padding */
8499
for (i = 0; i < right_pad; i++)
@@ -96,15 +111,18 @@ static void table_print_columns(const struct table *t)
96111
last_col = col == t->num_columns - 1 ? true : false;
97112
c = &t->columns[col];
98113
width = c->width;
99-
if (c->align == LEFT)
100-
width *= -1;
101-
102-
if (c->align == CENTERED) {
114+
switch (c->align) {
115+
case CENTERED:
103116
v.s = c->name;
104117
v.align = c->align;
105118
table_print_centered(&v, width, FMT_STRING, !last_col);
106-
} else {
119+
break;
120+
case LEFT:
121+
width *= -1;
122+
fallthrough;
123+
default:
107124
printf("%*s%s", width, c->name, last_col ? "" : " ");
125+
break;
108126
}
109127
}
110128

@@ -138,12 +156,14 @@ static void table_print_rows(const struct table *t)
138156
v = &r->val[col];
139157

140158
width = c->width;
141-
if (v->align == LEFT)
142-
width *= -1;
143-
144-
if (v->align == CENTERED) {
159+
switch (v->align) {
160+
case CENTERED:
145161
table_print_centered(v, width, v->type, !last_col);
146-
} else {
162+
break;
163+
case LEFT:
164+
width *= -1;
165+
fallthrough;
166+
default:
147167
switch (v->type) {
148168
case FMT_STRING:
149169
printf("%*s%s", width, v->s, last_col ? "" : " ");
@@ -164,6 +184,7 @@ static void table_print_rows(const struct table *t)
164184
fprintf(stderr, "Invalid format!\n");
165185
break;
166186
}
187+
break;
167188
}
168189
}
169190
printf("\n");

0 commit comments

Comments
 (0)