Archived
1
0

srnd: avoid quoting in some cases

This commit is contained in:
cathugger 2018-12-12 20:54:16 +02:00
parent a1d11c594c
commit af161968c8
No known key found for this signature in database
GPG Key ID: 9BADDA2DAF6F01A8

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 {