本文整理汇总了Golang中bytes.Index函数的典型用法代码### 示例。如果您正苦于以下问题:Golang Index函数的具体用法?Golang Index怎么用?Golang Index使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。
在下文中一共展示了Index函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。
示例1: BazaarDiffBranches
// 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
}
开发者ID:lambdaX,项目名称:rsc.godev,代码行数:27,代码来源:bazaar.go
示例2: GetJsonFromHttp
/*
* 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
}
开发者ID:KeluDiao,项目名称:gotube,代码行数:37,代码来源:analyzer.go
示例3: skipSpaceOrComment
// 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
}
开发者ID:danny8002,项目名称:go,代码行数:30,代码来源:build.go
示例4: extractEBNF
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()
}
开发者ID:ivanwyc,项目名称:google-go,代码行数:35,代码来源:ebnflint.go
示例5: ExampleIndex
func ExampleIndex() {
fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
// Output:
// 4
// -1
}
开发者ID:achanda,项目名称:go,代码行数:7,代码来源:example_test.go
示例6: split
//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
}
开发者ID:gernest,项目名称:bongo,代码行数:30,代码来源:front.go
示例7: extractComments
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()
}
开发者ID:spiraleye,项目名称:BrownLegoCSS,代码行数:25,代码来源:BrownLegoCSS.go
示例8: LoadFioJsonData
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
}
开发者ID:VibhorGupta,项目名称:effio,代码行数:35,代码来源:fio_output_json.go
示例9: fetchTitle
// 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)))
}
开发者ID:jasonrdsouza,项目名称:ircb,代码行数:32,代码来源:url.go
示例10: TestGolfsWithJsonFormatter
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`)
}
}
开发者ID:akutz,项目名称:golf,代码行数:33,代码来源:json_formatter_test.go
示例11: linkify
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]
}
}
开发者ID:8l,项目名称:go-learn,代码行数:28,代码来源:spec.go
示例12: Read
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
}
开发者ID:elechak,项目名称:fleet,代码行数:29,代码来源:fleet.go
示例13: Load
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
}
开发者ID:willings,项目名称:proxypool,代码行数:34,代码来源:org_sslproxies.go
示例14: TestGolfsWithTextFormatter
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")
}
}
开发者ID:akutz,项目名称:golf,代码行数:30,代码来源:text_formatter_test.go
示例15: fixHtml
// 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]
}
开发者ID:eaburns,项目名称:feedme,代码行数:37,代码来源:webfeed.go
示例16: Remove
// 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
}
开发者ID:musha68k,项目名称:docker-machine-driver-xhyve,代码行数:30,代码来源:nfsexports.go
示例17: Next
// 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
}
开发者ID:alexeyco,项目名称:go-gettext,代码行数:37,代码来源:mo.go
示例18: FetchPage
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})
}
}
}
}
}
开发者ID:nvcnvn,项目名称:imgidx,代码行数:32,代码来源:imgfetcher.go
示例19: hasModifiedPlayground
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
}
开发者ID:emergenesis,项目名称:godag,代码行数:28,代码来源:gdmake.go
示例20: parseCRILog
// 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
}
开发者ID:eljefedelrodeodeljefe,项目名称:kubernetes,代码行数:31,代码来源:kuberuntime_logs.go