better comments

This commit is contained in:
vedhavyas 2018-11-26 13:43:35 +01:00
parent da590e60ee
commit 0f9afde6d7
No known key found for this signature in database
GPG key ID: 317BF0923E3EB7E5
2 changed files with 11 additions and 8 deletions

View file

@ -254,7 +254,7 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
return nil, err return nil, err
} }
// check for dynamic types // check for dynamic types
if offsetRequired(input.Type) { if isDynamicType(input.Type) {
// set the offset // set the offset
ret = append(ret, packNum(reflect.ValueOf(inputOffset))...) ret = append(ret, packNum(reflect.ValueOf(inputOffset))...)
// calculate next offset // calculate next offset

View file

@ -194,7 +194,7 @@ func (t Type) pack(v reflect.Value) ([]byte, error) {
// calculate offset if any // calculate offset if any
offset := 0 offset := 0
offsetReq := offsetRequired(*t.Elem) offsetReq := isDynamicType(*t.Elem)
if offsetReq { if offsetReq {
offset = getDynamicTypeOffset(*t.Elem) * v.Len() offset = getDynamicTypeOffset(*t.Elem) * v.Len()
} }
@ -224,22 +224,25 @@ func (t Type) requiresLengthPrefix() bool {
return t.T == StringTy || t.T == BytesTy || t.T == SliceTy return t.T == StringTy || t.T == BytesTy || t.T == SliceTy
} }
// offsetRequired returns true if the type is considered dynamic // isDynamicType returns true if the type is dynamic.
func offsetRequired(t Type) bool { // StringTy, BytesTy, and SliceTy(irrespective of slice element type) are dynamic types
// ArrayTy is considered dynamic if and only if the Array element is a dynamic type.
// This function recursively checks the type for slice and array elements.
func isDynamicType(t Type) bool {
// dynamic types // dynamic types
// array is also a dynamic type if the array type is dynamic // array is also a dynamic type if the array type is dynamic
return t.T == StringTy || t.T == BytesTy || t.T == SliceTy || (t.T == ArrayTy && offsetRequired(*t.Elem)) return t.T == StringTy || t.T == BytesTy || t.T == SliceTy || (t.T == ArrayTy && isDynamicType(*t.Elem))
} }
// getDynamicTypeOffset returns the offset for the type. // getDynamicTypeOffset returns the offset for the type.
// See offsetRequired to know which types are considered dynamic. // See `isDynamicType` to know which types are considered dynamic.
// if the type t is an array and element type is not a dynamic type, then we consider it a static type and // If the type t is an array and element type is not a dynamic type, then we consider it a static type and
// return 32 * size of array since length prefix is not required. // return 32 * size of array since length prefix is not required.
// If t is a dynamic type or element type(for slices and arrays) is dynamic, then we simply return 32 as offset. // If t is a dynamic type or element type(for slices and arrays) is dynamic, then we simply return 32 as offset.
func getDynamicTypeOffset(t Type) int { func getDynamicTypeOffset(t Type) int {
// if it is an array and there are no dynamic types // if it is an array and there are no dynamic types
// then the array is static type // then the array is static type
if t.T == ArrayTy && !offsetRequired(*t.Elem) { if t.T == ArrayTy && !isDynamicType(*t.Elem) {
return 32 * t.Size return 32 * t.Size
} }
return 32 return 32