File tree Expand file tree Collapse file tree 3 files changed +47
-7
lines changed Expand file tree Collapse file tree 3 files changed +47
-7
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import (
24
24
"code.gitea.io/gitea/modules/log"
25
25
"code.gitea.io/gitea/modules/setting"
26
26
"code.gitea.io/gitea/modules/upload"
27
+ "code.gitea.io/gitea/modules/util"
27
28
"code.gitea.io/gitea/services/gitdiff"
28
29
)
29
30
@@ -568,12 +569,15 @@ func PrepareCompareDiff(
568
569
title = headBranch
569
570
}
570
571
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
+ }
575
580
}
576
- title = title [:254 ] + "…"
577
581
}
578
582
579
583
ctx .Data ["title" ] = title
Original file line number Diff line number Diff line change @@ -1042,8 +1042,10 @@ func CompareAndPullRequestPost(ctx *context.Context) {
1042
1042
}
1043
1043
1044
1044
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
1047
1049
}
1048
1050
middleware .AssignForm (form , ctx .Data )
1049
1051
You can’t perform that action at this time.
0 commit comments