More work on the transfer kitten

This commit is contained in:
Kovid Goyal
2023-04-28 17:25:17 +05:30
parent a3640b21ac
commit be7f276d3e
4 changed files with 310 additions and 1 deletions

View File

@@ -30,6 +30,8 @@ func Reversed[T any](s []T) []T {
func Remove[T comparable](s []T, q T) []T {
idx := slices.Index(s, q)
if idx > -1 {
var zero T
s[idx] = zero // if pointer this allows garbage collection
return slices.Delete(s, idx, idx+1)
}
return s

View File

@@ -13,8 +13,10 @@ import (
"os/user"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"unicode/utf8"
"golang.org/x/sys/unix"
)
@@ -291,3 +293,29 @@ func ResolveConfPath(path string) string {
}
return cs
}
// Longest common path. Must be passed paths that have been cleaned by filepath.Clean
func Commonpath(paths ...string) (longest_prefix string) {
switch len(paths) {
case 0:
return
case 1:
return paths[0]
default:
sort.Strings(paths)
a, b := paths[0], paths[len(paths)-1]
sz := 0
for a != "" && b != "" {
ra, na := utf8.DecodeRuneInString(a)
rb, nb := utf8.DecodeRuneInString(b)
if ra != rb {
break
}
sz += na
a = a[na:]
b = b[nb:]
}
longest_prefix = a[:sz]
}
return
}