camelCaseUncached is the original implementation without caching.
(name string, firstUpper, acronym bool)
| 121 | |
| 122 | // camelCaseUncached is the original implementation without caching. |
| 123 | func camelCaseUncached(name string, firstUpper, acronym bool) string { |
| 124 | runes := []rune(name) |
| 125 | // remove trailing invalid identifiers (makes code below simpler) |
| 126 | runes = removeTrailingInvalid(runes) |
| 127 | |
| 128 | // all characters are invalid |
| 129 | if len(runes) == 0 { |
| 130 | return "" |
| 131 | } |
| 132 | |
| 133 | w, i := 0, 0 // index of start of word, scan |
| 134 | for i+1 <= len(runes) { |
| 135 | eow := false // whether we hit the end of a word |
| 136 | |
| 137 | // remove leading invalid identifiers |
| 138 | runes = removeInvalidAtIndex(i, runes) |
| 139 | |
| 140 | switch { |
| 141 | case i+1 == len(runes): |
| 142 | eow = true |
| 143 | case !validIdentifier(runes[i]): |
| 144 | // get rid of it |
| 145 | runes = append(runes[:i], runes[i+1:]...) |
| 146 | case runes[i+1] == '_': |
| 147 | // underscore; shift the remainder forward over any run of underscores |
| 148 | eow = true |
| 149 | n := 1 |
| 150 | for i+n+1 < len(runes) && runes[i+n+1] == '_' { |
| 151 | n++ |
| 152 | } |
| 153 | copy(runes[i+1:], runes[i+n+1:]) |
| 154 | runes = runes[:len(runes)-n] |
| 155 | case isLower(runes[i]) && !isLower(runes[i+1]): |
| 156 | // lower->non-lower |
| 157 | eow = true |
| 158 | } |
| 159 | i++ |
| 160 | if !eow { |
| 161 | continue |
| 162 | } |
| 163 | |
| 164 | // [w,i] is a word. |
| 165 | word := string(runes[w:i]) |
| 166 | // is it one of our initialisms? |
| 167 | if u := strings.ToUpper(word); commonInitialisms[u] { |
| 168 | switch { |
| 169 | case firstUpper && acronym: |
| 170 | // u is already in upper case. Nothing to do here. |
| 171 | case firstUpper && !acronym: |
| 172 | u = expr.Title(strings.ToLower(u)) |
| 173 | case w > 0 && !acronym: |
| 174 | u = expr.Title(strings.ToLower(u)) |
| 175 | case w == 0: |
| 176 | u = strings.ToLower(u) |
| 177 | } |
| 178 | |
| 179 | // All the common initialisms are ASCII, |
| 180 | // so we can replace the bytes exactly. |
no test coverage detected