Skip to content

Commit

Permalink
pkg/metricutil: split complex boolean expr into standalone func
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Feb 17, 2017
1 parent ad4ad0d commit c21b12c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/metricutil/metricutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,25 @@ func convertCmdLabels() map[string]string {
return labels
}

func runesHasLowerNeighborAt(runes []rune, idx int) bool {
length := len(runes)

if idx+1 < length && unicode.IsLower(runes[idx+1]) {
return true
}
if idx > 1 && unicode.IsLower(runes[idx-1]) {
return true
}
return false
}

func camelCaseToSnakeCase(str string) string {
runes := []rune(str)
length := len(runes)

var ret []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
if i > 0 && unicode.IsUpper(runes[i]) && runesHasLowerNeighborAt(runes, i) {
ret = append(ret, '_')
}
ret = append(ret, unicode.ToLower(runes[i]))
Expand Down

0 comments on commit c21b12c

Please sign in to comment.