File: util.go

package info (click to toggle)
c2go 0.26.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: ansic: 6,037; sh: 82; makefile: 5
file content (62 lines) | stat: -rw-r--r-- 1,478 bytes parent folder | download | duplicates (3)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package util

import (
	"strconv"
	"strings"
)

// InStrings returns true if item exists in items. It must be an exact string
// match.
func InStrings(item string, items []string) bool {
	for _, v := range items {
		if item == v {
			return true
		}
	}

	return false
}

// Ucfirst returns the word with the first letter uppercased; none of the other
// letters in the word are modified. For example "fooBar" would return "FooBar".
func Ucfirst(word string) string {
	if word == "" {
		return ""
	}

	if len(word) == 1 {
		return strings.ToUpper(word)
	}

	return strings.ToUpper(string(word[0])) + word[1:]
}

// Atoi converts a string to an integer in cases where we are sure that s will
// be a valid integer, otherwise it will panic.
func Atoi(s string) int {
	i, err := strconv.Atoi(s)
	PanicOnError(err, "bad integer")

	return i
}

// GetExportedName returns a deterministic and Go safe name for a C type. For
// example, "*__foo[]" will return "FooSlice".
func GetExportedName(field string) string {
	if strings.Contains(field, "interface{}") ||
		strings.Contains(field, "Interface{}") {
		return "Interface"
	}

	// Convert "[]byte" into "byteSlice". This also works with multiple slices,
	// like "[][]byte" to "byteSliceSlice".
	for len(field) > 2 && field[:2] == "[]" {
		field = field[2:] + "Slice"
	}

	// NotFunc(int)()
	field = strings.Replace(field, "(", "_", -1)
	field = strings.Replace(field, ")", "_", -1)

	return Ucfirst(strings.TrimLeft(field, "*_"))
}