Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate messages with fewer characters #3072

Merged
merged 1 commit into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion notify/util.go
Expand Up @@ -90,7 +90,7 @@ func Truncate(s string, n int) (string, bool) {
if n <= 3 {
return string(r[:n]), true
}
return string(r[:n-3]) + "...", true
return string(r[:n-1]) + "", true
Copy link
Contributor

@pstibrany pstibrany Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"…" is encoded as 3 bytes in utf-8, so this makes string longer by 2 bytes compared to previous version.

Furthermore blindly cutting last 3 or 1 byte can actually break the utf-8 sequence of bytes. Ideally string would be truncated at rune boundary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, I missed that when checking changelog of alertmanager 0.26.0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

}

// TmplText is using monadic error handling in order to make string templating
Expand Down
6 changes: 3 additions & 3 deletions notify/util_test.go
Expand Up @@ -46,7 +46,7 @@ func TestTruncate(t *testing.T) {
{
in: "abcde",
n: 4,
out: "a...",
out: "abc…",
trunc: true,
},
{
Expand All @@ -58,7 +58,7 @@ func TestTruncate(t *testing.T) {
{
in: "abcdefgh",
n: 5,
out: "ab...",
out: "abcd…",
trunc: true,
},
{
Expand All @@ -70,7 +70,7 @@ func TestTruncate(t *testing.T) {
{
in: "a⌘cdef",
n: 5,
out: "a⌘...",
out: "a⌘cd…",
trunc: true,
},
}
Expand Down