1 // Copyright (c) 2018 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
23 func getBinapiTypeSize(binapiType string) int {
24 if _, ok := binapiTypes[binapiType]; ok {
25 b, err := strconv.Atoi(strings.TrimLeft(binapiType, "uif"))
35 // binapiTypes is a set of types used VPP binary API for translation to Go types
36 var binapiTypes = map[string]string{
50 func usesInitialism(s string) string {
51 if u := strings.ToUpper(s); commonInitialisms[u] {
53 } else if su, ok := specialInitialisms[u]; ok {
59 // commonInitialisms is a set of common initialisms that need to stay in upper case.
60 var commonInitialisms = map[string]bool{
63 //"ASCII": true, // there are only two use cases for ASCII which already have initialism before and after
105 // specialInitialisms is a set of special initialisms that need part to stay in upper case.
106 var specialInitialisms = map[string]string{
112 // camelCaseName returns correct name identifier (camelCase).
113 func camelCaseName(name string) (should string) {
114 name = strings.Title(name)
116 // Fast path for simple cases: "_" and all lowercase.
121 for _, r := range name {
122 if !unicode.IsLower(r) {
131 // Split camelCase at any lower->upper transition, and split on underscores.
132 // Check each word for common initialisms.
133 runes := []rune(name)
134 w, i := 0, 0 // index of start of word, scan
135 for i+1 <= len(runes) {
136 eow := false // whether we hit the end of a word
137 if i+1 == len(runes) {
139 } else if runes[i+1] == '_' {
140 // underscore; shift the remainder forward over any run of underscores
143 for i+n+1 < len(runes) && runes[i+n+1] == '_' {
147 // Leave at most one underscore if the underscore is between two digits
148 if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
152 copy(runes[i+1:], runes[i+n+1:])
153 runes = runes[:len(runes)-n]
154 } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
164 word := string(runes[w:i])
165 if u := usesInitialism(word); u != "" {
166 // Keep consistent case, which is lowercase only at the start.
167 if w == 0 && unicode.IsLower(runes[w]) {
168 u = strings.ToLower(u)
170 // All the common initialisms are ASCII,
171 // so we can replace the bytes exactly.
172 copy(runes[w:], []rune(u))
173 } else if w > 0 && strings.ToLower(word) == word {
174 // already all lowercase, and not the first word, so uppercase the first character.
175 runes[w] = unicode.ToUpper(runes[w])