github.com/googgoog/go-ethereum@v1.9.7/mobile/big.go (about)

     1  // Copyright 2016 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  // Contains all the wrappers from the math/big package.
    18  
    19  package geth
    20  
    21  import (
    22  	"errors"
    23  	"math/big"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  )
    27  
    28  // A BigInt represents a signed multi-precision integer.
    29  type BigInt struct {
    30  	bigint *big.Int
    31  }
    32  
    33  // NewBigInt allocates and returns a new BigInt set to x.
    34  func NewBigInt(x int64) *BigInt {
    35  	return &BigInt{big.NewInt(x)}
    36  }
    37  
    38  // GetBytes returns the absolute value of x as a big-endian byte slice.
    39  func (bi *BigInt) GetBytes() []byte {
    40  	return bi.bigint.Bytes()
    41  }
    42  
    43  // String returns the value of x as a formatted decimal string.
    44  func (bi *BigInt) String() string {
    45  	return bi.bigint.String()
    46  }
    47  
    48  // GetInt64 returns the int64 representation of x. If x cannot be represented in
    49  // an int64, the result is undefined.
    50  func (bi *BigInt) GetInt64() int64 {
    51  	return bi.bigint.Int64()
    52  }
    53  
    54  // SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets
    55  // the big int to that value.
    56  func (bi *BigInt) SetBytes(buf []byte) {
    57  	bi.bigint.SetBytes(common.CopyBytes(buf))
    58  }
    59  
    60  // SetInt64 sets the big int to x.
    61  func (bi *BigInt) SetInt64(x int64) {
    62  	bi.bigint.SetInt64(x)
    63  }
    64  
    65  // Sign returns:
    66  //
    67  //	-1 if x <  0
    68  //	 0 if x == 0
    69  //	+1 if x >  0
    70  //
    71  func (bi *BigInt) Sign() int {
    72  	return bi.bigint.Sign()
    73  }
    74  
    75  // SetString sets the big int to x.
    76  //
    77  // The string prefix determines the actual conversion base. A prefix of "0x" or
    78  // "0X" selects base 16; the "0" prefix selects base 8, and a "0b" or "0B" prefix
    79  // selects base 2. Otherwise the selected base is 10.
    80  func (bi *BigInt) SetString(x string, base int) {
    81  	bi.bigint.SetString(x, base)
    82  }
    83  
    84  // BigInts represents a slice of big ints.
    85  type BigInts struct{ bigints []*big.Int }
    86  
    87  // NewBigInts creates a slice of uninitialized big numbers.
    88  func NewBigInts(size int) *BigInts {
    89  	return &BigInts{
    90  		bigints: make([]*big.Int, size),
    91  	}
    92  }
    93  
    94  // Size returns the number of big ints in the slice.
    95  func (bi *BigInts) Size() int {
    96  	return len(bi.bigints)
    97  }
    98  
    99  // Get returns the bigint at the given index from the slice.
   100  func (bi *BigInts) Get(index int) (bigint *BigInt, _ error) {
   101  	if index < 0 || index >= len(bi.bigints) {
   102  		return nil, errors.New("index out of bounds")
   103  	}
   104  	return &BigInt{bi.bigints[index]}, nil
   105  }
   106  
   107  // Set sets the big int at the given index in the slice.
   108  func (bi *BigInts) Set(index int, bigint *BigInt) error {
   109  	if index < 0 || index >= len(bi.bigints) {
   110  		return errors.New("index out of bounds")
   111  	}
   112  	bi.bigints[index] = bigint.bigint
   113  	return nil
   114  }
   115  
   116  // GetString returns the value of x as a formatted string in some number base.
   117  func (bi *BigInt) GetString(base int) string {
   118  	return bi.bigint.Text(base)
   119  }