Add pure process-path to class normalization for Windows
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
// Package winapi is the Windows Win32 binding layer for AntiDrift's OS ports.
|
||||||
|
// The syscall-bound code lives in windows-tagged files; this untagged file
|
||||||
|
// holds the pure path logic so it builds and is tested on every platform.
|
||||||
|
package winapi
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// ClassFromImagePath derives the on-task class identity from a process image
|
||||||
|
// path: the base file name with any trailing ".exe" removed (case-insensitive),
|
||||||
|
// lowercased. It is the Windows analog of an X11 WM_CLASS. It parses both `\`
|
||||||
|
// and `/` separators so it is correct regardless of the host OS running the
|
||||||
|
// test. Returns "" for an empty path.
|
||||||
|
func ClassFromImagePath(p string) string {
|
||||||
|
if i := strings.LastIndexAny(p, `\/`); i >= 0 {
|
||||||
|
p = p[i+1:]
|
||||||
|
}
|
||||||
|
if len(p) >= 4 && strings.EqualFold(p[len(p)-4:], ".exe") {
|
||||||
|
p = p[:len(p)-4]
|
||||||
|
}
|
||||||
|
return strings.ToLower(p)
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package winapi
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestClassFromImagePath(t *testing.T) {
|
||||||
|
cases := []struct{ in, want string }{
|
||||||
|
{`C:\Program Files\Microsoft VS Code\Code.exe`, "code"},
|
||||||
|
{`C:\Windows\explorer.exe`, "explorer"},
|
||||||
|
{`chrome.exe`, "chrome"},
|
||||||
|
{`C:\x\FOO.EXE`, "foo"},
|
||||||
|
{`C:\x\My.App.exe`, "my.app"},
|
||||||
|
{`C:/forward/slash/Code.exe`, "code"},
|
||||||
|
{`firefox`, "firefox"},
|
||||||
|
{``, ""},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
if got := ClassFromImagePath(c.in); got != c.want {
|
||||||
|
t.Errorf("ClassFromImagePath(%q) = %q, want %q", c.in, got, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user