-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.go
146 lines (128 loc) · 3.84 KB
/
utils.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
package rivescript
// Miscellaneous utility functions.
import (
"fmt"
"regexp"
"strings"
)
// randomInt gets a random number using RiveScript's internal RNG.
func (rs *RiveScript) randomInt(max int) int {
rs.randomLock.Lock()
defer rs.randomLock.Unlock()
return rs.rng.Intn(max)
}
// wordCount counts the number of real words in a string.
func wordCount(pattern string, all bool) int {
var words []string
if all {
words = strings.Fields(pattern) // Splits at whitespaces
} else {
words = regSplit(pattern, `[\s\*\#\_\|]+`)
}
wc := 0
for _, word := range words {
if len(word) > 0 {
wc++
}
}
return wc
}
// stripNasties strips special characters out of a string.
func stripNasties(pattern string) string {
return reNasties.ReplaceAllString(pattern, "")
}
// isAtomic tells you whether a string is atomic or not.
func isAtomic(pattern string) bool {
// Atomic triggers don't contain any wildcards or parenthesis or anything of
// the sort. We don't need to test the full character set, just left brackets
// will do.
specials := []string{"*", "#", "_", "(", "[", "<", "@"}
for _, special := range specials {
if strings.Contains(pattern, special) {
return false
}
}
return true
}
// stringFormat formats a string.
func stringFormat(format string, text string) string {
if format == "uppercase" {
return strings.ToUpper(text)
} else if format == "lowercase" {
return strings.ToLower(text)
} else if format == "sentence" {
if len(text) > 1 {
return strings.ToUpper(text[0:1]) + strings.ToLower(text[1:])
}
return strings.ToUpper(text)
} else if format == "formal" {
words := strings.Split(text, " ")
result := []string{}
for _, word := range words {
if len(word) > 1 {
result = append(result, strings.ToUpper(word[0:1])+strings.ToLower(word[1:]))
} else {
result = append(result, strings.ToUpper(word))
}
}
return strings.Join(result, " ")
}
return text
}
// quotemeta escapes a string for use in a regular expression.
func quotemeta(pattern string) string {
unsafe := `\.+*?[^]$(){}=!<>|:`
for _, char := range strings.Split(unsafe, "") {
pattern = strings.Replace(pattern, char, fmt.Sprintf("\\%s", char), -1)
}
return pattern
}
// Sort a list of strings by length. Callable like:
// sort.Sort(byLength(strings)) where strings is a []string type.
// https://gobyexample.com/sorting-by-functions
type byLength []string
func (s byLength) Len() int {
return len(s)
}
func (s byLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byLength) Less(i, j int) bool {
return len(s[i]) < len(s[j])
}
// regSplit splits a string using a regular expression.
// http://stackoverflow.com/questions/4466091/split-string-using-regular-expression-in-go
func regSplit(text string, delimiter string) []string {
reg := regexp.MustCompile(delimiter)
indexes := reg.FindAllStringIndex(text, -1)
lastStart := 0
result := make([]string, len(indexes)+1)
for i, element := range indexes {
result[i] = text[lastStart:element[0]]
lastStart = element[1]
}
result[len(indexes)] = text[lastStart:]
return result
}
/*
regReplace quickly replaces a string using a regular expression.
This is a convenience function to do a RegExp-based find/replace in a
JavaScript-like fashion. Example usage:
message = regReplace(message, `hello (.+?)`, "goodbye $1")
Params:
input: The input string to run the substitution against.
pattern: Literal string for a regular expression pattern.
result: String to substitute the result out for. You can use capture group
placeholders like $1 in this string.
*/
func regReplace(input string, pattern string, result string) string {
reg := regexp.MustCompile(pattern)
match := reg.FindStringSubmatch(input)
input = reg.ReplaceAllString(input, result)
if len(match) > 1 {
for i := range match[1:] {
input = strings.Replace(input, fmt.Sprintf("$%d", i), match[i], -1)
}
}
return input
}