CamelCase returns the CamelCased name. If there is an interior underscore followed by a lower case letter, drop the underscore and convert the letter to upper case. There is a remote possibility of this rewrite causing a name collision, but it's so remote we're prepared to pretend it's nonexistent -
(s string)
| 2648 | // with different capitalizations. |
| 2649 | // In short, _my_field_name_2 becomes XMyFieldName_2. |
| 2650 | func CamelCase(s string) string { |
| 2651 | if s == "" { |
| 2652 | return "" |
| 2653 | } |
| 2654 | t := make([]byte, 0, 32) |
| 2655 | i := 0 |
| 2656 | if s[0] == '_' { |
| 2657 | // Need a capital letter; drop the '_'. |
| 2658 | t = append(t, 'X') |
| 2659 | i++ |
| 2660 | } |
| 2661 | // Invariant: if the next letter is lower case, it must be converted |
| 2662 | // to upper case. |
| 2663 | // That is, we process a word at a time, where words are marked by _ or |
| 2664 | // upper case letter. Digits are treated as words. |
| 2665 | for ; i < len(s); i++ { |
| 2666 | c := s[i] |
| 2667 | if c == '_' && i+1 < len(s) && isASCIILower(s[i+1]) { |
| 2668 | continue // Skip the underscore in s. |
| 2669 | } |
| 2670 | if isASCIIDigit(c) { |
| 2671 | t = append(t, c) |
| 2672 | continue |
| 2673 | } |
| 2674 | // Assume we have a letter now - if not, it's a bogus identifier. |
| 2675 | // The next word is a sequence of characters that must start upper case. |
| 2676 | if isASCIILower(c) { |
| 2677 | c ^= ' ' // Make it a capital letter. |
| 2678 | } |
| 2679 | t = append(t, c) // Guaranteed not lower case. |
| 2680 | // Accept lower case sequence that follows. |
| 2681 | for i+1 < len(s) && isASCIILower(s[i+1]) { |
| 2682 | i++ |
| 2683 | t = append(t, s[i]) |
| 2684 | } |
| 2685 | } |
| 2686 | return string(t) |
| 2687 | } |
| 2688 | |
| 2689 | // CamelCaseSlice is like CamelCase, but the argument is a slice of strings to |
| 2690 | // be joined with "_". |
no test coverage detected