Skip to content

Commit 5a2188a

Browse files
author
wintrmvte
committed
Added ShuffleSliceInt() + moved File2Slice() from coldfire.go
1 parent 5ac9850 commit 5a2188a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

data_manipulation.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"regexp"
99
"strconv"
1010
"strings"
11+
"os"
12+
"bufio"
1113
"time"
1214
"github.com/c-robinson/iplib"
1315
)
@@ -149,6 +151,15 @@ func ShuffleSlice(s []string) []string {
149151
return s
150152
}
151153

154+
// ShuffleSliceInt randomly shuffles a list of integers.
155+
func ShuffleSliceInt(s []int) []int {
156+
rand.Seed(time.Now().UnixNano())
157+
rand.Shuffle(len(s), func(i, j int) {
158+
s[i], s[j] = s[j], s[i]
159+
})
160+
return s
161+
}
162+
152163
// IpIncrement increments an IP address by 1.
153164
func IpIncrement(ip net.IP) {
154165
for j := len(ip) - 1; j >= 0; j-- {
@@ -228,6 +239,18 @@ func Interval2Seconds(interval string) int {
228239
return i
229240
}
230241

242+
// File2Slice reads a textfile and returns all lines as an array.
243+
func File2Slice(file string) []string {
244+
fil, _ := os.Open(file)
245+
defer fil.Close()
246+
var lines []string
247+
scanner := bufio.NewScanner(fil)
248+
for scanner.Scan() {
249+
lines = append(lines, scanner.Text())
250+
}
251+
return lines
252+
}
253+
231254
// RemoveNewLines removes possible newlines from a string.
232255
func RemoveNewlines(s string) string {
233256
re := regexp.MustCompile(`\r?\n`)

0 commit comments

Comments
 (0)