Golang bytes.Index类(方法)实例源码

下面列出了Golang bytes.Index 类(方法)源码代码实例,从而了解它的用法。

作者:lambda    项目:rsc.gode   
// BazaarDiffBranches returns a Delta between the bazaar branch at
// oldPath and the one at newPath.
func BazaarDiffBranches(oldPath, newPath string) (Delta, error) {
	output1, _, err := run("bzr", "log", "-l1", "--show-ids", "-r", "ancestor:"+oldPath, newPath)
	if err != nil {
		return nil, err
	}
	output2, _, err := run("bzr", "log", "-l1", "--show-ids", newPath)
	if err != nil {
		return nil, err
	}
	i1 := bytes.Index(output1, logRevId)
	i2 := bytes.Index(output2, logRevId)
	if i1 < 0 || i2 < 0 {
		return nil, errors.New("no revision-id in bzr log output")
	}
	output1 = output1[i1+len(logRevId):]
	output2 = output2[i2+len(logRevId):]
	i1 = bytes.Index(output1, []byte{'\n'})
	i2 = bytes.Index(output2, []byte{'\n'})
	if i1 < 0 || i2 < 0 {
		return nil, errors.New("bad revision-id in bzr log output")
	}
	oldRevision := string(output1[:i1])
	newRevision := string(output2[:i2])
	return &bzrBranches{oldPath, newPath, oldRevision, newRevision}, nil
}

作者:KeluDia    项目:gotub   
/*
* Get json data from http code.
 */
func GetJsonFromHttp(httpData []byte) (map[string]interface{}, error) {
	//Find out if this page is age-restricted
	if bytes.Index(httpData, []byte("og:restrictions:age")) != -1 {
		return nil, errors.New("this page is age-restricted")
	}
	//Find begining of json data
	jsonBeg := "ytplayer.config = {"
	beg := bytes.Index(httpData, []byte(jsonBeg))
	if beg == -1 { //pattern not found
		return nil, PatternNotFoundError{_pattern: jsonBeg}
	}
	beg += len(jsonBeg) //len(jsonBeg) returns the number of bytes in jsonBeg

	//Find offset of json data
	unmatchedBrackets := 1
	offset := 0
	for unmatchedBrackets > 0 {
		nextRight := bytes.Index(httpData[beg+offset:], []byte("}"))
		if nextRight == -1 {
			return nil, errors.New("unmatched brackets")
		}
		unmatchedBrackets -= 1
		unmatchedBrackets += bytes.Count(httpData[beg+offset:beg+offset+nextRight], []byte("{"))
		offset += nextRight + 1
	}

	//Load json data
	var f interface{}
	err := json.Unmarshal(httpData[beg-1:beg+offset], &f)
	if err != nil {
		return nil, err
	}
	return f.(map[string]interface{}), nil
}

作者:danny800    项目:g   
// skipSpaceOrComment returns data with any leading spaces or comments removed.
func skipSpaceOrComment(data []byte) []byte {
	for len(data) > 0 {
		switch data[0] {
		case ' ', '\t', '\r', '\n':
			data = data[1:]
			continue
		case '/':
			if bytes.HasPrefix(data, slashSlash) {
				i := bytes.Index(data, newline)
				if i < 0 {
					return nil
				}
				data = data[i+1:]
				continue
			}
			if bytes.HasPrefix(data, slashStar) {
				data = data[2:]
				i := bytes.Index(data, starSlash)
				if i < 0 {
					return nil
				}
				data = data[i+2:]
				continue
			}
		}
		break
	}
	return data
}

作者:ivanwy    项目:google-g   
func extractEBNF(src []byte) []byte {
	var buf bytes.Buffer

	for {
		// i = beginning of EBNF text
		i := bytes.Index(src, open)
		if i < 0 {
			break // no EBNF found - we are done
		}
		i += len(open)

		// write as many newlines as found in the excluded text
		// to maintain correct line numbers in error messages
		for _, ch := range src[0:i] {
			if ch == '\n' {
				buf.WriteByte('\n')
			}
		}

		// j = end of EBNF text (or end of source)
		j := bytes.Index(src[i:], close) // close marker
		if j < 0 {
			j = len(src) - i
		}
		j += i

		// copy EBNF text
		buf.Write(src[i:j])

		// advance
		src = src[j:]
	}

	return buf.Bytes()
}

