From af161968c8c9be6875d1268861a8cf3563eb7fd8 Mon Sep 17 00:00:00 2001 From: cathugger Date: Wed, 12 Dec 2018 20:54:16 +0200 Subject: [PATCH] srnd: avoid quoting in some cases --- contrib/backends/srndv2/src/srnd/util.go | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/contrib/backends/srndv2/src/srnd/util.go b/contrib/backends/srndv2/src/srnd/util.go index a7efb67..5cdcf44 100644 --- a/contrib/backends/srndv2/src/srnd/util.go +++ b/contrib/backends/srndv2/src/srnd/util.go @@ -28,7 +28,6 @@ import ( "strings" "time" "unicode" - "unicode/utf8" ) func DelFile(fname string) { @@ -201,21 +200,15 @@ func isQtext(r rune) bool { } func writeQuoted(b *strings.Builder, s string) { - last := 0 b.WriteByte('"') - for i, r := range s { - if !isQtext(r) && !isWSP(r) { - if i > last { - b.WriteString(s[last:i]) - } + for _, r := range s { + if isQtext(r) || isWSP(r) { + b.WriteRune(r) + } else { b.WriteByte('\\') b.WriteRune(r) - last = i + utf8.RuneLen(r) } } - if last < len(s) { - b.WriteString(s[last:]) - } b.WriteByte('"') } @@ -227,14 +220,20 @@ func formatAddress(name, email string) string { if name != "" { needsEncoding := false needsQuoting := false - for _, r := range name { + for i, r := range name { if r >= 0x80 || (!isWSP(r) && !isVchar(r)) { needsEncoding = true break } - if !isAtext(r) { - needsQuoting = true + if isAtext(r) { + 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 {