Skip to content

Commit 887c0f1

Browse files
committed
as per lunny
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 46deda4 commit 887c0f1

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

modules/util/truncate.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import "unicode/utf8"
8+
9+
func SplitStringAtByteN(input string, n int) (left, right string) {
10+
if len(input) <= n {
11+
left = input
12+
return
13+
}
14+
15+
if !utf8.ValidString(input) {
16+
left = input[:n-3] + "..."
17+
right = "..." + input[n-3:]
18+
return
19+
}
20+
21+
// in UTF8 "…" is 3 bytes so doesn't really gain us anything...
22+
end := 0
23+
for end <= n-3 {
24+
_, size := utf8.DecodeRuneInString(input[end:])
25+
if end+size > n-3 {
26+
break
27+
}
28+
end += size
29+
}
30+
31+
left = input[:end] + "…"
32+
right = "…" + input[end:]
33+
return
34+
}

routers/web/repo/compare.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"code.gitea.io/gitea/modules/log"
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/upload"
27+
"code.gitea.io/gitea/modules/util"
2728
"code.gitea.io/gitea/services/gitdiff"
2829
)
2930

@@ -568,12 +569,15 @@ func PrepareCompareDiff(
568569
title = headBranch
569570
}
570571
if len(title) > 255 {
571-
if ctx.Data["content"] != nil {
572-
ctx.Data["content"] = fmt.Sprintf("…%s\n\n%s", title[254:], ctx.Data["content"])
573-
} else {
574-
ctx.Data["content"] = "…" + title[254:] + "\n"
572+
var trailer string
573+
title, trailer = util.SplitStringAtByteN(title, 255)
574+
if len(trailer) > 0 {
575+
if ctx.Data["content"] != nil {
576+
ctx.Data["content"] = fmt.Sprintf("%s\n\n%s", trailer, ctx.Data["content"])
577+
} else {
578+
ctx.Data["content"] = trailer + "\n"
579+
}
575580
}
576-
title = title[:254] + "…"
577581
}
578582

579583
ctx.Data["title"] = title

routers/web/repo/pull.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,10 @@ func CompareAndPullRequestPost(ctx *context.Context) {
10421042
}
10431043

10441044
if len(form.Title) > 255 {
1045-
form.Content = "…" + form.Title[254:] + "\n\n" + form.Content
1046-
form.Title = form.Title[:254] + "…"
1045+
var trailer string
1046+
form.Title, trailer = util.SplitStringAtByteN(form.Title, 255)
1047+
1048+
form.Content = trailer + "\n\n" + form.Content
10471049
}
10481050
middleware.AssignForm(form, ctx.Data)
10491051

0 commit comments

Comments
 (0)