gitee.com/quant1x/gox@v1.21.2/api/string_camel.go (about)

     1  // Copyright (c) 2017, A. Stoewer <adrian.stoewer@rz.ifi.lmu.de>
     2  // All rights reserved.
     3  
     4  package api
     5  
     6  import (
     7  	"strings"
     8  )
     9  
    10  // UpperCamelCase converts a string into camel case starting with a upper case letter.
    11  func UpperCamelCase(s string) string {
    12  	return camelCase(s, true)
    13  }
    14  
    15  // LowerCamelCase converts a string into camel case starting with a lower case letter.
    16  func LowerCamelCase(s string) string {
    17  	return camelCase(s, false)
    18  }
    19  
    20  func camelCase(s string, upper bool) string {
    21  	s = strings.TrimSpace(s)
    22  	buffer := make([]rune, 0, len(s))
    23  
    24  	stringIter(s, func(prev, curr, next rune) {
    25  		if !isDelimiter(curr) {
    26  			if isDelimiter(prev) || (upper && prev == 0) {
    27  				buffer = append(buffer, toUpper(curr))
    28  			} else if isLower(prev) {
    29  				buffer = append(buffer, curr)
    30  			} else {
    31  				buffer = append(buffer, toLower(curr))
    32  			}
    33  		}
    34  	})
    35  
    36  	return string(buffer)
    37  }