Skip to content

Commit 1761459

Browse files
Refactor to use UnsafeStringToBytes (#31358)
The PR replaces all `goldmark/util.BytesToReadOnlyString` with `util.UnsafeBytesToString`, `goldmark/util.StringToReadOnlyBytes` with `util.UnsafeStringToBytes`. This removes one `TODO`. Co-authored-by: wxiaoguang <[email protected]>
1 parent 5b56d13 commit 1761459

File tree

6 files changed

+14
-18
lines changed

6 files changed

+14
-18
lines changed

modules/markup/markdown/prefixed_id.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99

1010
"code.gitea.io/gitea/modules/container"
1111
"code.gitea.io/gitea/modules/markup/common"
12+
"code.gitea.io/gitea/modules/util"
1213

1314
"github.com/yuin/goldmark/ast"
14-
"github.com/yuin/goldmark/util"
1515
)
1616

1717
type prefixedIDs struct {
@@ -36,7 +36,7 @@ func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte {
3636
if !bytes.HasPrefix(result, []byte("user-content-")) {
3737
result = append([]byte("user-content-"), result...)
3838
}
39-
if p.values.Add(util.BytesToReadOnlyString(result)) {
39+
if p.values.Add(util.UnsafeBytesToString(result)) {
4040
return result
4141
}
4242
for i := 1; ; i++ {
@@ -49,7 +49,7 @@ func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte {
4949

5050
// Put puts a given element id to the used ids table.
5151
func (p *prefixedIDs) Put(value []byte) {
52-
p.values.Add(util.BytesToReadOnlyString(value))
52+
p.values.Add(util.UnsafeBytesToString(value))
5353
}
5454

5555
func newPrefixedIDs() *prefixedIDs {

modules/markup/markdown/transform_heading.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"fmt"
88

99
"code.gitea.io/gitea/modules/markup"
10+
"code.gitea.io/gitea/modules/util"
1011

1112
"github.com/yuin/goldmark/ast"
1213
"github.com/yuin/goldmark/text"
13-
"github.com/yuin/goldmark/util"
1414
)
1515

1616
func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Heading, reader text.Reader, tocList *[]markup.Header) {
@@ -21,11 +21,11 @@ func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Headin
2121
}
2222
txt := v.Text(reader.Source())
2323
header := markup.Header{
24-
Text: util.BytesToReadOnlyString(txt),
24+
Text: util.UnsafeBytesToString(txt),
2525
Level: v.Level,
2626
}
2727
if id, found := v.AttributeString("id"); found {
28-
header.ID = util.BytesToReadOnlyString(id.([]byte))
28+
header.ID = util.UnsafeBytesToString(id.([]byte))
2929
}
3030
*tocList = append(*tocList, header)
3131
g.applyElementDir(v)

modules/references/references.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/log"
1515
"code.gitea.io/gitea/modules/markup/mdstripper"
1616
"code.gitea.io/gitea/modules/setting"
17-
18-
"github.com/yuin/goldmark/util"
17+
"code.gitea.io/gitea/modules/util"
1918
)
2019

2120
var (
@@ -341,7 +340,7 @@ func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool
341340
return false, nil
342341
}
343342
}
344-
r := getCrossReference(util.StringToReadOnlyBytes(content), match[2], match[3], false, prOnly)
343+
r := getCrossReference(util.UnsafeStringToBytes(content), match[2], match[3], false, prOnly)
345344
if r == nil {
346345
return false, nil
347346
}

modules/system/db.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import (
88

99
"code.gitea.io/gitea/models/system"
1010
"code.gitea.io/gitea/modules/json"
11-
12-
"github.com/yuin/goldmark/util"
11+
"code.gitea.io/gitea/modules/util"
1312
)
1413

1514
// DBStore can be used to store app state items in local filesystem
@@ -24,7 +23,7 @@ func (f *DBStore) Get(ctx context.Context, item StateItem) error {
2423
if content == "" {
2524
return nil
2625
}
27-
return json.Unmarshal(util.StringToReadOnlyBytes(content), item)
26+
return json.Unmarshal(util.UnsafeStringToBytes(content), item)
2827
}
2928

3029
// Set saves the state item
@@ -33,5 +32,5 @@ func (f *DBStore) Set(ctx context.Context, item StateItem) error {
3332
if err != nil {
3433
return err
3534
}
36-
return system.SaveAppStateContent(ctx, item.Name(), util.BytesToReadOnlyString(b))
35+
return system.SaveAppStateContent(ctx, item.Name(), util.UnsafeBytesToString(b))
3736
}

modules/util/sanitize.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package util
66
import (
77
"bytes"
88
"unicode"
9-
10-
"github.com/yuin/goldmark/util"
119
)
1210

1311
type sanitizedError struct {
@@ -33,7 +31,7 @@ var schemeSep = []byte("://")
3331

3432
// SanitizeCredentialURLs remove all credentials in URLs (starting with "scheme://") for the input string: "https://user:[email protected]" => "https://[email protected]"
3533
func SanitizeCredentialURLs(s string) string {
36-
bs := util.StringToReadOnlyBytes(s)
34+
bs := UnsafeStringToBytes(s)
3735
schemeSepPos := bytes.Index(bs, schemeSep)
3836
if schemeSepPos == -1 || bytes.IndexByte(bs[schemeSepPos:], '@') == -1 {
3937
return s // fast return if there is no URL scheme or no userinfo
@@ -70,5 +68,5 @@ func SanitizeCredentialURLs(s string) string {
7068
schemeSepPos = bytes.Index(bs, schemeSep)
7169
}
7270
out = append(out, bs...)
73-
return util.BytesToReadOnlyString(out)
71+
return UnsafeBytesToString(out)
7472
}

modules/util/string.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ func ToSnakeCase(input string) string {
8787
}
8888

8989
// UnsafeBytesToString uses Go's unsafe package to convert a byte slice to a string.
90-
// TODO: replace all "goldmark/util.BytesToReadOnlyString" with this official approach
9190
func UnsafeBytesToString(b []byte) string {
9291
return unsafe.String(unsafe.SliceData(b), len(b))
9392
}
9493

94+
// UnsafeStringToBytes uses Go's unsafe package to convert a string to a byte slice.
9595
func UnsafeStringToBytes(s string) []byte {
9696
return unsafe.Slice(unsafe.StringData(s), len(s))
9797
}

0 commit comments

Comments
 (0)