bump go-byteutil v1.2.0 -> v1.3.0 which has safer (as in long-term API consistency) byte <-> string conversions

This commit is contained in:
kim 2024-09-26 14:50:52 +01:00
parent f3e2d36d64
commit 8b1a1e1c3d
5 changed files with 10 additions and 78 deletions

2
go.mod
View file

@ -7,7 +7,7 @@ replace modernc.org/sqlite => gitlab.com/NyaaaWhatsUpDoc/sqlite v1.29.9-concurre
require (
codeberg.org/gruf/go-bytes v1.0.2
codeberg.org/gruf/go-bytesize v1.0.3
codeberg.org/gruf/go-byteutil v1.2.0
codeberg.org/gruf/go-byteutil v1.3.0
codeberg.org/gruf/go-cache/v3 v3.5.7
codeberg.org/gruf/go-debug v1.3.0
codeberg.org/gruf/go-errors/v2 v2.3.2

4
go.sum
View file

@ -34,8 +34,8 @@ codeberg.org/gruf/go-bytes v1.0.2 h1:malqE42Ni+h1nnYWBUAJaDDtEzF4aeN4uPN8DfMNNvo
codeberg.org/gruf/go-bytes v1.0.2/go.mod h1:1v/ibfaosfXSZtRdW2rWaVrDXMc9E3bsi/M9Ekx39cg=
codeberg.org/gruf/go-bytesize v1.0.3 h1:Tz8tCxhPLeyM5VryuBNjUHgKmLj4Bx9RbPaUSA3qg6g=
codeberg.org/gruf/go-bytesize v1.0.3/go.mod h1:n/GU8HzL9f3UNp/mUKyr1qVmTlj7+xacpp0OHfkvLPs=
codeberg.org/gruf/go-byteutil v1.2.0 h1:YoxkpUOoHS82BcPXfiIcWLe/YhS8QhpNUHdfuhN09QM=
codeberg.org/gruf/go-byteutil v1.2.0/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU=
codeberg.org/gruf/go-byteutil v1.3.0 h1:nRqJnCcRQ7xbfU6azw7zOzJrSMDIJHBqX6FL9vEMYmU=
codeberg.org/gruf/go-byteutil v1.3.0/go.mod h1:chgnZz1LUcfaObaIFglxF5MRYQkJGjQf4WwVz95ccCM=
codeberg.org/gruf/go-cache/v3 v3.5.7 h1:5hut49a8Wp3hdwrCEJYj6pHY2aRR1hyTmkK4+wHVYq4=
codeberg.org/gruf/go-cache/v3 v3.5.7/go.mod h1:Thahfuf3PgHSv2+1zHpvhRdX97tx1WXurVNGWpZucAM=
codeberg.org/gruf/go-debug v1.3.0 h1:PIRxQiWUFKtGOGZFdZ3Y0pqyfI0Xr87j224IYe2snZs=

View file

@ -1,7 +1,6 @@
package byteutil
import (
"reflect"
"unsafe"
)
@ -16,44 +15,13 @@ func Copy(b []byte) []byte {
}
// B2S returns a string representation of []byte without allocation.
//
// According to the Go spec strings are immutable and byte slices are not. The way this gets implemented is strings under the hood are:
//
// type StringHeader struct {
// Data uintptr
// Len int
// }
//
// while slices are:
//
// type SliceHeader struct {
// Data uintptr
// Len int
// Cap int
// }
//
// because being mutable, you can change the data, length etc, but the string has to promise to be read-only to all who get copies of it.
//
// So in practice when you do a conversion of `string(byteSlice)` it actually performs an allocation because it has to copy the contents of the byte slice into a safe read-only state.
//
// Being that the shared fields are in the same struct indices (no different offsets), means that if you have a byte slice you can "forcibly" cast it to a string. Which in a lot of situations can be risky, because then it means you have a string that is NOT immutable, as if someone changes the data in the originating byte slice then the string will reflect that change! Now while this does seem hacky, and it _kind_ of is, it is something that you see performed in the standard library. If you look at the definition for `strings.Builder{}.String()` you'll see this :)
// Since Go strings are immutable, the bytes passed to String must
// not be modified as long as the returned string value exists.
func B2S(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
return unsafe.String(unsafe.SliceData(b), len(b))
}
// S2B returns a []byte representation of string without allocation (minus slice header).
// See B2S() code comment, and this function's implementation for a better understanding.
// S2B returns a []byte representation of string without allocation.
func S2B(s string) []byte {
var b []byte
// Get byte + string headers
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
// Manually set bytes to string
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return b
return unsafe.Slice(unsafe.StringData(s), len(s))
}

View file

@ -1,36 +0,0 @@
package byteutil
import "bytes"
// Reader wraps a bytes.Reader{} to provide Rewind() capabilities.
type Reader struct {
B []byte
bytes.Reader
}
// NewReader returns a new Reader{} instance reset to b.
func NewReader(b []byte) *Reader {
r := &Reader{}
r.Reset(b)
return r
}
// Reset resets the Reader{} to be reading from b and sets Reader{}.B.
func (r *Reader) Reset(b []byte) {
r.B = b
r.Rewind()
}
// Rewind resets the Reader{} to be reading from the start of Reader{}.B.
func (r *Reader) Rewind() {
r.Reader.Reset(r.B)
}
// ReadNopCloser wraps a Reader{} to provide nop close method.
type ReadNopCloser struct {
Reader
}
func (*ReadNopCloser) Close() error {
return nil
}

4
vendor/modules.txt vendored
View file

@ -4,8 +4,8 @@ codeberg.org/gruf/go-bytes
# codeberg.org/gruf/go-bytesize v1.0.3
## explicit; go 1.17
codeberg.org/gruf/go-bytesize
# codeberg.org/gruf/go-byteutil v1.2.0
## explicit; go 1.16
# codeberg.org/gruf/go-byteutil v1.3.0
## explicit; go 1.20
codeberg.org/gruf/go-byteutil
# codeberg.org/gruf/go-cache/v3 v3.5.7
## explicit; go 1.19