github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/common/bytes.go (about)

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