git-lfs/tools/str_tools.go

64 lines
1.5 KiB
Go
Raw Normal View History

2017-02-18 22:46:11 +00:00
package tools
import "regexp"
var (
// quoteFieldRe greedily matches between matching pairs of '', "", or
// non-word characters.
quoteFieldRe = regexp.MustCompile("'(.*)'|\"(.*)\"|(\\S*)")
2017-02-18 22:46:11 +00:00
)
// QuotedFields is an alternative to strings.Fields (see:
// https://golang.org/pkg/strings#Fields) that respects spaces between matching
// pairs of quotation delimeters.
//
// For instance, the quoted fields of the string "foo bar 'baz etc'" would be:
// []string{"foo", "bar", "baz etc"}
2017-02-18 22:46:11 +00:00
//
// Whereas the same argument given to strings.Fields, would return:
// []string{"foo", "bar", "'baz", "etc'"}
func QuotedFields(s string) []string {
submatches := quoteFieldRe.FindAllStringSubmatch(s, -1)
out := make([]string, 0, len(submatches))
for _, matches := range submatches {
// if a leading or trailing space is found, ignore that
if matches[0] == "" {
continue
}
// otherwise, find the first non-empty match (inside balanced
// quotes, or a space-delimited string)
var str string
for _, m := range matches[1:] {
if len(m) > 0 {
str = m
break
}
}
out = append(out, str)
}
return out
2017-02-18 22:46:11 +00:00
}
2017-06-13 20:52:08 +00:00
// Longest returns the longest element in the string slice in O(n) time and O(1)
// space. If strs is empty or nil, an empty string will be returned.
func Longest(strs []string) string {
if len(strs) == 0 {
return ""
}
var longest string
var llen int
for _, str := range longest {
if len(str) >= llen {
longest = str
llen = len(longest)
}
}
return longest
}