feature(strings): add ToLower/ToUpper

This commit is contained in:
pyihe
2022-08-03 17:59:09 +08:00
parent f6187b99d1
commit b55a3d2a49

View File

@@ -5,6 +5,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"unsafe" "unsafe"
"github.com/pyihe/go-pkg/bytes"
) )
// Bytes string转换为[]byte // Bytes string转换为[]byte
@@ -203,3 +205,53 @@ func FilterRepeatByMap(slc []string) []string {
} }
return result return result
} }
func ToLower(s string) string {
if isLower(s) {
return s
}
b := make([]byte, len(s))
for i := range s {
c := s[i]
if c >= 'A' && c <= 'Z' {
c += 'a' - 'A'
}
b[i] = c
}
return bytes.String(b)
}
func isLower(s string) bool {
for i := range s {
c := s[i]
if c >= 'A' && c <= 'Z' {
return false
}
}
return true
}
func ToUpper(s string) string {
if isUpper(s) {
return s
}
b := make([]byte, len(s))
for i := range s {
c := s[i]
if c >= 'a' && c <= 'z' {
c -= 'a' - 'A'
}
b[i] = c
}
return bytes.String(b)
}
func isUpper(s string) bool {
for i := range s {
c := s[i]
if c >= 'a' && c <= 'z' {
return false
}
}
return true
}