Skip to content

Commit

Permalink
Refactor "string truncate" (#32984)
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang authored Dec 26, 2024
1 parent 594edad commit 9bfa9f4
Show file tree
Hide file tree
Showing 26 changed files with 199 additions and 140 deletions.
6 changes: 3 additions & 3 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
return err
}
run.Index = index
run.Title, _ = util.SplitStringAtByteN(run.Title, 255)
run.Title = util.EllipsisDisplayString(run.Title, 255)

if err := db.Insert(ctx, run); err != nil {
return err
Expand Down Expand Up @@ -308,7 +308,7 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
} else {
hasWaiting = true
}
job.Name, _ = util.SplitStringAtByteN(job.Name, 255)
job.Name = util.EllipsisDisplayString(job.Name, 255)
runJobs = append(runJobs, &ActionRunJob{
RunID: run.ID,
RepoID: run.RepoID,
Expand Down Expand Up @@ -402,7 +402,7 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
if len(cols) > 0 {
sess.Cols(cols...)
}
run.Title, _ = util.SplitStringAtByteN(run.Title, 255)
run.Title = util.EllipsisDisplayString(run.Title, 255)
affected, err := sess.Update(run)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions models/actions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func GetRunnerByID(ctx context.Context, id int64) (*ActionRunner, error) {
// UpdateRunner updates runner's information.
func UpdateRunner(ctx context.Context, r *ActionRunner, cols ...string) error {
e := db.GetEngine(ctx)
r.Name, _ = util.SplitStringAtByteN(r.Name, 255)
r.Name = util.EllipsisDisplayString(r.Name, 255)
var err error
if len(cols) == 0 {
_, err = e.ID(r.ID).AllCols().Update(r)
Expand All @@ -279,7 +279,7 @@ func CreateRunner(ctx context.Context, t *ActionRunner) error {
// Remove OwnerID to avoid confusion; it's not worth returning an error here.
t.OwnerID = 0
}
t.Name, _ = util.SplitStringAtByteN(t.Name, 255)
t.Name = util.EllipsisDisplayString(t.Name, 255)
return db.Insert(ctx, t)
}

Expand Down
3 changes: 1 addition & 2 deletions models/actions/runner_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
)
Expand Down Expand Up @@ -52,7 +51,7 @@ func GetRunnerToken(ctx context.Context, token string) (*ActionRunnerToken, erro
if err != nil {
return nil, err
} else if !has {
return nil, fmt.Errorf(`runner token "%s...": %w`, base.TruncateString(token, 3), util.ErrNotExist)
return nil, fmt.Errorf(`runner token "%s...": %w`, util.TruncateRunes(token, 3), util.ErrNotExist)
}
return &runnerToken, nil
}
Expand Down
2 changes: 1 addition & 1 deletion models/actions/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {

// Loop through each schedule row
for _, row := range rows {
row.Title, _ = util.SplitStringAtByteN(row.Title, 255)
row.Title = util.EllipsisDisplayString(row.Title, 255)
// Create new schedule row
if err = db.Insert(ctx, row); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion models/actions/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask
if len(workflowJob.Steps) > 0 {
steps := make([]*ActionTaskStep, len(workflowJob.Steps))
for i, v := range workflowJob.Steps {
name, _ := util.SplitStringAtByteN(v.String(), 255)
name := util.EllipsisDisplayString(v.String(), 255)
steps[i] = &ActionTaskStep{
Name: name,
TaskID: task.ID,
Expand Down
8 changes: 4 additions & 4 deletions models/activities/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
"xorm.io/xorm/schemas"
Expand Down Expand Up @@ -226,7 +226,7 @@ func (a *Action) GetActUserName(ctx context.Context) string {
// ShortActUserName gets the action's user name trimmed to max 20
// chars.
func (a *Action) ShortActUserName(ctx context.Context) string {
return base.EllipsisString(a.GetActUserName(ctx), 20)
return util.EllipsisDisplayString(a.GetActUserName(ctx), 20)
}

// GetActDisplayName gets the action's display name based on DEFAULT_SHOW_FULL_NAME, or falls back to the username if it is blank.
Expand Down Expand Up @@ -260,7 +260,7 @@ func (a *Action) GetRepoUserName(ctx context.Context) string {
// ShortRepoUserName returns the name of the action repository owner
// trimmed to max 20 chars.
func (a *Action) ShortRepoUserName(ctx context.Context) string {
return base.EllipsisString(a.GetRepoUserName(ctx), 20)
return util.EllipsisDisplayString(a.GetRepoUserName(ctx), 20)
}

// GetRepoName returns the name of the action repository.
Expand All @@ -275,7 +275,7 @@ func (a *Action) GetRepoName(ctx context.Context) string {
// ShortRepoName returns the name of the action repository
// trimmed to max 33 chars.
func (a *Action) ShortRepoName(ctx context.Context) string {
return base.EllipsisString(a.GetRepoName(ctx), 33)
return util.EllipsisDisplayString(a.GetRepoName(ctx), 33)
}

// GetRepoPath returns the virtual path to the action repository.
Expand Down
4 changes: 2 additions & 2 deletions models/issues/issue_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func ChangeIssueTitle(ctx context.Context, issue *Issue, doer *user_model.User,
}
defer committer.Close()

issue.Title, _ = util.SplitStringAtByteN(issue.Title, 255)
issue.Title = util.EllipsisDisplayString(issue.Title, 255)
if err = UpdateIssueCols(ctx, issue, "name"); err != nil {
return fmt.Errorf("updateIssueCols: %w", err)
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *Issue, la
}

issue.Index = idx
issue.Title, _ = util.SplitStringAtByteN(issue.Title, 255)
issue.Title = util.EllipsisDisplayString(issue.Title, 255)

if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
Repo: repo,
Expand Down
2 changes: 1 addition & 1 deletion models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *Iss
}

issue.Index = idx
issue.Title, _ = util.SplitStringAtByteN(issue.Title, 255)
issue.Title = util.EllipsisDisplayString(issue.Title, 255)

if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
Repo: repo,
Expand Down
4 changes: 2 additions & 2 deletions models/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func NewProject(ctx context.Context, p *Project) error {
return util.NewInvalidArgumentErrorf("project type is not valid")
}

p.Title, _ = util.SplitStringAtByteN(p.Title, 255)
p.Title = util.EllipsisDisplayString(p.Title, 255)

return db.WithTx(ctx, func(ctx context.Context) error {
if err := db.Insert(ctx, p); err != nil {
Expand Down Expand Up @@ -311,7 +311,7 @@ func UpdateProject(ctx context.Context, p *Project) error {
p.CardType = CardTypeTextOnly
}

p.Title, _ = util.SplitStringAtByteN(p.Title, 255)
p.Title = util.EllipsisDisplayString(p.Title, 255)
_, err := db.GetEngine(ctx).ID(p.ID).Cols(
"title",
"description",
Expand Down
2 changes: 1 addition & 1 deletion models/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func IsReleaseExist(ctx context.Context, repoID int64, tagName string) (bool, er

// UpdateRelease updates all columns of a release
func UpdateRelease(ctx context.Context, rel *Release) error {
rel.Title, _ = util.SplitStringAtByteN(rel.Title, 255)
rel.Title = util.EllipsisDisplayString(rel.Title, 255)
_, err := db.GetEngine(ctx).ID(rel.ID).AllCols().Update(rel)
return err
}
Expand Down
10 changes: 5 additions & 5 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ func (u *User) BeforeUpdate() {
}

u.LowerName = strings.ToLower(u.Name)
u.Location = base.TruncateString(u.Location, 255)
u.Website = base.TruncateString(u.Website, 255)
u.Description = base.TruncateString(u.Description, 255)
u.Location = util.TruncateRunes(u.Location, 255)
u.Website = util.TruncateRunes(u.Website, 255)
u.Description = util.TruncateRunes(u.Description, 255)
}

// AfterLoad is invoked from XORM after filling all the fields of this object.
Expand Down Expand Up @@ -501,9 +501,9 @@ func (u *User) GitName() string {
// ShortName ellipses username to length
func (u *User) ShortName(length int) string {
if setting.UI.DefaultShowFullName && len(u.FullName) > 0 {
return base.EllipsisString(u.FullName, length)
return util.EllipsisDisplayString(u.FullName, length)
}
return base.EllipsisString(u.Name, length)
return util.EllipsisDisplayString(u.Name, length)
}

// IsMailable checks if a user is eligible
Expand Down
25 changes: 2 additions & 23 deletions modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

"github.com/dustin/go-humanize"
)
Expand All @@ -35,7 +35,7 @@ func EncodeSha256(str string) string {
// ShortSha is basically just truncating.
// It is DEPRECATED and will be removed in the future.
func ShortSha(sha1 string) string {
return TruncateString(sha1, 10)
return util.TruncateRunes(sha1, 10)
}

// BasicAuthDecode decode basic auth string
Expand Down Expand Up @@ -116,27 +116,6 @@ func FileSize(s int64) string {
return humanize.IBytes(uint64(s))
}

// EllipsisString returns a truncated short string,
// it appends '...' in the end of the length of string is too large.
func EllipsisString(str string, length int) string {
if length <= 3 {
return "..."
}
if utf8.RuneCountInString(str) <= length {
return str
}
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 utf8.RuneCountInString(str) < limit {
return str
}
return string([]rune(str)[:limit])
}

// StringsToInt64s converts a slice of string to a slice of int64.
func StringsToInt64s(strs []string) ([]int64, error) {
if strs == nil {
Expand Down
30 changes: 0 additions & 30 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,36 +113,6 @@ func TestFileSize(t *testing.T) {
assert.Equal(t, "2.0 EiB", FileSize(size))
}

func TestEllipsisString(t *testing.T) {
assert.Equal(t, "...", EllipsisString("foobar", 0))
assert.Equal(t, "...", EllipsisString("foobar", 1))
assert.Equal(t, "...", EllipsisString("foobar", 2))
assert.Equal(t, "...", EllipsisString("foobar", 3))
assert.Equal(t, "f...", EllipsisString("foobar", 4))
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) {
assert.Equal(t, "", TruncateString("foobar", 0))
assert.Equal(t, "f", TruncateString("foobar", 1))
assert.Equal(t, "fo", TruncateString("foobar", 2))
assert.Equal(t, "foo", TruncateString("foobar", 3))
assert.Equal(t, "foob", TruncateString("foobar", 4))
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) {
testSuccess := func(input []string, expected []int64) {
result, err := StringsToInt64s(input)
Expand Down
2 changes: 1 addition & 1 deletion modules/issue/template/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) {

it.Content = string(content)
it.Name = path.Base(it.FileName) // paths in Git are always '/' separated - do not use filepath!
it.About, _ = util.SplitStringAtByteN(it.Content, 80)
it.About = util.EllipsisDisplayString(it.Content, 80)
} else {
it.Content = templateBody
if it.About == "" {
Expand Down
2 changes: 1 addition & 1 deletion modules/markup/html_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func linkProcessor(ctx *RenderContext, node *html.Node) {

uri := node.Data[m[0]:m[1]]
remaining := node.Data[m[1]:]
if util.IsLikelySplitLeftPart(remaining) {
if util.IsLikelyEllipsisLeftPart(remaining) {
return
}
replaceContent(node, m[0], m[1], createLink(ctx, uri, uri, "" /*link*/))
Expand Down
6 changes: 3 additions & 3 deletions modules/markup/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ func TestRender_links(t *testing.T) {
"ftps://gitea.com",
`<p>ftps://gitea.com</p>`)

t.Run("LinkSplit", func(t *testing.T) {
input, _ := util.SplitStringAtByteN("http://10.1.2.3", 12)
t.Run("LinkEllipsis", func(t *testing.T) {
input := util.EllipsisDisplayString("http://10.1.2.3", 12)
assert.Equal(t, "http://10…", input)
test(input, "<p>http://10…</p>")

input, _ = util.SplitStringAtByteN("http://10.1.2.3", 13)
input = util.EllipsisDisplayString("http://10.1.2.3", 13)
assert.Equal(t, "http://10.…", input)
test(input, "<p>http://10.…</p>")
})
Expand Down
4 changes: 2 additions & 2 deletions modules/templates/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"strings"
texttmpl "text/template"

"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)

var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}\s*$`)
Expand All @@ -24,7 +24,7 @@ func mailSubjectTextFuncMap() texttmpl.FuncMap {
"dict": dict,
"Eval": evalTokens,

"EllipsisString": base.EllipsisString,
"EllipsisString": util.EllipsisDisplayString,
"AppName": func() string {
return setting.AppName
},
Expand Down
4 changes: 2 additions & 2 deletions modules/templates/util_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"html/template"
"strings"

"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/util"
)

type StringUtils struct{}
Expand Down Expand Up @@ -54,7 +54,7 @@ func (su *StringUtils) Cut(s, sep string) []any {
}

func (su *StringUtils) EllipsisString(s string, maxLength int) string {
return base.EllipsisString(s, maxLength)
return util.EllipsisDisplayString(s, maxLength)
}

func (su *StringUtils) ToUpper(s string) string {
Expand Down
Loading

0 comments on commit 9bfa9f4

Please sign in to comment.