github.com/ethereum/go-ethereum@v1.16.1/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  // HexOrDecimal64 marshals uint64 as hex or decimal.
    26  type HexOrDecimal64 uint64
    27  
    28  // UnmarshalJSON implements json.Unmarshaler.
    29  //
    30  // It is similar to UnmarshalText, but allows parsing real decimals too, not just
    31  // quoted decimal strings.
    32  func (i *HexOrDecimal64) UnmarshalJSON(input []byte) error {
    33  	if len(input) > 1 && input[0] == '"' {
    34  		input = input[1 : len(input)-1]
    35  	}
    36  	return i.UnmarshalText(input)
    37  }
    38  
    39  // UnmarshalText implements encoding.TextUnmarshaler.
    40  func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
    41  	n, ok := ParseUint64(string(input))
    42  	if !ok {
    43  		return fmt.Errorf("invalid hex or decimal integer %q", input)
    44  	}
    45  	*i = HexOrDecimal64(n)
    46  	return nil
    47  }
    48  
    49  // MarshalText implements encoding.TextMarshaler.
    50  func (i HexOrDecimal64) MarshalText() ([]byte, error) {
    51  	return fmt.Appendf(nil, "%#x", uint64(i)), nil
    52  }
    53  
    54  // ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
    55  // Leading zeros are accepted. The empty string parses as zero.
    56  func ParseUint64(s string) (uint64, bool) {
    57  	if s == "" {
    58  		return 0, true
    59  	}
    60  	if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
    61  		v, err := strconv.ParseUint(s[2:], 16, 64)
    62  		return v, err == nil
    63  	}
    64  	v, err := strconv.ParseUint(s, 10, 64)
    65  	return v, err == nil
    66  }
    67  
    68  // MustParseUint64 parses s as an integer and panics if the string is invalid.
    69  func MustParseUint64(s string) uint64 {
    70  	v, ok := ParseUint64(s)
    71  	if !ok {
    72  		panic("invalid unsigned 64 bit integer: " + s)
    73  	}
    74  	return v
    75  }
    76  
    77  // SafeSub returns x-y and checks for overflow.
    78  func SafeSub(x, y uint64) (uint64, bool) {
    79  	diff, borrowOut := bits.Sub64(x, y, 0)
    80  	return diff, borrowOut != 0
    81  }
    82  
    83  // SafeAdd returns x+y and checks for overflow.
    84  func SafeAdd(x, y uint64) (uint64, bool) {
    85  	sum, carryOut := bits.Add64(x, y, 0)
    86  	return sum, carryOut != 0
    87  }
    88  
    89  // SafeMul returns x*y and checks for overflow.
    90  func SafeMul(x, y uint64) (uint64, bool) {
    91  	hi, lo := bits.Mul64(x, y)
    92  	return lo, hi != 0
    93  }