hi everyone, I was thinking about the method ToLower in standard library strings then I saw as the pkg Redis implements this idea. Then I identified that is very better than the standard library. Maybe we can improve this package strings.
Let's go to talk about this?
More details below:
e.g
func isLower(s string) bool {
for i := 0; i < len(s); i++ {
c := s[i]
if c >= 'A' && c <= 'Z' {
return false
}
}
return true
}
func ToLower(s string) string {
if isLower(s) {
return s
}
b := make([]byte, len(s))
for i := range b {
c := s[i]
if c >= 'A' && c <= 'Z' {
c += 'a' - 'A'
}
b[i] = c
}
return BytesToString(b)
}
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
reference: https://github.com/go-redis/redis/blob/6e4eb2e3acad9575fb1e2c8690a3c1decf2e59e5/internal/util.go#L22
One little detail I believe that we cannot use unsafe, I would use just string(bytes)
//BenchmarkTestToLowerStdLib 4497225 265.1 ns/op 64 B/op 1 allocs/op
func BenchmarkTestToLowerStdLib(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = strings.ToLower(`RENAN BASTOS 93 AOSDAJDJAIDJAIDAJIaidsjjaidijadsjiadjiOOKKO`)
}
}
//BenchmarkTestToLowerInternalRedis 15342235 77.65 ns/op 64 B/op 1 allocs/op
func BenchmarkTestToLowerInternalRedis(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ToLower(`RENAN BASTOS 93 AOSDAJDJAIDJAIDAJIaidsjjaidijadsjiadjiOOKKO`)
}
}
I am sorry for anything but I want to help our loved Go.
hi everyone, I was thinking about the method ToLower in standard library strings then I saw as the pkg Redis implements this idea. Then I identified that is very better than the standard library. Maybe we can improve this package
strings.Let's go to talk about this?
More details below:
e.g
reference: https://github.com/go-redis/redis/blob/6e4eb2e3acad9575fb1e2c8690a3c1decf2e59e5/internal/util.go#L22
One little detail I believe that we cannot use unsafe, I would use just
string(bytes)I am sorry for anything but I want to help our loved Go.