github.com/aergoio/aergo@v1.3.1/internal/common/bytes.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/gob"
     6  )
     7  
     8  // IsZero returns true if argument is empty or zero
     9  func IsZero(argv []byte) bool {
    10  	if len(argv) == 0 {
    11  		return true
    12  	}
    13  	for i := range argv {
    14  		if argv[i] != 0x00 {
    15  			return false
    16  		}
    17  	}
    18  	return true
    19  }
    20  
    21  // Compactz returns nil if argument is empty or zero
    22  func Compactz(argv []byte) []byte {
    23  	if IsZero(argv) {
    24  		return nil
    25  	}
    26  	return argv
    27  }
    28  
    29  // GobEncode encodes e by using gob and returns.
    30  func GobEncode(e interface{}) ([]byte, error) {
    31  	var buf bytes.Buffer
    32  	err := gob.NewEncoder(&buf).Encode(e)
    33  
    34  	return buf.Bytes(), err
    35  }
    36  
    37  // GobDecode decodes a gob-encoded value v.
    38  func GobDecode(v []byte, e interface{}) error {
    39  	dec := gob.NewDecoder(bytes.NewBuffer(v))
    40  	return dec.Decode(e)
    41  }