Skip to content

Commit b59f47e

Browse files
authored
Refactor: split delAllArgs into delAllArgs and delAllArgsStable (#1945)
- Renamed the original `delAllArgs` method to `delAllArgsStable` to maintain stable behavior. - Added a new `delAllArgs` method for non-stable functionality, improving runtime efficiency.
1 parent bb94b26 commit b59f47e

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

args.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ func (a *Args) WriteTo(w io.Writer) (int64, error) {
155155

156156
// Del deletes argument with the given key from query args.
157157
func (a *Args) Del(key string) {
158-
a.args = delAllArgs(a.args, key)
158+
a.args = delAllArgsStable(a.args, key)
159159
}
160160

161161
// DelBytes deletes argument with the given key from query args.
162162
func (a *Args) DelBytes(key []byte) {
163-
a.args = delAllArgs(a.args, b2s(key))
163+
a.args = delAllArgsStable(a.args, b2s(key))
164164
}
165165

166166
// Add adds 'key=value' argument.
@@ -385,11 +385,7 @@ func copyArgs(dst, src []argsKV) []argsKV {
385385
return dst
386386
}
387387

388-
func delAllArgsBytes(args []argsKV, key []byte) []argsKV {
389-
return delAllArgs(args, b2s(key))
390-
}
391-
392-
func delAllArgs(args []argsKV, key string) []argsKV {
388+
func delAllArgsStable(args []argsKV, key string) []argsKV {
393389
for i, n := 0, len(args); i < n; i++ {
394390
kv := &args[i]
395391
if key == string(kv.key) {
@@ -404,6 +400,18 @@ func delAllArgs(args []argsKV, key string) []argsKV {
404400
return args
405401
}
406402

403+
func delAllArgs(args []argsKV, key string) []argsKV {
404+
n := len(args)
405+
for i := 0; i < n; i++ {
406+
if key == string(args[i].key) {
407+
args[i], args[n-1] = args[n-1], args[i]
408+
n--
409+
i--
410+
}
411+
}
412+
return args[:n]
413+
}
414+
407415
func setArgBytes(h []argsKV, key, value []byte, noValue bool) []argsKV {
408416
return setArg(h, b2s(key), b2s(value), noValue)
409417
}

header.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (h *ResponseHeader) SetConnectionClose() {
193193
func (h *ResponseHeader) ResetConnectionClose() {
194194
if h.connectionClose {
195195
h.connectionClose = false
196-
h.h = delAllArgsBytes(h.h, strConnection)
196+
h.h = delAllArgs(h.h, HeaderConnection)
197197
}
198198
}
199199

@@ -211,7 +211,7 @@ func (h *RequestHeader) SetConnectionClose() {
211211
func (h *RequestHeader) ResetConnectionClose() {
212212
if h.connectionClose {
213213
h.connectionClose = false
214-
h.h = delAllArgsBytes(h.h, strConnection)
214+
h.h = delAllArgs(h.h, HeaderConnection)
215215
}
216216
}
217217

@@ -251,7 +251,7 @@ func (h *ResponseHeader) SetContentLength(contentLength int) {
251251
h.contentLength = contentLength
252252
if contentLength >= 0 {
253253
h.contentLengthBytes = AppendUint(h.contentLengthBytes[:0], contentLength)
254-
h.h = delAllArgsBytes(h.h, strTransferEncoding)
254+
h.h = delAllArgs(h.h, HeaderTransferEncoding)
255255
return
256256
} else if contentLength == -1 {
257257
h.contentLengthBytes = h.contentLengthBytes[:0]
@@ -296,7 +296,7 @@ func (h *RequestHeader) SetContentLength(contentLength int) {
296296
h.contentLength = contentLength
297297
if contentLength >= 0 {
298298
h.contentLengthBytes = AppendUint(h.contentLengthBytes[:0], contentLength)
299-
h.h = delAllArgsBytes(h.h, strTransferEncoding)
299+
h.h = delAllArgs(h.h, HeaderTransferEncoding)
300300
} else {
301301
h.contentLengthBytes = h.contentLengthBytes[:0]
302302
h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
@@ -1360,7 +1360,7 @@ func (h *ResponseHeader) del(key []byte) {
13601360
case HeaderTrailer:
13611361
h.trailer = h.trailer[:0]
13621362
}
1363-
h.h = delAllArgsBytes(h.h, key)
1363+
h.h = delAllArgs(h.h, b2s(key))
13641364
}
13651365

13661366
// Del deletes header with the given key.
@@ -1394,7 +1394,7 @@ func (h *RequestHeader) del(key []byte) {
13941394
case HeaderTrailer:
13951395
h.trailer = h.trailer[:0]
13961396
}
1397-
h.h = delAllArgsBytes(h.h, key)
1397+
h.h = delAllArgs(h.h, b2s(key))
13981398
}
13991399

14001400
// setSpecialHeader handles special headers and return true when a header is processed.

0 commit comments

Comments
 (0)