forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamel.go
63 lines (52 loc) · 1.15 KB
/
camel.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package names
import (
"strings"
)
// toCamelCase converts a string to CamelCase.
func toCamelCase(in string, initialCap bool) string {
out := strings.Builder{}
nextIsCap := initialCap
prevIsCap := false
for i, ch := range []byte(in) {
isCap := isCapitalLetter(ch)
isLow := isLowercaseLetter(ch)
isDig := isNumeric(ch)
if nextIsCap {
if isLow {
ch = toUppercaseLetter(ch)
}
} else if i == 0 {
if isCap {
ch = toLowercaseLetter(ch)
}
} else if prevIsCap && isCap {
ch = toLowercaseLetter(ch)
}
prevIsCap = isCap
if isCap || isLow {
out.WriteByte(ch)
nextIsCap = false
} else if isDig {
out.WriteByte(ch)
nextIsCap = true
} else {
nextIsCap = ch == '_' || ch == ' ' || ch == '-' || ch == '.'
}
}
return out.String()
}
func toUppercaseLetter(ch byte) byte {
ch += 'A'
ch -= 'a'
return ch
}
// ToCamelCase converts a string to CamelCase.
func ToCamelCase(in string) string {
return toCamelCase(in, true)
}
// ToLowerCamelCase converts a string to camelCase.
func ToLowerCamelCase(in string) string {
return toCamelCase(in, false)
}