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:
+15
-2
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user