MCPcopy Create free account
hub / github.com/Agelessbaby/BloomBlog / Camel2Snake

Function Camel2Snake

util/string/string.go:19–38  ·  view source on GitHub ↗

驼峰转为蛇形。例如userNameAbd转为user_name_abd User user

(s string)

Source from the content-addressed store, hash-verified

17
18// 驼峰转为蛇形。例如userNameAbd转为user_name_abd User user
19func Camel2Snake(s string) string {
20 if len(s) == 0 {
21 return ""
22 }
23 t := make([]byte, 0, len(s)+4)
24 if IsASCIIUpper(s[0]) { //首字母为大写时,转为小写
25 t = append(t, UpperLowerExchange(s[0]))
26 } else {
27 t = append(t, s[0])
28 }
29 for i := 1; i < len(s); i++ {
30 c := s[i]
31 if IsASCIIUpper(c) { //碰到大写,则转为小写,并在前面加个_
32 t = append(t, '_', UpperLowerExchange(c))
33 } else {
34 t = append(t, c)
35 }
36 }
37 return string(t)
38}
39
40var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
41

Callers

nothing calls this directly

Calls 2

IsASCIIUpperFunction · 0.85
UpperLowerExchangeFunction · 0.85

Tested by

no test coverage detected