Decodes a subject line. Currently only supports quoted-printable UTF-8. This format is the result of a `git format-patch` when the commit title has a non-ASCII character (i.e. an emoji). See for reference: https://stackoverflow.com/questions/27695749/gmail-api-not-respecting-utf-encoding-in-subject
(encoded string)
| 448 | // of a `git format-patch` when the commit title has a non-ASCII character (i.e. an emoji). |
| 449 | // See for reference: https://stackoverflow.com/questions/27695749/gmail-api-not-respecting-utf-encoding-in-subject |
| 450 | func decodeSubject(encoded string) string { |
| 451 | if !strings.HasPrefix(encoded, "=?UTF-8?q?") { |
| 452 | // not UTF-8 encoded |
| 453 | return encoded |
| 454 | } |
| 455 | |
| 456 | // If the subject is too long, `git format-patch` may produce a subject line across |
| 457 | // multiple lines. When parsed, this can look like the following: |
| 458 | // <UTF8-prefix><first-line> <UTF8-prefix><second-line> |
| 459 | payload := " " + encoded |
| 460 | payload = strings.ReplaceAll(payload, " =?UTF-8?q?", "") |
| 461 | payload = strings.ReplaceAll(payload, "?=", "") |
| 462 | |
| 463 | decoded, err := ioutil.ReadAll(quotedprintable.NewReader(strings.NewReader(payload))) |
| 464 | if err != nil { |
| 465 | // if err, abort decoding and return original subject |
| 466 | return encoded |
| 467 | } |
| 468 | |
| 469 | return string(decoded) |
| 470 | } |