github.com/amazechain/amc@v0.1.3/common/types/bytes.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  )
    23  
    24  // FromHex returns the bytes represented by the hexadecimal string s.
    25  // s may be prefixed with "0x".
    26  func FromHex1(s string) []byte {
    27  	if has0xPrefix(s) {
    28  		s = s[2:]
    29  	}
    30  	if len(s)%2 == 1 {
    31  		s = "0" + s
    32  	}
    33  	return Hex2Bytes(s)
    34  }
    35  
    36  // has0xPrefix validates str begins with '0x' or '0X'.
    37  func has0xPrefix(str string) bool {
    38  	return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
    39  }
    40  
    41  // Hex2Bytes returns the bytes represented by the hexadecimal string str.
    42  func Hex2Bytes(str string) []byte {
    43  	h, _ := hex.DecodeString(str)
    44  	return h
    45  }
    46  
    47  func KeyCmp(key1, key2 []byte) (int, bool) {
    48  	switch {
    49  	//both keys are empty
    50  	case len(key1) == 0 && len(key2) == 0:
    51  		return 0, true
    52  	//	key1 is empty
    53  	case len(key1) == 0 && len(key2) != 0:
    54  		return 1, false
    55  	//	key2 is empty
    56  	case len(key1) != 0 && len(key2) == 0:
    57  		return -1, false
    58  	default:
    59  		return bytes.Compare(key1, key2), false
    60  	}
    61  }
    62  
    63  // CopyBytes returns an exact copy of the provided bytes.
    64  func CopyBytes(b []byte) (copiedBytes []byte) {
    65  	if b == nil {
    66  		return nil
    67  	}
    68  	copiedBytes = make([]byte, len(b))
    69  	copy(copiedBytes, b)
    70  
    71  	return
    72  }
    73  
    74  // RightPadBytes zero-pads slice to the right up to length l.
    75  func RightPadBytes(slice []byte, l int) []byte {
    76  	if l <= len(slice) {
    77  		return slice
    78  	}
    79  
    80  	padded := make([]byte, l)
    81  	copy(padded, slice)
    82  
    83  	return padded
    84  }
    85  
    86  // LeftPadBytes zero-pads slice to the left up to length l.
    87  func LeftPadBytes(slice []byte, l int) []byte {
    88  	if l <= len(slice) {
    89  		return slice
    90  	}
    91  
    92  	padded := make([]byte, l)
    93  	copy(padded[l-len(slice):], slice)
    94  
    95  	return padded
    96  }
    97  
    98  // TrimLeftZeroes returns a subslice of s without leading zeroes
    99  func TrimLeftZeroes(s []byte) []byte {
   100  	idx := 0
   101  	for ; idx < len(s); idx++ {
   102  		if s[idx] != 0 {
   103  			break
   104  		}
   105  	}
   106  	return s[idx:]
   107  }
   108  
   109  // TrimRightZeroes returns a subslice of s without trailing zeroes
   110  func TrimRightZeroes(s []byte) []byte {
   111  	idx := len(s)
   112  	for ; idx > 0; idx-- {
   113  		if s[idx-1] != 0 {
   114  			break
   115  		}
   116  	}
   117  	return s[:idx]
   118  }