-
Notifications
You must be signed in to change notification settings - Fork 0
/
case_changer.go
73 lines (65 loc) · 1.23 KB
/
case_changer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package stringutil
import (
"unicode"
"unicode/utf8"
)
type buffer struct {
r []byte
runeBytes [utf8.UTFMax]byte
}
func (b *buffer) write(r rune) {
// if inside utf 8 range
if r < utf8.RuneSelf {
b.r = append(b.r, byte(r))
return
}
n := utf8.EncodeRune(b.runeBytes[0:], r)
b.r = append(b.r, b.runeBytes[0:n]...)
}
func (b *buffer) underscoreWithIndent() {
// guard check to not underscore the first character
if len(b.r) > 0 {
b.r = append(b.r, '_')
}
}
// Underscore : take a camel cased string input and turn it to snakecased
//
// Example :
// value := Underscore("someThingLong")
// fmt.Println(value) // some_thing_long
func Underscore(s string) string {
b := buffer{
r: make([]byte, 0, len(s)),
}
var m rune
var w bool
for _, ch := range s {
// if lower , underscore the character
// if not at th beginning we can underscore and indent
if unicode.IsUpper(ch) {
if m != 0 {
if !w {
b.underscoreWithIndent()
w = true
}
b.write(m)
}
m = unicode.ToLower(ch)
} else {
if m != 0 {
b.underscoreWithIndent()
b.write(m)
m = 0
w = false
}
b.write(ch)
}
}
if m != 0 {
if !w {
b.underscoreWithIndent()
}
b.write(m)
}
return string(b.r)
}