From 50133b88ac73f791661e13dacab535188b66fb2e Mon Sep 17 00:00:00 2001 From: dflse <119652918+dflse@users.noreply.github.com> Date: Sun, 5 Jan 2025 22:23:29 +0300 Subject: [PATCH] fix: Handle spaces inside angle brackets (#360) Co-authored-by: Andrei Sidorenko --- internal/stringutil/addr.go | 20 +++++++++++++++++++- internal/stringutil/addr_test.go | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/internal/stringutil/addr.go b/internal/stringutil/addr.go index c3eaa42d..4a02a83a 100644 --- a/internal/stringutil/addr.go +++ b/internal/stringutil/addr.go @@ -34,6 +34,7 @@ func EnsureCommaDelimitedAddresses(s string) string { inQuotes := false inDomain := false escapeSequence := false + inAngles := false sb := strings.Builder{} for i, r := range s { if escapeSequence { @@ -46,6 +47,19 @@ func EnsureCommaDelimitedAddresses(s string) string { sb.WriteRune(r) continue } + + if r == '<' { + inAngles = true + sb.WriteRune(r) + continue + } + + if r == '>' { + inAngles = false + sb.WriteRune(r) + continue + } + if inQuotes { if r == '\\' { escapeSequence = true @@ -74,13 +88,17 @@ func EnsureCommaDelimitedAddresses(s string) string { sb.WriteRune(r) continue } - if r == ' ' { + if r == ' ' && !inAngles { inDomain = false sb.WriteRune(',') sb.WriteRune(r) continue } } + + if inAngles && r == ' ' { + continue + } } sb.WriteRune(r) } diff --git a/internal/stringutil/addr_test.go b/internal/stringutil/addr_test.go index 0ce4eac5..a4e3624d 100644 --- a/internal/stringutil/addr_test.go +++ b/internal/stringutil/addr_test.go @@ -108,6 +108,22 @@ func TestCommaDelimitedAddressLists(t *testing.T) { have: `"Joe @ Company" ;`, want: `"Joe @ Company" ,`, }, + { + have: `"Joe @ Company" ;`, + want: `"Joe @ Company" ,`, + }, + { + have: `"Joe @ Company" ;`, + want: `"Joe @ Company" ,`, + }, + { + have: `"Joe @ Company" ;< other@company.com>`, + want: `"Joe @ Company" ,`, + }, + { + have: `"Joe @ Company" < joe@company.com >;< other@company.com >`, + want: `"Joe @ Company" ,`, + }, } for _, tt := range testData { t.Run(tt.have, func(t *testing.T) {