1
0
mirror of https://github.com/garraflavatra/go-fmp.git synced 2025-06-28 04:25:11 +00:00

Restructure, add script step constants

This commit is contained in:
2025-06-14 16:28:41 +02:00
parent 53134a0d09
commit 10285084eb
8 changed files with 390 additions and 200 deletions

View File

@ -1,5 +1,56 @@
package fmp
import "slices"
type FmpDict map[uint64]*FmpDictEntry
type FmpDictEntry struct {
Value []byte
Children *FmpDict
}
func (dict *FmpDict) GetEntry(path []uint64) *FmpDictEntry {
for i, key := range path {
_, ok := (*dict)[key]
if !ok {
return nil
}
if i == len(path)-1 {
return (*dict)[key]
} else {
dict = (*dict)[key].Children
if dict == nil {
return nil
}
}
}
return nil
}
func (dict *FmpDict) GetValue(path []uint64) []byte {
ent := dict.GetEntry(path)
if ent != nil {
return ent.Value
}
return nil
}
func (dict *FmpDict) SetValue(path []uint64, value []byte) {
for i, key := range path {
_, ok := (*dict)[key]
if !ok {
(*dict)[key] = &FmpDictEntry{Children: &FmpDict{}}
}
if i == len(path)-1 {
(*dict)[key].Value = value
} else {
dict = (*dict)[key].Children
}
}
}
func parseVarUint64(payload []byte) uint64 {
var length uint64
n := min(len(payload), 8) // clamp to uint64
@ -17,3 +68,23 @@ func decodeByteSeq(payload []byte) string {
}
return result
}
func addIf(cond bool, val uint64) uint64 {
if cond {
return val
}
return 0
}
func slicesHaveSameElements[Type comparable](a, b []Type) bool {
if len(a) != len(b) {
return false
}
for _, av := range a {
found := slices.Contains(b, av)
if !found {
return false
}
}
return true
}