作者:achand    项目:g   
func ExampleIndex() {
	fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
	fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
	// Output:
	// 4
	// -1
}

作者:gernes    项目:bong   
//split implements bufio.SplitFunc for spliting fron matter from the body text.
func (m *Matter) split(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	if m.delim == "" {
		delim, err := sniffDelim(data)
		if err != nil {
			return 0, nil, err
		}
		m.delim = delim
	}
	if _, ok := m.handlers[m.delim]; !ok {
		return 0, nil, ErrUnknownDelim
	}
	if x := bytes.Index(data, []byte(m.delim)); x >= 0 {
		// check the next delim index
		if next := bytes.Index(data[x+len(m.delim):], []byte(m.delim)); next > 0 {
			if !m.lastDelim {
				m.lastDelim = true
				m.lastIndex = next + len(m.delim)
				return next + len(m.delim)*2, dropSpace(data[x : next+len(m.delim)]), nil
			}
		}
	}
	if atEOF {
		return len(data), data, nil
	}
	return 0, nil, nil
}

作者:spiraley    项目:BrownLegoCS   
func (c *CssCompressor) extractComments() {
	var sb bytes.Buffer
	startIndex := 0
	endIndex := 0

	tmpCss := c.Css
	for startIndex = bytes.Index(tmpCss, []byte("/*")); startIndex >= 0; {
		sb.WriteString(string(tmpCss[:startIndex]))

		endIndex = bytes.Index(tmpCss[startIndex+2:], []byte("*/"))
		if endIndex < 0 {
			endIndex = len(tmpCss)
		}
		c.comments = append(c.comments, string(tmpCss[startIndex+2:endIndex+startIndex+2]))
		sb.WriteString(
			string("/*___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" +
				(strconv.Itoa(len(c.comments) - 1)) +
				"___*/"))

		tmpCss = tmpCss[startIndex+2+endIndex+2:]
		startIndex = bytes.Index(tmpCss, []byte("/*"))
	}
	sb.WriteString(string(tmpCss))
	c.Css = sb.Bytes()
}

作者:VibhorGupt    项目:effi   
func LoadFioJsonData(filename string) (fdata FioJsonData) {
	dataBytes, err := ioutil.ReadFile(filename)

	if os.IsNotExist(err) {
		log.Fatalf("Could not read file %s: %s", filename, err)
	}

	// data loaded OK
	fdata.Filename = filename

	// fio writes a bunch of crap out to the output file before the JSON so for
	// now do the easy thing and find the first { after a \n and call it good
	offset := bytes.Index(dataBytes, []byte("\n{"))
	// bytes.Index will return -1 for not found, in which case we assume that it
	// been trimmed from the input file and start at index 0
	if offset == -1 {
		offset = 0
	}

	// sometimes it also puts junk at the end of the file
	eof := bytes.Index(dataBytes, []byte("\n}"))
	if eof < offset {
		eof = len(dataBytes)
	}

	err = json.Unmarshal(dataBytes[offset:eof+2], &fdata)
	if err != nil {
		log.Fatalf("Could not parse fio --output=json JSON in file '%s': %s", filename, err)
	}

	fdata.HeaderGarbage = string(dataBytes[0:offset])
	fdata.FooterGarbage = string(dataBytes[eof+1:])

	return
}

