14 "github.com/mattn/go-runewidth"
17 // Util.go is a collection of utility functions that are used throughout
20 // Count returns the length of a string in runes
21 // This is exactly equivalent to utf8.RuneCountInString(), just less characters
22 func Count(s string) int {
23 return utf8.RuneCountInString(s)
26 // NumOccurrences counts the number of occurences of a byte in a string
27 func NumOccurrences(s string, c byte) int {
29 for i := 0; i < len(s); i++ {
37 // Spaces returns a string with n spaces
38 func Spaces(n int) string {
40 for i := 0; i < n; i++ {
46 // Min takes the min of two ints
47 func Min(a, b int) int {
54 // Max takes the max of two ints
55 func Max(a, b int) int {
62 // IsWordChar returns whether or not the string is a 'word character'
63 // If it is a unicode character, then it does not match
64 // Word characters are defined as [A-Za-z0-9_]
65 func IsWordChar(str string) bool {
71 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
74 // IsWhitespace returns true if the given rune is a space, tab, or newline
75 func IsWhitespace(c rune) bool {
76 return c == ' ' || c == '\t' || c == '\n'
79 // Contains returns whether or not a string array contains a given string
80 func Contains(list []string, a string) bool {
81 for _, b := range list {
89 // Insert makes a simple insert into a string at the given position
90 func Insert(str string, pos int, value string) string {
91 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
94 // GetLeadingWhitespace returns the leading whitespace of the given string
95 func GetLeadingWhitespace(str string) string {
97 for _, c := range str {
98 if c == ' ' || c == '\t' {
107 // IsSpaces checks if a given string is only spaces
108 func IsSpaces(str string) bool {
109 for _, c := range str {
118 // IsSpacesOrTabs checks if a given string contains only spaces and tabs
119 func IsSpacesOrTabs(str string) bool {
120 for _, c := range str {
121 if c != ' ' && c != '\t' {
129 // ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
130 // as 'true' and 'false' respectively
131 func ParseBool(str string) (bool, error) {
138 return strconv.ParseBool(str)
141 // EscapePath replaces every path separator in a given path with a %
142 func EscapePath(path string) string {
143 path = filepath.ToSlash(path)
144 return strings.Replace(path, "/", "%", -1)
147 // GetModTime returns the last modification time for a given file
148 // It also returns a boolean if there was a problem accessing the file
149 func GetModTime(path string) (time.Time, bool) {
150 info, err := os.Stat(path)
152 return time.Now(), false
154 return info.ModTime(), true
157 // StringWidth returns the width of a string where tabs count as `tabsize` width
158 func StringWidth(str string, tabsize int) int {
159 sw := runewidth.StringWidth(str)
160 sw += NumOccurrences(str, '\t') * (tabsize - 1)
164 // WidthOfLargeRunes searches all the runes in a string and counts up all the widths of runes
165 // that have a width larger than 1 (this also counts tabs as `tabsize` width)
166 func WidthOfLargeRunes(str string, tabsize int) int {
168 for _, ch := range str {
173 w = runewidth.RuneWidth(ch)
182 // RunePos returns the rune index of a given byte index
183 // This could cause problems if the byte index is between code points
184 func runePos(p int, str string) int {
185 return utf8.RuneCountInString(str[:p])
188 func lcs(a, b string) string {
193 for i, r := range arunes {
194 if i >= len(brunes) {
206 func CommonSubstring(arr ...string) string {
209 for _, str := range arr[1:] {
210 commonStr = lcs(commonStr, str)
216 // Abs is a simple absolute value function for ints
217 func Abs(n int) int {
224 // FuncName returns the name of a given function object
225 func FuncName(i interface{}) string {
226 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
229 // SplitCommandArgs seperates multiple command arguments which may be quoted.
230 // The returned slice contains at least one string
231 func SplitCommandArgs(input string) []string {
233 curArg := new(bytes.Buffer)
237 appendResult := func() {
238 str := curArg.String()
241 if strings.HasPrefix(str, `"`) && strings.HasSuffix(str, `"`) {
242 if unquoted, err := strconv.Unquote(str); err == nil {
246 result = append(result, str)
250 for _, r := range input {
251 if r == ' ' && !inQuote {
256 if r == '"' && !inQuote {
259 if inQuote && !escape {
277 // JoinCommandArgs joins multiple command arguments and quote the strings if needed.
278 func JoinCommandArgs(args ...string) string {
279 buf := new(bytes.Buffer)
281 for _, arg := range args {
287 quoted := strconv.Quote(arg)
288 if quoted[1:len(quoted)-1] != arg || strings.ContainsRune(arg, ' ') {
289 buf.WriteString(quoted)