mirror of
https://github.com/go-gitea/gitea
synced 2024-11-22 00:01:35 +01:00
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 <wxiaoguang@gmail.com>
This commit is contained in:
parent
5b56d13e0d
commit
1761459ebc
6 changed files with 14 additions and 18 deletions
|
@ -9,9 +9,9 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/container"
|
"code.gitea.io/gitea/modules/container"
|
||||||
"code.gitea.io/gitea/modules/markup/common"
|
"code.gitea.io/gitea/modules/markup/common"
|
||||||
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"github.com/yuin/goldmark/ast"
|
"github.com/yuin/goldmark/ast"
|
||||||
"github.com/yuin/goldmark/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type prefixedIDs struct {
|
type prefixedIDs struct {
|
||||||
|
@ -36,7 +36,7 @@ func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte {
|
||||||
if !bytes.HasPrefix(result, []byte("user-content-")) {
|
if !bytes.HasPrefix(result, []byte("user-content-")) {
|
||||||
result = append([]byte("user-content-"), result...)
|
result = append([]byte("user-content-"), result...)
|
||||||
}
|
}
|
||||||
if p.values.Add(util.BytesToReadOnlyString(result)) {
|
if p.values.Add(util.UnsafeBytesToString(result)) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
for i := 1; ; i++ {
|
for i := 1; ; i++ {
|
||||||
|
@ -49,7 +49,7 @@ func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte {
|
||||||
|
|
||||||
// Put puts a given element id to the used ids table.
|
// Put puts a given element id to the used ids table.
|
||||||
func (p *prefixedIDs) Put(value []byte) {
|
func (p *prefixedIDs) Put(value []byte) {
|
||||||
p.values.Add(util.BytesToReadOnlyString(value))
|
p.values.Add(util.UnsafeBytesToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPrefixedIDs() *prefixedIDs {
|
func newPrefixedIDs() *prefixedIDs {
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/markup"
|
"code.gitea.io/gitea/modules/markup"
|
||||||
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"github.com/yuin/goldmark/ast"
|
"github.com/yuin/goldmark/ast"
|
||||||
"github.com/yuin/goldmark/text"
|
"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) {
|
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
|
||||||
}
|
}
|
||||||
txt := v.Text(reader.Source())
|
txt := v.Text(reader.Source())
|
||||||
header := markup.Header{
|
header := markup.Header{
|
||||||
Text: util.BytesToReadOnlyString(txt),
|
Text: util.UnsafeBytesToString(txt),
|
||||||
Level: v.Level,
|
Level: v.Level,
|
||||||
}
|
}
|
||||||
if id, found := v.AttributeString("id"); found {
|
if id, found := v.AttributeString("id"); found {
|
||||||
header.ID = util.BytesToReadOnlyString(id.([]byte))
|
header.ID = util.UnsafeBytesToString(id.([]byte))
|
||||||
}
|
}
|
||||||
*tocList = append(*tocList, header)
|
*tocList = append(*tocList, header)
|
||||||
g.applyElementDir(v)
|
g.applyElementDir(v)
|
||||||
|
|
|
@ -14,8 +14,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/markup/mdstripper"
|
"code.gitea.io/gitea/modules/markup/mdstripper"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
"code.gitea.io/gitea/modules/util"
|
||||||
"github.com/yuin/goldmark/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -341,7 +340,7 @@ func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool
|
||||||
return false, nil
|
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 {
|
if r == nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,7 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/system"
|
"code.gitea.io/gitea/models/system"
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
|
"code.gitea.io/gitea/modules/util"
|
||||||
"github.com/yuin/goldmark/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DBStore can be used to store app state items in local filesystem
|
// 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 {
|
||||||
if content == "" {
|
if content == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return json.Unmarshal(util.StringToReadOnlyBytes(content), item)
|
return json.Unmarshal(util.UnsafeStringToBytes(content), item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set saves the state item
|
// Set saves the state item
|
||||||
|
@ -33,5 +32,5 @@ func (f *DBStore) Set(ctx context.Context, item StateItem) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return system.SaveAppStateContent(ctx, item.Name(), util.BytesToReadOnlyString(b))
|
return system.SaveAppStateContent(ctx, item.Name(), util.UnsafeBytesToString(b))
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,6 @@ package util
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/yuin/goldmark/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type sanitizedError struct {
|
type sanitizedError struct {
|
||||||
|
@ -33,7 +31,7 @@ var schemeSep = []byte("://")
|
||||||
|
|
||||||
// SanitizeCredentialURLs remove all credentials in URLs (starting with "scheme://") for the input string: "https://user:pass@domain.com" => "https://sanitized-credential@domain.com"
|
// SanitizeCredentialURLs remove all credentials in URLs (starting with "scheme://") for the input string: "https://user:pass@domain.com" => "https://sanitized-credential@domain.com"
|
||||||
func SanitizeCredentialURLs(s string) string {
|
func SanitizeCredentialURLs(s string) string {
|
||||||
bs := util.StringToReadOnlyBytes(s)
|
bs := UnsafeStringToBytes(s)
|
||||||
schemeSepPos := bytes.Index(bs, schemeSep)
|
schemeSepPos := bytes.Index(bs, schemeSep)
|
||||||
if schemeSepPos == -1 || bytes.IndexByte(bs[schemeSepPos:], '@') == -1 {
|
if schemeSepPos == -1 || bytes.IndexByte(bs[schemeSepPos:], '@') == -1 {
|
||||||
return s // fast return if there is no URL scheme or no userinfo
|
return s // fast return if there is no URL scheme or no userinfo
|
||||||
|
@ -70,5 +68,5 @@ func SanitizeCredentialURLs(s string) string {
|
||||||
schemeSepPos = bytes.Index(bs, schemeSep)
|
schemeSepPos = bytes.Index(bs, schemeSep)
|
||||||
}
|
}
|
||||||
out = append(out, bs...)
|
out = append(out, bs...)
|
||||||
return util.BytesToReadOnlyString(out)
|
return UnsafeBytesToString(out)
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,11 +87,11 @@ func ToSnakeCase(input string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnsafeBytesToString uses Go's unsafe package to convert a byte slice to a 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 {
|
func UnsafeBytesToString(b []byte) string {
|
||||||
return unsafe.String(unsafe.SliceData(b), len(b))
|
return unsafe.String(unsafe.SliceData(b), len(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnsafeStringToBytes uses Go's unsafe package to convert a string to a byte slice.
|
||||||
func UnsafeStringToBytes(s string) []byte {
|
func UnsafeStringToBytes(s string) []byte {
|
||||||
return unsafe.Slice(unsafe.StringData(s), len(s))
|
return unsafe.Slice(unsafe.StringData(s), len(s))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue