Browse Source

Empty string check (#1101)

田欧 8 years ago
parent
commit
3b300929e8
5 changed files with 11 additions and 12 deletions
  1. 2 2
      auth.go
  2. 1 1
      gin.go
  3. 1 1
      mode.go
  4. 2 2
      render/html.go
  5. 5 6
      utils.go

+ 2 - 2
auth.go

@@ -24,7 +24,7 @@ type authPair struct {
 type authPairs []authPair
 type authPairs []authPair
 
 
 func (a authPairs) searchCredential(authValue string) (string, bool) {
 func (a authPairs) searchCredential(authValue string) (string, bool) {
-	if len(authValue) == 0 {
+	if authValue == "" {
 		return "", false
 		return "", false
 	}
 	}
 	for _, pair := range a {
 	for _, pair := range a {
@@ -71,7 +71,7 @@ func processAccounts(accounts Accounts) authPairs {
 	assert1(len(accounts) > 0, "Empty list of authorized credentials")
 	assert1(len(accounts) > 0, "Empty list of authorized credentials")
 	pairs := make(authPairs, 0, len(accounts))
 	pairs := make(authPairs, 0, len(accounts))
 	for user, password := range accounts {
 	for user, password := range accounts {
-		assert1(len(user) > 0, "User can not be empty")
+		assert1(user != "", "User can not be empty")
 		value := authorizationHeader(user, password)
 		value := authorizationHeader(user, password)
 		pairs = append(pairs, authPair{
 		pairs = append(pairs, authPair{
 			Value: value,
 			Value: value,

+ 1 - 1
gin.go

@@ -231,7 +231,7 @@ func (engine *Engine) rebuild405Handlers() {
 
 
 func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
 func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
 	assert1(path[0] == '/', "path must begin with '/'")
 	assert1(path[0] == '/', "path must begin with '/'")
-	assert1(len(method) > 0, "HTTP method can not be empty")
+	assert1(method != "", "HTTP method can not be empty")
 	assert1(len(handlers) > 0, "there must be at least one handler")
 	assert1(len(handlers) > 0, "there must be at least one handler")
 
 
 	debugPrintRoute(method, path, handlers)
 	debugPrintRoute(method, path, handlers)

+ 1 - 1
mode.go

@@ -39,7 +39,7 @@ var modeName = DebugMode
 
 
 func init() {
 func init() {
 	mode := os.Getenv(ENV_GIN_MODE)
 	mode := os.Getenv(ENV_GIN_MODE)
-	if len(mode) == 0 {
+	if mode == "" {
 		SetMode(DebugMode)
 		SetMode(DebugMode)
 	} else {
 	} else {
 		SetMode(mode)
 		SetMode(mode)

+ 2 - 2
render/html.go

@@ -60,7 +60,7 @@ func (r HTMLDebug) loadTemplate() *template.Template {
 	if len(r.Files) > 0 {
 	if len(r.Files) > 0 {
 		return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
 		return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
 	}
 	}
-	if len(r.Glob) > 0 {
+	if r.Glob != "" {
 		return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
 		return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
 	}
 	}
 	panic("the HTML debug render was created without files or glob pattern")
 	panic("the HTML debug render was created without files or glob pattern")
@@ -69,7 +69,7 @@ func (r HTMLDebug) loadTemplate() *template.Template {
 func (r HTML) Render(w http.ResponseWriter) error {
 func (r HTML) Render(w http.ResponseWriter) error {
 	r.WriteContentType(w)
 	r.WriteContentType(w)
 
 
-	if len(r.Name) == 0 {
+	if r.Name == "" {
 		return r.Template.Execute(w, r.Data)
 		return r.Template.Execute(w, r.Data)
 	}
 	}
 	return r.Template.ExecuteTemplate(w, r.Name, r.Data)
 	return r.Template.ExecuteTemplate(w, r.Name, r.Data)

+ 5 - 6
utils.go

@@ -103,7 +103,7 @@ func parseAccept(acceptHeader string) []string {
 		if index := strings.IndexByte(part, ';'); index >= 0 {
 		if index := strings.IndexByte(part, ';'); index >= 0 {
 			part = part[0:index]
 			part = part[0:index]
 		}
 		}
-		if part = strings.TrimSpace(part); len(part) > 0 {
+		if part = strings.TrimSpace(part); part != "" {
 			out = append(out, part)
 			out = append(out, part)
 		}
 		}
 	}
 	}
@@ -111,11 +111,10 @@ func parseAccept(acceptHeader string) []string {
 }
 }
 
 
 func lastChar(str string) uint8 {
 func lastChar(str string) uint8 {
-	size := len(str)
-	if size == 0 {
+	if str == "" {
 		panic("The length of the string can't be 0")
 		panic("The length of the string can't be 0")
 	}
 	}
-	return str[size-1]
+	return str[len(str)-1]
 }
 }
 
 
 func nameOfFunction(f interface{}) string {
 func nameOfFunction(f interface{}) string {
@@ -123,7 +122,7 @@ func nameOfFunction(f interface{}) string {
 }
 }
 
 
 func joinPaths(absolutePath, relativePath string) string {
 func joinPaths(absolutePath, relativePath string) string {
-	if len(relativePath) == 0 {
+	if relativePath == "" {
 		return absolutePath
 		return absolutePath
 	}
 	}
 
 
@@ -138,7 +137,7 @@ func joinPaths(absolutePath, relativePath string) string {
 func resolveAddress(addr []string) string {
 func resolveAddress(addr []string) string {
 	switch len(addr) {
 	switch len(addr) {
 	case 0:
 	case 0:
-		if port := os.Getenv("PORT"); len(port) > 0 {
+		if port := os.Getenv("PORT"); port != "" {
 			debugPrint("Environment variable PORT=\"%s\"", port)
 			debugPrint("Environment variable PORT=\"%s\"", port)
 			return ":" + port
 			return ":" + port
 		}
 		}