This commit is contained in:
parent
dbc865ee67
commit
562c839835
37 changed files with 742 additions and 45 deletions
6
src/dll/Library.go
Normal file
6
src/dll/Library.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package dll
|
||||
|
||||
type Library struct {
|
||||
Name string
|
||||
Functions []string
|
||||
}
|
55
src/dll/List.go
Normal file
55
src/dll/List.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package dll
|
||||
|
||||
import "slices"
|
||||
|
||||
// List is a slice of DLLs.
|
||||
type List []Library
|
||||
|
||||
// Append adds a function for the given DLL if it doesn't exist yet.
|
||||
func (list List) Append(dllName string, funcName string) List {
|
||||
for i, dll := range list {
|
||||
if dll.Name != dllName {
|
||||
continue
|
||||
}
|
||||
|
||||
if slices.Contains(dll.Functions, funcName) {
|
||||
return list
|
||||
}
|
||||
|
||||
list[i].Functions = append(list[i].Functions, funcName)
|
||||
return list
|
||||
}
|
||||
|
||||
return append(list, Library{Name: dllName, Functions: []string{funcName}})
|
||||
}
|
||||
|
||||
// Contains returns true if the library exists.
|
||||
func (list List) Contains(dllName string) bool {
|
||||
for _, dll := range list {
|
||||
if dll.Name == dllName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Index returns the position of the given function name.
|
||||
func (list List) Index(dllName string, funcName string) int {
|
||||
index := 0
|
||||
|
||||
for _, dll := range list {
|
||||
if dll.Name != dllName {
|
||||
index += len(dll.Functions) + 1
|
||||
continue
|
||||
}
|
||||
|
||||
for i, fn := range dll.Functions {
|
||||
if fn == funcName {
|
||||
return index + i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
34
src/dll/List_test.go
Normal file
34
src/dll/List_test.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package dll_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.urbach.dev/cli/q/src/dll"
|
||||
"git.urbach.dev/go/assert"
|
||||
)
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
libs := dll.List{}
|
||||
assert.False(t, libs.Contains("kernel32"))
|
||||
assert.Equal(t, -1, libs.Index("kernel32", "ExitProcess"))
|
||||
|
||||
libs = libs.Append("kernel32", "ExitProcess")
|
||||
assert.True(t, libs.Contains("kernel32"))
|
||||
assert.Equal(t, 0, libs.Index("kernel32", "ExitProcess"))
|
||||
|
||||
libs = libs.Append("user32", "MessageBox")
|
||||
assert.True(t, libs.Contains("kernel32"))
|
||||
assert.True(t, libs.Contains("user32"))
|
||||
assert.Equal(t, 0, libs.Index("kernel32", "ExitProcess"))
|
||||
assert.Equal(t, 2, libs.Index("user32", "MessageBox"))
|
||||
|
||||
libs = libs.Append("kernel32", "SetConsoleCP")
|
||||
assert.Equal(t, 0, libs.Index("kernel32", "ExitProcess"))
|
||||
assert.Equal(t, 1, libs.Index("kernel32", "SetConsoleCP"))
|
||||
assert.Equal(t, 3, libs.Index("user32", "MessageBox"))
|
||||
|
||||
libs = libs.Append("kernel32", "ExitProcess")
|
||||
assert.Equal(t, 0, libs.Index("kernel32", "ExitProcess"))
|
||||
assert.Equal(t, 1, libs.Index("kernel32", "SetConsoleCP"))
|
||||
assert.Equal(t, 3, libs.Index("user32", "MessageBox"))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue