github.com/XinFinOrg/xdcchain@v1.1.0/common/bytes.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum 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 len(s) > 1 {
    47  		if s[0:2] == "0x" || s[0:2] == "0X" {
    48  			s = s[2:]
    49  		}
    50  		if (s[0] == 'x' || s[0] == 'X') && (s[1] == 'd' || s[1] == 'D') && (s[2] == 'c' || s[2] == 'C') {
    51  			s = s[3:]
    52  		}
    53  	}
    54  	if len(s)%2 == 1 {
    55  		s = "0" + s
    56  	}
    57  	return Hex2Bytes(s)
    58  }
    59  
    60  // CopyBytes returns an exact copy of the provided bytes.
    61  func CopyBytes(b []byte) (copiedBytes []byte) {
    62  	if b == nil {
    63  		return nil
    64  	}
    65  	copiedBytes = make([]byte, len(b))
    66  	copy(copiedBytes, b)
    67  
    68  	return
    69  }
    70  
    71  func hasXDCPrefix(str string) bool {
    72  	return len(str) >= 3 && (str[0] == 'x' || str[0] == 'X') && (str[1] == 'd' || str[1] == 'D') && (str[2] == 'c' || str[2] == 'C')
    73  }
    74  
    75  // hasHexPrefix validates str begins with '0x' or '0X'.
    76  func hasHexPrefix(str string) bool {
    77  	return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
    78  }
    79  
    80  // isHexCharacter returns bool of c being a valid hexadecimal.
    81  func isHexCharacter(c byte) bool {
    82  	return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
    83  }
    84  
    85  // isHex validates whether each byte is valid hexadecimal string.
    86  func isHex(str string) bool {
    87  	if len(str)%2 != 0 {
    88  		return false
    89  	}
    90  	for _, c := range []byte(str) {
    91  		if !isHexCharacter(c) {
    92  			return false
    93  		}
    94  	}
    95  	return true
    96  }
    97  
    98  // Bytes2Hex returns the hexadecimal encoding of d.
    99  func Bytes2Hex(d []byte) string {
   100  	return hex.EncodeToString(d)
   101  }
   102  
   103  // Hex2Bytes returns the bytes represented by the hexadecimal string str.
   104  func Hex2Bytes(str string) []byte {
   105  	h, _ := hex.DecodeString(str)
   106  	return h
   107  }
   108  
   109  // Hex2BytesFixed returns bytes of a specified fixed length flen.
   110  func Hex2BytesFixed(str string, flen int) []byte {
   111  	h, _ := hex.DecodeString(str)
   112  	if len(h) == flen {
   113  		return h
   114  	}
   115  	if len(h) > flen {
   116  		return h[len(h)-flen:]
   117  	}
   118  	hh := make([]byte, flen)
   119  	copy(hh[flen-len(h):flen], h)
   120  	return hh
   121  }
   122  
   123  // RightPadBytes zero-pads slice to the right up to length l.
   124  func RightPadBytes(slice []byte, l int) []byte {
   125  	if l <= len(slice) {
   126  		return slice
   127  	}
   128  
   129  	padded := make([]byte, l)
   130  	copy(padded, slice)
   131  
   132  	return padded
   133  }
   134  
   135  // LeftPadBytes zero-pads slice to the left up to length l.
   136  func LeftPadBytes(slice []byte, l int) []byte {
   137  	if l <= len(slice) {
   138  		return slice
   139  	}
   140  
   141  	padded := make([]byte, l)
   142  	copy(padded[l-len(slice):], slice)
   143  
   144  	return padded
   145  }