gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/bytes.go (about)

     1  // Copyright 2018 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain 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 aquachain 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 aquachain 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  func ToHex(b []byte) string {
    23  	hex := Bytes2Hex(b)
    24  	// Prefer output of "0x0" instead of "0x"
    25  	if len(hex) == 0 {
    26  		hex = "0"
    27  	}
    28  	return "0x" + hex
    29  }
    30  
    31  func FromHex(s string) []byte {
    32  	if len(s) > 1 {
    33  		if s[0:2] == "0x" || s[0:2] == "0X" {
    34  			s = s[2:]
    35  		}
    36  	}
    37  	if len(s)%2 == 1 {
    38  		s = "0" + s
    39  	}
    40  	return Hex2Bytes(s)
    41  }
    42  
    43  // Copy bytes
    44  //
    45  // Returns an exact copy of the provided bytes
    46  func CopyBytes(b []byte) (copiedBytes []byte) {
    47  	if b == nil {
    48  		return nil
    49  	}
    50  	copiedBytes = make([]byte, len(b))
    51  	copy(copiedBytes, b)
    52  
    53  	return
    54  }
    55  
    56  func hasHexPrefix(str string) bool {
    57  	return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
    58  }
    59  
    60  func isHexCharacter(c byte) bool {
    61  	return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
    62  }
    63  
    64  func isHex(str string) bool {
    65  	if len(str)%2 != 0 {
    66  		return false
    67  	}
    68  	for _, c := range []byte(str) {
    69  		if !isHexCharacter(c) {
    70  			return false
    71  		}
    72  	}
    73  	return true
    74  }
    75  
    76  func Bytes2Hex(d []byte) string {
    77  	return hex.EncodeToString(d)
    78  }
    79  
    80  func Hex2Bytes(str string) []byte {
    81  	h, _ := hex.DecodeString(str)
    82  
    83  	return h
    84  }
    85  
    86  func Hex2BytesFixed(str string, flen int) []byte {
    87  	h, _ := hex.DecodeString(str)
    88  	if len(h) == flen {
    89  		return h
    90  	} else {
    91  		if len(h) > flen {
    92  			return h[len(h)-flen:]
    93  		} else {
    94  			hh := make([]byte, flen)
    95  			copy(hh[flen-len(h):flen], h[:])
    96  			return hh
    97  		}
    98  	}
    99  }
   100  
   101  func RightPadBytes(slice []byte, l int) []byte {
   102  	if l <= len(slice) {
   103  		return slice
   104  	}
   105  
   106  	padded := make([]byte, l)
   107  	copy(padded, slice)
   108  
   109  	return padded
   110  }
   111  
   112  func LeftPadBytes(slice []byte, l int) []byte {
   113  	if l <= len(slice) {
   114  		return slice
   115  	}
   116  
   117  	padded := make([]byte, l)
   118  	copy(padded[l-len(slice):], slice)
   119  
   120  	return padded
   121  }