]> git.lizzy.rs Git - micro.git/blob - cmd/micro/util.go
Rename to tabstospaces for consistency
[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 // 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'
68 }
69
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 {
73                 if b == a {
74                         return true
75                 }
76         }
77         return false
78 }
79
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:])
83 }
84
85 // GetLeadingWhitespace returns the leading whitespace of the given string
86 func GetLeadingWhitespace(str string) string {
87         ws := ""
88         for _, c := range str {
89                 if c == ' ' || c == '\t' {
90                         ws += string(c)
91                 } else {
92                         break
93                 }
94         }
95         return ws
96 }
97
98 // IsSpaces checks if a given string is only spaces
99 func IsSpaces(str string) bool {
100         for _, c := range str {
101                 if c != ' ' {
102                         return false
103                 }
104         }
105
106         return true
107 }
108
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) {
112         if str == "on" {
113                 return true, nil
114         }
115         if str == "off" {
116                 return false, nil
117         }
118         return strconv.ParseBool(str)
119 }