mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-02 09:45:50 +00:00
stackitem: make ByteArray
an alias to []byte
Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
4f98ec2f53
commit
1dfef4ba26
2 changed files with 26 additions and 33 deletions
|
@ -101,13 +101,9 @@ func Make(v interface{}) Item {
|
||||||
case uint64:
|
case uint64:
|
||||||
return (*BigInteger)(new(big.Int).SetUint64(val))
|
return (*BigInteger)(new(big.Int).SetUint64(val))
|
||||||
case []byte:
|
case []byte:
|
||||||
return &ByteArray{
|
return NewByteArray(val)
|
||||||
value: val,
|
|
||||||
}
|
|
||||||
case string:
|
case string:
|
||||||
return &ByteArray{
|
return NewByteArray([]byte(val))
|
||||||
value: []byte(val),
|
|
||||||
}
|
|
||||||
case bool:
|
case bool:
|
||||||
return Bool(val)
|
return Bool(val)
|
||||||
case []Item:
|
case []Item:
|
||||||
|
@ -538,25 +534,21 @@ func (i Bool) Convert(typ Type) (Item, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ByteArray represents a byte array on the stack.
|
// ByteArray represents a byte array on the stack.
|
||||||
type ByteArray struct {
|
type ByteArray []byte
|
||||||
value []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewByteArray returns an new ByteArray object.
|
// NewByteArray returns an new ByteArray object.
|
||||||
func NewByteArray(b []byte) *ByteArray {
|
func NewByteArray(b []byte) *ByteArray {
|
||||||
return &ByteArray{
|
return (*ByteArray)(&b)
|
||||||
value: b,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value implements Item interface.
|
// Value implements Item interface.
|
||||||
func (i *ByteArray) Value() interface{} {
|
func (i *ByteArray) Value() interface{} {
|
||||||
return i.value
|
return []byte(*i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface.
|
// MarshalJSON implements the json.Marshaler interface.
|
||||||
func (i *ByteArray) MarshalJSON() ([]byte, error) {
|
func (i *ByteArray) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(hex.EncodeToString(i.value))
|
return json.Marshal(hex.EncodeToString(*i))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *ByteArray) String() string {
|
func (i *ByteArray) String() string {
|
||||||
|
@ -565,10 +557,10 @@ func (i *ByteArray) String() string {
|
||||||
|
|
||||||
// TryBool implements Item interface.
|
// TryBool implements Item interface.
|
||||||
func (i *ByteArray) TryBool() (bool, error) {
|
func (i *ByteArray) TryBool() (bool, error) {
|
||||||
if len(i.value) > MaxBigIntegerSizeBits/8 {
|
if len(*i) > MaxBigIntegerSizeBits/8 {
|
||||||
return false, errTooBigInteger
|
return false, errTooBigInteger
|
||||||
}
|
}
|
||||||
for _, b := range i.value {
|
for _, b := range *i {
|
||||||
if b != 0 {
|
if b != 0 {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
@ -577,21 +569,21 @@ func (i *ByteArray) TryBool() (bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryBytes implements Item interface.
|
// TryBytes implements Item interface.
|
||||||
func (i *ByteArray) TryBytes() ([]byte, error) {
|
func (i ByteArray) TryBytes() ([]byte, error) {
|
||||||
return i.value, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryInteger implements Item interface.
|
// TryInteger implements Item interface.
|
||||||
func (i *ByteArray) TryInteger() (*big.Int, error) {
|
func (i ByteArray) TryInteger() (*big.Int, error) {
|
||||||
if len(i.value) > MaxBigIntegerSizeBits/8 {
|
if len(i) > MaxBigIntegerSizeBits/8 {
|
||||||
return nil, errTooBigInteger
|
return nil, errTooBigInteger
|
||||||
}
|
}
|
||||||
return bigint.FromBytes(i.value), nil
|
return bigint.FromBytes(i), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equals implements Item interface.
|
// Equals implements Item interface.
|
||||||
func (i *ByteArray) Equals(s Item) bool {
|
func (i *ByteArray) Equals(s Item) bool {
|
||||||
if len(i.value) > MaxByteArrayComparableSize {
|
if len(*i) > MaxByteArrayComparableSize {
|
||||||
panic(errTooBigComparable)
|
panic(errTooBigComparable)
|
||||||
}
|
}
|
||||||
if i == s {
|
if i == s {
|
||||||
|
@ -603,15 +595,16 @@ func (i *ByteArray) Equals(s Item) bool {
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if len(val.value) > MaxByteArrayComparableSize {
|
if len(*val) > MaxByteArrayComparableSize {
|
||||||
panic(errTooBigComparable)
|
panic(errTooBigComparable)
|
||||||
}
|
}
|
||||||
return bytes.Equal(i.value, val.value)
|
return bytes.Equal(*i, *val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dup implements Item interface.
|
// Dup implements Item interface.
|
||||||
func (i *ByteArray) Dup() Item {
|
func (i *ByteArray) Dup() Item {
|
||||||
return &ByteArray{slice.Copy(i.value)}
|
ba := slice.Copy(*i)
|
||||||
|
return (*ByteArray)(&ba)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type implements Item interface.
|
// Type implements Item interface.
|
||||||
|
@ -1146,7 +1139,7 @@ func deepCopy(item Item, seen map[Item]Item) Item {
|
||||||
bi := new(big.Int).Set(it.Big())
|
bi := new(big.Int).Set(it.Big())
|
||||||
return (*BigInteger)(bi)
|
return (*BigInteger)(bi)
|
||||||
case *ByteArray:
|
case *ByteArray:
|
||||||
return NewByteArray(slice.Copy(it.value))
|
return NewByteArray(slice.Copy(*it))
|
||||||
case *Buffer:
|
case *Buffer:
|
||||||
return NewBuffer(slice.Copy(it.value))
|
return NewBuffer(slice.Copy(it.value))
|
||||||
case Bool:
|
case Bool:
|
||||||
|
|
|
@ -47,19 +47,19 @@ var makeStackItemTestCases = []struct {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: []byte{1, 2, 3, 4},
|
input: []byte{1, 2, 3, 4},
|
||||||
result: &ByteArray{value: []byte{1, 2, 3, 4}},
|
result: NewByteArray([]byte{1, 2, 3, 4}),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: []byte{},
|
input: []byte{},
|
||||||
result: &ByteArray{value: []byte{}},
|
result: NewByteArray([]byte{}),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "bla",
|
input: "bla",
|
||||||
result: &ByteArray{value: []byte("bla")},
|
result: NewByteArray([]byte("bla")),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "",
|
input: "",
|
||||||
result: &ByteArray{value: []byte{}},
|
result: NewByteArray([]byte{}),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: true,
|
input: true,
|
||||||
|
@ -70,8 +70,8 @@ var makeStackItemTestCases = []struct {
|
||||||
result: Bool(false),
|
result: Bool(false),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: []Item{(*BigInteger)(big.NewInt(3)), &ByteArray{value: []byte{1, 2, 3}}},
|
input: []Item{(*BigInteger)(big.NewInt(3)), NewByteArray([]byte{1, 2, 3})},
|
||||||
result: &Array{value: []Item{(*BigInteger)(big.NewInt(3)), &ByteArray{value: []byte{1, 2, 3}}}},
|
result: &Array{value: []Item{(*BigInteger)(big.NewInt(3)), NewByteArray([]byte{1, 2, 3})}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: []int{1, 2, 3},
|
input: []int{1, 2, 3},
|
||||||
|
@ -441,7 +441,7 @@ var marshalJSONTestCases = []struct {
|
||||||
result: []byte(`"010203"`),
|
result: []byte(`"010203"`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: &Array{value: []Item{(*BigInteger)(big.NewInt(3)), &ByteArray{value: []byte{1, 2, 3}}}},
|
input: &Array{value: []Item{(*BigInteger)(big.NewInt(3)), NewByteArray([]byte{1, 2, 3})}},
|
||||||
result: []byte(`[3,"010203"]`),
|
result: []byte(`[3,"010203"]`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue