github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/amino/genproto/camelcase.go (about)

     1  package genproto
     2  
     3  // From https://github.com/golang/protobuf/blob/master/protoc-gen-go/generator/generator.go#L2648:
     4  // This is needed to get the P3Go name for a given type.
     5  func CamelCase(s string) string {
     6  	if s == "" {
     7  		return ""
     8  	}
     9  	t := make([]byte, 0, 32)
    10  	i := 0
    11  	if s[0] == '_' {
    12  		// Need a capital letter; drop the '_'.
    13  		t = append(t, 'X')
    14  		i++
    15  	}
    16  	// Invariant: if the next letter is lower case, it must be converted
    17  	// to upper case.
    18  	// That is, we process a word at a time, where words are marked by _ or
    19  	// upper case letter. Digits are treated as words.
    20  	for ; i < len(s); i++ {
    21  		c := s[i]
    22  		if c == '_' && i+1 < len(s) && isASCIILower(s[i+1]) {
    23  			continue // Skip the underscore in s.
    24  		}
    25  		if isASCIIDigit(c) {
    26  			t = append(t, c)
    27  			continue
    28  		}
    29  		// Assume we have a letter now - if not, it's a bogus identifier.
    30  		// The next word is a sequence of characters that must start upper case.
    31  		if isASCIILower(c) {
    32  			c ^= ' ' // Make it a capital letter.
    33  		}
    34  		t = append(t, c) // Guaranteed not lower case.
    35  		// Accept lower case sequence that follows.
    36  		for i+1 < len(s) && isASCIILower(s[i+1]) {
    37  			i++
    38  			t = append(t, s[i])
    39  		}
    40  	}
    41  	return string(t)
    42  }
    43  
    44  // Is c an ASCII lower-case letter?
    45  func isASCIILower(c byte) bool {
    46  	return 'a' <= c && c <= 'z'
    47  }
    48  
    49  // Is c an ASCII digit?
    50  func isASCIIDigit(c byte) bool {
    51  	return '0' <= c && c <= '9'
    52  }