github.com/ethereum/go-ethereum@v1.14.3/common/math/integer.go (about)

     1  // Copyright 2017 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 math
    18  
    19  import (
    20  	"fmt"
    21  	"math/bits"
    22  	"strconv"
    23  )
    24  
    25  // Integer limit values.
    26  const (
    27  	MaxInt8   = 1<<7 - 1
    28  	MinInt8   = -1 << 7
    29  	MaxInt16  = 1<<15 - 1
    30  	MinInt16  = -1 << 15
    31  	MaxInt32  = 1<<31 - 1
    32  	MinInt32  = -1 << 31
    33  	MaxInt64  = 1<<63 - 1
    34  	MinInt64  = -1 << 63
    35  	MaxUint8  = 1<<8 - 1
    36  	MaxUint16 = 1<<16 - 1
    37  	MaxUint32 = 1<<32 - 1
    38  	MaxUint64 = 1<<64 - 1
    39  )
    40  
    41  // HexOrDecimal64 marshals uint64 as hex or decimal.
    42  type HexOrDecimal64 uint64
    43  
    44  // UnmarshalJSON implements json.Unmarshaler.
    45  //
    46  // It is similar to UnmarshalText, but allows parsing real decimals too, not just
    47  // quoted decimal strings.
    48  func (i *HexOrDecimal64) UnmarshalJSON(input []byte) error {
    49  	if len(input) > 0 && input[0] == '"' {
    50  		input = input[1 : len(input)-1]
    51  	}
    52  	return i.UnmarshalText(input)
    53  }
    54  
    55  // UnmarshalText implements encoding.TextUnmarshaler.
    56  func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
    57  	int, ok := ParseUint64(string(input))
    58  	if !ok {
    59  		return fmt.Errorf("invalid hex or decimal integer %q", input)
    60  	}
    61  	*i = HexOrDecimal64(int)
    62  	return nil
    63  }
    64  
    65  // MarshalText implements encoding.TextMarshaler.
    66  func (i HexOrDecimal64) MarshalText() ([]byte, error) {
    67  	return []byte(fmt.Sprintf("%#x", uint64(i))), nil
    68  }
    69  
    70  // ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
    71  // Leading zeros are accepted. The empty string parses as zero.
    72  func ParseUint64(s string) (uint64, bool) {
    73  	if s == "" {
    74  		return 0, true
    75  	}
    76  	if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
    77  		v, err := strconv.ParseUint(s[2:], 16, 64)
    78  		return v, err == nil
    79  	}
    80  	v, err := strconv.ParseUint(s, 10, 64)
    81  	return v, err == nil
    82  }
    83  
    84  // MustParseUint64 parses s as an integer and panics if the string is invalid.
    85  func MustParseUint64(s string) uint64 {
    86  	v, ok := ParseUint64(s)
    87  	if !ok {
    88  		panic("invalid unsigned 64 bit integer: " + s)
    89  	}
    90  	return v
    91  }
    92  
    93  // SafeSub returns x-y and checks for overflow.
    94  func SafeSub(x, y uint64) (uint64, bool) {
    95  	diff, borrowOut := bits.Sub64(x, y, 0)
    96  	return diff, borrowOut != 0
    97  }
    98  
    99  // SafeAdd returns x+y and checks for overflow.
   100  func SafeAdd(x, y uint64) (uint64, bool) {
   101  	sum, carryOut := bits.Add64(x, y, 0)
   102  	return sum, carryOut != 0
   103  }
   104  
   105  // SafeMul returns x*y and checks for overflow.
   106  func SafeMul(x, y uint64) (uint64, bool) {
   107  	hi, lo := bits.Mul64(x, y)
   108  	return lo, hi != 0
   109  }