-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Replace SplitStringAtByteN
with EllipsisStringWholeWord
#31822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
auth_model "code.gitea.io/gitea/models/auth" | ||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/models/unit" | ||
"code.gitea.io/gitea/modules/base" | ||
"code.gitea.io/gitea/modules/container" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
@@ -298,9 +299,8 @@ 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) | ||
steps[i] = &ActionTaskStep{ | ||
Name: name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above |
||
Name: base.EllipsisStringWholeWord(v.String(), 255), | ||
TaskID: task.ID, | ||
Index: int64(i), | ||
RepoID: task.RepoID, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,38 @@ func TestEllipsisString(t *testing.T) { | |
assert.Equal(t, "测试文本一二三四", EllipsisString("测试文本一二三四", 10)) | ||
} | ||
|
||
func TestEllipsisStringWholeWord(t *testing.T) { | ||
assert.Equal(t, "…", EllipsisStringWholeWord("foobar", 0)) | ||
assert.Equal(t, "f…", EllipsisStringWholeWord("foobar", 1)) | ||
assert.Equal(t, "fo…", EllipsisStringWholeWord("foobar", 2)) | ||
assert.Equal(t, "foo…", EllipsisStringWholeWord("foobar", 3)) | ||
assert.Equal(t, "foob…", EllipsisStringWholeWord("foobar", 4)) | ||
assert.Equal(t, "fooba…", EllipsisStringWholeWord("foobar", 5)) | ||
assert.Equal(t, "foobar", EllipsisStringWholeWord("foobar", 6)) | ||
assert.Equal(t, "foobar", EllipsisStringWholeWord("foobar", 10)) | ||
|
||
assert.Equal(t, "…", EllipsisStringWholeWord("foo bar", 0)) | ||
assert.Equal(t, "f…", EllipsisStringWholeWord("foo bar", 1)) | ||
assert.Equal(t, "fo…", EllipsisStringWholeWord("foo bar", 2)) | ||
assert.Equal(t, "foo…", EllipsisStringWholeWord("foo bar", 3)) | ||
assert.Equal(t, "foo…", EllipsisStringWholeWord("foo bar", 4)) | ||
assert.Equal(t, "foo…", EllipsisStringWholeWord("foo bar", 5)) | ||
assert.Equal(t, "foo…", EllipsisStringWholeWord("foo bar", 6)) | ||
assert.Equal(t, "foo bar", EllipsisStringWholeWord("foo bar", 7)) | ||
assert.Equal(t, "foo bar", EllipsisStringWholeWord("foo bar", 10)) | ||
|
||
assert.Equal(t, "foo bar…", EllipsisStringWholeWord("foo bar foo", 7)) | ||
assert.Equal(t, "foo bar…", EllipsisStringWholeWord("foo bar foo", 8)) | ||
assert.Equal(t, "foo bar…", EllipsisStringWholeWord("foo bar foo", 9)) | ||
assert.Equal(t, "foo bar…", EllipsisStringWholeWord("foo bar foo", 10)) | ||
assert.Equal(t, "foo bar foo", EllipsisStringWholeWord("foo bar foo", 11)) | ||
|
||
assert.Equal(t, "测试文本…", EllipsisStringWholeWord("测试文本一二三四", 4)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The truncation should be done at "byte" level, but not "rune" level. The problem of "rune" level is that CJK chars will be too much longer than expected.
See the difference. That's why the old function is "SplitStringAtByteN" (History: #17928 (review) ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, it was intended to be at rune level because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually there is usually no "database restriction", because I think the real problem is that CJK chars render much longer than ASCII chars with the same rune length on the UI ......... for example, assume one ASCII char's width is about W.
So "9 CJK runes" exceeds the width limitation too much on the UI, while usually "9 bytes CJK chars" won't exceed the expected "9 bytes ASCII" width. (I don't mean blocking for this change, just share the backgrounds, maybe it's worth to document the details no matter what method is finally chosen) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> Refactor "string truncate" #32984 |
||
assert.Equal(t, "测试文本…", EllipsisStringWholeWord("测试文本 一二三四", 6)) // contains special unicode space U+2008 | ||
assert.Equal(t, "测试文本一二…", EllipsisStringWholeWord("测试文本一二三四", 6)) | ||
assert.Equal(t, "测试文本一二三四", EllipsisStringWholeWord("测试文本一二三四", 10)) | ||
} | ||
|
||
func TestTruncateString(t *testing.T) { | ||
assert.Equal(t, "", TruncateString("foobar", 0)) | ||
assert.Equal(t, "f", TruncateString("foobar", 1)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,8 @@ import ( | |
repo_model "code.gitea.io/gitea/models/repo" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/actions" | ||
"code.gitea.io/gitea/modules/base" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/util" | ||
actions_service "code.gitea.io/gitea/services/actions" | ||
|
||
runnerv1 "code.gitea.io/actions-proto-go/runner/v1" | ||
|
@@ -69,10 +69,9 @@ func (s *Service) Register( | |
labels := req.Msg.Labels | ||
|
||
// create new runner | ||
name, _ := util.SplitStringAtByteN(req.Msg.Name, 255) | ||
runner := &actions_model.ActionRunner{ | ||
UUID: gouuid.New().String(), | ||
Name: name, | ||
Name: base.EllipsisStringWholeWord(req.Msg.Name, 255), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we changing an API response in the first place? |
||
OwnerID: runnerToken.OwnerID, | ||
RepoID: runnerToken.RepoID, | ||
Version: req.Msg.Version, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,11 @@ import ( | |
issues_model "code.gitea.io/gitea/models/issues" | ||
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/git" | ||
"code.gitea.io/gitea/modules/json" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/repository" | ||
"code.gitea.io/gitea/modules/util" | ||
notify_service "code.gitea.io/gitea/services/notify" | ||
) | ||
|
||
|
@@ -109,15 +109,7 @@ func (a *actionNotifier) CreateIssueComment(ctx context.Context, doer *user_mode | |
IsPrivate: issue.Repo.IsPrivate, | ||
} | ||
|
||
truncatedContent, truncatedRight := util.SplitStringAtByteN(comment.Content, 200) | ||
if truncatedRight != "" { | ||
// in case the content is in a Latin family language, we remove the last broken word. | ||
lastSpaceIdx := strings.LastIndex(truncatedContent, " ") | ||
if lastSpaceIdx != -1 && (len(truncatedContent)-lastSpaceIdx < 15) { | ||
truncatedContent = truncatedContent[:lastSpaceIdx] + "…" | ||
} | ||
} | ||
act.Content = fmt.Sprintf("%d|%s", issue.Index, truncatedContent) | ||
act.Content = fmt.Sprintf("%d|%s", issue.Index, base.EllipsisStringWholeWord(comment.Content, 200)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above |
||
|
||
if issue.IsPull { | ||
act.OpType = activities_model.ActionCommentPull | ||
|
Uh oh!
There was an error while loading. Please reload this page.