redis/internal/util.go

63 lines
1.1 KiB
Go
Raw Normal View History

2016-10-14 10:37:30 +03:00
package internal
2017-02-01 11:36:33 +03:00
import "reflect"
2016-10-14 10:37:30 +03:00
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
}
2017-01-13 14:39:59 +03:00
return BytesToString(b)
2016-10-14 10:37:30 +03:00
}
func isLower(s string) bool {
for i := 0; i < len(s); i++ {
c := s[i]
if c >= 'A' && c <= 'Z' {
return false
}
}
return true
}
2017-02-01 11:36:33 +03:00
2017-11-01 16:33:53 +03:00
func MakeSliceNextElemFunc(v reflect.Value) func() reflect.Value {
2017-02-01 11:36:33 +03:00
elemType := v.Type().Elem()
if elemType.Kind() == reflect.Ptr {
2017-11-01 16:33:53 +03:00
elemType = elemType.Elem()
return func() reflect.Value {
if v.Len() < v.Cap() {
v.Set(v.Slice(0, v.Len()+1))
elem := v.Index(v.Len() - 1)
if elem.IsNil() {
elem.Set(reflect.New(elemType))
}
return elem.Elem()
}
elem := reflect.New(elemType)
v.Set(reflect.Append(v, elem))
return elem.Elem()
}
2017-02-01 11:36:33 +03:00
}
2017-11-01 16:33:53 +03:00
zero := reflect.Zero(elemType)
return func() reflect.Value {
if v.Len() < v.Cap() {
v.Set(v.Slice(0, v.Len()+1))
return v.Index(v.Len() - 1)
}
v.Set(reflect.Append(v, zero))
return v.Index(v.Len() - 1)
}
2017-02-01 11:36:33 +03:00
}