github.com/BurntSushi/xgb@v0.0.0-20210121224620-deaf085860bc/xgbgen/misc.go (about)

     1  package main
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  // AllCaps is a regex to test if a string identifier is made of
     9  // all upper case letters.
    10  var allCaps = regexp.MustCompile("^[A-Z0-9]+$")
    11  
    12  // popCount counts number of bits 'set' in mask.
    13  func popCount(mask uint) uint {
    14  	m := uint32(mask)
    15  	n := uint(0)
    16  	for i := uint32(0); i < 32; i++ {
    17  		if m&(1<<i) != 0 {
    18  			n++
    19  		}
    20  	}
    21  	return n
    22  }
    23  
    24  // pad makes sure 'n' aligns on 4 bytes.
    25  func pad(n int) int {
    26  	return (n + 3) & ^3
    27  }
    28  
    29  // splitAndTitle takes a string, splits it by underscores, capitalizes the
    30  // first letter of each chunk, and smushes'em back together.
    31  func splitAndTitle(s string) string {
    32  	// If the string is all caps, lower it and capitalize first letter.
    33  	if allCaps.MatchString(s) {
    34  		return strings.Title(strings.ToLower(s))
    35  	}
    36  
    37  	// If the string has no underscores, capitalize it and leave it be.
    38  	if i := strings.Index(s, "_"); i == -1 {
    39  		return strings.Title(s)
    40  	}
    41  
    42  	// Now split the name at underscores, capitalize the first
    43  	// letter of each chunk, and smush'em back together.
    44  	chunks := strings.Split(s, "_")
    45  	for i, chunk := range chunks {
    46  		chunks[i] = strings.Title(strings.ToLower(chunk))
    47  	}
    48  	return strings.Join(chunks, "")
    49  }