// Parses the FlowSpec numeric operator using the given submatch which should be
// the return value of func (*Regexp) FindStringSubmatch.
359901
func parseFlowSpecNumericOperator(submatch []string) (operator uint8, err error) {
	if submatch[1] == "&" {
		operator = DEC_NUM_OP_AND
	}
	value, ok := DECNumOpValueMap[submatch[2]]
	if !ok {
		return 0, fmt.Errorf("invalid numeric operator: %s%s", submatch[1], submatch[2])
	}
	operator |= uint8(value)
	return operator, nil
}
912460
func (p *Parser) parseField() (*Field, error) {
	f := &Field{}

	// Attempt to parse a regex.
	re, err := p.parseRegex()
	if err != nil {
		return nil, err
	} else if re != nil {
		f.Expr = re
	} else {
		_, pos, _ := p.ScanIgnoreWhitespace()
		p.Unscan()
		// Parse the expression first.
		expr, err := p.ParseExpr()
		if err != nil {
			return nil, err
		}
		var c validateField
		Walk(&c, expr)
		if c.foundInvalid {
			return nil, fmt.Errorf("invalid operator %s in SELECT clause at line %d, char %d; operator is intended for WHERE clause", c.badToken, pos.Line+1, pos.Char+1)
		}
		f.Expr = expr
	}

	// Parse the alias if the current and next tokens are "WS AS".
	alias, err := p.parseAlias()
	if err != nil {
		return nil, err
	}
	f.Alias = alias

	// Consume all trailing whitespace.
	p.consumeWhitespace()

	return f, nil
}
1092854
func parseFunction(expr *influxql.Call) (*function, error) {
	switch expr.Name {
	case "count":
		if exp, got := 1, len(expr.Args); exp != got {
			return nil, fmt.Errorf("invalid number of arguments for %s, expected %d, got %d", expr.Name, exp, got)
		}

		switch ref := expr.Args[0].(type) {
		case *influxql.VarRef:
			return &function{
				Ref:  ref,
				call: expr,
			}, nil
		case *influxql.Call:
			if ref.Name == "distinct" {
				return nil, errors.New("unimplemented: count(distinct)")
			}
			return nil, fmt.Errorf("expected field argument in %s()", expr.Name)
		case *influxql.Distinct:
			return nil, errors.New("unimplemented: count(distinct)")
		case *influxql.Wildcard:
			return nil, errors.New("unimplemented: wildcard function")
		case *influxql.RegexLiteral:
			return nil, errors.New("unimplemented: wildcard regex function")
		default:
			return nil, fmt.Errorf("expected field argument in %s()", expr.Name)
		}
	case "min", "max", "sum", "first", "last", "mean", "median":
		if exp, got := 1, len(expr.Args); exp != got {
			return nil, fmt.Errorf("invalid number of arguments for %s, expected %d, got %d", expr.Name, exp, got)
		}

		switch ref := expr.Args[0].(type) {
		case *influxql.VarRef:
			return &function{
				Ref:  ref,
				call: expr,
			}, nil
		case *influxql.Wildcard:
			return nil, errors.New("unimplemented: wildcard function")
		case *influxql.RegexLiteral:
			return nil, errors.New("unimplemented: wildcard regex function")
		default:
			return nil, fmt.Errorf("expected field argument in %s()", expr.Name)
		}
	case "percentile":
		if exp, got := 2, len(expr.Args); exp != got {
			return nil, fmt.Errorf("invalid number of arguments for %s, expected %d, got %d", expr.Name, exp, got)
		}

		var functionRef *influxql.VarRef

		switch ref := expr.Args[0].(type) {
		case *influxql.VarRef:
			functionRef = ref
		case *influxql.Wildcard:
			return nil, errors.New("unimplemented: wildcard function")
		case *influxql.RegexLiteral:
			return nil, errors.New("unimplemented: wildcard regex function")
		default:
			return nil, fmt.Errorf("expected field argument in %s()", expr.Name)
		}

		switch expr.Args[1].(type) {
		case *influxql.IntegerLiteral:
		case *influxql.NumberLiteral:
		default:
			return nil, fmt.Errorf("expected float argument in %s()", expr.Name)
		}

		return &function{
			Ref:  functionRef,
			call: expr,
		}, nil
	default:
		return nil, fmt.Errorf("unimplemented function: %q", expr.Name)
	}

}
1062354
func ParseMatchers(regexes, inverseRegexes, globs, inverseGlobs []string) (m Matcher, err error) {
	var matchers multiMatcher
	if len(regexes) == 0 && len(globs) == 0 {
		matchers = multiMatcher{matchAll{}}
	}
	for _, r := range regexes {
		regex, err := regexp.Compile(r)
		if err != nil {
			return nil, err
		}
		matchers = append(matchers, newRegexMatcher(regex, false))
	}
	for _, r := range inverseRegexes {
		regex, err := regexp.Compile(r)
		if err != nil {
			return nil, err
		}
		matchers = append(matchers, newRegexMatcher(regex, true))
	}
	for _, g := range globs {
		matchers = append(matchers, &globMatcher{glob: g})
	}
	for _, g := range inverseGlobs {
		matchers = append(matchers, &globMatcher{
			glob:    g,
			inverse: true,
		})
	}
	return matchers, nil
}
1253656
func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool {
	if h, ok := a.t.(tHelper); ok {
		h.Helper()
	}
	return Regexp(a.t, rx, str, msgAndArgs...)
}
416973
func MatchesExpr(cr expr.EvalContext, node expr.Node) (bool, bool) {
	return matchesExpr(cr, node, 0)
}
593173
func RegexMatchFunc(args ...interface{}) (interface{}, error) {
	name1 := args[0].(string)
	name2 := args[1].(string)

	return bool(RegexMatch(name1, name2)), nil
}
439815
func matchSimpleCall(expr syntax.Expr) (fn []string, args []syntax.Expr) {
	call, ok := expr.(*syntax.CallExpr)
	if !ok {
		return nil, nil
	}
	if fn = matchRefPath(call.Fn); len(fn) == 0 {
		return nil, nil
	}
	return fn, call.Args
}
152980
func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool {
	return Regexp(a.t, rx, str, msgAndArgs...)
}
32989
func parseLine(r *regexp.Regexp, line string) (int, error) {
	matches := r.FindStringSubmatch(line)
	switch len(matches) {
	case 0:
		return -1, nil
	case 2:
		return strconv.Atoi(matches[1])
	default:
		return 0, ErrBadSgdiskOutput
	}
}
735041
func Regexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool {
	if h, ok := t.(tHelper); ok {
		h.Helper()
	}
	return Regexp(t, rx, str, append([]interface{}{msg}, args...)...)
}
557795
func parseAct(desc string) (string, actFunc) {
	for k, v := range actMap {
		if strings.HasPrefix(desc, k) {
			return k, v
		}
	}
	return "", nil
}
316994
func StringMatch(r *regexp.Regexp, message string) schema.SchemaValidateFunc {
	return func(i interface{}, k string) ([]string, []error) {
		v, ok := i.(string)
		if !ok {
			return nil, []error{fmt.Errorf("expected type of %s to be string", k)}
		}

		if ok := r.MatchString(v); !ok {
			if message != "" {
				return nil, []error{fmt.Errorf("invalid value for %s (%s)", k, message)}

			}
			return nil, []error{fmt.Errorf("expected value of %s to match regular expression %q", k, r)}
		}
		return nil, nil
	}
}
960727
func (r *rule) sub(text string) string {
	if !r.pattern.MatchString(text) {
		return text
	}

	orig := len(text)
	diff := 0
	for _, submat := range r.pattern.FindAllStringSubmatchIndex(text, -1) {
		for idx, mat := range submat {
			if mat != -1 && idx > 0 && idx%2 == 0 {
				loc := []int{mat - diff, submat[idx+1] - diff}
				text = text[:loc[0]] + r.replacement + text[loc[1]:]
				diff = orig - len(text)
			}
		}
	}

	return text
}
1294841
func matchRegex(re *syntax.Regexp) ([]string, bool) {
	// Exit if we see a case-insensitive flag as it is not something we support at this time.
	if re.Flags&syntax.FoldCase != 0 {
		return nil, false
	}

	switch re.Op {
	case syntax.OpLiteral:
		// We can rewrite this regex.
		return []string{string(re.Rune)}, true
	case syntax.OpCapture:
		return matchRegex(re.Sub[0])
	case syntax.OpConcat:
		// Go through each of the subs and concatenate the result to each one.
		names, ok := matchRegex(re.Sub[0])
		if !ok {
			return nil, false
		}

		for _, sub := range re.Sub[1:] {
			vals, ok := matchRegex(sub)
			if !ok {
				return nil, false
			}

			// If there is only one value, concatenate it to all strings rather
			// than allocate a new slice.
			if len(vals) == 1 {
				for i := range names {
					names[i] += vals[0]
				}
				continue
			} else if len(names) == 1 {
				// If there is only one value, then do this concatenation in
				// the opposite direction.
				for i := range vals {
					vals[i] = names[0] + vals[i]
				}
				names = vals
				continue
			}

			// The long method of using multiple concatenations.
			concat := make([]string, len(names)*len(vals))
			for i := range names {
				for j := range vals {
					concat[i*len(vals)+j] = names[i] + vals[j]
				}
			}
			names = concat
		}
		return names, true
	case syntax.OpCharClass:
		var sz int
		for i := 0; i < len(re.Rune); i += 2 {
			sz += int(re.Rune[i+1]) - int(re.Rune[i]) + 1
		}

		names := make([]string, 0, sz)
		for i := 0; i < len(re.Rune); i += 2 {
			for r := int(re.Rune[i]); r <= int(re.Rune[i+1]); r++ {
				names = append(names, string([]rune{rune(r)}))
			}
		}
		return names, true
	case syntax.OpAlternate:
		var names []string
		for _, sub := range re.Sub {
			vals, ok := matchRegex(sub)
			if !ok {
				return nil, false
			}
			names = append(names, vals...)
		}
		return names, true
	}
	return nil, false
}
202871
func (p *Parser) Match(typ TokenType, val string) *Token {
	if t := p.Peek(typ, val); t != nil {
		p.Consume()
		return t
	}
	return nil
}
464307
func NamedFindStringSubmatch(rexp *regexp.Regexp, text string) (map[string]string, bool) {
	match := rexp.FindStringSubmatch(text)
	if match == nil {
		return nil, false
	}
	result := map[string]string{}
	for i, name := range rexp.SubexpNames() {
		if i != 0 {
			result[name] = match[i]
		}
	}
	return result, true
}