Skip to content

Commit 3265304

Browse files
committed
Merge branch 'jc/mailinfo-header-cleanup'
Code clean-up. * jc/mailinfo-header-cleanup: mailinfo: we parse fixed headers
2 parents c5ee8f2 + be10ac7 commit 3265304

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

mailinfo.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,8 @@ static void cleanup_subject(struct mailinfo *mi, struct strbuf *subject)
348348
strbuf_trim(subject);
349349
}
350350

351-
#define MAX_HDR_PARSED 10
352-
static const char *header[MAX_HDR_PARSED] = {
353-
"From","Subject","Date",
351+
static const char * const header[] = {
352+
"From", "Subject", "Date",
354353
};
355354

356355
static inline int skip_header(const struct strbuf *line, const char *hdr,
@@ -585,7 +584,7 @@ static int check_header(struct mailinfo *mi,
585584
struct strbuf sb = STRBUF_INIT;
586585

587586
/* search for the interesting parts */
588-
for (i = 0; header[i]; i++) {
587+
for (i = 0; i < ARRAY_SIZE(header); i++) {
589588
if ((!hdr_data[i] || overwrite) &&
590589
parse_header(line, header[i], mi, &sb)) {
591590
handle_header(&hdr_data[i], &sb);
@@ -627,7 +626,7 @@ static int is_inbody_header(const struct mailinfo *mi,
627626
{
628627
int i;
629628
const char *val;
630-
for (i = 0; header[i]; i++)
629+
for (i = 0; i < ARRAY_SIZE(header); i++)
631630
if (!mi->s_hdr_data[i] && skip_header(line, header[i], &val))
632631
return 1;
633632
return 0;
@@ -774,7 +773,7 @@ static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
774773
return is_format_patch_separator(line->buf + 1, line->len - 1);
775774
if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
776775
int i;
777-
for (i = 0; header[i]; i++)
776+
for (i = 0; i < ARRAY_SIZE(header); i++)
778777
if (!strcmp("Subject", header[i])) {
779778
handle_header(&mi->s_hdr_data[i], line);
780779
return 1;
@@ -826,7 +825,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
826825
* We may have already read "secondary headers"; purge
827826
* them to give ourselves a clean restart.
828827
*/
829-
for (i = 0; header[i]; i++) {
828+
for (i = 0; i < ARRAY_SIZE(header); i++) {
830829
if (mi->s_hdr_data[i])
831830
strbuf_release(mi->s_hdr_data[i]);
832831
FREE_AND_NULL(mi->s_hdr_data[i]);
@@ -1157,7 +1156,7 @@ static void handle_info(struct mailinfo *mi)
11571156
struct strbuf *hdr;
11581157
int i;
11591158

1160-
for (i = 0; header[i]; i++) {
1159+
for (i = 0; i < ARRAY_SIZE(header); i++) {
11611160
/* only print inbody headers if we output a patch file */
11621161
if (mi->patch_lines && mi->s_hdr_data[i])
11631162
hdr = mi->s_hdr_data[i];
@@ -1208,8 +1207,8 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
12081207
return -1;
12091208
}
12101209

1211-
mi->p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->p_hdr_data)));
1212-
mi->s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->s_hdr_data)));
1210+
mi->p_hdr_data = xcalloc(ARRAY_SIZE(header), sizeof(*(mi->p_hdr_data)));
1211+
mi->s_hdr_data = xcalloc(ARRAY_SIZE(header), sizeof(*(mi->s_hdr_data)));
12131212

12141213
do {
12151214
peek = fgetc(mi->input);
@@ -1292,15 +1291,15 @@ void clear_mailinfo(struct mailinfo *mi)
12921291
strbuf_release(&mi->inbody_header_accum);
12931292
free(mi->message_id);
12941293

1295-
for (size_t i = 0; header[i]; i++) {
1294+
for (size_t i = 0; i < ARRAY_SIZE(header); i++) {
12961295
if (!mi->p_hdr_data[i])
12971296
continue;
12981297
strbuf_release(mi->p_hdr_data[i]);
12991298
free(mi->p_hdr_data[i]);
13001299
}
13011300
free(mi->p_hdr_data);
13021301

1303-
for (size_t i = 0; header[i]; i++) {
1302+
for (size_t i = 0; i < ARRAY_SIZE(header); i++) {
13041303
if (!mi->s_hdr_data[i])
13051304
continue;
13061305
strbuf_release(mi->s_hdr_data[i]);

0 commit comments

Comments
 (0)