mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-04 13:50:39 +00:00
fix truncate utf8 string (#15828)
* fix truncate utf8 string. * revoke truncated user info.
This commit is contained in:
parent
6d2a333fdc
commit
27b29ffb22
2 changed files with 13 additions and 4 deletions
|
@ -21,6 +21,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
@ -213,19 +214,19 @@ func EllipsisString(str string, length int) string {
|
|||
if length <= 3 {
|
||||
return "..."
|
||||
}
|
||||
if len(str) <= length {
|
||||
if utf8.RuneCountInString(str) <= length {
|
||||
return str
|
||||
}
|
||||
return str[:length-3] + "..."
|
||||
return string([]rune(str)[:length-3]) + "..."
|
||||
}
|
||||
|
||||
// TruncateString returns a truncated string with given limit,
|
||||
// it returns input string if length is not reached limit.
|
||||
func TruncateString(str string, limit int) string {
|
||||
if len(str) < limit {
|
||||
if utf8.RuneCountInString(str) < limit {
|
||||
return str
|
||||
}
|
||||
return str[:limit]
|
||||
return string([]rune(str)[:limit])
|
||||
}
|
||||
|
||||
// StringsToInt64s converts a slice of string to a slice of int64.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue