-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
110 lines (101 loc) · 2.67 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
package main
import (
"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)
}
// searchInStringToMap Reads a string and returns all lowercased matches in the regular expression as map keys
func searchInStringToMap(total string, expression string) map[string]bool {
defer timeTrack(time.Now(), "searchInStringToMap")
r, err := regexp.Compile(expression)
if err != nil {
panic(err)
}
allMatches := r.FindAllString(total, -1)
a := make(map[string]bool)
for _, v := range allMatches {
a[strings.ToLower(v)] = false
}
return a
}
// searchInStringToMapCS Reads a string and returns all matches (case sensitive) in the regular expression as map keys
func searchInStringToMapCS(total string, expression string) map[string]bool {
defer timeTrack(time.Now(), "searchInStringToMapCS")
r, err := regexp.Compile(expression)
if err != nil {
panic(err)
}
allMatches := r.FindAllString(total, -1)
a := make(map[string]bool)
for _, v := range allMatches {
a[v] = false
}
return a
}
// Compare Compares 2 maps with words as what to search and boleans false value. Transforms in true when the key exists in the other map.
func Compare(a map[string]bool, b map[string]bool) (map[string]bool, map[string]bool) {
defer timeTrack(time.Now(), "Compare")
var y bool
for key := range a {
_, y = b[key]
if y == true {
a[key] = true
}
}
for key := range b {
_, y = a[key]
if y == true {
b[key] = true
}
}
return a, b
}
// mapKeysToSlice Adds the keys with val to a slice
func mapKeysToSlice(m map[string]bool, val bool) []string {
defer timeTrack(time.Now(), "mapKeysToSlice")
var result []string
for k, v := range m {
if v == val {
result = append(result, k)
}
}
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)
}
}
// trashFiles deletes files created by ecompare
func trashFiles() {
os.Remove("in-a-but-not-in-b.txt")
os.Remove("in-b-but-not-in-a.txt")
os.Remove("in-both-a-and-b.txt")
}