-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.go
243 lines (219 loc) · 5.11 KB
/
lib.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// This file is part of Fwew.
// Fwew is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Fwew is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Fwew. If not, see http://gnu.org/licenses/
// Package main contains all the things. lib.go handles common functions.
package main
import (
"crypto/sha1"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"unicode"
)
// Contains returns true if anything in q is also in s
func Contains(s []string, q []string) bool {
if len(q) == 0 || len(s) == 0 {
return false
}
for _, x := range q {
for _, y := range s {
if y == x {
return true
}
}
}
return false
}
// ContainsStr returns true if q is in s
func ContainsStr(s []string, q string) bool {
if len(q) == 0 || len(s) == 0 {
return false
}
for _, x := range s {
if q == x {
return true
}
}
return false
}
// DeleteElement "deletes" all occurrences of q in s
// actually returns a new string slice containing the original minus all q
func DeleteElement(s []string, q string) []string {
if len(s) == 0 {
return s
}
var r []string
for _, str := range s {
if str != q {
r = append(r, str)
}
}
return r
}
// DeleteEmpty "deletes" all empty string entries in s
// actually returns a new string slice containing all non-empty strings in s
func DeleteEmpty(s []string) []string {
return DeleteElement(s, "")
}
// Index return the index of q in s
func Index(s []string, q string) int {
for i, v := range s {
if v == q {
return i
}
}
return -1
}
// IndexStr return the index of q in s
func IndexStr(s string, q rune) int {
for i, v := range s {
if v == q {
return i
}
}
return -1
}
// IsLetter returns true if s is an alphabet character or apostrophe
func IsLetter(s string) bool {
for _, r := range s {
if unicode.IsLetter(r) || r == '\'' || r == '‘' {
return true
}
}
return false
}
// Reverse returns the reversed version of s
func Reverse(s string) string {
n := len(s)
runes := make([]rune, n)
for _, r := range s {
n--
runes[n] = r
}
return string(runes[n:])
}
// StripChars strips all the characters in chr out of str
func StripChars(str, chr string) string {
return strings.Map(func(r rune) rune {
if strings.IndexRune(chr, r) < 0 {
return r
}
return -1
}, str)
}
// Valid validates range of integers for input
func Valid(input int64, reverse bool) bool {
const (
maxIntDec int64 = 32767
maxIntOct int64 = 77777
)
if reverse {
if 0 <= input && input <= maxIntDec {
return true
}
return false
}
if 0 <= input && input <= maxIntOct {
return true
}
return false
}
// DownloadDict downloads the latest released version of the dictionary file
func DownloadDict() error {
var (
filepath = Text("dictionary")
url = Text("dictURL")
)
out, err := os.Create(filepath)
if err != nil {
return err
}
resp, err := http.Get(url)
if err != nil {
return err
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
err = out.Close()
if err != nil {
return err
}
err = resp.Body.Close()
if err != nil {
return err
}
fmt.Println(Text("dlSuccess"))
return nil
}
// GLOB https://github.com/ryanuber/go-glob
// The character which is treated like a glob
const GLOB = "*"
// Glob will test a string pattern, potentially containing globs, against a
// subject string. The result is a simple true/false, determining whether or
// not the glob pattern matched the subject text.
func Glob(pattern, subj string) bool {
// Empty pattern can only match empty subject
if pattern == "" {
return subj == pattern
}
// If the pattern _is_ a glob, it matches everything
if pattern == GLOB {
return true
}
parts := strings.Split(pattern, GLOB)
if len(parts) == 1 {
// No globs in pattern, so test for equality
return subj == pattern
}
leadingGlob := strings.HasPrefix(pattern, GLOB)
trailingGlob := strings.HasSuffix(pattern, GLOB)
end := len(parts) - 1
// Go over the leading parts and ensure they match.
for i := 0; i < end; i++ {
idx := strings.Index(subj, parts[i])
switch i {
case 0:
// Check the first section. Requires special handling.
if !leadingGlob && idx != 0 {
return false
}
default:
// Check that the middle parts match.
if idx < 0 {
return false
}
}
// Trim evaluated text from subj as we loop over the pattern.
subj = subj[idx+len(parts[i]):]
}
// Reached the last section. Requires special handling.
return trailingGlob || strings.HasSuffix(subj, parts[end])
}
// SHA1Hash gets hash of dictionary file
func SHA1Hash(filename string) string {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
return fmt.Sprintf("%x", h.Sum(nil))[0:8]
}