@@ -219,35 +219,42 @@ static int is_multipart_boundary(const struct strbuf *line)
static void cleanup_subject(struct strbuf *subject)
{
- char *pos;
- size_t remove;
- while (subject->len) {
- switch (*subject->buf) {
- case 'r': case 'R':
- if (subject->len <= 3)
- break;
- if (!memcmp(subject->buf + 1, "e:", 2)) {
- strbuf_remove(subject, 0, 3);
- continue;
- }
- break;
- case ' ': case '\t': case ':':
- strbuf_remove(subject, 0, 1);
- continue;
- case '[':
- if ((pos = strchr(subject->buf, ']'))) {
- remove = pos - subject->buf;
- if (remove <= (subject->len - remove) * 2) {
- strbuf_remove(subject, 0, remove + 1);
- continue;
- }
- } else
- strbuf_remove(subject, 0, 1);
- break;
- }
+ int status;
+ regex_t regex;
+ regmatch_t match[4];
+
+ /* Strip off 'Re:' and/or the first text in square brackets, such as
+ '[PATCH]' at the start of the mail Subject. */
+ status = regcomp(®ex,
+ "^([Rr]e:)?([^]]*\\[[^]]+\\])(.*)$",
+ REG_EXTENDED);
+
+ if (status) {
+ /* Compiling the regex failed. Find out why and tell
+ the user. This is always a bug in the code. */
+ int esize = regerror(status, ®ex, NULL, 0);
+ struct strbuf etext = STRBUF_INIT;
+
+ strbuf_grow(&etext, esize);
+ regerror(status, ®ex, etext.buf, esize);
+ fprintf (stderr,
+ "Error compiling regular expression: %s\n",
+ etext.buf);
+ strbuf_release(&etext);
+ exit(1);
+ }
+
+ /* Store any matches in match. */
+ status = regexec(®ex, subject->buf, 4, match, 0);
+
+ /* If there was a match for \3 in the regex, trim the subject
+ to this match. */
+ if (!status && match[3].rm_so > 0) {
+ strbuf_remove(subject, 0, match[3].rm_so);
strbuf_trim(subject);
- return;
}
+
+ return;
}
static void cleanup_space(struct strbuf *sb)