Skip to content

Instantly share code, notes, and snippets.

@zxh
Created March 17, 2018 06:19
Show Gist options
  • Save zxh/cee082053aa9674812e8cd4387088301 to your computer and use it in GitHub Desktop.
Save zxh/cee082053aa9674812e8cd4387088301 to your computer and use it in GitHub Desktop.
Convert CamelCase to underscore in golang
// Camelcase to underscore style.
func ToUnderScore(name string) string {
l := len(name)
ss := strings.Split(name, "")
// we just care about the key of idx map,
// the value of map is meaningless
idx := make(map[int]int, 1)
var rs []rune
for _, s := range name {
rs = append(rs, []rune(string(s))...)
}
for i := l - 1; i >= 0; {
if unicode.IsUpper(rs[i]) {
var start, end int
end = i
j := i - 1
for ; j >= 0; j-- {
if unicode.IsLower(rs[j]) {
start = j + 1
break
}
}
// handle the case: "BBC" or "AaBBB" case
if end == l-1 {
idx[start] = 1
} else {
if start == end {
// value=1 is meaningless
idx[start] = 1
} else {
idx[start] = 1
idx[end] = 1
}
}
i = j - 1
} else {
i--
}
}
for i := l - 1; i >= 0; i-- {
ss[i] = strings.ToLower(ss[i])
if _, ok := idx[i]; ok && i != 0 {
ss = append(ss[0:i], append([]string{"_"}, ss[i:]...)...)
}
}
return strings.Join(ss, "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment