gin/utils.go
田欧 87d536c001 utils: use strings.Split instead of strings.IndexByte (#1400)
And I test them benchmark:

code:

```go
# stringsbench.go
package stringsbench

import "strings"

func index(part string) string {
	if index := strings.IndexByte(part, ';'); index >= 0 {
		if part := strings.TrimSpace(strings.Split(part, ";")[0]); part != "" {
			return part[0:index]
		}
	}
	return ""
}

func split(part string) string {
	return strings.Split(part, ";")[0]
}
```

```go
# stringsbench_test.go
package stringsbench

import (
	"testing"
)

func BenchmarkIndex(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			index("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
		}
	})
}

func BenchmarkSplit(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			split("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
		}
	})
}
```

And the result:

```shell
➜  strings go test --bench=.
goos: darwin
goarch: amd64
BenchmarkIndex-8   	30000000	        46.1 ns/op
BenchmarkSplit-8   	50000000	        35.9 ns/op
PASS
ok  	_/Users/tianou/strings	3.271s
➜  strings go test --bench=.
goos: darwin
goarch: amd64
BenchmarkIndex-8   	30000000	        44.2 ns/op
BenchmarkSplit-8   	50000000	        34.7 ns/op
PASS
ok  	_/Users/tianou/strings	3.156s
➜  strings go test --bench=.
goos: darwin
goarch: amd64
BenchmarkIndex-8   	30000000	        45.6 ns/op
BenchmarkSplit-8   	50000000	        35.3 ns/op
PASS
ok  	_/Users/tianou/strings	3.230s
```
2018-06-21 09:31:43 +08:00

152 lines
3.2 KiB
Go

// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"encoding/xml"
"net/http"
"os"
"path"
"reflect"
"runtime"
"strings"
)
const BindKey = "_gin-gonic/gin/bindkey"
func Bind(val interface{}) HandlerFunc {
value := reflect.ValueOf(val)
if value.Kind() == reflect.Ptr {
panic(`Bind struct can not be a pointer. Example:
Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
`)
}
typ := value.Type()
return func(c *Context) {
obj := reflect.New(typ).Interface()
if c.Bind(obj) == nil {
c.Set(BindKey, obj)
}
}
}
// WrapF is a helper function for wrapping http.HandlerFunc
// Returns a Gin middleware
func WrapF(f http.HandlerFunc) HandlerFunc {
return func(c *Context) {
f(c.Writer, c.Request)
}
}
// WrapH is a helper function for wrapping http.Handler
// Returns a Gin middleware
func WrapH(h http.Handler) HandlerFunc {
return func(c *Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
// H is a shortcut for map[string]interface{}
type H map[string]interface{}
// MarshalXML allows type H to be used with xml.Marshal.
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{
Space: "",
Local: "map",
}
if err := e.EncodeToken(start); err != nil {
return err
}
for key, value := range h {
elem := xml.StartElement{
Name: xml.Name{Space: "", Local: key},
Attr: []xml.Attr{},
}
if err := e.EncodeElement(value, elem); err != nil {
return err
}
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
func assert1(guard bool, text string) {
if !guard {
panic(text)
}
}
func filterFlags(content string) string {
for i, char := range content {
if char == ' ' || char == ';' {
return content[:i]
}
}
return content
}
func chooseData(custom, wildcard interface{}) interface{} {
if custom == nil {
if wildcard == nil {
panic("negotiation config is invalid")
}
return wildcard
}
return custom
}
func parseAccept(acceptHeader string) []string {
parts := strings.Split(acceptHeader, ",")
out := make([]string, 0, len(parts))
for _, part := range parts {
if part = strings.TrimSpace(strings.Split(part, ";")[0]); part != "" {
out = append(out, part)
}
}
return out
}
func lastChar(str string) uint8 {
if str == "" {
panic("The length of the string can't be 0")
}
return str[len(str)-1]
}
func nameOfFunction(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
func joinPaths(absolutePath, relativePath string) string {
if relativePath == "" {
return absolutePath
}
finalPath := path.Join(absolutePath, relativePath)
appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
if appendSlash {
return finalPath + "/"
}
return finalPath
}
func resolveAddress(addr []string) string {
switch len(addr) {
case 0:
if port := os.Getenv("PORT"); port != "" {
debugPrint("Environment variable PORT=\"%s\"", port)
return ":" + port
}
debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
return ":8080"
case 1:
return addr[0]
default:
panic("too much parameters")
}
}