]> git.lizzy.rs Git - micro.git/blob - cmd/micro/util.go
b6a6632695feae6e936c257a74c1d8ef2cee63e8
[micro.git] / cmd / micro / util.go
1 package main
2
3 import (
4         "strconv"
5         "unicode/utf8"
6 )
7
8 // Util.go is a collection of utility functions that are used throughout
9 // the program
10
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)
15 }
16
17 // NumOccurences counts the number of occurences of a byte in a string
18 func NumOccurences(s string, c byte) int {
19         var n int
20         for i := 0; i < len(s); i++ {
21                 if s[i] == c {
22                         n++
23                 }
24         }
25         return n
26 }
27
28 // Spaces returns a string with n spaces
29 func Spaces(n int) string {
30         var str string
31         for i := 0; i < n; i++ {
32                 str += " "
33         }
34         return str
35 }
36
37 // Min takes the min of two ints
38 func Min(a, b int) int {
39         if a > b {
40                 return b
41         }
42         return a
43 }
44
45 // Max takes the max of two ints
46 func Max(a, b int) int {
47         if a > b {
48                 return a
49         }
50         return b
51 }
52
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 {
57         if len(str) > 1 {
58                 // Unicode
59                 return false
60         }
61         c := str[0]
62         return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
63 }
64
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 {
68                 if b == a {
69                         return true
70                 }
71         }
72         return false
73 }
74
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:])
78 }
79
80 // GetLeadingWhitespace returns the leading whitespace of the given string
81 func GetLeadingWhitespace(str string) string {
82         ws := ""
83         for _, c := range str {
84                 if c == ' ' || c == '\t' {
85                         ws += string(c)
86                 } else {
87                         break
88                 }
89         }
90         return ws
91 }
92
93 // IsSpaces checks if a given string is only spaces
94 func IsSpaces(str string) bool {
95         for _, c := range str {
96                 if c != ' ' {
97                         return false
98                 }
99         }
100
101         return true
102 }
103
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) {
107         if str == "on" {
108                 return true, nil
109         }
110         if str == "off" {
111                 return false, nil
112         }
113         return strconv.ParseBool(str)
114 }