作者:jasonrdsouz    项目:irc   
// fetchTitle attempts to retrieve the title element for a given url.
func fetchTitle(c *proto.Client, m *proto.Message, url string) {
	resp, err := http.Get(url)
	if err != nil {
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	if err != nil {
		return
	}

	body = bytes.ToLower(body)
	s := bytes.Index(body, []byte("<title>"))
	if s == -1 {
		return
	}

	body = body[s+7:]

	e := bytes.Index(body, []byte("</title>"))
	if e == -1 {
		e = len(body) - 1
	}

	body = bytes.TrimSpace(body[:e])

	c.PrivMsg(m.Receiver, "%s's link shows: %s",
		m.SenderName, html.UnescapeString(string(body)))
}

作者:akut    项目:gol   
func TestGolfsWithJsonFormatter(t *testing.T) {
	p := &Person{
		Name:  "Bruce",
		Alias: "Batman",
		Hideout: &Hideout{
			Name:        "JLU Tower",
			DimensionId: 52,
		},
	}

	jf := newJsonFormatter()
	b, err := jf.Format(&log.Entry{
		Message: "the dark knight", Data: log.Fields{"hero": p}})
	if err != nil {
		t.Fatal("Unable to format entry: ", err)
	}

	if bytes.Index(b, ([]byte)(`"hero.name":"Bruce"`)) < 0 {
		t.Fatalf(`missing "hero.name":"Bruce"`)
	}

	if bytes.Index(b, ([]byte)(`"hero.alias":"Batman"`)) < 0 {
		t.Fatalf(`missing "hero.alias":"Batman"`)
	}

	if bytes.Index(b, ([]byte)(`"hero.hideout.name":"JLU Tower"`)) < 0 {
		t.Fatalf(`missing "hero.hideout.name":"JLU Tower"`)
	}

	if bytes.Index(b, ([]byte)(`"hero.hideout.dimensionId":52`)) < 0 {
		t.Fatalf(`missing "hero.hideout.dimensionId":52`)
	}
}

作者:8    项目:go-lear   
func linkify(out io.Writer, src []byte) {
	for len(src) > 0 {
		n := len(src)

		// i: beginning of EBNF text (or end of source)
		i := bytes.Index(src, openTag)
		if i < 0 {
			i = n - len(openTag)
		}
		i += len(openTag)

		// j: end of EBNF text (or end of source)
		j := bytes.Index(src[i:n], closeTag) // close marker
		if j < 0 {
			j = n - i
		}
		j += i

		// write text before EBNF
		out.Write(src[0:i])
		// parse and write EBNF
		var p ebnfParser
		p.parse(out, src[i:j])

		// advance
		src = src[j:n]
	}
}

作者:elecha    项目:flee   
func (self *Interp) Read() int64 {
	// stdout
	b := self.stdout_buffer.Bytes()
	i := bytes.Index(b, []byte{'\x04'})
	if i != -1 {
		s, _ := self.stdout_buffer.ReadBytes('\x04')
		self.stdout_read_time = time.Now().UnixNano()
		if i > 0 {
			self.Stdout = string(s[0:i])
		}
	}

	// stderr
	b = self.stderr_buffer.Bytes()
	i = bytes.Index(b, []byte{'\x04'})
	if i != -1 {
		s, _ := self.stderr_buffer.ReadBytes('\x04')
		self.stderr_read_time = time.Now().UnixNano()
		if i > 0 {
			self.Stderr = string(s[0:i])
		}
	}

	if self.stdout_read_time != 0 && self.stderr_read_time != 0 {
		return imax64(self.stdout_read_time, self.stderr_read_time)
	}

	return self.exit_read_time
}

作者:willing    项目:proxypoo   
func (p *Org_sslproxies) Load() ([]*ProxyItem, error) {
	b, err := httpGet(SSLPROXIES_URL, p.client)
	if err != nil {
		return nil, errors.New("Failed to read stream")
	}

	startBytes := []byte("<tbody>")
	endBytes := []byte("</tbody>")

	tbodyStart := bytes.Index(b, startBytes)
	tbodyEnd := bytes.Index(b, endBytes)
	if tbodyEnd <= tbodyStart {
		return nil, errors.New("Failed to parse stream")
	}

	bytes := b[tbodyStart : tbodyEnd+len(endBytes)]
	tbl := Tbody{}
	err = xml.Unmarshal(bytes, &tbl)
	if err != nil {
		return nil, err
	}

	ret := make([]*ProxyItem, len(tbl.Tr))
	cnt := 0
	for _, tr := range tbl.Tr {
		item := p.convert(&tr)
		if item != nil {
			ret[cnt] = item
			cnt++
		}
	}

	return ret, nil
}

作者:akut    项目:gol   
func TestGolfsWithTextFormatter(t *testing.T) {
	p := &Person{
		Name:  "Bruce",
		Alias: "Batman",
		Hideout: &Hideout{
			Name:        "JLU Tower",
			DimensionId: 52,
		},
	}

	tf := newTextFormatter()
	b, _ := tf.Format(&log.Entry{
		Message: "the dark knight", Data: log.Fields{"hero": p}})

	if bytes.Index(b, ([]byte)("hero.name=Bruce")) < 0 {
		t.Fatalf("missing hero.name=Bruce")
	}

	if bytes.Index(b, ([]byte)("hero.alias=Batman")) < 0 {
		t.Fatalf("missing hero.alias=Batman")
	}

	if bytes.Index(b, ([]byte)(`hero.hideout.name="JLU Tower"`)) < 0 {
		t.Fatalf(`missing hero.hideout.name="JLU Tower"`)
	}

	if bytes.Index(b, ([]byte)("hero.hideout.dimensionId=52")) < 0 {
		t.Fatalf("missing hero.hideout.dimensionId=52")
	}
}

作者:eaburn    项目:feedm   
// FixHtml parses bytes as HTML and returns well-formed HTML if the parse
// was successful, or escaped HTML, if not.
func fixHtml(linkUrl string, wild []byte) (well []byte) {
	n, err := html.Parse(bytes.NewReader(wild))
	if err != nil {
		return []byte(html.EscapeString(string(wild)))
	}

	fixImgs(linkUrl, n)

	defer func() {
		if err := recover(); err == bytes.ErrTooLarge {
			well = []byte(html.EscapeString(string(wild)))
		} else if err != nil {
			panic(err)
		}
	}()
	buf := bytes.NewBuffer(make([]byte, 0, len(wild)*2))
	if err := html.Render(buf, n); err != nil {
		return []byte(html.EscapeString(string(wild)))
	}

	well = buf.Bytes()
	openBody := []byte("<body>")
	i := bytes.Index(well, openBody)
	if i < 0 {
		return []byte(html.EscapeString(string(wild)))
	}
	well = well[i+len(openBody):]

	closeBody := []byte("</body>")
	i = bytes.Index(well, closeBody)
	if i < 0 {
		return []byte(html.EscapeString(string(wild)))
	}
	return well[:i]
}

作者:musha68    项目:docker-machine-driver-xhyv   
// Remove export, if exportsFile is an empty string /etc/exports is used
func Remove(exportsFile string, identifier string) ([]byte, error) {
	if exportsFile == "" {
		exportsFile = defaultExportsFile
	}

	exports, err := ioutil.ReadFile(exportsFile)
	if err != nil {
		return nil, err
	}

	beginMark := []byte(fmt.Sprintf("# BEGIN: %s", identifier))
	endMark := []byte(fmt.Sprintf("# END: %s\n", identifier))

	begin := bytes.Index(exports, beginMark)
	end := bytes.Index(exports, endMark)

	if begin == -1 || end == -1 {
		return nil, fmt.Errorf("Couldn't not find export %s in %s", identifier, exportsFile)
	}

	newExports := append(exports[:begin], exports[end+len(endMark):]...)
	newExports = append(bytes.TrimSpace(newExports), '\n')

	if err := ioutil.WriteFile(exportsFile, newExports, 0644); err != nil {
		return nil, err
	}

	return newExports, nil
}

作者:alexeyc    项目:go-gettex   
// Next returns the next message. At the end of the iteration,
// io.EOF is returned as the error.
func (r *moReader) Next() (*Message, error) {
	r.init()
	if r.err != nil {
		return nil, r.err
	}
	if r.pos >= r.header.MsgCount {
		r.err = io.EOF
		return nil, r.err
	}
	msg := Message{}
	var err error
	// Read msgid and msgstr.
	if msg.Id, err = r.readMessage(r.header.IdTableOffset + r.pos*8); err != nil {
		r.err = err
		return nil, err
	}
	if msg.Str, err = r.readMessage(r.header.StrTableOffset + r.pos*8); err != nil {
		r.err = err
		return nil, err
	}
	// Is this a context message?
	if idx := bytes.Index(msg.Id, eotBytes); idx != -1 {
		msg.Ctxt = msg.Id[:idx]
		msg.Id = msg.Id[idx+1:]
	}
	// Is this a plural message?
	if idx := bytes.Index(msg.Id, nulBytes); idx != -1 {
		msg.IdPlural = msg.Id[idx+1:]
		msg.Id = msg.Id[:idx]
		msg.StrPlural = bytes.Split(msg.Str, nulBytes)
		msg.Str = nil
	}
	r.pos += 1
	return &msg, nil
}

作者:nvcnv    项目:imgid   
func (i *ImageIndex) FetchPage(data []byte, deep int) {
	// openTag: <a
	openTag := []byte{0x3c, 0x61}
	openPos := 0
	closePos := 0
	// hrefTag: href
	hrefTag := []byte{0x68, 0x72, 0x65, 0x66}
	hrefPos := 0
	// quote: " (0x22)
	quoteOpenPos := 0
	quoteClosePos := 0
	found := bytes.Index(data[openPos:], openTag)
	var tmpSlice []byte
	var url string
	for found = bytes.Index(data[openPos:], openTag); found != -1; found = bytes.Index(data[openPos:], openTag) {
		openPos = openPos + found + 3
		closePos = bytes.IndexByte(data[openPos:], 0x3e)
		tmpSlice = data[openPos : openPos+closePos]

		hrefPos = bytes.Index(tmpSlice, hrefTag)
		if hrefPos != -1 {
			quoteOpenPos = bytes.IndexByte(tmpSlice[hrefPos+5:], 0x22)
			if quoteOpenPos != -1 {
				quoteClosePos = bytes.IndexByte(tmpSlice[hrefPos+5+quoteOpenPos+1:], 0x22)
				if quoteClosePos != -1 {
					url, _ = FullURL(i.rootURL, string(tmpSlice[hrefPos+5+quoteOpenPos+1:hrefPos+5+quoteOpenPos+quoteClosePos+1]))
					i.pageList.PushBack(pageInfo{url, deep})
				}
			}
		}
	}
}

作者:emergenesi    项目:goda   
func hasModifiedPlayground(fname string) (mod string, ok bool) {

	var start, stop, content, playground []byte
	var startOffset, stopOffset int

	start = []byte("// PLAYGROUND START\n")
	stop = []byte("// PLAYGROUND STOP\n")

	content, err := ioutil.ReadFile(fname)

	if err != nil {
		log.Fatalf("[ERROR] %s\n", err)
	}

	startOffset = bytes.Index(content, start)
	stopOffset = bytes.Index(content, stop)

	if startOffset == -1 || stopOffset == -1 {
		return "", false
	}

	playground = content[startOffset+len(start) : stopOffset]

	ok = (string(playground) != PlaygroundTmpl)

	return string(playground), ok

}

作者:eljefedelrodeodeljef    项目:kubernete   
// parseCRILog parses logs in CRI log format. CRI Log format example:
//   2016-10-06T00:17:09.669794202Z stdout log content 1
//   2016-10-06T00:17:09.669794203Z stderr log content 2
func parseCRILog(log []byte, msg *logMessage) error {
	var err error
	// Parse timestamp
	idx := bytes.Index(log, delimiter)
	if idx < 0 {
		return fmt.Errorf("timestamp is not found")
	}
	msg.timestamp, err = time.Parse(timeFormat, string(log[:idx]))
	if err != nil {
		return fmt.Errorf("unexpected timestamp format %q: %v", timeFormat, err)
	}

	// Parse stream type
	log = log[idx+1:]
	idx = bytes.Index(log, delimiter)
	if idx < 0 {
		return fmt.Errorf("stream type is not found")
	}
	msg.stream = streamType(log[:idx])
	if msg.stream != stdoutType && msg.stream != stderrType {
		return fmt.Errorf("unexpected stream type %q", msg.stream)
	}

	// Get log content
	msg.log = log[idx+1:]

	return nil
}


问题


面经


文章

微信
公众号

扫码关注公众号