fix(share): slice content label by utf-8 runes (#4634)

* fix(share): slice content label by utf-8 runes

* Apply suggestions about avoiding allocations

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* lint: remove unused import

* test: add test cases for CJK truncation

* test: add tests for ASCII labels too

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
beerpsi
2025-11-07 02:24:07 +07:00
committed by GitHub
parent 3dfaa8cca1
commit fe1cee0159
2 changed files with 47 additions and 2 deletions
+15 -2
View File
@@ -119,8 +119,21 @@ 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] + "..."
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)