Add sortkeys (#2118)

* added Args.SortKeys

* add test for SortKeys

* fix comment
This commit is contained in:
pj
2025-12-30 05:00:35 +11:00
committed by GitHub
parent 42f89fbefd
commit 7b5cb77b95
2 changed files with 25 additions and 0 deletions
+9
View File
@@ -147,6 +147,15 @@ func (a *Args) Sort(f func(x, y []byte) int) {
})
}
// SortKeys sorts Args by key only using 'f' as comparison function.
//
// For example args.SortKeys(bytes.Compare).
func (a *Args) SortKeys(f func(x, y []byte) int) {
sort.SliceStable(a.args, func(i, j int) bool {
return f(a.args[i].key, a.args[j].key) == -1
})
}
// AppendBytes appends query string to dst and returns the extended dst.
func (a *Args) AppendBytes(dst []byte) []byte {
for i, n := 0, len(a.args); i < n; i++ {
+16
View File
@@ -101,6 +101,22 @@ func TestArgsAdd(t *testing.T) {
}
}
func TestArgsSortKeys(t *testing.T) {
t.Parallel()
var a Args
a.Add("a", "789")
a.Add("b", "456")
a.Add("a", "123")
a.SortKeys(bytes.Compare)
s := a.String()
expectedS := "a=789&a=123&b=456"
if s != expectedS {
t.Fatalf("unexpected result: %q. Expecting %q", s, expectedS)
}
}
func TestArgsAcquireReleaseSequential(t *testing.T) {
testArgsAcquireRelease(t)
}