Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some code improvements #1909

Merged
merged 1 commit into from May 21, 2019
Merged

Some code improvements #1909

merged 1 commit into from May 21, 2019

Conversation

sosiska
Copy link
Contributor

@sosiska sosiska commented May 21, 2019

  • strings.ToLower comparison changed to strings.EqualFold.
  • Rewrite switch statement with only one case as if.

* strings.ToLower comparison changed to strings.EqualFold.
* Rewrite switch statement with only one case as if.
@codecov
Copy link

codecov bot commented May 21, 2019

Codecov Report

Merging #1909 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1909      +/-   ##
==========================================
- Coverage   98.74%   98.74%   -0.01%     
==========================================
  Files          38       38              
  Lines        2147     2145       -2     
==========================================
- Hits         2120     2118       -2     
  Misses         15       15              
  Partials       12       12
Impacted Files Coverage Δ
context.go 98.36% <100%> (ø) ⬆️
binding/form_mapping.go 100% <100%> (ø) ⬆️
tree.go 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8ee9d95...9c469ea. Read the comment docs.

@appleboy
Copy link
Member

Good Catch. some benchmark result:

package main

import (
	"strings"
	"testing"
)

var EqualFoldTests = []struct {
	s, t string
	out  bool
}{
	{"abc", "abc", true},
	{"ABcd", "ABcd", true},
	{"123abc", "123ABC", true},
	{"αβδ", "ΑΒΔ", true},
	{"abc", "xyz", false},
	{"abc", "XYZ", false},
	{"abcdefghijk", "abcdefghijX", false},
	{"abcdefghijk", "abcdefghij\u212A", true},
	{"abcdefghijK", "abcdefghij\u212A", true},
	{"abcdefghijkz", "abcdefghij\u212Ay", false},
	{"abcdefghijKz", "abcdefghij\u212Ay", false},
	{"1", "2", false},
	{"utf-8", "US-ASCII", false},
}

func TestEqualFold(t *testing.T) {
	for _, tt := range EqualFoldTests {
		if out := strings.EqualFold(tt.s, tt.t); out != tt.out {
			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
		}
		if out := strings.EqualFold(tt.t, tt.s); out != tt.out {
			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
		}
	}
}

func BenchmarkToLower(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, tt := range EqualFoldTests {
			if out := (strings.ToLower(tt.s) == strings.ToLower(tt.t)); out != tt.out {
				b.Fatal("wrong result")
			}
		}
	}
}

func BenchmarkEqualFold(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, tt := range EqualFoldTests {
			if out := strings.EqualFold(tt.s, tt.t); out != tt.out {
				b.Fatal("wrong result")
			}
		}
	}
}

result:

goos: darwin
goarch: amd64
pkg: 
BenchmarkToLower-8       1000000              1641 ns/op             208 B/op         13 allocs/op
BenchmarkEqualFold-8     3000000               481 ns/op               0 B/op          0 allocs/op
PASS

@appleboy appleboy added this to the 1.5 milestone May 21, 2019
@thinkerou thinkerou merged commit b1d607a into gin-gonic:master May 21, 2019
ThomasObenaus pushed a commit to ThomasObenaus/gin that referenced this pull request Feb 19, 2020
* strings.ToLower comparison changed to strings.EqualFold.
* Rewrite switch statement with only one case as if.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants