revert the snakeString

This commit is contained in:
astaxie 2016-08-30 00:15:02 +08:00
parent bb860d2752
commit 3f67c62dd8
2 changed files with 12 additions and 30 deletions

View File

@ -2135,13 +2135,13 @@ func TestSnake(t *testing.T) {
"i": "i",
"I": "i",
"iD": "i_d",
"ID": "id",
"NO": "no",
"NOO": "noo",
"NOOooOOoo": "noo_oo_oo_oo",
"OrderNO": "order_no",
"ID": "i_d",
"NO": "n_o",
"NOO": "n_o_o",
"NOOooOOoo": "n_o_ooo_o_ooo",
"OrderNO": "order_n_o",
"tagName": "tag_name",
"tag_Name": "tag_name",
"tag_Name": "tag__name",
"tag_name": "tag_name",
"_tag_name": "_tag_name",
"tag_666name": "tag_666name",

View File

@ -184,33 +184,15 @@ func ToInt64(value interface{}) (d int64) {
// snake string, XxYy to xx_yy , XxYY to xx_yy
func snakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d != '_' && s[i-1] != '_' {
need := false
// upper as 1, lower as 0
// XX -> 11 -> 11
// Xx -> 10 -> 10
// XxYyZZ -> 101011 -> 10_10_11
isUpper := d >= 'A' && d <= 'Z'
preIsUpper := s[i-1] >= 'A' && s[i-1] <= 'Z'
if isUpper {
// like : xxYy
if !preIsUpper {
need = true
}
} else {
if preIsUpper {
// ignore "Xy" in "xxXyy"
if i-2 >= 0 && s[i-2] >= 'A' && s[i-2] <= 'Z' {
need = true
}
}
}
if need {
data = append(data, '_')
}
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}