File tree Expand file tree Collapse file tree 2 files changed +47
-12
lines changed
Expand file tree Collapse file tree 2 files changed +47
-12
lines changed Original file line number Diff line number Diff line change 1818
1919package ql
2020
21- import "fmt"
21+ import (
22+ "strings"
23+ )
2224
2325// Node represents a node in the abstract syntax tree.
2426type Node interface {
@@ -36,7 +38,14 @@ type ParenExpr struct {
3638}
3739
3840// String returns a string representation of the parenthesized expression.
39- func (e * ParenExpr ) String () string { return fmt .Sprintf ("(%s)" , e .Expr .String ()) }
41+ func (e * ParenExpr ) String () string {
42+ var b strings.Builder
43+ b .Grow (len (e .Expr .String ()) + 2 )
44+ b .WriteString ("(" )
45+ b .WriteString (e .Expr .String ())
46+ b .WriteString (")" )
47+ return b .String ()
48+ }
4049
4150// BinaryExpr represents an operation between two expressions.
4251type BinaryExpr struct {
@@ -47,7 +56,20 @@ type BinaryExpr struct {
4756
4857// String returns a string representation of the binary expression.
4958func (e * BinaryExpr ) String () string {
50- return fmt .Sprintf ("%s %s %s" , e .LHS .String (), e .Op .String (), e .RHS .String ())
59+ var b strings.Builder
60+
61+ lhs := e .LHS .String ()
62+ op := e .Op .String ()
63+ rhs := e .RHS .String ()
64+
65+ b .Grow (len (lhs ) + len (op ) + len (rhs ) + 2 )
66+ b .WriteString (lhs )
67+ b .WriteString (" " )
68+ b .WriteString (op )
69+ b .WriteString (" " )
70+ b .WriteString (rhs )
71+
72+ return b .String ()
5173}
5274
5375// NotExpr represents an unary not expression.
@@ -56,4 +78,11 @@ type NotExpr struct {
5678}
5779
5880// String returns a string representation of the not expression.
59- func (e * NotExpr ) String () string { return fmt .Sprintf ("(%s)" , e .Expr .String ()) }
81+ func (e * NotExpr ) String () string {
82+ var b strings.Builder
83+ b .Grow (len (e .Expr .String ()) + 2 )
84+ b .WriteString ("(" )
85+ b .WriteString (e .Expr .String ())
86+ b .WriteString (")" )
87+ return b .String ()
88+ }
Original file line number Diff line number Diff line change 1919package ql
2020
2121import (
22- "bytes"
2322 "fmt"
2423 "github.com/rabbitstack/fibratus/pkg/filter/fields"
2524 "github.com/rabbitstack/fibratus/pkg/kevent"
@@ -129,16 +128,23 @@ type ListLiteral struct {
129128
130129// String returns a string representation of the literal.
131130func (s * ListLiteral ) String () string {
132- var buf bytes.Buffer
133- _ , _ = buf .WriteString ("(" )
134- for idx , tagKey := range s .Values {
131+ var n int
132+ for _ , elem := range s .Values {
133+ n += len (elem ) + 2
134+ }
135+
136+ var b strings.Builder
137+ b .Grow (n + 2 )
138+ b .WriteString ("(" )
139+ for idx , elem := range s .Values {
135140 if idx != 0 {
136- _ , _ = buf .WriteString (", " )
141+ b .WriteString (", " )
137142 }
138- _ , _ = buf .WriteString (tagKey )
143+ b .WriteString (elem )
139144 }
140- _ , _ = buf .WriteString (")" )
141- return buf .String ()
145+ b .WriteString (")" )
146+
147+ return b .String ()
142148}
143149
144150// Function represents a function call.
You can’t perform that action at this time.
0 commit comments