-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
80 lines (68 loc) · 1.21 KB
/
helpers.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
package verbo
import (
"regexp"
"unicode/utf8"
)
func adjacent(str string, direction int) string {
if utf8.RuneCountInString(str) == 0 {
return ""
}
return string(int(str[utf8.RuneCountInString(str)-1]) + direction)
}
func defaultToWhiteSpace(characters string) string {
if characters == "" {
return "\\s"
} else {
return "[" + escapeRegExp(characters) + "]"
}
}
func escapeRegExp(str string) string {
re := regexp.MustCompile(`([.*+?^=!:${}()|[\]\/\\])`)
return re.ReplaceAllString(str, `\\$1`)
}
func sizeOfLastRunes(str string, count int) int {
return len(str[len(str) - count:])
}
func stringLengthToRunesSize(str string, length int) int {
count := utf8.RuneCountInString(str)
if count < length {
length = count
}
size := 0
i := 0
for i < length {
_, s := utf8.DecodeRuneInString(str)
str = str[s:]
size += s
i++
}
return size
}
func min(a, b int) int {
if a <= b {
return a
} else {
return b
}
}
func strRepeat(str string, qty int) string {
if qty < 1 {
return ""
}
result := ""
for qty > 0 {
if qty&1 == 1 {
result += str
}
qty >>= 1
str += str
}
return result
}
func toPositive(number int) int {
if number < 0 {
return 0
} else {
return +number
}
}