Skip to content

Commit

Permalink
Refactor to use UnsafeStringToBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Jun 13, 2024
1 parent e61e9a3 commit b66b014
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
6 changes: 3 additions & 3 deletions modules/markup/markdown/prefixed_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/markup/common"
"code.gitea.io/gitea/modules/util"

"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/util"
)

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

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

func newPrefixedIDs() *prefixedIDs {
Expand Down
6 changes: 3 additions & 3 deletions modules/markup/markdown/transform_heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"fmt"

"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/util"

"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)

func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Heading, reader text.Reader, tocList *[]markup.Header) {
Expand All @@ -21,11 +21,11 @@ func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Headin
}
txt := v.Text(reader.Source())
header := markup.Header{
Text: util.BytesToReadOnlyString(txt),
Text: util.UnsafeBytesToString(txt),
Level: v.Level,
}
if id, found := v.AttributeString("id"); found {
header.ID = util.BytesToReadOnlyString(id.([]byte))
header.ID = util.UnsafeBytesToString(id.([]byte))
}
*tocList = append(*tocList, header)
g.applyElementDir(v)
Expand Down
5 changes: 2 additions & 3 deletions modules/references/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup/mdstripper"
"code.gitea.io/gitea/modules/setting"

"github.com/yuin/goldmark/util"
"code.gitea.io/gitea/modules/util"
)

var (
Expand Down Expand Up @@ -341,7 +340,7 @@ func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool
return false, nil
}
}
r := getCrossReference(util.StringToReadOnlyBytes(content), match[2], match[3], false, prOnly)
r := getCrossReference(util.UnsafeStringToBytes(content), match[2], match[3], false, prOnly)
if r == nil {
return false, nil
}
Expand Down
17 changes: 13 additions & 4 deletions modules/system/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ package system

import (
"context"
"unsafe"

"code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/modules/json"

"github.com/yuin/goldmark/util"
)

// DBStore can be used to store app state items in local filesystem
Expand All @@ -24,7 +23,7 @@ func (f *DBStore) Get(ctx context.Context, item StateItem) error {
if content == "" {
return nil
}
return json.Unmarshal(util.StringToReadOnlyBytes(content), item)
return json.Unmarshal(unsafeStringToBytes(content), item)
}

// Set saves the state item
Expand All @@ -33,5 +32,15 @@ func (f *DBStore) Set(ctx context.Context, item StateItem) error {
if err != nil {
return err
}
return system.SaveAppStateContent(ctx, item.Name(), util.BytesToReadOnlyString(b))
return system.SaveAppStateContent(ctx, item.Name(), unsafeBytesToString(b))
}

// unsafeBytesToString is copied from modules/util/string.go to avoid import cycle.
func unsafeBytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}

// unsafeStringToBytes is copied from modules/util/string.go to avoid import cycle.
func unsafeStringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
6 changes: 2 additions & 4 deletions modules/util/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package util
import (
"bytes"
"unicode"

"github.com/yuin/goldmark/util"
)

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

// SanitizeCredentialURLs remove all credentials in URLs (starting with "scheme://") for the input string: "https://user:[email protected]" => "https://[email protected]"
func SanitizeCredentialURLs(s string) string {
bs := util.StringToReadOnlyBytes(s)
bs := UnsafeStringToBytes(s)
schemeSepPos := bytes.Index(bs, schemeSep)
if schemeSepPos == -1 || bytes.IndexByte(bs[schemeSepPos:], '@') == -1 {
return s // fast return if there is no URL scheme or no userinfo
Expand Down Expand Up @@ -70,5 +68,5 @@ func SanitizeCredentialURLs(s string) string {
schemeSepPos = bytes.Index(bs, schemeSep)
}
out = append(out, bs...)
return util.BytesToReadOnlyString(out)
return UnsafeBytesToString(out)
}
1 change: 0 additions & 1 deletion modules/util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func ToSnakeCase(input string) string {
}

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

0 comments on commit b66b014

Please sign in to comment.