From bae4c4381f622cd84cf4aa005b7d62cf10cec27f Mon Sep 17 00:00:00 2001 From: beer-psi Date: Fri, 31 Oct 2025 00:31:18 +0700 Subject: [PATCH 1/2] fix(share): slice content label by utf-8 runes --- core/share.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/share.go b/core/share.go index 202c27d89..530c6e324 100644 --- a/core/share.go +++ b/core/share.go @@ -4,6 +4,7 @@ import ( "context" "strings" "time" + "unicode/utf8" "github.com/Masterminds/squirrel" "github.com/deluan/rest" @@ -119,8 +120,9 @@ func (r *shareRepositoryWrapper) Save(entity interface{}) (string, error) { log.Error(r.ctx, "Invalid Resource ID", "id", firstId) return "", model.ErrNotFound } - if len(s.Contents) > 30 { - s.Contents = s.Contents[:26] + "..." + + if utf8.RuneCountInString(s.Contents) > 30 { + s.Contents = string([]rune(s.Contents)[:26]) + "..." } id, err = r.Persistable.Save(s) From 644792e18ad749273e10d52943eceb085a0c883a Mon Sep 17 00:00:00 2001 From: beerpsi <92439990+beer-psi@users.noreply.github.com> Date: Fri, 31 Oct 2025 00:47:33 +0700 Subject: [PATCH 2/2] Apply suggestions about avoiding allocations Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- core/share.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/core/share.go b/core/share.go index 530c6e324..98a3ed3ca 100644 --- a/core/share.go +++ b/core/share.go @@ -121,8 +121,20 @@ func (r *shareRepositoryWrapper) Save(entity interface{}) (string, error) { return "", model.ErrNotFound } - if utf8.RuneCountInString(s.Contents) > 30 { - s.Contents = string([]rune(s.Contents)[:26]) + "..." + const maxContentRunes = 30 + const truncateToRunes = 26 + + var runeCount int + var truncateIndex int + for i := range s.Contents { + runeCount++ + if runeCount == truncateToRunes+1 { + truncateIndex = i + } + } + + if runeCount > maxContentRunes { + s.Contents = s.Contents[:truncateIndex] + "..." } id, err = r.Persistable.Save(s)