7 // Util.go is a collection of utility functions that are used throughout
10 // Count returns the length of a string in runes
11 // This is exactly equivalent to utf8.RuneCountInString(), just less characters
12 func Count(s string) int {
13 return utf8.RuneCountInString(s)
16 // NumOccurences counts the number of occurences of a byte in a string
17 func NumOccurences(s string, c byte) int {
19 for i := 0; i < len(s); i++ {
27 // Spaces returns a string with n spaces
28 func Spaces(n int) string {
30 for i := 0; i < n; i++ {
36 // Min takes the min of two ints
37 func Min(a, b int) int {
44 // Max takes the max of two ints
45 func Max(a, b int) int {
52 // IsWordChar returns whether or not the string is a 'word character'
53 // If it is a unicode character, then it does not match
54 // Word characters are defined as [A-Za-z0-9_]
55 func IsWordChar(str string) bool {
61 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
64 // Contains returns whether or not a string array contains a given string
65 func Contains(list []string, a string) bool {
66 for _, b := range list {
74 // Insert makes a simple insert into a string at the given position
75 func Insert(str string, pos int, value string) string {
76 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])