@@ -145,41 +145,59 @@ func (q *Query) init(sql string) {
145
145
}
146
146
147
147
func QuoteString (dst []byte , str string ) []byte {
148
- const quote = "'"
148
+ const quote = '\''
149
149
150
- n := strings .Count (str , quote )
150
+ // Preallocate space for the worst case scenario
151
+ dst = slices .Grow (dst , len (str )* 2 + 2 )
151
152
152
- dst = append (dst , quote ... )
153
+ // Add opening quote
154
+ dst = append (dst , quote )
153
155
154
- p := slices .Grow (dst [len (dst ):], 2 * len (quote )+ len (str )+ 2 * n )
155
-
156
- for len (str ) > 0 {
157
- i := strings .Index (str , quote )
158
- if i < 0 {
159
- p = append (p , str ... )
160
- break
156
+ // Iterate through the string without allocating
157
+ for i := 0 ; i < len (str ); i ++ {
158
+ if str [i ] == quote {
159
+ dst = append (dst , quote , quote )
160
+ } else {
161
+ dst = append (dst , str [i ])
161
162
}
162
- p = append (p , str [:i ]... )
163
- p = append (p , "''" ... )
164
- str = str [i + 1 :]
165
163
}
166
164
167
- dst = append (dst , p ... )
168
-
169
- dst = append (dst , quote ... )
165
+ // Add closing quote
166
+ dst = append (dst , quote )
170
167
171
168
return dst
172
169
}
173
170
174
171
func QuoteBytes (dst , buf []byte ) []byte {
175
- dst = append (dst , `'\x` ... )
172
+ if len (buf ) == 0 {
173
+ return append (dst , `'\x'` ... )
174
+ }
175
+
176
+ // Calculate required length
177
+ requiredLen := 3 + hex .EncodedLen (len (buf )) + 1
178
+
179
+ // Ensure dst has enough capacity
180
+ if cap (dst )- len (dst ) < requiredLen {
181
+ newDst := make ([]byte , len (dst ), len (dst )+ requiredLen )
182
+ copy (newDst , dst )
183
+ dst = newDst
184
+ }
185
+
186
+ // Record original length and extend slice
187
+ origLen := len (dst )
188
+ dst = dst [:origLen + requiredLen ]
189
+
190
+ // Add prefix
191
+ dst [origLen ] = '\''
192
+ dst [origLen + 1 ] = '\\'
193
+ dst [origLen + 2 ] = 'x'
194
+
195
+ // Encode bytes directly into dst
196
+ hex .Encode (dst [origLen + 3 :len (dst )- 1 ], buf )
176
197
177
- n := hex .EncodedLen (len (buf ))
178
- p := slices .Grow (dst [len (dst ):], n )[:n ]
179
- hex .Encode (p , buf )
180
- dst = append (dst , p ... )
198
+ // Add suffix
199
+ dst [len (dst )- 1 ] = '\''
181
200
182
- dst = append (dst , `'` ... )
183
201
return dst
184
202
}
185
203
0 commit comments