-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
77 lines (71 loc) · 2.05 KB
/
parser.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
74
75
76
77
package strcase
import (
"strings"
)
func parse(input string, tokenSet ...string) []string {
var currentTokenSet []string
if tokenSet != nil {
currentTokenSet = tokenSet
}
nextIndex := findNextDelimiter(input)
if nextIndex == -1 {
currentTokenSet = append(currentTokenSet, normalizeToken(input))
return currentTokenSet
}
currentTokenSet = append(currentTokenSet, normalizeToken(input[:nextIndex]))
return parse(input[nextIndex:], currentTokenSet...)
}
func normalizeToken(input string) string {
return strings.ToLower(strings.Trim(input, string(byteTypeList)))
}
// findNextDelimiter returns the index position of the first
// transition from the left of the input `input`. If no
// transitions are found, -1 is returned
func findNextDelimiter(input string) int {
previousByteType := typeInit
for i := range input {
currentByteType := getByteType(input[i])
var nextByteType byteType = typeInvalid
if i+1 < len(input) {
nextByteType = getByteType(input[i+1])
}
if i > 0 && isDelimiter(previousByteType, currentByteType, nextByteType) {
return i
}
previousByteType = currentByteType
}
return -1
}
func isDelimiter(previous byteType, current byteType, next byteType) bool {
if previous&typeAlpha == current&typeAlpha {
if previous&typeLower == typeLower && current&typeUpper == typeUpper {
return true
}
if current&typeUpper == typeUpper && next&typeLower == typeLower {
return true
}
}
if previous&typeNumber == typeNumber && current&typeNumber != typeNumber {
return true
}
if previous&typeDelimiter != typeDelimiter && current&typeDelimiter == typeDelimiter {
return true
}
return false
}
func getByteType(input byte) byteType {
typeOfByte := typeUnknown
switch true {
case input >= 'A' && input <= 'Z':
typeOfByte = typeAlpha | typeUpper
case input >= 'a' && input <= 'z':
typeOfByte = typeAlpha | typeLower
case input >= '0' && input <= '9':
typeOfByte = typeNumber
default:
if predefinedByteType, ok := byteTypeMap[input]; ok {
typeOfByte = typeDelimiter | predefinedByteType
}
}
return typeOfByte
}