Last active
October 11, 2021 08:03
-
-
Save DrSensor/0c498e24ff727e5b4760b8459e005fef to your computer and use it in GitHub Desktop.
Questionable Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2021 Fahmi Akbar Wildana <~drsensor/[email protected]> | |
// SPDX-License-Identifier: FSFAP | |
package utils | |
import ( | |
"path/filepath" | |
"sort" | |
"strings" | |
) | |
// BUG: probably 🤔 (plus it's not optimized) | |
// BUG: updir `..` not handled properly | |
// TODO: handle absoulute path | |
// TODO: document this algorithm step-by-step-explain-output as Stack Overflow question | |
func NearestWorkDirs(paths []string) []string { | |
if !sort.StringsAreSorted(paths) { | |
sort.Strings(paths) | |
} | |
var workdir []string | |
keys := map[string]bool{} | |
reset := func() { | |
paths = workdir | |
workdir = []string{} | |
keys = map[string]bool{} | |
} | |
depths := map[string]int{} // depth of path | |
for _, entry := range paths { | |
if filepath.Ext(entry) != "" { | |
entry = filepath.Dir(entry) | |
} | |
if _, isDuplicate := keys[entry]; !isDuplicate { | |
keys[entry] = true | |
workdir = append(workdir, entry) | |
if spath := SlicePath(entry); spath[0] == ".." { | |
k := Count(spath, "..") | |
depths[filepath.Join(spath[:k]...)] = len(spath[k:]) | |
} else { | |
depths[spath[0]] = len(spath) | |
} | |
} | |
} | |
reset() | |
counts := map[string]int{} // count spath[0] | |
for _, entry := range paths { | |
spath := SlicePath(entry) | |
if depths[spath[0]] == 1 && len(spath) > 1 { | |
entry = filepath.Dir(entry) | |
} | |
if _, isDuplicate := keys[entry]; !isDuplicate { | |
keys[entry] = true | |
workdir = append(workdir, entry) | |
if spath := SlicePath(entry); spath[0] != ".." { | |
counts[spath[0]]++ | |
} | |
} | |
} | |
reset() | |
for _, entry := range paths { // dedupe based on spath[0] | |
if spath := SlicePath(entry); counts[spath[0]] > 1 && len(spath) > 1 { | |
entry = filepath.Join(spath[:len(spath)-depths[spath[0]]+1]...) | |
} | |
if _, isDuplicate := keys[entry]; !isDuplicate { | |
keys[entry] = true | |
workdir = append(workdir, entry) | |
} | |
} | |
return workdir | |
} | |
func SlicePath(pathfile string) []string { | |
return strings.Split(filepath.ToSlash(pathfile), "/") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment