github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/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 "strconv"
    20  
    21  const (
    22  	// Integer limit values.
    23  	MaxInt8   = 1<<7 - 1
    24  	MinInt8   = -1 << 7
    25  	MaxInt16  = 1<<15 - 1
    26  	MinInt16  = -1 << 15
    27  	MaxInt32  = 1<<31 - 1
    28  	MinInt32  = -1 << 31
    29  	MaxInt64  = 1<<63 - 1
    30  	MinInt64  = -1 << 63
    31  	MaxUint8  = 1<<8 - 1
    32  	MaxUint16 = 1<<16 - 1
    33  	MaxUint32 = 1<<32 - 1
    34  	MaxUint64 = 1<<64 - 1
    35  )
    36  
    37  // ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
    38  // Leading zeros are accepted. The empty string parses as zero.
    39  func ParseUint64(s string) (uint64, bool) {
    40  	if s == "" {
    41  		return 0, true
    42  	}
    43  	if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
    44  		v, err := strconv.ParseUint(s[2:], 16, 64)
    45  		return v, err == nil
    46  	}
    47  	v, err := strconv.ParseUint(s, 10, 64)
    48  	return v, err == nil
    49  }
    50  
    51  // MustParseUint64 parses s as an integer and panics if the string is invalid.
    52  func MustParseUint64(s string) uint64 {
    53  	v, ok := ParseUint64(s)
    54  	if !ok {
    55  		panic("invalid unsigned 64 bit integer: " + s)
    56  	}
    57  	return v
    58  }
    59  
    60  // NOTE: The following methods need to be optimised using either bit checking or asm
    61  
    62  // SafeSub returns subtraction result and whether overflow occurred.
    63  func SafeSub(x, y uint64) (uint64, bool) {
    64  	return x - y, x < y
    65  }
    66  
    67  // SafeAdd returns the result and whether overflow occurred.
    68  func SafeAdd(x, y uint64) (uint64, bool) {
    69  	return x + y, y > MaxUint64-x
    70  }
    71  
    72  // SafeMul returns multiplication result and whether overflow occurred.
    73  func SafeMul(x, y uint64) (uint64, bool) {
    74  	if x == 0 || y == 0 {
    75  		return 0, false
    76  	}
    77  	return x * y, y > MaxUint64/x
    78  }