github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/bytes.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // Package common contains various helper functions.
    19  package common
    20  
    21  import "encoding/hex"
    22  
    23  // ToHex returns the hex representation of b, prefixed with '0x'.
    24  // For empty slices, the return value is "0x0".
    25  //
    26  // Deprecated: use hexutil.Encode instead.
    27  func ToHex(b []byte) string {
    28  	hex := Bytes2Hex(b)
    29  	if len(hex) == 0 {
    30  		hex = "0"
    31  	}
    32  	return "0x" + hex
    33  }
    34  
    35  // ToHexArray creates a array of hex-string based on []byte
    36  func ToHexArray(b [][]byte) []string {
    37  	r := make([]string, len(b))
    38  	for i := range b {
    39  		r[i] = ToHex(b[i])
    40  	}
    41  	return r
    42  }
    43  
    44  // FromHex returns the bytes represented by the hexadecimal string s.
    45  // s may be prefixed with "0x".
    46  func FromHex(s string) []byte {
    47  	if has0xPrefix(s) {
    48  		s = s[2:]
    49  	}
    50  	if len(s)%2 == 1 {
    51  		s = "0" + s
    52  	}
    53  	return Hex2Bytes(s)
    54  }
    55  
    56  // CopyBytes returns an exact copy of the provided bytes.
    57  func CopyBytes(b []byte) (copiedBytes []byte) {
    58  	if b == nil {
    59  		return nil
    60  	}
    61  	copiedBytes = make([]byte, len(b))
    62  	copy(copiedBytes, b)
    63  
    64  	return
    65  }
    66  
    67  // has0xPrefix validates str begins with '0x' or '0X'.
    68  func has0xPrefix(str string) bool {
    69  	return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
    70  }
    71  
    72  // isHexCharacter returns bool of c being a valid hexadecimal.
    73  func isHexCharacter(c byte) bool {
    74  	return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
    75  }
    76  
    77  // isHex validates whether each byte is valid hexadecimal string.
    78  func isHex(str string) bool {
    79  	if len(str)%2 != 0 {
    80  		return false
    81  	}
    82  	for _, c := range []byte(str) {
    83  		if !isHexCharacter(c) {
    84  			return false
    85  		}
    86  	}
    87  	return true
    88  }
    89  
    90  // Bytes2Hex returns the hexadecimal encoding of d.
    91  func Bytes2Hex(d []byte) string {
    92  	return hex.EncodeToString(d)
    93  }
    94  
    95  // Hex2Bytes returns the bytes represented by the hexadecimal string str.
    96  func Hex2Bytes(str string) []byte {
    97  	h, _ := hex.DecodeString(str)
    98  	return h
    99  }
   100  
   101  // Hex2BytesFixed returns bytes of a specified fixed length flen.
   102  func Hex2BytesFixed(str string, flen int) []byte {
   103  	h, _ := hex.DecodeString(str)
   104  	if len(h) == flen {
   105  		return h
   106  	}
   107  	if len(h) > flen {
   108  		return h[len(h)-flen:]
   109  	}
   110  	hh := make([]byte, flen)
   111  	copy(hh[flen-len(h):flen], h)
   112  	return hh
   113  }
   114  
   115  // RightPadBytes zero-pads slice to the right up to length l.
   116  func RightPadBytes(slice []byte, l int) []byte {
   117  	if l <= len(slice) {
   118  		return slice
   119  	}
   120  
   121  	padded := make([]byte, l)
   122  	copy(padded, slice)
   123  
   124  	return padded
   125  }
   126  
   127  // LeftPadBytes zero-pads slice to the left up to length l.
   128  func LeftPadBytes(slice []byte, l int) []byte {
   129  	if l <= len(slice) {
   130  		return slice
   131  	}
   132  
   133  	padded := make([]byte, l)
   134  	copy(padded[l-len(slice):], slice)
   135  
   136  	return padded
   137  }
   138  
   139  // TrimLeftZeroes returns a subslice of s without leading zeroes
   140  func TrimLeftZeroes(s []byte) []byte {
   141  	idx := 0
   142  	for ; idx < len(s); idx++ {
   143  		if s[idx] != 0 {
   144  			break
   145  		}
   146  	}
   147  	return s[idx:]
   148  }