-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathioutil.go
More file actions
92 lines (84 loc) · 2.07 KB
/
Copy pathioutil.go
File metadata and controls
92 lines (84 loc) · 2.07 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
82
83
84
85
86
87
88
89
90
91
92
package repomap
import (
"path/filepath"
"runtime"
"golang.org/x/sync/errgroup"
)
// collectNonNil filters nil pointers from a slice.
func collectNonNil[T any](slice []*T) []*T {
var result []*T
for _, v := range slice {
if v != nil {
result = append(result, v)
}
}
return result
}
// relPath returns the relative path from root to path, falling back to path on error.
func relPath(root, path string) string {
rel, err := filepath.Rel(root, path)
if err != nil {
return path
}
return rel
}
// parallelParse runs fn on each item in parallel using an errgroup bounded
// to NumCPU goroutines. Returns non-nil results in input order.
func parallelParse[T any](items []T, fn func(T) *FileSymbols) []*FileSymbols {
results := make([]*FileSymbols, len(items))
g := new(errgroup.Group)
g.SetLimit(runtime.NumCPU())
for i, item := range items {
g.Go(func() error {
results[i] = fn(item)
return nil
})
}
_ = g.Wait()
return collectNonNil(results)
}
// longestCommonPrefix returns the longest common prefix of a sorted slice of strings.
// The prefix is trimmed to an identifier boundary (underscore or camelCase).
// Operates on runes to avoid splitting multi-byte UTF-8 sequences.
func longestCommonPrefix(names []string) string {
if len(names) == 0 {
return ""
}
prefix := []rune(names[0])
for _, name := range names[1:] {
nameR := []rune(name)
// Trim prefix to the common run with nameR.
n := len(prefix)
if len(nameR) < n {
n = len(nameR)
}
i := 0
for i < n && prefix[i] == nameR[i] {
i++
}
prefix = prefix[:i]
if len(prefix) == 0 {
return ""
}
}
return trimIdentifierPrefix(string(prefix))
}
func trimIdentifierPrefix(prefix string) string {
if prefix == "" {
return ""
}
runes := []rune(prefix)
lastBoundary := -1
for i := 1; i < len(runes); i++ {
if runes[i] == '_' || isCamelBoundary(runes[i-1], runes[i]) {
lastBoundary = i
}
}
if lastBoundary > 0 {
return string(runes[:lastBoundary])
}
return prefix
}
func isCamelBoundary(prev, curr rune) bool {
return prev >= 'a' && prev <= 'z' && curr >= 'A' && curr <= 'Z'
}