-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.go
137 lines (124 loc) · 2.74 KB
/
str.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
126
127
128
129
130
131
132
133
134
135
136
137
package str
import (
"crypto/rand"
"fmt"
"regexp"
"strconv"
"strings"
)
// GenRandom generates a random string of a specified length.
//
// **Parameters:**
//
// length: Length of the random string to be generated.
//
// **Returns:**
//
// string: Generated random string.
// error: An error if random string generation fails.
func GenRandom(length int) (string, error) {
b := make([]byte, length)
if _, err := rand.Read(b); err != nil {
return "", err
}
return fmt.Sprintf("%x", b)[:length], nil
}
// InSlice determines if a specified string exists in a given slice.
//
// **Parameters:**
//
// strToFind: String to search for in the slice.
// inputSlice: Slice of strings to be searched.
//
// **Returns:**
//
// bool: true if string is found in the slice, false otherwise.
func InSlice(strToFind string, inputSlice []string) bool {
for _, value := range inputSlice {
if strings.Contains(value, strToFind) {
return true
}
}
return false
}
// IsNumeric checks if a string is entirely composed of numeric characters.
//
// **Parameters:**
//
// s: String to check for numeric characters.
//
// **Returns:**
//
// bool: true if the string is numeric, false otherwise.
func IsNumeric(s string) bool {
for _, char := range s {
if _, err := strconv.Atoi(string(char)); err != nil {
return false
}
}
return true
}
// ToInt64 converts a string to int64.
//
// **Parameters:**
//
// value: String to be converted to int64.
//
// **Returns:**
//
// int64: int64 equivalent of the string.
// error: An error if the conversion fails.
func ToInt64(value string) (int64, error) {
n, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return -1, err
}
return n, nil
}
// ToSlice converts a string to a slice of strings using a delimiter.
//
// **Parameters:**
//
// delimStr: String to be split into a slice.
// delim: Delimiter to be used for splitting the string.
//
// **Returns:**
//
// []string: Slice of strings from the split input string.
func ToSlice(delimStr string, delim string) []string {
return strings.Split(delimStr, delim)
}
// SlicesEqual compares two slices of strings for equality.
//
// **Parameters:**
//
// a: First string slice for comparison.
// b: Second string slice for comparison.
//
// **Returns:**
//
// bool: true if slices are equal, false otherwise.
func SlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// StripANSI removes ANSI escape codes from a string.
//
// **Parameters:**
//
// str: String to remove ANSI escape codes from.
//
// **Returns:**
//
// string: String with ANSI escape codes removed.
func StripANSI(str string) string {
re := regexp.MustCompile(`\x1B\[[0-9;]*[a-zA-Z]`)
return re.ReplaceAllString(str, "")
}