8 // Util.go is a collection of utility functions that are used throughout
11 // Count returns the length of a string in runes
12 // This is exactly equivalent to utf8.RuneCountInString(), just less characters
13 func Count(s string) int {
14 return utf8.RuneCountInString(s)
17 // NumOccurences counts the number of occurences of a byte in a string
18 func NumOccurences(s string, c byte) int {
20 for i := 0; i < len(s); i++ {
28 // Spaces returns a string with n spaces
29 func Spaces(n int) string {
31 for i := 0; i < n; i++ {
37 // Min takes the min of two ints
38 func Min(a, b int) int {
45 // Max takes the max of two ints
46 func Max(a, b int) int {
53 // IsWordChar returns whether or not the string is a 'word character'
54 // If it is a unicode character, then it does not match
55 // Word characters are defined as [A-Za-z0-9_]
56 func IsWordChar(str string) bool {
62 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
65 // Contains returns whether or not a string array contains a given string
66 func Contains(list []string, a string) bool {
67 for _, b := range list {
75 // Insert makes a simple insert into a string at the given position
76 func Insert(str string, pos int, value string) string {
77 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
80 // GetLeadingWhitespace returns the leading whitespace of the given string
81 func GetLeadingWhitespace(str string) string {
83 for _, c := range str {
84 if c == ' ' || c == '\t' {
93 // IsSpaces checks if a given string is only spaces
94 func IsSpaces(str string) bool {
95 for _, c := range str {
104 // ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
105 // as 'true' and 'false' respectively
106 func ParseBool(str string) (bool, error) {
113 return strconv.ParseBool(str)