github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/common/types_template.go (about)

     1  //go:build none
     2  // +build none
     3  
     4  //sed -e 's/_N_/Hash/g' -e 's/_S_/32/g' -e '1d' types_template.go | gofmt -w hash.go
     5  
     6  package common
     7  
     8  import "math/big"
     9  
    10  type _N_ [_S_]byte
    11  
    12  func BytesTo_N_(b []byte) _N_ {
    13  	var h _N_
    14  	h.SetBytes(b)
    15  	return h
    16  }
    17  func StringTo_N_(s string) _N_ { return BytesTo_N_([]byte(s)) }
    18  func BigTo_N_(b *big.Int) _N_  { return BytesTo_N_(b.Bytes()) }
    19  func HexTo_N_(s string) _N_    { return BytesTo_N_(FromHex(s)) }
    20  
    21  func (h _N_) Str() string   { return string(h[:]) }
    22  func (h _N_) Bytes() []byte { return h[:] }
    23  func (h _N_) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
    24  func (h _N_) Hex() string   { return "0x" + Bytes2Hex(h[:]) }
    25  
    26  func (h *_N_) SetBytes(b []byte) {
    27  	if len(b) > len(h) {
    28  		b = b[len(b)-_S_:]
    29  	}
    30  
    31  	for i := len(b) - 1; i >= 0; i-- {
    32  		h[_S_-len(b)+i] = b[i]
    33  	}
    34  }
    35  
    36  func (h *_N_) SetString(s string) { h.SetBytes([]byte(s)) }
    37  
    38  func (h *_N_) Set(other _N_) {
    39  	for i, v := range other {
    40  		h[i] = v
    41  	}
    42  }