aboutsummaryrefslogtreecommitdiffstats
path: root/modules/base
diff options
context:
space:
mode:
authoryan <sxty32@gmail.com>2021-05-13 15:50:57 +0800
committerGitHub <noreply@github.com>2021-05-13 08:50:57 +0100
commit27b29ffb227ae1f563c38b40679a232f920f9c19 (patch)
tree8125dc245c26ab7bd38a40ae205dacbe258657a7 /modules/base
parent6d2a333fdcd7c59ab3a31593913e53dfd3ae55ca (diff)
downloadgitea-27b29ffb227ae1f563c38b40679a232f920f9c19.tar.gz
gitea-27b29ffb227ae1f563c38b40679a232f920f9c19.zip
fix truncate utf8 string (#15828)
* fix truncate utf8 string. * revoke truncated user info.
Diffstat (limited to 'modules/base')
-rw-r--r--modules/base/tool.go9
-rw-r--r--modules/base/tool_test.go8
2 files changed, 13 insertions, 4 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index d721d47e9d..c9530473e2 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -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.
diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go
index b6baeb8c3c..fcd3ca296a 100644
--- a/modules/base/tool_test.go
+++ b/modules/base/tool_test.go
@@ -170,6 +170,10 @@ func TestEllipsisString(t *testing.T) {
assert.Equal(t, "fo...", EllipsisString("foobar", 5))
assert.Equal(t, "foobar", EllipsisString("foobar", 6))
assert.Equal(t, "foobar", EllipsisString("foobar", 10))
+ assert.Equal(t, "测...", EllipsisString("测试文本一二三四", 4))
+ assert.Equal(t, "测试...", EllipsisString("测试文本一二三四", 5))
+ assert.Equal(t, "测试文...", EllipsisString("测试文本一二三四", 6))
+ assert.Equal(t, "测试文本一二三四", EllipsisString("测试文本一二三四", 10))
}
func TestTruncateString(t *testing.T) {
@@ -181,6 +185,10 @@ func TestTruncateString(t *testing.T) {
assert.Equal(t, "fooba", TruncateString("foobar", 5))
assert.Equal(t, "foobar", TruncateString("foobar", 6))
assert.Equal(t, "foobar", TruncateString("foobar", 7))
+ assert.Equal(t, "测试文本", TruncateString("测试文本一二三四", 4))
+ assert.Equal(t, "测试文本一", TruncateString("测试文本一二三四", 5))
+ assert.Equal(t, "测试文本一二", TruncateString("测试文本一二三四", 6))
+ assert.Equal(t, "测试文本一二三", TruncateString("测试文本一二三四", 7))
}
func TestStringsToInt64s(t *testing.T) {