Skip to content

Commit

Permalink
pkg/util: expose encoding.UnsafeString to external callers
Browse files Browse the repository at this point in the history
Previously, unsafeString can only be used within the encoding package. This
commit makes that function public so that external callers could use it. We
will use this for the connection migration work in the next commit.

Release note: None
  • Loading branch information
jaylim-crl committed Feb 28, 2022
1 parent 9b45e99 commit 6c958c8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pkg/util/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ func prettyPrintInvertedIndexKey(b []byte) (string, []byte, error) {
switch tempB[i+1] {
case escapedTerm:
if len(tempB[:i]) > 0 {
outBytes = outBytes + strconv.Quote(unsafeString(tempB[:i]))
outBytes = outBytes + strconv.Quote(UnsafeString(tempB[:i]))
} else {
lenOut := len(outBytes)
if lenOut > 1 && outBytes[lenOut-1] == '/' {
Expand All @@ -724,7 +724,7 @@ func prettyPrintInvertedIndexKey(b []byte) (string, []byte, error) {
}
return outBytes, tempB[i+escapeLength:], nil
case escapedJSONObjectKeyTerm:
outBytes = outBytes + strconv.Quote(unsafeString(tempB[:i])) + "/"
outBytes = outBytes + strconv.Quote(UnsafeString(tempB[:i])) + "/"
case escapedJSONArray:
outBytes = outBytes + "Arr/"
default:
Expand Down Expand Up @@ -823,11 +823,11 @@ func EncodeStringDescending(b []byte, s string) []byte {
return EncodeBytesDescending(b, arg)
}

// unsafeString performs an unsafe conversion from a []byte to a string. The
// UnsafeString performs an unsafe conversion from a []byte to a string. The
// returned string will share the underlying memory with the []byte which thus
// allows the string to be mutable through the []byte. We're careful to use
// this method only in situations in which the []byte will not be modified.
func unsafeString(b []byte) string {
func UnsafeString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

Expand All @@ -838,15 +838,15 @@ func unsafeString(b []byte) string {
// string may share storage with the input buffer.
func DecodeUnsafeStringAscending(b []byte, r []byte) ([]byte, string, error) {
b, r, err := DecodeBytesAscending(b, r)
return b, unsafeString(r), err
return b, UnsafeString(r), err
}

// DecodeUnsafeStringAscendingDeepCopy is the same as
// DecodeUnsafeStringAscending but the returned string will never share storage
// with the input buffer.
func DecodeUnsafeStringAscendingDeepCopy(b []byte, r []byte) ([]byte, string, error) {
b, r, err := DecodeBytesAscendingDeepCopy(b, r)
return b, unsafeString(r), err
return b, UnsafeString(r), err
}

// DecodeUnsafeStringDescending decodes a string value from the input buffer which
Expand All @@ -857,7 +857,7 @@ func DecodeUnsafeStringAscendingDeepCopy(b []byte, r []byte) ([]byte, string, er
// buffer.
func DecodeUnsafeStringDescending(b []byte, r []byte) ([]byte, string, error) {
b, r, err := DecodeBytesDescending(b, r)
return b, unsafeString(r), err
return b, UnsafeString(r), err
}

// EncodeNullAscending encodes a NULL value. The encodes bytes are appended to the
Expand Down

0 comments on commit 6c958c8

Please sign in to comment.