-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
util.go
executable file
·331 lines (290 loc) · 7.67 KB
/
util.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package core
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"unicode"
"github.com/errata-ai/vale/v3/internal/nlp"
)
var defaultIgnoreDirectories = []string{
"node_modules", ".git",
}
var spaces = regexp.MustCompile(" +")
var reANSI = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")
var sanitizer = strings.NewReplacer(
"’", "'",
"\r\n", "\n",
"\r", "\n")
// CapFirst capitalizes the first letter of a string.
func CapFirst(s string) string {
if len(s) == 0 {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
}
// Sanitize prepares text for our check functions.
func Sanitize(txt string) string {
// TODO: symbols?
return sanitizer.Replace(txt)
}
// StripANSI removes all ANSI characters from the given string.
func StripANSI(s string) string {
return reANSI.ReplaceAllString(s, "")
}
// WhitespaceToSpace converts newlines and multiple spaces (e.g., " ") into a
// single space.
func WhitespaceToSpace(msg string) string {
msg = strings.ReplaceAll(msg, "\n", " ")
msg = spaces.ReplaceAllString(msg, " ")
return msg
}
// ShouldIgnoreDirectory will check if directory should be ignored
func ShouldIgnoreDirectory(directoryName string) bool {
for _, directory := range defaultIgnoreDirectories {
if directory == directoryName {
return true
}
}
return false
}
// ToSentence converts a slice of terms into sentence.
func ToSentence(words []string, andOrOr string) string {
l := len(words)
if l == 1 {
return fmt.Sprintf("'%s'", words[0])
} else if l == 2 {
return fmt.Sprintf("'%s' or '%s'", words[0], words[1])
}
wordsForSentence := []string{}
for _, w := range words {
wordsForSentence = append(wordsForSentence, fmt.Sprintf("'%s'", w))
}
wordsForSentence[l-1] = andOrOr + " " + wordsForSentence[l-1]
return strings.Join(wordsForSentence, ", ")
}
// IsLetter returns `true` if s contains all letter characters and false if not.
func IsLetter(s string) bool {
for _, r := range s {
if !unicode.IsLetter(r) {
return false
}
}
return true
}
// IsCode returns `true` if s is a code-like token.
func IsCode(s string) bool {
for _, r := range s {
if r != '*' && r != '@' {
return false
}
}
return true
}
// IsPhrase returns `true` is s is a phrase-like token.
//
// This is used to differentiate regex tokens from non-regex.
func IsPhrase(s string) bool {
for _, r := range s {
if !unicode.IsLetter(r) && r != ' ' && !unicode.IsDigit(r) && r != '-' {
return false
}
}
return true
}
// InRange determines if the range r contains the integer n.
func InRange(n int, r []int) bool {
return len(r) == 2 && (r[0] <= n && n <= r[1])
}
// Which checks for the existence of any command in `cmds`.
func Which(cmds []string) string {
for _, cmd := range cmds {
path, err := exec.LookPath(cmd)
if err == nil {
return path
}
}
return ""
}
// CondSprintf is sprintf, ignores extra arguments.
func CondSprintf(format string, v ...interface{}) string {
v = append(v, "")
format += fmt.Sprint("%[", len(v), "]s")
return fmt.Sprintf(format, v...)
}
// FormatMessage inserts `subs` into `msg`.
func FormatMessage(msg string, subs ...string) string {
return CondSprintf(msg, StringsToInterface(subs)...)
}
// Substitute replaces the substring `sub` with a string of asterisks.
func Substitute(src, sub string, char rune) (string, bool) {
idx := strings.Index(src, sub)
if idx < 0 {
return src, false
}
repl := strings.Map(func(r rune) rune {
if r != '\n' {
return char
}
return r
}, sub)
return strings.Replace(src, sub, repl, 1), true
}
// StringsToInterface converts a slice of strings to an interface.
func StringsToInterface(strings []string) []interface{} {
intf := make([]interface{}, len(strings))
for i, v := range strings {
intf[i] = v
}
return intf
}
// Indent adds padding to every line of `text`.
func Indent(text, indent string) string {
if text[len(text)-1:] == "\n" {
result := ""
for _, j := range strings.Split(text[:len(text)-1], "\n") {
result += indent + j + "\n"
}
return result
}
result := ""
for _, j := range strings.Split(strings.TrimRight(text, "\n"), "\n") {
result += indent + j + "\n"
}
return result[:len(result)-1]
}
// IsDir determines if the path given by `filename` is a directory.
func IsDir(filename string) bool {
fi, err := os.Stat(filename)
return err == nil && fi.IsDir()
}
// FileExists determines if the path given by `filename` exists.
func FileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
// StringInSlice determines if `slice` contains the string `a`.
func StringInSlice(a string, slice []string) bool {
for _, b := range slice {
if a == b {
return true
}
}
return false
}
// IntInSlice determines if `slice` contains the int `a`.
func IntInSlice(a int, slice []int) bool {
for _, b := range slice {
if a == b {
return true
}
}
return false
}
// AllStringsInSlice determines if `slice` contains the `strings`.
func AllStringsInSlice(strings []string, slice []string) bool {
for _, s := range strings {
if !StringInSlice(s, slice) {
return false
}
}
return true
}
// SplitLines splits on CRLF, CR not followed by LF, and LF.
func SplitLines(data []byte, atEOF bool) (adv int, token []byte, err error) { //nolint:nonamedreturns
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexAny(data, "\r\n"); i >= 0 {
if data[i] == '\n' {
return i + 1, data[0:i], nil
}
adv = i + 1
if len(data) > i+1 && data[i+1] == '\n' {
adv++
}
return adv, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}
func normalizePath(path string) string {
// expand tilde
homedir, err := os.UserHomeDir()
if err != nil {
return path
}
if path == "~" {
return homedir
} else if strings.HasPrefix(path, filepath.FromSlash("~/")) {
path = filepath.Join(homedir, path[2:])
}
return path
}
func TextToContext(text string, meta *nlp.Info) []nlp.TaggedWord {
context := []nlp.TaggedWord{}
for idx, line := range strings.Split(text, "\n") {
plain := stripMarkdown(line)
pos := 0
for _, tok := range nlp.TextToTokens(plain, meta) {
if strings.TrimSpace(tok.Text) != "" {
s := strings.Index(line[pos:], tok.Text) + len(line[:pos])
if !StringInSlice(tok.Tag, []string{"''", "``"}) {
context = append(context, nlp.TaggedWord{
Line: idx + 1,
Token: tok,
Span: []int{s + 1, s + len(tok.Text)},
})
}
pos = s
line, _ = Substitute(line, tok.Text, '*')
}
}
}
return context
}
func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
result := ""
lastIndex := 0
for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
groups := []string{}
for i := 0; i < len(v); i += 2 {
if v[i] == -1 || v[i+1] == -1 {
groups = append(groups, "")
} else {
groups = append(groups, str[v[i]:v[i+1]])
}
}
result += str[lastIndex:v[0]] + repl(groups)
lastIndex = v[1]
}
return result + str[lastIndex:]
}
func HasAnySuffix(s string, suffixes []string) bool {
n := len(s)
for _, suffix := range suffixes {
if n > len(suffix) && strings.HasSuffix(s, suffix) {
return true
}
}
return false
}
// ReplaceExt replaces the extension of `fp` with `ext` if the extension of
// `fp` is in `formats`.
//
// This is used in places where we need to normalize file extensions (e.g.,
// `foo.mdx` -> `foo.md`) in order to respect format associations.
func ReplaceExt(fp string, formats map[string]string) string {
var ext string
old := filepath.Ext(fp)
if normed, found := formats[strings.Trim(old, ".")]; found {
ext = "." + normed
fp = fp[0:len(fp)-len(old)] + ext
}
return fp
}