You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package core
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// byName and its associated functions provide a quick method of sorting FileInfos by name
|
|
type byName []os.FileInfo
|
|
|
|
func (s byName) Len() int { return len(s) }
|
|
func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
|
|
func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
// SplitBy takes an input string and a delimiter, returning the resulting two strings from the split (ALWAYS 2)
|
|
func SplitBy(input, delim string) (string, string) {
|
|
index := strings.Index(input, delim)
|
|
if index == -1 {
|
|
return input, ""
|
|
}
|
|
return input[:index], input[index+len(delim):]
|
|
}
|
|
|
|
// SplitByBefore takes an input string and a delimiter, returning resulting two string from split with the delim at beginning of 2nd
|
|
func SplitByBefore(input, delim string) (string, string) {
|
|
index := strings.Index(input, delim)
|
|
if index == -1 {
|
|
return input, ""
|
|
}
|
|
return input[:index], input[index:]
|
|
}
|
|
|
|
// SplitByLast takes an input string and a delimiter, returning resulting two strings from split at LAST occurence
|
|
func SplitByLast(input, delim string) (string, string) {
|
|
index := strings.LastIndex(input, delim)
|
|
if index == -1 {
|
|
return input, ""
|
|
}
|
|
return input[:index], input[index+len(delim):]
|
|
}
|
|
|
|
// FileExt returns the file extension of a file with a supplied path, doesn't include '.'
|
|
func FileExt(path string) string {
|
|
i := len(path) - 1
|
|
for ; i >= 0; i-- {
|
|
switch path[i] {
|
|
case '/':
|
|
return ""
|
|
case '.':
|
|
return path[i+1:]
|
|
}
|
|
}
|
|
return ""
|
|
}
|