Ai
1 Star 0 Fork 0

loll/req

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sort.go 795 Bytes
一键复制 编辑 原始数据 按行查看 历史
loll 提交于 2024-12-04 10:58 +08:00 . first commit
package header
import (
"net/textproto"
"sort"
)
type KeyValues struct {
Key string
Values []string
}
type sorter struct {
order map[string]int
kvs []KeyValues
}
func (s *sorter) Len() int { return len(s.kvs) }
func (s *sorter) Swap(i, j int) { s.kvs[i], s.kvs[j] = s.kvs[j], s.kvs[i] }
func (s *sorter) Less(i, j int) bool {
if index, ok := s.order[textproto.CanonicalMIMEHeaderKey(s.kvs[i].Key)]; ok {
i = index
}
if index, ok := s.order[textproto.CanonicalMIMEHeaderKey(s.kvs[j].Key)]; ok {
j = index
}
return i < j
}
func SortKeyValues(kvs []KeyValues, orderedKeys []string) {
order := make(map[string]int)
for i, key := range orderedKeys {
order[textproto.CanonicalMIMEHeaderKey(key)] = i
}
s := &sorter{
order: order,
kvs: kvs,
}
sort.Sort(s)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lollity/req.git
git@gitee.com:lollity/req.git
lollity
req
req
v1.1.0

搜索帮助