mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
b2fea65faa
* add unit test * fix #907 * format code * format code * format code Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package util
|
|
|
|
import "strings"
|
|
|
|
// Title returns a string value with s[0] which has been convert into upper case that
|
|
// there are not empty input text
|
|
func Title(s string) string {
|
|
if len(s) == 0 {
|
|
return s
|
|
}
|
|
|
|
return strings.ToUpper(s[:1]) + s[1:]
|
|
}
|
|
|
|
// Untitle returns a string value with s[0] which has been convert into lower case that
|
|
// there are not empty input text
|
|
func Untitle(s string) string {
|
|
if len(s) == 0 {
|
|
return s
|
|
}
|
|
|
|
return strings.ToLower(s[:1]) + s[1:]
|
|
}
|
|
|
|
// Index returns the index where the item equal,it will return -1 if mismatched
|
|
func Index(slice []string, item string) int {
|
|
for i := range slice {
|
|
if slice[i] == item {
|
|
return i
|
|
}
|
|
}
|
|
|
|
return -1
|
|
}
|
|
|
|
// SafeString converts the input string into a safe naming style in golang
|
|
func SafeString(in string) string {
|
|
if len(in) == 0 {
|
|
return in
|
|
}
|
|
|
|
data := strings.Map(func(r rune) rune {
|
|
if isSafeRune(r) {
|
|
return r
|
|
}
|
|
return '_'
|
|
}, in)
|
|
|
|
headRune := rune(data[0])
|
|
if isNumber(headRune) {
|
|
return "_" + data
|
|
}
|
|
return data
|
|
}
|
|
|
|
func isSafeRune(r rune) bool {
|
|
return isLetter(r) || isNumber(r) || r == '_'
|
|
}
|
|
|
|
func isLetter(r rune) bool {
|
|
return 'A' <= r && r <= 'z'
|
|
}
|
|
|
|
func isNumber(r rune) bool {
|
|
return '0' <= r && r <= '9'
|
|
}
|