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 occurrences 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 func FSize(f *os.File) int64 {
68 // IsWordChar returns whether or not the string is a 'word character'
69 // If it is a unicode character, then it does not match
70 // Word characters are defined as [A-Za-z0-9_]
71 func IsWordChar(str string) bool {
77 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
80 // IsWhitespace returns true if the given rune is a space, tab, or newline
81 func IsWhitespace(c rune) bool {
82 return c == ' ' || c == '\t' || c == '\n'
85 // Contains returns whether or not a string array contains a given string
86 func Contains(list []string, a string) bool {
87 for _, b := range list {
95 // Insert makes a simple insert into a string at the given position
96 func Insert(str string, pos int, value string) string {
97 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
100 // MakeRelative will attempt to make a relative path between path and base
101 func MakeRelative(path, base string) (string, error) {
103 rel, err := filepath.Rel(base, path)
112 // GetLeadingWhitespace returns the leading whitespace of the given string
113 func GetLeadingWhitespace(str string) string {
115 for _, c := range str {
116 if c == ' ' || c == '\t' {
125 // IsSpaces checks if a given string is only spaces
126 func IsSpaces(str string) bool {
127 for _, c := range str {
136 // IsSpacesOrTabs checks if a given string contains only spaces and tabs
137 func IsSpacesOrTabs(str string) bool {
138 for _, c := range str {
139 if c != ' ' && c != '\t' {
147 // ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
148 // as 'true' and 'false' respectively
149 func ParseBool(str string) (bool, error) {
156 return strconv.ParseBool(str)
159 // EscapePath replaces every path separator in a given path with a %
160 func EscapePath(path string) string {
161 path = filepath.ToSlash(path)
162 return strings.Replace(path, "/", "%", -1)
165 // GetModTime returns the last modification time for a given file
166 // It also returns a boolean if there was a problem accessing the file
167 func GetModTime(path string) (time.Time, bool) {
168 info, err := os.Stat(path)
170 return time.Now(), false
172 return info.ModTime(), true
175 // StringWidth returns the width of a string where tabs count as `tabsize` width
176 func StringWidth(str string, tabsize int) int {
177 sw := runewidth.StringWidth(str)
179 for _, ch := range str {
182 ts := tabsize - (lineIdx % tabsize)
194 // WidthOfLargeRunes searches all the runes in a string and counts up all the widths of runes
195 // that have a width larger than 1 (this also counts tabs as `tabsize` width)
196 func WidthOfLargeRunes(str string, tabsize int) int {
199 for _, ch := range str {
202 w = tabsize - (lineIdx % tabsize)
204 w = runewidth.RuneWidth(ch)
218 // RunePos returns the rune index of a given byte index
219 // This could cause problems if the byte index is between code points
220 func runePos(p int, str string) int {
221 return utf8.RuneCountInString(str[:p])
224 func lcs(a, b string) string {
229 for i, r := range arunes {
230 if i >= len(brunes) {
242 func CommonSubstring(arr ...string) string {
245 for _, str := range arr[1:] {
246 commonStr = lcs(commonStr, str)
252 // Abs is a simple absolute value function for ints
253 func Abs(n int) int {
260 // FuncName returns the name of a given function object
261 func FuncName(i interface{}) string {
262 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
265 // SplitCommandArgs separates multiple command arguments which may be quoted.
266 // The returned slice contains at least one string
267 func SplitCommandArgs(input string) []string {
269 var curQuote *bytes.Buffer
271 curArg := new(bytes.Buffer)
274 finishQuote := func() {
278 str := curQuote.String()
279 if unquoted, err := strconv.Unquote(str); err == nil {
282 curArg.WriteString(str)
286 appendResult := func() {
290 str := curArg.String()
291 result = append(result, str)
295 for _, r := range input {
296 if r == ' ' && curQuote == nil {
300 appendRuneToBuff := func() {
302 curQuote.WriteRune(r)
309 if r == '"' && curQuote == nil {
310 curQuote = new(bytes.Buffer)
313 if curQuote != nil && !escape {
317 } else if r == '\\' {
335 // JoinCommandArgs joins multiple command arguments and quote the strings if needed.
336 func JoinCommandArgs(args ...string) string {
337 buf := new(bytes.Buffer)
339 for _, arg := range args {
345 quoted := strconv.Quote(arg)
346 if quoted[1:len(quoted)-1] != arg || strings.ContainsRune(arg, ' ') {
347 buf.WriteString(quoted)