// findDiskIndex - returns the i,j'th position of the input `format` against the reference
// format, after successful validation.
514246
func findDiskIndex(refFormat, format *formatXLV3) (int, int, error) {
	if err := formatXLV3Check(refFormat, format); err != nil {
		return 0, 0, err
	}

	if format.XL.This == offlineDiskUUID {
		return -1, -1, fmt.Errorf("diskID: %s is offline", format.XL.This)
	}

	for i := 0; i < len(refFormat.XL.Sets); i++ {
		for j := 0; j < len(refFormat.XL.Sets[0]); j++ {
			if refFormat.XL.Sets[i][j] == format.XL.This {
				return i, j, nil
			}
		}
	}

	return -1, -1, fmt.Errorf("diskID: %s not found", format.XL.This)
}
352004
func FindDeepText(n *html.Node) (data string) {
	var buffer string
	var f func(*html.Node)
	f = func(input *html.Node) {
		for c := input.FirstChild; c != nil; c = c.NextSibling {
			if c.Type == html.TextNode {
				buffer = strings.TrimSpace(c.Data)
				if len(buffer) > 0 {
					data += " " + buffer
				}
			}
			f(c)
		}
	}
	f(n)
	return strings.TrimSpace(data)
}
838280
func findDocIdent(docs *doc.Package, ident string) (node interface{}, pos token.Pos) {
	searchFuncs := func(funcs []*doc.Func) bool {
		for _, f := range funcs {
			if f.Name == ident {
				node = f
				pos = f.Decl.Pos()
				return true
			}
		}
		return false
	}

	searchVars := func(vars []*doc.Value) bool {
		for _, v := range vars {
			for _, spec := range v.Decl.Specs {
				switch t := spec.(type) {
				case *ast.ValueSpec:
					for _, name := range t.Names {
						if name.Name == ident {
							node = v
							pos = name.Pos()
							return true
						}
					}
				}
			}
		}
		return false
	}

	if searchFuncs(docs.Funcs) {
		return
	}

	if searchVars(docs.Consts) {
		return
	}

	if searchVars(docs.Vars) {
		return
	}

	for _, t := range docs.Types {
		if t.Name == ident {
			node = t
			pos = t.Decl.Pos()
			return
		}

		if searchFuncs(t.Funcs) {
			return
		}

		if searchVars(t.Consts) {
			return
		}

		if searchVars(t.Vars) {
			return
		}
	}

	return
}
1246999
func findValue(v reflect.Value) (reflect.Value, bool) {
	for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
		if v.IsNil() {
			return v, true
		}
		if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
			break
		}
	}
	return v, false
}
1242468
func formatFindTabular(writer io.Writer, value interface{}) error {
	endpoints, ok := value.(map[string]ApplicationOfferResult)
	if !ok {
		return errors.Errorf("expected value of type %T, got %T", endpoints, value)
	}
	return formatFoundEndpointsTabular(writer, endpoints)
}
1076157
func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
	if len(idx.FanoutMapping) <= int(h[0]) {
		return 0, plumbing.ErrObjectNotFound
	}

	k := idx.FanoutMapping[h[0]]
	i, ok := idx.findHashIndex(h)
	if !ok {
		return 0, plumbing.ErrObjectNotFound
	}

	offset := idx.getOffset(k, i)

	if !idx.offsetHashIsFull {
		// Save the offset for reverse lookup
		if idx.offsetHash == nil {
			idx.offsetHash = make(map[int64]plumbing.Hash)
		}
		idx.offsetHash[int64(offset)] = h
	}

	return int64(offset), nil
}
692204
func (p *contentProvider) findOffset(filename bool, r uint32) uint32 {
	if p.id.metaData.PlainASCII {
		return r
	}

	sample := p.id.runeOffsets
	runeEnds := p.id.fileEndRunes
	fileStartByte := p.id.boundaries[p.idx]
	if filename {
		sample = p.id.fileNameRuneOffsets
		runeEnds = p.id.fileNameEndRunes
		fileStartByte = p.id.fileNameIndex[p.idx]
	}

	absR := r
	if p.idx > 0 {
		absR += runeEnds[p.idx-1]
	}

	byteOff := sample[absR/runeOffsetFrequency]
	left := absR % runeOffsetFrequency

	var data []byte

	if filename {
		data = p.id.fileNameContent[byteOff:]
	} else {
		data, p.err = p.id.readContentSlice(byteOff, 3*runeOffsetFrequency)
		if p.err != nil {
			return 0
		}
	}
	for left > 0 {
		_, sz := utf8.DecodeRune(data)
		byteOff += uint32(sz)
		data = data[sz:]
		left--
	}

	byteOff -= fileStartByte
	return byteOff
}
1112123
func getDisposition(defaultInlineValue bool, mimeType string) string {
	res, found := supportedContentTypes[mimeType]
	if (found && res) || (!found && defaultInlineValue) {
		return "inline"
	}
	return "attachment"
}
877801
func Find(ctx context.Context, ir io.Reader, hashesToFind []uint32, size int) (map[uint32][]int64, error) {
	if ir == nil || len(hashesToFind) == 0 {
		return nil, nil
	}

	r := bufio.NewReader(ir)
	hf := adler32.New()

	n, err := io.CopyN(hf, r, int64(size))
	if err == io.EOF {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	if n != int64(size) {
		return nil, io.ErrShortBuffer
	}

	offsets := make(map[uint32][]int64)
	for _, hashToFind := range hashesToFind {
		offsets[hashToFind] = make([]int64, 0, maxWeakhashFinderHits)
	}

	var i int64
	var hash uint32
	for {
		select {
		case <-ctx.Done():
			return nil, ctx.Err()
		default:
		}

		hash = hf.Sum32()
		if existing, ok := offsets[hash]; ok && len(existing) < maxWeakhashFinderHits {
			offsets[hash] = append(existing, i)
		}
		i++

		bt, err := r.ReadByte()
		if err == io.EOF {
			break
		} else if err != nil {
			return offsets, err
		}
		hf.Roll(bt)
	}
	return offsets, nil
}
789357
func parseStandardIndex(indexSpec string) (*mgo.Index, error) {

	key := parseIndexKey(indexSpec)

	if key == "" {
		// invalid key format, was not parsed successfully
		return nil, errors.New("Standard key not of format: <key>_(-)1")
	}

	return &mgo.Index{
		Key: []string{key},
	}, nil
}
177415
func (obj *GCSObject) extractKindNameForDatastoreAdmin(name string) string {
	if v := strings.LastIndex(name, "/"); v != -1 {
		name = name[v:]
	}
	vs := strings.Split(name, ".")
	if len(vs) != 3 {
		return ""
	}
	if vs[2] != "backup_info" {
		return ""
	}
	return vs[1]
}
475507
func (obj *GCSObject) extractKindNameForDatastoreExport(name string) string {
	if v := strings.LastIndex(name, "."); v != -1 {
		if name[v:] != ".export_metadata" {
			return ""
		}
	} else {
		// The expected value is the value of ".export_metadata" at the end, so exclude if there is no.
		return ""
	}

	if v := strings.LastIndex(name, "/"); v != -1 {
		name = name[:v]
	}
	if v := strings.LastIndex(name, "/"); v != -1 {
		name = name[v:]
	}

	return name[len("/kind_"):]
}
147711
func (t Types) GetBySuffix(suffix string) (tp Type, found bool) {
	for _, tt := range t {
		if match := tt.matchSuffix(suffix); match != "" {
			if found {
				// ambiguous
				found = false
				return
			}
			tp = tt
			tp.fileSuffix = match
			found = true
		}
	}
	return
}
607943
func findExpressionPos(s string) [][]byte {
	matches := rangeExp.FindAllStringIndex(s, -1)
	result := make([][]byte, len(matches))
	for i, r := range matches {
		result[i] = []byte{s[r[0]], s[r[1]-1]}
	}
	return result
}
1289605
func closestIndex(s []rune, r rune, pos int) int {

	desc := naiveSearchDescending(s, r, pos)
	asc := naiveSearchAscending(s, r, pos)

	da := math.Abs(float64(desc - pos))
	aa := math.Abs(float64(asc - pos))

	if da < aa {
		return desc
	}
	return asc
}
182519
func MidIndex(s string, sep byte) int {
	i := strings.IndexByte(s, sep)
	if i <= 0 || i == len(s)-1 {
		return -1
	}

	return i
}
481838
func (self *Model) FindFunc(flt interface{}, destZeroValue interface{}, resultFn ResultFunc) error {
	if f, err := self.filterFromInterface(flt); err == nil {
		f.IdentityField = self.collection.IdentityField

		if search := self.db.WithSearch(self.collection, f); search != nil {
			_, err := search.Query(self.collection, f, func(record *dal.Record, err error, _ backends.IndexPage) error {
				if err == nil {
					if _, ok := destZeroValue.(*dal.Record); ok {
						resultFn(record, nil)
					} else if _, ok := destZeroValue.(dal.Record); ok {
						resultFn(*record, nil)
					} else {
						into := reflect.New(reflect.TypeOf(destZeroValue)).Interface()

						// populate that type with data from this record
						if err := record.Populate(into, self.collection); err == nil {
							resultFn(into, nil)
						} else {
							return err
						}
					}
				} else {
					resultFn(nil, err)
				}

				return nil
			})

			return err
		} else {
			return fmt.Errorf("backend %T does not support searching", self.db)
		}
	} else {
		return err
	}
}