Skip to content

Commit

Permalink
lib-storage: mail_search_arg_to_imap() - minor FLAGS writing optimiza…
Browse files Browse the repository at this point in the history
…tion

Avoid an extra str_delete() by immediately calculating whether the
parenthesis are needed.
  • Loading branch information
sirainen committed Apr 27, 2017
1 parent cd955fd commit fe43895
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/lib-storage/mail-search-args-imap.c
Expand Up @@ -57,22 +57,20 @@ mail_search_arg_to_imap_flags(string_t *dest, enum mail_flags flags)
static const char *flag_names[] = {
"ANSWERED", "FLAGGED", "DELETED", "SEEN", "DRAFT", "RECENT"
};
unsigned int count = 0, start_pos = str_len(dest);

str_append_c(dest, '(');
i_assert(flags != 0);

if (!bits_is_power_of_two(flags))
str_append_c(dest, '(');
for (unsigned int i = 0; i < N_ELEMENTS(flag_names); i++) {
if ((flags & (1 << i)) != 0) {
str_append(dest, flag_names[i]);
str_append_c(dest, ' ');
count++;
}
}
i_assert(count > 0);

str_truncate(dest, str_len(dest)-1);
if (count == 1)
str_delete(dest, start_pos, 1);
else
if (!bits_is_power_of_two(flags))
str_append_c(dest, ')');
}

Expand Down

0 comments on commit fe43895

Please sign in to comment.