]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/util.go
Fix crash
[micro.git] / cmd / micro / util.go
index 069aff6fa57409f515cc6bf10c2e38aeb6e974bb..f456705ed504f02efb6c048ee46bce63ed44fa69 100644 (file)
@@ -1,6 +1,7 @@
 package main
 
 import (
+       "strconv"
        "unicode/utf8"
 )
 
@@ -61,6 +62,11 @@ func IsWordChar(str string) bool {
        return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
 }
 
+// IsWhitespace returns true if the given rune is a space, tab, or newline
+func IsWhitespace(c rune) bool {
+       return c == ' ' || c == '\t' || c == '\n'
+}
+
 // Contains returns whether or not a string array contains a given string
 func Contains(list []string, a string) bool {
        for _, b := range list {
@@ -75,3 +81,39 @@ func Contains(list []string, a string) bool {
 func Insert(str string, pos int, value string) string {
        return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
 }
+
+// GetLeadingWhitespace returns the leading whitespace of the given string
+func GetLeadingWhitespace(str string) string {
+       ws := ""
+       for _, c := range str {
+               if c == ' ' || c == '\t' {
+                       ws += string(c)
+               } else {
+                       break
+               }
+       }
+       return ws
+}
+
+// IsSpaces checks if a given string is only spaces
+func IsSpaces(str string) bool {
+       for _, c := range str {
+               if c != ' ' {
+                       return false
+               }
+       }
+
+       return true
+}
+
+// ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
+// as 'true' and 'false' respectively
+func ParseBool(str string) (bool, error) {
+       if str == "on" {
+               return true, nil
+       }
+       if str == "off" {
+               return false, nil
+       }
+       return strconv.ParseBool(str)
+}