Apply suggestions about avoiding allocations

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
beerpsi 2025-10-31 00:47:33 +07:00 committed by GitHub
parent bae4c4381f
commit 644792e18a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)