return utf8.RuneCountInString(s)
}
-// NumOccurrences counts the number of occurences of a byte in a string
+// NumOccurrences counts the number of occurrences of a byte in a string
func NumOccurrences(s string, c byte) int {
var n int
for i := 0; i < len(s); i++ {
return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
}
+// MakeRelative will attempt to make a relative path between path and base
+func MakeRelative(path, base string) (string, error) {
+ if len(path) > 0 {
+ rel, err := filepath.Rel(base, path)
+ if err != nil {
+ return path, err
+ }
+ return rel, nil
+ }
+ return path, nil
+}
+
// GetLeadingWhitespace returns the leading whitespace of the given string
func GetLeadingWhitespace(str string) string {
ws := ""
// StringWidth returns the width of a string where tabs count as `tabsize` width
func StringWidth(str string, tabsize int) int {
sw := runewidth.StringWidth(str)
- sw += NumOccurrences(str, '\t') * (tabsize - 1)
+ lineIdx := 0
+ for _, ch := range str {
+ switch ch {
+ case '\t':
+ ts := tabsize - (lineIdx % tabsize)
+ sw += ts - 1
+ lineIdx += ts
+ case '\n':
+ lineIdx = 0
+ default:
+ lineIdx++
+ }
+ }
return sw
}
// that have a width larger than 1 (this also counts tabs as `tabsize` width)
func WidthOfLargeRunes(str string, tabsize int) int {
count := 0
+ lineIdx := 0
for _, ch := range str {
var w int
if ch == '\t' {
- w = tabsize
+ w = tabsize - (lineIdx % tabsize)
} else {
w = runewidth.RuneWidth(ch)
}
if w > 1 {
count += (w - 1)
}
+ if ch == '\n' {
+ lineIdx = 0
+ } else {
+ lineIdx += w
+ }
}
return count
}
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
-// SplitCommandArgs seperates multiple command arguments which may be quoted.
+// SplitCommandArgs separates multiple command arguments which may be quoted.
// The returned slice contains at least one string
func SplitCommandArgs(input string) []string {
var result []string
- var curQuote *bytes.Buffer = nil
+ var curQuote *bytes.Buffer
curArg := new(bytes.Buffer)
escape := false