mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Add sortkeys (#2118)
* added Args.SortKeys * add test for SortKeys * fix comment
This commit is contained in:
@@ -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++ {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user