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 // IsStrWhitespace returns true if the given string is all whitespace
86 func IsStrWhitespace(str string) bool {
87 for _, c := range str {
95 // Contains returns whether or not a string array contains a given string
96 func Contains(list []string, a string) bool {
97 for _, b := range list {
105 // Insert makes a simple insert into a string at the given position
106 func Insert(str string, pos int, value string) string {
107 return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
110 // MakeRelative will attempt to make a relative path between path and base
111 func MakeRelative(path, base string) (string, error) {
113 rel, err := filepath.Rel(base, path)
122 // GetLeadingWhitespace returns the leading whitespace of the given string
123 func GetLeadingWhitespace(str string) string {
125 for _, c := range str {
126 if c == ' ' || c == '\t' {
135 // IsSpaces checks if a given string is only spaces
136 func IsSpaces(str string) bool {
137 for _, c := range str {
146 // IsSpacesOrTabs checks if a given string contains only spaces and tabs
147 func IsSpacesOrTabs(str string) bool {
148 for _, c := range str {
149 if c != ' ' && c != '\t' {
157 // ParseBool is almost exactly like strconv.ParseBool, except it also accepts 'on' and 'off'
158 // as 'true' and 'false' respectively
159 func ParseBool(str string) (bool, error) {
166 return strconv.ParseBool(str)
169 // EscapePath replaces every path separator in a given path with a %
170 func EscapePath(path string) string {
171 path = filepath.ToSlash(path)
172 return strings.Replace(path, "/", "%", -1)
175 // GetModTime returns the last modification time for a given file
176 // It also returns a boolean if there was a problem accessing the file
177 func GetModTime(path string) (time.Time, bool) {
178 info, err := os.Stat(path)
180 return time.Now(), false
182 return info.ModTime(), true
185 // StringWidth returns the width of a string where tabs count as `tabsize` width
186 func StringWidth(str string, tabsize int) int {
187 sw := runewidth.StringWidth(str)
189 for _, ch := range str {
192 ts := tabsize - (lineIdx % tabsize)
204 // WidthOfLargeRunes searches all the runes in a string and counts up all the widths of runes
205 // that have a width larger than 1 (this also counts tabs as `tabsize` width)
206 func WidthOfLargeRunes(str string, tabsize int) int {
209 for _, ch := range str {
212 w = tabsize - (lineIdx % tabsize)
214 w = runewidth.RuneWidth(ch)
228 // RunePos returns the rune index of a given byte index
229 // This could cause problems if the byte index is between code points
230 func runePos(p int, str string) int {
231 return utf8.RuneCountInString(str[:p])
234 func lcs(a, b string) string {
239 for i, r := range arunes {
240 if i >= len(brunes) {
252 func CommonSubstring(arr ...string) string {
255 for _, str := range arr[1:] {
256 commonStr = lcs(commonStr, str)
262 // Abs is a simple absolute value function for ints
263 func Abs(n int) int {
270 // FuncName returns the name of a given function object
271 func FuncName(i interface{}) string {
272 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
275 // SplitCommandArgs separates multiple command arguments which may be quoted.
276 // The returned slice contains at least one string
277 func SplitCommandArgs(input string) []string {
279 var curQuote *bytes.Buffer
281 curArg := new(bytes.Buffer)
284 finishQuote := func() {
288 str := curQuote.String()
289 if unquoted, err := strconv.Unquote(str); err == nil {
292 curArg.WriteString(str)
296 appendResult := func() {
300 str := curArg.String()
301 result = append(result, str)
305 for _, r := range input {
306 if r == ' ' && curQuote == nil {
310 appendRuneToBuff := func() {
312 curQuote.WriteRune(r)
319 if r == '"' && curQuote == nil {
320 curQuote = new(bytes.Buffer)
323 if curQuote != nil && !escape {
327 } else if r == '\\' {
345 // JoinCommandArgs joins multiple command arguments and quote the strings if needed.
346 func JoinCommandArgs(args ...string) string {
347 buf := new(bytes.Buffer)
349 for _, arg := range args {
355 quoted := strconv.Quote(arg)
356 if quoted[1:len(quoted)-1] != arg || strings.ContainsRune(arg, ' ') {
357 buf.WriteString(quoted)