func splitTerms(fieldSelector string) []string {
	if len(fieldSelector) == 0 {
		return nil
	}
	terms := make([]string, 0, 1)
	startIndex := 0
	inSlash := false
	for i, c := range fieldSelector {
		switch {
		case inSlash:
			inSlash = false
		case c == '\\':
			inSlash = true
		case c == ',':
			terms = append(terms, fieldSelector[startIndex:i])
			startIndex = i + 1
		}
	}
	terms = append(terms, fieldSelector[startIndex:])
	return terms
}
splitTerms returns the comma-separated terms contained in the given fieldSelector.
Backslash-escaped commas are treated as data instead of delimiters, and are included in the returned terms, with the leading backslash preserved.
package fields
import (
	"bytes"
	"fmt"
	"sort"
	"strings"
	"k8s.io/apimachinery/pkg/selection"
)
type Selector interface {
	Matches(Fields) bool
	Empty() bool
	RequiresExactMatch(field string) (value string, found bool)
	Transform(fn TransformFunc) (Selector, error)
	Requirements() Requirements
	String() string
	DeepCopySelector() Selector
}
type nothingSelector struct{}
func (n nothingSelector) Matches(_ Fields) bool                                      { return false }
func (n nothingSelector) Empty() bool                                                { return false }
func (n nothingSelector) String() string                                             { return "" }
func (n nothingSelector) Requirements() Requirements                                 { return nil }
func (n nothingSelector) DeepCopySelector() Selector                                 { return n }
func (n nothingSelector) RequiresExactMatch(field string) (value string, found bool) { return "", false }
func (n nothingSelector) Transform(fn TransformFunc) (Selector, error)               { return n, nil }
func Nothing() Selector {
	return nothingSelector{}
}
func Everything() Selector {
	return andTerm{}
}
type hasTerm struct {
	field, value string
}
func (t *hasTerm) Matches(ls Fields) bool {
	return ls.Get(t.field) == t.value
}
func (t *hasTerm) Empty() bool {
	return false
}
func (t *hasTerm) RequiresExactMatch(field string) (value string, found bool) {
	if t.field == field {
		return t.value, true
	}
	return "", false
}
func (t *hasTerm) Transform(fn TransformFunc) (Selector, error) {
	field, value, err := fn(t.field, t.value)
	if err != nil {
		return nil, err
	}
	if len(field) == 0 && len(value) == 0 {
		return Everything(), nil
	}
	return &hasTerm{field, value}, nil
}
func (t *hasTerm) Requirements() Requirements {
	return []Requirement{{
		Field:    t.field,
		Operator: selection.Equals,
		Value:    t.value,
	}}
}
func (t *hasTerm) String() string {
	return fmt.Sprintf("%v=%v", t.field, EscapeValue(t.value))
}
func (t *hasTerm) DeepCopySelector() Selector {
	if t == nil {
		return nil
	}
	out := new(hasTerm)
	*out = *t
	return out
}
type notHasTerm struct {
	field, value string
}
func (t *notHasTerm) Matches(ls Fields) bool {
	return ls.Get(t.field) != t.value
}
func (t *notHasTerm) Empty() bool {
	return false
}
func (t *notHasTerm) RequiresExactMatch(field string) (value string, found bool) {
	return "", false
}
func (t *notHasTerm) Transform(fn TransformFunc) (Selector, error) {
	field, value, err := fn(t.field, t.value)
	if err != nil {
		return nil, err
	}
	if len(field) == 0 && len(value) == 0 {
		return Everything(), nil
	}
	return &notHasTerm{field, value}, nil
}
func (t *notHasTerm) Requirements() Requirements {
	return []Requirement{{
		Field:    t.field,
		Operator: selection.NotEquals,
		Value:    t.value,
	}}
}
func (t *notHasTerm) String() string {
	return fmt.Sprintf("%v!=%v", t.field, EscapeValue(t.value))
}
func (t *notHasTerm) DeepCopySelector() Selector {
	if t == nil {
		return nil
	}
	out := new(notHasTerm)
	*out = *t
	return out
}
type andTerm []Selector
func (t andTerm) Matches(ls Fields) bool {
	for _, q := range t {
		if !q.Matches(ls) {
			return false
		}
	}
	return true
}
func (t andTerm) Empty() bool {
	if t == nil {
		return true
	}
	if len([]Selector(t)) == 0 {
		return true
	}
	for i := range t {
		if !t[i].Empty() {
			return false
		}
	}
	return true
}
func (t andTerm) RequiresExactMatch(field string) (string, bool) {
	if t == nil || len([]Selector(t)) == 0 {
		return "", false
	}
	for i := range t {
		if value, found := t[i].RequiresExactMatch(field); found {
			return value, found
		}
	}
	return "", false
}
func (t andTerm) Transform(fn TransformFunc) (Selector, error) {
	next := make([]Selector, 0, len([]Selector(t)))
	for _, s := range []Selector(t) {
		n, err := s.Transform(fn)
		if err != nil {
			return nil, err
		}
		if !n.Empty() {
			next = append(next, n)
		}
	}
	return andTerm(next), nil
}
func (t andTerm) Requirements() Requirements {
	reqs := make([]Requirement, 0, len(t))
	for _, s := range []Selector(t) {
		rs := s.Requirements()
		reqs = append(reqs, rs...)
	}
	return reqs
}
func (t andTerm) String() string {
	var terms []string
	for _, q := range t {
		terms = append(terms, q.String())
	}
	return strings.Join(terms, ",")
}
func (t andTerm) DeepCopySelector() Selector {
	if t == nil {
		return nil
	}
	out := make([]Selector, len(t))
	for i := range t {
		out[i] = t[i].DeepCopySelector()
	}
	return andTerm(out)
}
func SelectorFromSet(ls Set) Selector {
	if ls == nil {
		return Everything()
	}
	items := make([]Selector, 0, len(ls))
	for field, value := range ls {
		items = append(items, &hasTerm{field: field, value: value})
	}
	if len(items) == 1 {
		return items[0]
	}
	return andTerm(items)
}
var valueEscaper = strings.NewReplacer(
	`\`, `\\`,
	`,`, `\,`,
	`=`, `\=`,
)
func EscapeValue(s string) string {
	return valueEscaper.Replace(s)
}
type InvalidEscapeSequence struct {
	sequence string
}
func (i InvalidEscapeSequence) Error() string {
	return fmt.Sprintf("invalid field selector: invalid escape sequence: %s", i.sequence)
}
type UnescapedRune struct {
	r rune
}
func (i UnescapedRune) Error() string {
	return fmt.Sprintf("invalid field selector: unescaped character in value: %v", i.r)
}
func UnescapeValue(s string) (string, error) {
	if !strings.ContainsAny(s, `\,=`) {
		return s, nil
	}
	v := bytes.NewBuffer(make([]byte, 0, len(s)))
	inSlash := false
	for _, c := range s {
		if inSlash {
			switch c {
			case '\\', ',', '=':
				v.WriteRune(c)
			default:
				return "", InvalidEscapeSequence{sequence: string([]rune{'\\', c})}
			}
			inSlash = false
			continue
		}
		switch c {
		case '\\':
			inSlash = true
		case ',', '=':
			return "", UnescapedRune{r: c}
		default:
			v.WriteRune(c)
		}
	}
	if inSlash {
		return "", InvalidEscapeSequence{sequence: "\\"}
	}
	return v.String(), nil
}
func ParseSelectorOrDie(s string) Selector {
	selector, err := ParseSelector(s)
	if err != nil {
		panic(err)
	}
	return selector
}
func ParseSelector(selector string) (Selector, error) {
	return parseSelector(selector,
		func(lhs, rhs string) (newLhs, newRhs string, err error) {
			return lhs, rhs, nil
		})
}
func ParseAndTransformSelector(selector string, fn TransformFunc) (Selector, error) {
	return parseSelector(selector, fn)
}
type TransformFunc func(field, value string) (newField, newValue string, err error)