mirror of
https://github.com/fumiama/unibase2n.git
synced 2026-06-28 07:50:31 +08:00
完善 pack
This commit is contained in:
2
base.go
2
base.go
@@ -42,7 +42,7 @@ func NewBase(off, til uint16, bit uint8) (*Base, error) {
|
|||||||
if tile > 0x10000 {
|
if tile > 0x10000 {
|
||||||
return nil, ErrTailOverflow
|
return nil, ErrTailOverflow
|
||||||
}
|
}
|
||||||
if tile > uint32(off) || uint32(til) < offe {
|
if tile > uint32(off) && tile <= offe {
|
||||||
return nil, ErrTailInCodingArea
|
return nil, ErrTailInCodingArea
|
||||||
}
|
}
|
||||||
return &Base{
|
return &Base{
|
||||||
|
|||||||
11
go.mod
11
go.mod
@@ -2,4 +2,13 @@ module github.com/fumiama/unibase2n
|
|||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require golang.org/x/text v0.3.7
|
require (
|
||||||
|
github.com/stretchr/testify v1.8.0
|
||||||
|
golang.org/x/text v0.3.7
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
|
|||||||
15
go.sum
15
go.sum
@@ -1,2 +1,17 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
5
pack.go
5
pack.go
@@ -2,7 +2,6 @@ package unibase2n
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"math/bits"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,9 +48,7 @@ func New(pack Pack) *Base {
|
|||||||
*(*Pack)(unsafe.Pointer(b)) = pack
|
*(*Pack)(unsafe.Pointer(b)) = pack
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
// change to native endian
|
field := (*[8]byte)(unsafe.Pointer(&pack))
|
||||||
n := bits.Reverse64(uint64(pack))
|
|
||||||
field := (*[8]byte)(unsafe.Pointer(&n))
|
|
||||||
if isitle { // packed in little endian but I am big
|
if isitle { // packed in little endian but I am big
|
||||||
b.off = binary.BigEndian.Uint16(field[6:8])
|
b.off = binary.BigEndian.Uint16(field[6:8])
|
||||||
b.til = binary.BigEndian.Uint16(field[4:6])
|
b.til = binary.BigEndian.Uint16(field[4:6])
|
||||||
|
|||||||
26
pack_test.go
Normal file
26
pack_test.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package unibase2n
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPackUnpack(t *testing.T) {
|
||||||
|
bs, err := NewBase(0x1234, 0x5678, 8)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
p := bs.Pack()
|
||||||
|
bs1 := New(p)
|
||||||
|
assert.Equal(t, bs, bs1)
|
||||||
|
ismele := isLittleEndian()
|
||||||
|
if ismele {
|
||||||
|
// simulate be pack -> le unpack
|
||||||
|
bs2 := New(0x1234567808000000)
|
||||||
|
assert.Equal(t, bs, bs2)
|
||||||
|
} else { // simulate le pack -> be unpack
|
||||||
|
bs2 := New(0x0000000878563412)
|
||||||
|
assert.Equal(t, bs, bs2)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user