Skip to content

Commit

Permalink
fix: Handle spaces inside angle brackets (#360)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrei Sidorenko <[email protected]>
  • Loading branch information
dflse and Flassie authored Jan 5, 2025
1 parent 1d9fc9c commit 50133b8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
20 changes: 19 additions & 1 deletion internal/stringutil/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions internal/stringutil/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ func TestCommaDelimitedAddressLists(t *testing.T) {
have: `"Joe @ Company" <[email protected]>;<[email protected]>`,
want: `"Joe @ Company" <[email protected]>,<[email protected]>`,
},
{
have: `"Joe @ Company" <[email protected] >;<[email protected]>`,
want: `"Joe @ Company" <[email protected]>,<[email protected]>`,
},
{
have: `"Joe @ Company" <[email protected]>;<[email protected] >`,
want: `"Joe @ Company" <[email protected]>,<[email protected]>`,
},
{
have: `"Joe @ Company" <[email protected]>;< [email protected]>`,
want: `"Joe @ Company" <[email protected]>,<[email protected]>`,
},
{
have: `"Joe @ Company" < [email protected] >;< [email protected] >`,
want: `"Joe @ Company" <[email protected]>,<[email protected]>`,
},
}
for _, tt := range testData {
t.Run(tt.have, func(t *testing.T) {
Expand Down

0 comments on commit 50133b8

Please sign in to comment.