github.com/klaytn/klaytn@v1.12.1/common/math/integer.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from common/math/integer.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package math
    22  
    23  import (
    24  	"fmt"
    25  	"strconv"
    26  )
    27  
    28  const (
    29  	// Integer limit values.
    30  	MaxInt8   = 1<<7 - 1
    31  	MinInt8   = -1 << 7
    32  	MaxInt16  = 1<<15 - 1
    33  	MinInt16  = -1 << 15
    34  	MaxInt32  = 1<<31 - 1
    35  	MinInt32  = -1 << 31
    36  	MaxInt64  = 1<<63 - 1
    37  	MinInt64  = -1 << 63
    38  	MaxUint8  = 1<<8 - 1
    39  	MaxUint16 = 1<<16 - 1
    40  	MaxUint32 = 1<<32 - 1
    41  	MaxUint64 = 1<<64 - 1
    42  )
    43  
    44  // HexOrDecimal64 marshals uint64 as hex or decimal.
    45  type HexOrDecimal64 uint64
    46  
    47  // UnmarshalText implements encoding.TextUnmarshaler.
    48  func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
    49  	int, ok := ParseUint64(string(input))
    50  	if !ok {
    51  		return fmt.Errorf("invalid hex or decimal integer %q", input)
    52  	}
    53  	*i = HexOrDecimal64(int)
    54  	return nil
    55  }
    56  
    57  // MarshalText implements encoding.TextMarshaler.
    58  func (i HexOrDecimal64) MarshalText() ([]byte, error) {
    59  	return []byte(fmt.Sprintf("%#x", uint64(i))), nil
    60  }
    61  
    62  // ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
    63  // Leading zeros are accepted. The empty string parses as zero.
    64  func ParseUint64(s string) (uint64, bool) {
    65  	if s == "" {
    66  		return 0, true
    67  	}
    68  	if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
    69  		v, err := strconv.ParseUint(s[2:], 16, 64)
    70  		return v, err == nil
    71  	}
    72  	v, err := strconv.ParseUint(s, 10, 64)
    73  	return v, err == nil
    74  }
    75  
    76  // MustParseUint64 parses s as an integer and panics if the string is invalid.
    77  func MustParseUint64(s string) uint64 {
    78  	v, ok := ParseUint64(s)
    79  	if !ok {
    80  		panic("invalid unsigned 64 bit integer: " + s)
    81  	}
    82  	return v
    83  }
    84  
    85  // NOTE: The following methods need to be optimised using either bit checking or asm
    86  
    87  // SafeSub returns subtraction result and whether overflow occurred.
    88  func SafeSub(x, y uint64) (uint64, bool) {
    89  	return x - y, x < y
    90  }
    91  
    92  // SafeAdd returns the result and whether overflow occurred.
    93  func SafeAdd(x, y uint64) (uint64, bool) {
    94  	return x + y, y > MaxUint64-x
    95  }
    96  
    97  // SafeMul returns multiplication result and whether overflow occurred.
    98  func SafeMul(x, y uint64) (uint64, bool) {
    99  	if x == 0 || y == 0 {
   100  		return 0, false
   101  	}
   102  	return x * y, y > MaxUint64/x
   103  }