Wip errors reporting
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package errors
|
||||
package unsafe
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -8,12 +8,10 @@ import (
|
||||
|
||||
const maxInt = uintptr(int(^uint(0) >> 1))
|
||||
|
||||
|
||||
func UnsafeSubsliceOffset(data []byte, subslice []byte) int {
|
||||
func SubsliceOffset(data []byte, subslice []byte) int {
|
||||
datap := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
||||
hlp := (*reflect.SliceHeader)(unsafe.Pointer(&subslice))
|
||||
|
||||
|
||||
if hlp.Data < datap.Data {
|
||||
panic(fmt.Errorf("subslice address (%d) is before data address (%d)", hlp.Data, datap.Data))
|
||||
}
|
||||
@@ -29,7 +27,7 @@ func UnsafeSubsliceOffset(data []byte, subslice []byte) int {
|
||||
panic(fmt.Errorf("slice offset (%d) is farther than data length (%d)", intoffset, datap.Len))
|
||||
}
|
||||
|
||||
if intoffset + hlp.Len > datap.Len {
|
||||
if intoffset+hlp.Len > datap.Len {
|
||||
panic(fmt.Errorf("slice ends (%d+%d) is farther than data length (%d)", intoffset, hlp.Len, datap.Len))
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package errors_test
|
||||
package unsafe_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -6,13 +6,13 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/pelletier/go-toml/v2/internal/errors"
|
||||
"github.com/pelletier/go-toml/v2/internal/unsafe"
|
||||
)
|
||||
|
||||
func TestUnsafeSubsliceOffsetValid(t *testing.T) {
|
||||
examples := []struct{
|
||||
desc string
|
||||
test func() ([]byte, []byte)
|
||||
examples := []struct {
|
||||
desc string
|
||||
test func() ([]byte, []byte)
|
||||
offset int
|
||||
}{
|
||||
{
|
||||
@@ -28,14 +28,14 @@ func TestUnsafeSubsliceOffsetValid(t *testing.T) {
|
||||
for _, e := range examples {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
d, s := e.test()
|
||||
offset := errors.UnsafeSubsliceOffset(d, s)
|
||||
offset := unsafe.SubsliceOffset(d, s)
|
||||
assert.Equal(t, e.offset, offset)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsafeSubsliceOffsetInvalid(t *testing.T) {
|
||||
examples := []struct{
|
||||
examples := []struct {
|
||||
desc string
|
||||
test func() ([]byte, []byte)
|
||||
}{
|
||||
@@ -72,7 +72,7 @@ func TestUnsafeSubsliceOffsetInvalid(t *testing.T) {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
d, s := e.test()
|
||||
require.Panics(t, func() {
|
||||
errors.UnsafeSubsliceOffset(d, s)
|
||||
unsafe.SubsliceOffset(d, s)
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user