Skip to content

Commit

Permalink
Merge pull request #428 from Ektaros/no-for-loop
Browse files Browse the repository at this point in the history
Remove for loop from getFieldPathForName
  • Loading branch information
markus-wa authored Oct 2, 2023
2 parents b70c827 + b8a4f97 commit 28a80ab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/demoinfocs/sendtables2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (p *Parser) ParsePacket(b []byte) error {
}

// add the field to the serializer
serializer.fields = append(serializer.fields, fields[i])
serializer.addField(fields[i])
}

// store the serializer for field reference
Expand Down
43 changes: 33 additions & 10 deletions pkg/demoinfocs/sendtables2/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import (
"strings"
)

type fieldIndex struct {
index int
field *field
}

type serializer struct {
name string
version int32
fields []*field
name string
version int32
fields []*field
fieldIndexes map[string]*fieldIndex
}

func (s *serializer) id() string {
Expand Down Expand Up @@ -36,15 +42,18 @@ func (s *serializer) getFieldForFieldPath(fp *fieldPath, pos int) *field {
}

func (s *serializer) getFieldPathForName(fp *fieldPath, name string) bool {
for i, f := range s.fields {
if name == f.varName {
fp.path[fp.last] = i
return true
}
if s.fieldIndexes[name] != nil {
fp.path[fp.last] = s.fieldIndexes[name].index
return true
}

if strings.HasPrefix(name, f.varName+".") {
fp.path[fp.last] = i
dotIndex := strings.Index(name, ".")
if dotIndex != -1 {
nameBeforeDot := name[:dotIndex]
if s.fieldIndexes[nameBeforeDot] != nil {
fp.path[fp.last] = s.fieldIndexes[nameBeforeDot].index
fp.last++
f := s.fieldIndexes[nameBeforeDot].field
return f.getFieldPathForName(fp, name[len(f.varName)+1:])
}
}
Expand All @@ -64,3 +73,17 @@ func (s *serializer) getFieldPaths(fp *fieldPath, state *fieldState) []*fieldPat
func serializerId(name string, version int32) string {
return fmt.Sprintf("%s(%d)", name, version)
}

func (s *serializer) addField(f *field) {
newFieldIndex := len(s.fields)
s.fields = append(s.fields, f)

if s.fieldIndexes == nil {
s.fieldIndexes = make(map[string]*fieldIndex)
}

s.fieldIndexes[f.varName] = &fieldIndex{
index: newFieldIndex,
field: f,
}
}

0 comments on commit 28a80ab

Please sign in to comment.