1
0
mirror of https://github.com/golang/go synced 2024-11-23 18:20:04 -07:00

net/http: avoid some allocations in DetectContentType

Change-Id: I64985f8de7ca09e63208e8c949a5d4f4fc09073f
Reviewed-on: https://go-review.googlesource.com/1230
Reviewed-by: David Symonds <dsymonds@golang.org>
This commit is contained in:
Brad Fitzpatrick 2014-12-09 11:45:19 +11:00 committed by Brad Fitzpatrick
parent c5de72b213
commit e67a39ed08

View File

@ -38,7 +38,11 @@ func DetectContentType(data []byte) string {
}
func isWS(b byte) bool {
return bytes.IndexByte([]byte("\t\n\x0C\r "), b) != -1
switch b {
case '\t', '\n', '\x0c', '\r', ' ':
return true
}
return false
}
type sniffSig interface {
@ -161,6 +165,8 @@ func (h htmlSig) match(data []byte, firstNonWS int) string {
return "text/html; charset=utf-8"
}
var mp4ftype = []byte("ftyp")
type mp4Sig int
func (mp4Sig) match(data []byte, firstNonWS int) string {
@ -172,7 +178,7 @@ func (mp4Sig) match(data []byte, firstNonWS int) string {
if boxSize%4 != 0 || len(data) < boxSize {
return ""
}
if !bytes.Equal(data[4:8], []byte("ftyp")) {
if !bytes.Equal(data[4:8], mp4ftype) {
return ""
}
for st := 8; st < boxSize; st += 4 {