1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 00:11:00 +00:00

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",
"I": "i", "I": "i",
"iD": "i_d", "iD": "i_d",
"ID": "id", "ID": "i_d",
"NO": "no", "NO": "n_o",
"NOO": "noo", "NOO": "n_o_o",
"NOOooOOoo": "noo_oo_oo_oo", "NOOooOOoo": "n_o_ooo_o_ooo",
"OrderNO": "order_no", "OrderNO": "order_n_o",
"tagName": "tag_name", "tagName": "tag_name",
"tag_Name": "tag_name", "tag_Name": "tag__name",
"tag_name": "tag_name", "tag_name": "tag_name",
"_tag_name": "_tag_name", "_tag_name": "_tag_name",
"tag_666name": "tag_666name", "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 // snake string, XxYy to xx_yy , XxYY to xx_yy
func snakeString(s string) string { func snakeString(s string) string {
data := make([]byte, 0, len(s)*2) data := make([]byte, 0, len(s)*2)
j := false
num := len(s) num := len(s)
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
d := s[i] d := s[i]
if i > 0 && d != '_' && s[i-1] != '_' { if i > 0 && d >= 'A' && d <= 'Z' && j {
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, '_') data = append(data, '_')
} }
if d != '_' {
j = true
} }
data = append(data, d) data = append(data, d)
} }