-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
75 lines (66 loc) · 1.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
package athena
import (
"crypto/rand"
"encoding/hex"
"strings"
"unicode"
)
// randomAddress generates a random 20-byte ChecksumAddress in Go.
func randomAddress() (string, error) {
bytes := make([]byte, 20)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return "0x" + hex.EncodeToString(bytes), nil
}
// uintOverUnderFlow handles uint overflow and underflow based on the specified precision.
func uintOverUnderFlow(value int64, precision int) int64 {
mod := int64(1 << precision)
if value < 0 {
return value + mod
}
if value >= mod {
return value - mod
}
return value
}
// camelToSnake converts a CamelCase string to snake_case.
func camelToSnake(name string) string {
var outString strings.Builder
lastChar := rune(name[0])
for _, char := range name[1:] {
if unicode.IsUpper(char) {
if unicode.IsLower(lastChar) || unicode.IsDigit(lastChar) {
outString.WriteRune(lastChar)
outString.WriteRune('_')
} else {
outString.WriteRune(unicode.ToLower(lastChar))
}
} else if unicode.IsDigit(char) {
if unicode.IsLetter(lastChar) {
outString.WriteRune(unicode.ToLower(lastChar))
outString.WriteRune('_')
} else {
outString.WriteRune(unicode.ToLower(lastChar))
}
} else {
outString.WriteRune(unicode.ToLower(lastChar))
}
lastChar = char
}
return outString.String()
}
// pprintList formats a list of strings to fit within a terminal width, wrapping lines as needed.
func pprintList(writeArray []string, termWidth int) []string {
currentLine := ""
output := []string{}
for _, writeVal := range writeArray {
if len(currentLine)+len(writeVal)+1 > termWidth {
output = append(output, currentLine)
currentLine = ""
}
currentLine += "'" + writeVal + "', "
}
output = append(output, currentLine)
return output
}