From 6dd37dd07625053b8da00c0acd88c2c394f6517e Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 20 Sep 2019 19:08:58 +0300 Subject: [PATCH 1/3] io: unexport getVarIntSize and getVarStringSize All external users should just use GetVarSize(). --- pkg/io/size.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/io/size.go b/pkg/io/size.go index ea8bcc562..594d6bb0a 100644 --- a/pkg/io/size.go +++ b/pkg/io/size.go @@ -31,9 +31,9 @@ func (cw *counterWriter) Write(p []byte) (int, error) { return n, nil } -// GetVarIntSize returns the size in number of bytes of a variable integer +// getVarIntSize returns the size in number of bytes of a variable integer // (reference: GetVarSize(int value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs) -func GetVarIntSize(value int) int { +func getVarIntSize(value int) int { var size uintptr if value < 0xFD { @@ -46,11 +46,11 @@ func GetVarIntSize(value int) int { return int(size) } -// GetVarStringSize returns the size of a variable string +// getVarStringSize returns the size of a variable string // (reference: GetVarSize(this string value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs) -func GetVarStringSize(value string) int { +func getVarStringSize(value string) int { valueSize := len([]byte(value)) - return GetVarIntSize(valueSize) + valueSize + return getVarIntSize(valueSize) + valueSize } // GetVarSize return the size om bytes of a variable. This implementation is not exactly like the C# @@ -60,19 +60,19 @@ func GetVarSize(value interface{}) int { v := reflect.ValueOf(value) switch v.Kind() { case reflect.String: - return GetVarStringSize(v.String()) + return getVarStringSize(v.String()) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return GetVarIntSize(int(v.Int())) + return getVarIntSize(int(v.Int())) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return GetVarIntSize(int(v.Uint())) + return getVarIntSize(int(v.Uint())) case reflect.Ptr: vser, ok := v.Interface().(Serializable) if !ok { @@ -106,7 +106,7 @@ func GetVarSize(value interface{}) int { } } - return GetVarIntSize(valueLength) + valueSize + return getVarIntSize(valueLength) + valueSize default: panic(fmt.Sprintf("unable to calculate GetVarSize, %s", reflect.TypeOf(value))) } From d01b7740e2de1e7afece5b901d2c16cba724a8ef Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 20 Sep 2019 19:10:13 +0300 Subject: [PATCH 2/3] io: improve GetVarSize() documentation --- pkg/io/size.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/io/size.go b/pkg/io/size.go index 594d6bb0a..7b062f6a4 100644 --- a/pkg/io/size.go +++ b/pkg/io/size.go @@ -53,9 +53,10 @@ func getVarStringSize(value string) int { return getVarIntSize(valueSize) + valueSize } -// GetVarSize return the size om bytes of a variable. This implementation is not exactly like the C# -// (reference: GetVarSize(this T[] value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs#L53) as in the C# variable -// like Uint160, Uint256 are not supported. +// GetVarSize returns the number of bytes in a serialized variable. It supports ints/uints (estimating +// them using variable-length encoding that is used in NEO), strings, pointers to Serializable structures, +// slices and arrays of ints/uints or Serializable structures. It's similar to GetVarSize(this T[] value) +// used in C#, but differs in that it also supports things like Uint160 or Uint256. func GetVarSize(value interface{}) int { v := reflect.ValueOf(value) switch v.Kind() { From f69adc59ad2a960f236bf7177ee9d493890ca623 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 20 Sep 2019 20:01:56 +0300 Subject: [PATCH 3/3] io: drop getVarStringSize() completely After unexport this doesn't make much sense at all as it has just one user. --- pkg/io/size.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/io/size.go b/pkg/io/size.go index 7b062f6a4..14bda390c 100644 --- a/pkg/io/size.go +++ b/pkg/io/size.go @@ -46,13 +46,6 @@ func getVarIntSize(value int) int { return int(size) } -// getVarStringSize returns the size of a variable string -// (reference: GetVarSize(this string value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs) -func getVarStringSize(value string) int { - valueSize := len([]byte(value)) - return getVarIntSize(valueSize) + valueSize -} - // GetVarSize returns the number of bytes in a serialized variable. It supports ints/uints (estimating // them using variable-length encoding that is used in NEO), strings, pointers to Serializable structures, // slices and arrays of ints/uints or Serializable structures. It's similar to GetVarSize(this T[] value) @@ -61,7 +54,8 @@ func GetVarSize(value interface{}) int { v := reflect.ValueOf(value) switch v.Kind() { case reflect.String: - return getVarStringSize(v.String()) + valueSize := len([]byte(v.String())) + return getVarIntSize(valueSize) + valueSize case reflect.Int, reflect.Int8, reflect.Int16,