github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/mobile/big.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 // Contains all the wrappers from the math/big package. 13 14 package geth 15 16 import ( 17 "errors" 18 "math/big" 19 20 "github.com/Sberex/go-sberex/common" 21 ) 22 23 // A BigInt represents a signed multi-precision integer. 24 type BigInt struct { 25 bigint *big.Int 26 } 27 28 // NewBigInt allocates and returns a new BigInt set to x. 29 func NewBigInt(x int64) *BigInt { 30 return &BigInt{big.NewInt(x)} 31 } 32 33 // GetBytes returns the absolute value of x as a big-endian byte slice. 34 func (bi *BigInt) GetBytes() []byte { 35 return bi.bigint.Bytes() 36 } 37 38 // String returns the value of x as a formatted decimal string. 39 func (bi *BigInt) String() string { 40 return bi.bigint.String() 41 } 42 43 // GetInt64 returns the int64 representation of x. If x cannot be represented in 44 // an int64, the result is undefined. 45 func (bi *BigInt) GetInt64() int64 { 46 return bi.bigint.Int64() 47 } 48 49 // SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets 50 // the big int to that value. 51 func (bi *BigInt) SetBytes(buf []byte) { 52 bi.bigint.SetBytes(common.CopyBytes(buf)) 53 } 54 55 // SetInt64 sets the big int to x. 56 func (bi *BigInt) SetInt64(x int64) { 57 bi.bigint.SetInt64(x) 58 } 59 60 // Sign returns: 61 // 62 // -1 if x < 0 63 // 0 if x == 0 64 // +1 if x > 0 65 // 66 func (bi *BigInt) Sign() int { 67 return bi.bigint.Sign() 68 } 69 70 // SetString sets the big int to x. 71 // 72 // The string prefix determines the actual conversion base. A prefix of "0x" or 73 // "0X" selects base 16; the "0" prefix selects base 8, and a "0b" or "0B" prefix 74 // selects base 2. Otherwise the selected base is 10. 75 func (bi *BigInt) SetString(x string, base int) { 76 bi.bigint.SetString(x, base) 77 } 78 79 // BigInts represents a slice of big ints. 80 type BigInts struct{ bigints []*big.Int } 81 82 // Size returns the number of big ints in the slice. 83 func (bi *BigInts) Size() int { 84 return len(bi.bigints) 85 } 86 87 // Get returns the bigint at the given index from the slice. 88 func (bi *BigInts) Get(index int) (bigint *BigInt, _ error) { 89 if index < 0 || index >= len(bi.bigints) { 90 return nil, errors.New("index out of bounds") 91 } 92 return &BigInt{bi.bigints[index]}, nil 93 } 94 95 // Set sets the big int at the given index in the slice. 96 func (bi *BigInts) Set(index int, bigint *BigInt) error { 97 if index < 0 || index >= len(bi.bigints) { 98 return errors.New("index out of bounds") 99 } 100 bi.bigints[index] = bigint.bigint 101 return nil 102 } 103 104 // GetString returns the value of x as a formatted string in some number base. 105 func (bi *BigInt) GetString(base int) string { 106 return bi.bigint.Text(base) 107 }