Skip to content

Commit

Permalink
binary: fix encoding attribute count when there are empty values (#658)
Browse files Browse the repository at this point in the history
Co-authored-by: Tulir Asokan <tulir@maunium.net>
  • Loading branch information
nlitsme and tulir authored Sep 17, 2024
1 parent ea8c175 commit 061c065
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions binary/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (w *binaryEncoder) writeNode(n Node) {
hasContent = 1
}

w.writeListStart(2*len(n.Attrs) + tagSize + hasContent)
w.writeListStart(2*w.countAttributes(n.Attrs) + tagSize + hasContent)
w.writeString(n.Tag)
w.writeAttributes(n.Attrs)
if n.Content != nil {
Expand Down Expand Up @@ -187,10 +187,6 @@ func (w *binaryEncoder) writeJID(jid types.JID) {
}

func (w *binaryEncoder) writeAttributes(attributes Attrs) {
if attributes == nil {
return
}

for key, val := range attributes {
if val == "" || val == nil {
continue
Expand All @@ -201,6 +197,16 @@ func (w *binaryEncoder) writeAttributes(attributes Attrs) {
}
}

func (w *binaryEncoder) countAttributes(attributes Attrs) (count int) {
for _, val := range attributes {
if val == "" || val == nil {
continue
}
count += 1
}
return
}

func (w *binaryEncoder) writeListStart(listSize int) {
if listSize == 0 {
w.pushByte(byte(token.ListEmpty))
Expand Down

0 comments on commit 061c065

Please sign in to comment.