aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/misc.go
blob: 9adcf5d676b402e774f2ad7a346ddc34905104f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
	"regexp"
	"strings"
)

// AllCaps is a regex to test if a string identifier is made of
// all upper case letters.
var AllCaps = regexp.MustCompile("^[A-Z0-9]+$")

// popCount counts number of bits 'set' in mask.
func popCount(mask uint) uint {
	m := uint32(mask)
	n := uint(0)
	for i := uint32(0); i < 32; i++ {
		if m&(1<<i) != 0 {
			n++
		}
	}
	return n
}

// splitAndTitle takes a string, splits it by underscores, capitalizes the
// first letter of each chunk, and smushes'em back together.
func splitAndTitle(s string) string {
	// If the string is all caps, lower it and capitalize first letter.
	if AllCaps.MatchString(s) {
		return strings.Title(strings.ToLower(s))
	}

	// If the string has no underscores, leave it be.
	if i := strings.Index(s, "_"); i == -1 {
		return s
	}

	// Now split the name at underscores, capitalize the first
	// letter of each chunk, and smush'em back together.
	chunks := strings.Split(s, "_")
	for i, chunk := range chunks {
		chunks[i] = strings.Title(strings.ToLower(chunk))
	}
	return strings.Join(chunks, "")
}