From 86e37a25fab2bab8e0a0e5977402b5959a26a46d Mon Sep 17 00:00:00 2001 From: itsme Date: Thu, 12 Sep 2024 14:05:21 +0200 Subject: [PATCH 1/3] corrected count for writeListStart, since writeAttributes omits empty values --- binary/encoder.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/binary/encoder.go b/binary/encoder.go index 21e22cf8..49f26592 100644 --- a/binary/encoder.go +++ b/binary/encoder.go @@ -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 { @@ -201,6 +201,21 @@ func (w *binaryEncoder) writeAttributes(attributes Attrs) { } } +func (w *binaryEncoder) countAttributes(attributes Attrs) int { + if attributes == nil { + return 0 + } + + var count = 0 + for _, val := range attributes { + if val == "" || val == nil { + continue + } + count += 1 + } + return count +} + func (w *binaryEncoder) writeListStart(listSize int) { if listSize == 0 { w.pushByte(byte(token.ListEmpty)) From 9cc22b5d88d1335ee24c42123f8261615bbc922b Mon Sep 17 00:00:00 2001 From: itsme Date: Thu, 12 Sep 2024 14:18:43 +0200 Subject: [PATCH 2/3] using tabs instead of spaces --- binary/encoder.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/binary/encoder.go b/binary/encoder.go index 49f26592..c982feb3 100644 --- a/binary/encoder.go +++ b/binary/encoder.go @@ -206,14 +206,14 @@ func (w *binaryEncoder) countAttributes(attributes Attrs) int { return 0 } - var count = 0 + var count = 0 for _, val := range attributes { if val == "" || val == nil { continue } - count += 1 + count += 1 } - return count + return count } func (w *binaryEncoder) writeListStart(listSize int) { From a75da60d3e063523fee31fa64dab94234aa74a3f Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 17 Sep 2024 12:36:53 +0300 Subject: [PATCH 3/3] Remove unnecessary lines --- binary/encoder.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/binary/encoder.go b/binary/encoder.go index c982feb3..eed07184 100644 --- a/binary/encoder.go +++ b/binary/encoder.go @@ -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 @@ -201,19 +197,14 @@ func (w *binaryEncoder) writeAttributes(attributes Attrs) { } } -func (w *binaryEncoder) countAttributes(attributes Attrs) int { - if attributes == nil { - return 0 - } - - var count = 0 +func (w *binaryEncoder) countAttributes(attributes Attrs) (count int) { for _, val := range attributes { if val == "" || val == nil { continue } count += 1 } - return count + return } func (w *binaryEncoder) writeListStart(listSize int) {