From efea2565061f44d83befccd48bca7e53022eec1b Mon Sep 17 00:00:00 2001 From: srpvpn Date: Sun, 28 Dec 2025 23:57:42 +0300 Subject: [PATCH] refactor: Update node fstring methods to append to a strings.Builder instead of returning a string. --- trie/node.go | 53 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/trie/node.go b/trie/node.go index 74fac4fd4e..ac5c084503 100644 --- a/trie/node.go +++ b/trie/node.go @@ -30,7 +30,7 @@ var indices = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b type node interface { cache() (hashNode, bool) encode(w rlp.EncoderBuffer) - fstring(string) string + fstring(string, *strings.Builder) } type ( @@ -94,31 +94,52 @@ func (n hashNode) cache() (hashNode, bool) { return nil, true } func (n valueNode) cache() (hashNode, bool) { return nil, true } // Pretty printing. -func (n *fullNode) String() string { return n.fstring("") } -func (n *shortNode) String() string { return n.fstring("") } -func (n hashNode) String() string { return n.fstring("") } -func (n valueNode) String() string { return n.fstring("") } +func (n *fullNode) String() string { + var b strings.Builder + n.fstring("", &b) + return b.String() +} +func (n *shortNode) String() string { + var b strings.Builder + n.fstring("", &b) + return b.String() +} +func (n hashNode) String() string { + var b strings.Builder + n.fstring("", &b) + return b.String() +} +func (n valueNode) String() string { + var b strings.Builder + n.fstring("", &b) + return b.String() +} -func (n *fullNode) fstring(ind string) string { - resp := fmt.Sprintf("[\n%s ", ind) +func (n *fullNode) fstring(ind string, b *strings.Builder) { + fmt.Fprintf(b, "[\n%s ", ind) for i, node := range &n.Children { if node == nil { - resp += fmt.Sprintf("%s: ", indices[i]) + fmt.Fprintf(b, "%s: ", indices[i]) } else { - resp += fmt.Sprintf("%s: %v", indices[i], node.fstring(ind+" ")) + fmt.Fprintf(b, "%s: ", indices[i]) + node.fstring(ind+" ", b) } } - return resp + fmt.Sprintf("\n%s] ", ind) + fmt.Fprintf(b, "\n%s] ", ind) } -func (n *shortNode) fstring(ind string) string { - return fmt.Sprintf("{%x: %v} ", n.Key, n.Val.fstring(ind+" ")) +func (n *shortNode) fstring(ind string, b *strings.Builder) { + fmt.Fprintf(b, "{%x: ", n.Key) + n.Val.fstring(ind+" ", b) + b.WriteString("} ") } -func (n hashNode) fstring(ind string) string { - return fmt.Sprintf("<%x> ", []byte(n)) + +func (n hashNode) fstring(ind string, b *strings.Builder) { + fmt.Fprintf(b, "<%x> ", []byte(n)) } -func (n valueNode) fstring(ind string) string { - return fmt.Sprintf("%x ", []byte(n)) + +func (n valueNode) fstring(ind string, b *strings.Builder) { + fmt.Fprintf(b, "%x ", []byte(n)) } // mustDecodeNode is a wrapper of decodeNode and panic if any error is encountered.