integer.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. package math
  17. import (
  18. "fmt"
  19. "math/bits"
  20. "strconv"
  21. )
  22. // Integer limit values.
  23. const (
  24. MaxInt8 = 1<<7 - 1
  25. MinInt8 = -1 << 7
  26. MaxInt16 = 1<<15 - 1
  27. MinInt16 = -1 << 15
  28. MaxInt32 = 1<<31 - 1
  29. MinInt32 = -1 << 31
  30. MaxInt64 = 1<<63 - 1
  31. MinInt64 = -1 << 63
  32. MaxUint8 = 1<<8 - 1
  33. MaxUint16 = 1<<16 - 1
  34. MaxUint32 = 1<<32 - 1
  35. MaxUint64 = 1<<64 - 1
  36. )
  37. // HexOrDecimal64 marshals uint64 as hex or decimal.
  38. type HexOrDecimal64 uint64
  39. // UnmarshalText implements encoding.TextUnmarshaler.
  40. func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
  41. int, ok := ParseUint64(string(input))
  42. if !ok {
  43. return fmt.Errorf("invalid hex or decimal integer %q", input)
  44. }
  45. *i = HexOrDecimal64(int)
  46. return nil
  47. }
  48. // MarshalText implements encoding.TextMarshaler.
  49. func (i HexOrDecimal64) MarshalText() ([]byte, error) {
  50. return []byte(fmt.Sprintf("%#x", uint64(i))), nil
  51. }
  52. // ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
  53. // Leading zeros are accepted. The empty string parses as zero.
  54. func ParseUint64(s string) (uint64, bool) {
  55. if s == "" {
  56. return 0, true
  57. }
  58. if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
  59. v, err := strconv.ParseUint(s[2:], 16, 64)
  60. return v, err == nil
  61. }
  62. v, err := strconv.ParseUint(s, 10, 64)
  63. return v, err == nil
  64. }
  65. // MustParseUint64 parses s as an integer and panics if the string is invalid.
  66. func MustParseUint64(s string) uint64 {
  67. v, ok := ParseUint64(s)
  68. if !ok {
  69. panic("invalid unsigned 64 bit integer: " + s)
  70. }
  71. return v
  72. }
  73. // SafeSub returns x-y and checks for overflow.
  74. func SafeSub(x, y uint64) (uint64, bool) {
  75. diff, borrowOut := bits.Sub64(x, y, 0)
  76. return diff, borrowOut != 0
  77. }
  78. // SafeAdd returns x+y and checks for overflow.
  79. func SafeAdd(x, y uint64) (uint64, bool) {
  80. sum, carryOut := bits.Add64(x, y, 0)
  81. return sum, carryOut != 0
  82. }
  83. // SafeMul returns x*y and checks for overflow.
  84. func SafeMul(x, y uint64) (uint64, bool) {
  85. hi, lo := bits.Mul64(x, y)
  86. return lo, hi != 0
  87. }