mirror of
https://github.com/golang/go
synced 2024-11-12 05:50:21 -07:00
net/http: avoid fmt.Fprintf in Header.WriteSubset
R=golang-dev, dsymonds, r CC=golang-dev https://golang.org/cl/6242062
This commit is contained in:
parent
1e814df79b
commit
0605c0c656
@ -5,7 +5,6 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"sort"
|
"sort"
|
||||||
@ -61,7 +60,7 @@ var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")
|
|||||||
func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error {
|
func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error {
|
||||||
keys := make([]string, 0, len(h))
|
keys := make([]string, 0, len(h))
|
||||||
for k := range h {
|
for k := range h {
|
||||||
if exclude == nil || !exclude[k] {
|
if !exclude[k] {
|
||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,11 +69,13 @@ func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error {
|
|||||||
for _, v := range h[k] {
|
for _, v := range h[k] {
|
||||||
v = headerNewlineToSpace.Replace(v)
|
v = headerNewlineToSpace.Replace(v)
|
||||||
v = strings.TrimSpace(v)
|
v = strings.TrimSpace(v)
|
||||||
if _, err := fmt.Fprintf(w, "%s: %s\r\n", k, v); err != nil {
|
for _, s := range []string{k, ": ", v, "\r\n"} {
|
||||||
|
if _, err := io.WriteString(w, s); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,3 +122,17 @@ func TestHasToken(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkHeaderWriteSubset(b *testing.B) {
|
||||||
|
h := Header(map[string][]string{
|
||||||
|
"Content-Length": {"123"},
|
||||||
|
"Content-Type": {"text/plain"},
|
||||||
|
"Date": {"some date at some time Z"},
|
||||||
|
"Server": {"Go http package"},
|
||||||
|
})
|
||||||
|
var buf bytes.Buffer
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
buf.Reset()
|
||||||
|
h.WriteSubset(&buf, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user