This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
utils.go
125 lines (109 loc) · 2.77 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package tezos
import (
"encoding/hex"
"fmt"
"io"
"sync"
)
// HexBytes represents bytes as a JSON string of hexadecimal digits
type HexBytes []byte
// UnmarshalText umarshals a hex string to bytes. It implements the
// encoding.TextUnmarshaler interface, so that HexBytes can be used in Go
// structs in combination with the standard JSON library.
func (h *HexBytes) UnmarshalText(data []byte) error {
dst := make([]byte, hex.DecodedLen(len(data)))
if _, err := hex.Decode(dst, data); err != nil {
return err
}
*h = dst
return nil
}
// ReadBytes copies size bytes from r into h. If h is nil or too short,
// a new byte slice is allocated. Fails with io.ErrShortBuffer when
// less than size bytes can be read from r.
func (h *HexBytes) ReadBytes(r io.Reader, size int) error {
if cap(*h) < size {
*h = make([]byte, size)
} else {
*h = (*h)[:size]
}
n, err := r.Read(*h)
if err != nil {
return err
}
if n < size {
return io.ErrShortBuffer
}
return nil
}
// MarshalText converts a byte slice to a hex encoded string. It implements the
// encoding.TextMarshaler interface, so that HexBytes can be used in Go
// structs in combination with the standard JSON library.
func (h HexBytes) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(h)), nil
}
// String converts a byte slice to a hex encoded string
func (h HexBytes) String() string {
return hex.EncodeToString(h)
}
// Bytes type-casts HexBytes back to a byte slice
func (h HexBytes) Bytes() []byte {
return []byte(h)
}
// UnmarshalBinary umarshals a binary slice. It implements the
// encoding.BinaryUnmarshaler interface.
func (h *HexBytes) UnmarshalBinary(data []byte) error {
if cap(*h) < len(data) {
*h = make([]byte, len(data))
}
*h = (*h)[:len(data)]
copy(*h, data)
return nil
}
// MarshalBinary marshals as binary slice. It implements the
// encoding.BinaryMarshaler interface.
func (h HexBytes) MarshalBinary() ([]byte, error) {
return h, nil
}
// Ratio represents a numeric ratio used in Ithaca constants
type Ratio struct {
Num int `json:"numerator"`
Den int `json:"denominator"`
}
func (r Ratio) Float64() float64 {
if r.Den == 0 {
return 0
}
return float64(r.Num) / float64(r.Den)
}
func Short(v any) string {
var s string
if str, ok := v.(fmt.Stringer); ok {
s = str.String()
} else {
s = v.(string)
}
if len(s) <= 12 {
return s
}
return s[:8] + "..." + s[len(s)-4:]
}
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
func b2i(b bool) (i int) {
// The compiler currently only optimizes this form into movzbl.
// See https://0x0f.me/blog/golang-compiler-optimization/
// See issue 6011.
if b {
i = 1
}
return
}
var bufPool32 = &sync.Pool{
New: func() any { return make([]byte, 32) },
}