-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
112 lines (102 loc) · 2.57 KB
/
functions.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
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"time"
)
// timeTrack is used to debug each function by measuring how long it takes to execute.
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
if *debug == true {
log.Printf("%s took %s", name, elapsed)
}
}
// fileToString reads a file into a sting.
func fileToString(fileName string) string {
defer timeTrack(time.Now(), "fileToString")
if _, err := os.Stat(fileName); os.IsNotExist(err) {
fmt.Println("ERROR: The file/path", fileName, "does not exist here")
os.Exit(-1)
}
dat, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
return string(dat)
}
// searchInString reads a string and returns all matches in the string.
func searchInString(total string, expression string) []string {
defer timeTrack(time.Now(), "searchInString")
r, err := regexp.Compile(expression)
if err != nil {
panic(err)
}
allMatches := r.FindAllString(total, -1)
return allMatches
}
// arrayToLowercase Lowercases all the items in an array.
func arrayToLowercase(a []string) []string {
defer timeTrack(time.Now(), "arrayToLowercase")
var result []string
for _, v := range a {
lv := strings.ToLower(v)
result = append(result, lv)
}
return result
}
// arrayToUpercase Upercases all the items in an array.
func arrayToUpercase(a []string) []string {
defer timeTrack(time.Now(), "arrayToUpercase")
var result []string
for _, v := range a {
lv := strings.ToUpper(v)
result = append(result, lv)
}
return result
}
//uniquesInArray Finds uniques in an array and returns.
func uniquesInArray(a []string) []string {
defer timeTrack(time.Now(), "uniquesInArray")
set := make(map[string]struct{})
for _, v := range a {
set[v] = struct{}{}
}
var uniques []string
for k := range set {
uniques = append(uniques, k)
}
return uniques
}
// stringToSha256 converts a string to sha 256.
func stringToSha256(s string) string {
h := sha256.New()
h.Write([]byte(s))
bs := h.Sum(nil)
str := hex.EncodeToString(bs)
return str
}
// arrayToSha256 Iterates a slice and converts it to sha256.
func arrayToSha256(a []string) []string {
defer timeTrack(time.Now(), "arrayToSha256")
var result []string
var encodedSha string
for _, v := range a {
encodedSha = stringToSha256(v)
result = append(result, encodedSha)
}
return result
}
// stringToFile writes a string to a file.
func stringToFile(fileName string, dat string) {
defer timeTrack(time.Now(), "stringToFile")
err := ioutil.WriteFile(fileName, []byte(dat), 0644)
if err != nil {
panic(err)
}
}