// Finds the first sub pattern that has match for data after pos
320096
func (p *Pattern) FirstMatch(data string, pos int) (pat *Pattern, ret textmate.MatchObject) {
	startIdx := -1
	for i := 0; i < len(p.cachedPatterns); {
		ip, im := p.cachedPatterns[i].Cache(data, pos)
		// If it wasn't found now, it'll never be found,
		// so the pattern can be popped from the cache
		if im == nil {
			copy(p.cachedPatterns[i:], p.cachedPatterns[i+1:])
			p.cachedPatterns = p.cachedPatterns[:len(p.cachedPatterns)-1]
			continue
		}
		// ???: what is startIdx > im[0] for?
		if startIdx < 0 || startIdx > im[0] {
			startIdx, pat, ret = im[0], ip, im
			// This match is right at the start, we're not
			// going to find a better pattern than this,
			// so stop the search
			if im[0] == pos {
				break
			}
		}
		i++
	}
	return
}
337573
func (e *Error) FindStr(sub string) int {
	deep := 0
	for err := e; err != nil; err = err.next {
		if err.Contains(sub) {
			return deep
		}
		deep = deep + 1
	}
	return -1
}
996500
func (l *lex) spanRegexp(re *regexp.Regexp) (before string, match string, success bool) {
	loc := re.FindStringSubmatchIndex(l.s[l.p:])
	if loc == nil {
		return "", "", false
	}
	success = true
	start, end := loc[2], loc[3]
	before = l.s[l.p : l.p+start]
	match = l.s[l.p:][start:end]
	l.p += end
	return
}
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
}
335418
func getPatternFromLine(line string) (*regexp.Regexp, bool) {
	// Trim OS-specific carriage returns.
	line = strings.TrimRight(line, "\r")

	// Strip comments [Rule 2]
	if strings.HasPrefix(line, `#`) {
		return nil, false
	}

	// Trim string [Rule 3]
	// TODO: Handle [Rule 3], when the " " is escaped with a \
	line = strings.Trim(line, " ")

	// Exit for no-ops and return nil which will prevent us from
	// appending a pattern against this line
	if line == "" {
		return nil, false
	}

	// TODO: Handle [Rule 4] which negates the match for patterns leading with "!"
	negatePattern := false
	if line[0] == '!' {
		negatePattern = true
		line = line[1:]
	}

	// Handle [Rule 2, 4], when # or ! is escaped with a \
	// Handle [Rule 4] once we tag negatePattern, strip the leading ! char
	if regexp.MustCompile(`^(\#|\!)`).MatchString(line) {
		line = line[1:]
	}

	// If we encounter a foo/*.blah in a folder, prepend the / char
	if regexp.MustCompile(`([^\/+])/.*\*\.`).MatchString(line) && line[0] != '/' {
		line = "/" + line
	}

	// Handle escaping the "." char
	line = regexp.MustCompile(`\.`).ReplaceAllString(line, `\.`)

	magicStar := "#$~"

	// Handle "/**/" usage
	if strings.HasPrefix(line, "/**/") {
		line = line[1:]
	}
	line = regexp.MustCompile(`/\*\*/`).ReplaceAllString(line, `(/|/.+/)`)
	line = regexp.MustCompile(`\*\*/`).ReplaceAllString(line, `(|.`+magicStar+`/)`)
	line = regexp.MustCompile(`/\*\*`).ReplaceAllString(line, `(|/.`+magicStar+`)`)

	// Handle escaping the "*" char
	line = regexp.MustCompile(`\\\*`).ReplaceAllString(line, `\`+magicStar)
	line = regexp.MustCompile(`\*`).ReplaceAllString(line, `([^/]*)`)

	// Handle escaping the "?" char
	line = strings.Replace(line, "?", `\?`, -1)

	line = strings.Replace(line, magicStar, "*", -1)

	// Temporary regex
	var expr = ""
	if strings.HasSuffix(line, "/") {
		expr = line + "(|.*)$"
	} else {
		expr = line + "(|/.*)$"
	}
	if strings.HasPrefix(expr, "/") {
		expr = "^(|/)" + expr[1:]
	} else {
		expr = "^(|.*/)" + expr
	}
	pattern, _ := regexp.Compile(expr)

	return pattern, negatePattern
}
319463
func (re *Regexp) FindStringMatch(s string) (*Match, error) {
	// convert string to runes
	return re.run(false, -1, getRunes(s))
}
1067227
func (loc *Location) lastMatchedLineIndex(re *regexp.Regexp) int {
	for i := len(loc.Line) - 1; i >= 0; i-- {
		if fn := loc.Line[i].Function; fn != nil {
			if re.MatchString(fn.Name) || re.MatchString(fn.Filename) {
				return i
			}
		}
	}
	return -1
}
851720
func (l *Lexer) indexRegexp(r *regexp.Regexp) int {
	loc := r.FindStringIndex(l.input[l.pos:])
	if loc == nil {
		return -1
	}
	return loc[0]
}
1329332
func (l *Lexer) findRegexp(r *regexp.Regexp) string {
	return r.FindString(l.input[l.pos:])
}
848911
func FindSubnet(c *kops.Cluster, subnetName string) *kops.ClusterSubnetSpec {
	for i := range c.Spec.Subnets {
		if c.Spec.Subnets[i].Name == subnetName {
			return &c.Spec.Subnets[i]
		}
	}
	return nil
}
764006
func FindPrefixOfIndex(cols []*Column, idxColIDs []int64) []*Column {
	retCols := make([]*Column, 0, len(idxColIDs))
idLoop:
	for _, id := range idxColIDs {
		for _, col := range cols {
			if col.UniqueID == id {
				retCols = append(retCols, col)
				continue idLoop
			}
		}
		// If no matching column is found, just return.
		return retCols
	}
	return retCols
}
202960
func (this *Context) Find(re interface{}) *Context {
    r := getRegexp(re)
    matches := r.FindSubmatch(this.Bytes())
    names := make(map[string]int)

    for k, v := range r.SubexpNames() {
        if v != "" {
            names[v] = k
        }
    }

    result := &Context {
        Parent: this,
        List: matches,
        Names: names,
    }

    return result
}
64891
func (this *Context) FindAll(re interface{}) Collection {
    r := getRegexp(re)

    matches := r.FindAllSubmatch(this.Bytes(), -1)

    names := make(map[string]int)

    for k, v := range r.SubexpNames() {
        if v != "" {
            names[v] = k
        }
    }

    result := make(Collection, len(matches))
    for i, match := range matches {
        result[i] = &Context {
            Parent: this,
            List: match,
            Names: names,
        }
    }

    return result
}
512038
func RegexMatch(key1 string, key2 string) bool {
	res, err := regexp.MatchString(key2, key1)
	if err != nil {
		panic(err)
	}
	return res
}
311841
func findCommaFromEnd(b []byte, offset int) int {
	for i := len(b) - 1; i > offset; i-- {
		if b[i] == ',' {
			return i
		}
	}
	panic(fmt.Errorf("no comma found in %q after offset %d", b, offset))
}
1236311
func nextParenthesis(pattern string) (start, end int) {
	level := 0
	for i := 0; i < len(pattern); i++ {
		c := pattern[i]
		switch c {
		case '(':
			level++
			if level == 1 {
				start = i
			}
		case ')':
			level--
			if level == 0 {
				end = i
				return
			} else if level < 0 {
				panicm("too many closed parenthesis in %s", pattern)
			}
		}
	}

	if level != 0 {
		panicm("unbalanced parenthesis in %s", pattern)
	}

	return
}
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
}