git-send-email: introduce quote_subject()

The quote_rfc2047() always adds RFC2047 quoting. To avoid quoting ASCII subjects, before calling quote_rfc2047() subject must be tested for non-ASCII characters. This patch introduces a new quote_subject() function, which performs the test and calls quote_rfc2047 only if necessary. Signed-off-by: Krzysztof Mazur <krzysiek@podlesie.net> Signed-off-by: Jeff King <peff@peff.net>

Krzysztof Mazur committed Oct 24, 2012 at 23:08 UTC ce5478006c7318f12e6b2abeb0c0477009db63b5
1 file changed +19 -6
git-send-email.perl
+19 -6
@@ -657,9 +657,7 @@ EOT
657 $initial_subject = $1;
658 my $subject = $initial_subject;
659 $_ = "Subject: " .
660 - ($subject =~ /[^[:ascii:]]/ ?
661 - quote_rfc2047($subject, $compose_encoding) :
662 - $subject) .
660 + quote_subject($subject, $compose_encoding) .
661 "\n";
662 } elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
663 $initial_reply_to = $1;
@@ -907,6 +905,22 @@ sub is_rfc2047_quoted {
905 $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o;
906 }
907
908 +sub subject_needs_rfc2047_quoting {
909 + my $s = shift;
910 +
911 + return ($s =~ /[^[:ascii:]]/);
912 +}
913 +
914 +sub quote_subject {
915 + local $subject = shift;
916 + my $encoding = shift || 'UTF-8';
917 +
918 + if (subject_needs_rfc2047_quoting($subject)) {
919 + return quote_rfc2047($subject, $encoding);
920 + }
921 + return $subject;
922 +}
923 +
924 # use the simplest quoting being able to handle the recipient
925 sub sanitize_address {
926 my ($recipient) = @_;
@@ -1327,9 +1341,8 @@ foreach my $t (@files) {
1341 $body_encoding = $auto_8bit_encoding;
1342 }
1343
1330 - if ($broken_encoding{$t} && !is_rfc2047_quoted($subject) &&
1331 - ($subject =~ /[^[:ascii:]]/)) {
1332 - $subject = quote_rfc2047($subject, $auto_8bit_encoding);
1344 + if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1345 + $subject = quote_subject($subject, $auto_8bit_encoding);
1346 }
1347
1348 if (defined $author and $author ne $sender) {