-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.go
More file actions
81 lines (72 loc) · 1.29 KB
/
helpers.go
File metadata and controls
81 lines (72 loc) · 1.29 KB
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
package str
import (
"strings"
"unicode"
"unicode/utf8"
)
func clampRange(start, end, length int) (int, int) {
if start < 0 {
start = 0
}
if end < 0 {
end = 0
}
if start > length {
start = length
}
if end > length {
end = length
}
return start, end
}
func runeSubstring(s string, start, end int) string {
runes := []rune(s)
start, end = clampRange(start, end, len(runes))
if start >= end {
return ""
}
return string(runes[start:end])
}
func runeIndex(s, sub string, last bool) int {
if sub == "" {
return 0
}
var byteIdx int
if last {
byteIdx = strings.LastIndex(s, sub)
} else {
byteIdx = strings.Index(s, sub)
}
if byteIdx == -1 {
return -1
}
return utf8.RuneCountInString(s[:byteIdx])
}
func splitWordsRunes(s string) []string {
var words []string
var buf []rune
var prev rune
flush := func() {
if len(buf) == 0 {
return
}
words = append(words, string(buf))
buf = buf[:0]
}
for _, r := range s {
isWord := unicode.IsLetter(r) || unicode.IsDigit(r)
if !isWord {
flush()
prev = 0
continue
}
// camelCase / PascalCase split: lower-to-upper transition starts new word.
if len(buf) > 0 && unicode.IsUpper(r) && (unicode.IsLower(prev) || unicode.IsDigit(prev)) {
flush()
}
buf = append(buf, r)
prev = r
}
flush()
return words
}