git 命令

大驼峰转换工具

法则 基本满足大驼峰命名法则 首字母大写 “_” 忽略后大写

大驼峰到网络标准json串自动转换

带有特殊字符特殊处理:如下

1
"ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS"

以下采用golang实现

  1. 数据初始化:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    // Copied from golint
    var commonInitialisms = []string{"ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS"}
    var commonInitialismsReplacer *strings.Replacer
    var uncommonInitialismsReplacer *strings.Replacer
    
    func init() {
    	var commonInitialismsForReplacer []string
    	var uncommonInitialismsForReplacer []string
    	for _, initialism := range commonInitialisms {
    		commonInitialismsForReplacer = append(commonInitialismsForReplacer, initialism, strings.Title(strings.ToLower(initialism)))
    		uncommonInitialismsForReplacer = append(uncommonInitialismsForReplacer, strings.Title(strings.ToLower(initialism)), initialism)
    	}
    	commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...)
    	uncommonInitialismsReplacer = strings.NewReplacer(uncommonInitialismsForReplacer...)
    }
  2. 转换大驼峰函数:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    /*
    	转换为大驼峰命名法则
    	首字母大写,“_” 忽略后大写
    */
    func Marshal(name string) string {
    	if name == "" {
    		return ""
    	}
    
    	temp := strings.Split(name, "_")
    	var s string
    	for _, v := range temp {
    		vv := []rune(v)
    		if len(vv) > 0 {
    			if bool(vv[0] >= 'a' && vv[0] <= 'z') { //首字母大写
    				vv[0] -= 32
    			}
    			s += string(vv)
    		}
    	}
    
    	s = uncommonInitialismsReplacer.Replace(s)
    	//smap.Set(name, s)
    	return s
    }
  3. 回退函数

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    
    /*
    回退网络模式
    */
    func UnMarshal(name string) string {
    	const (
    		lower = false
    		upper = true
    	)
    
    	if name == "" {
    		return ""
    	}
    
    	var (
    		value                                    = commonInitialismsReplacer.Replace(name)
    		buf                                      = bytes.NewBufferString("")
    		lastCase, currCase, nextCase, nextNumber bool
    	)
    
    	for i, v := range value[:len(value)-1] {
    		nextCase = bool(value[i+1] >= 'A' && value[i+1] <= 'Z')
    		nextNumber = bool(value[i+1] >= '0' && value[i+1] <= '9')
    
    		if i > 0 {
    			if currCase == upper {
    				if lastCase == upper && (nextCase == upper || nextNumber == upper) {
    					buf.WriteRune(v)
    				} else {
    					if value[i-1] != '_' && value[i+1] != '_' {
    						buf.WriteRune('_')
    					}
    					buf.WriteRune(v)
    				}
    			} else {
    				buf.WriteRune(v)
    				if i == len(value)-2 && (nextCase == upper && nextNumber == lower) {
    					buf.WriteRune('_')
    				}
    			}
    		} else {
    			currCase = upper
    			buf.WriteRune(v)
    		}
    		lastCase = currCase
    		currCase = nextCase
    	}
    
    	buf.WriteByte(value[len(value)-1])
    
    	s := strings.ToLower(buf.String())
    	return s
    }
  4. 测试:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    func Test_cache(t *testing.T) {
    	SS := "OauthIDAPI"
    
    	tmp0 := UnMarshal(SS)
    	fmt.Println(tmp0)
    	tmp1 := Marshal(tmp0)
    	fmt.Println(tmp1)
    
    	if SS != tmp1 {
    		fmt.Println("false.")
    	}
    }

输入: OauthIDAPI 输出: oauth_id_api OauthIDAPI

5:传送门:

github