Archived
1
0

Merge pull request #170 from cathugger/master

srnd: avoid quoting name in some cases
This commit is contained in:
Jeff 2018-12-12 14:02:01 -05:00 committed by GitHub
commit 34be78da8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,7 +28,6 @@ import (
"strings" "strings"
"time" "time"
"unicode" "unicode"
"unicode/utf8"
) )
func DelFile(fname string) { func DelFile(fname string) {
@ -201,21 +200,15 @@ func isQtext(r rune) bool {
} }
func writeQuoted(b *strings.Builder, s string) { func writeQuoted(b *strings.Builder, s string) {
last := 0
b.WriteByte('"') b.WriteByte('"')
for i, r := range s { for _, r := range s {
if !isQtext(r) && !isWSP(r) { if isQtext(r) || isWSP(r) {
if i > last { b.WriteRune(r)
b.WriteString(s[last:i]) } else {
}
b.WriteByte('\\') b.WriteByte('\\')
b.WriteRune(r) b.WriteRune(r)
last = i + utf8.RuneLen(r)
} }
} }
if last < len(s) {
b.WriteString(s[last:])
}
b.WriteByte('"') b.WriteByte('"')
} }
@ -227,14 +220,20 @@ func formatAddress(name, email string) string {
if name != "" { if name != "" {
needsEncoding := false needsEncoding := false
needsQuoting := false needsQuoting := false
for _, r := range name { for i, r := range name {
if r >= 0x80 || (!isWSP(r) && !isVchar(r)) { if r >= 0x80 || (!isWSP(r) && !isVchar(r)) {
needsEncoding = true needsEncoding = true
break break
} }
if !isAtext(r) { if isAtext(r) {
needsQuoting = true continue
} }
if r == ' ' && i > 0 && name[i-1] != ' ' && i < len(name)-1 {
// allow spaces but only surrounded by non-spaces
// otherwise they will be removed by receiver
continue
}
needsQuoting = true
} }
if needsEncoding { if needsEncoding {