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 // IsWhitespace returns true if the given rune is a space, tab, or newline
66 func IsWhitespace(c rune) bool {
67 return c == ' ' || c == '\t' || c == '\n'
70 // Contains returns whether or not a string array contains a given string
71 func Contains(list []string, a string) bool {
72 for _, b := range list {
80 // Insert makes a simple insert into a string at the given position
81 func Insert(str string, pos int, value string) string {
82 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
85 // GetLeadingWhitespace returns the leading whitespace of the given string
86 func GetLeadingWhitespace(str string) string {
88 for _, c := range str {
89 if c == ' ' || c == '\t' {
98 // IsSpaces checks if a given string is only spaces
99 func IsSpaces(str string) bool {
100 for _, c := range str {
109 // ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
110 // as 'true' and 'false' respectively
111 func ParseBool(str string) (bool, error) {
118 return strconv.ParseBool(str)