Merge pull request #7 from gobwas/fix/mixed_snake

Fix to `camelToSnake` function to handle properly `Some_Snake_Case` string
This commit is contained in:
Victor Starodub
2016-04-02 00:00:19 +04:00
2 changed files with 5 additions and 1 deletions
+3 -1
View File
@@ -303,6 +303,7 @@ func camelToSnake(name string) string {
multipleUpper := false
var lastUpper rune
var beforeUpper rune
for _, c := range name {
// Non-lowercase character after uppercase is considered to be uppercase too.
@@ -316,7 +317,7 @@ func camelToSnake(name string) string {
firstInRow := !multipleUpper
lastInRow := !isUpper
if ret.Len() > 0 && (firstInRow || lastInRow) {
if ret.Len() > 0 && (firstInRow || lastInRow) && beforeUpper != '_' {
ret.WriteByte('_')
}
ret.WriteRune(unicode.ToLower(lastUpper))
@@ -332,6 +333,7 @@ func camelToSnake(name string) string {
ret.WriteRune(c)
lastUpper = 0
beforeUpper = c
multipleUpper = false
}
+2
View File
@@ -16,6 +16,8 @@ func TestCamelToSnake(t *testing.T) {
{"SomeHTTPStuff", "some_http_stuff"},
{"WriteJSON", "write_json"},
{"HTTP2Server", "http2_server"},
{"Some_Mixed_Case", "some_mixed_case"},
{"do_nothing", "do_nothing"},
{"JSONHTTPRPCServer", "jsonhttprpc_server"}, // nothing can be done here without a dictionary
} {