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

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

作者:hid    项目:too   
func parseLine(line []byte) logKv {
	m := bytes.LastIndex(line, startKey)
	if m < 1 {
		return nil
	}
	n := bytes.LastIndex(line, stopKey)
	if n < m {
		return nil
	}
	kv := make(logKv)

	sub := line[m+1 : n]
	items := bytes.Split(sub, kvSpace)

	for _, item := range items {
		x := bytes.Index(item, eqSign)
		if x < 1 {
			continue
		}
		key := string(item[:x])
		val := string(item[x+1:])
		kv[key] = val
	}

	return kv
}

作者:marknewmai    项目:go   
// handle the socket request
func (this *Server) HandleRequest(conn net.Conn, d []byte) {
	// example: {"usr":"","pwd":"123"}>>Member.Login
	//	defer func(){
	//		if e := recover();e!= nil{
	//			Println(e)
	//			debug.PrintStack()
	//		}
	//	}()

	if len(d) < len(CmdOperateBytes)+len(cmdDot) {
		conn.Write(invalidBytes)
		return
	}

	i := bytes.LastIndex(d, CmdOperateBytes)
	di := bytes.LastIndex(d, cmdDot)

	if i != -1 && di != -1 {
		rd, err := this.handle(d[i+len(CmdOperateBytes):di],
			d[di+len(cmdDot):], d[:i])

		if err != nil {
			Println("[Server][ ERROR]: " + err.Error())
			conn.Write([]byte(err.Error()))
		} else {
			Println(fmt.Sprintf("[Server][Output]:%s", string(rd)))
			conn.Write(rd)
		}
	} else {
		conn.Write(invalidBytes)
	}
}

作者:achand    项目:g   
func ExampleLastIndex() {
	fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
	// Output:
	// 0
	// 3
	// -1
}

作者:alekseyfadeev8    项目:Tutoria   
// Функция разбора входящих данных и формирования объектов HttpRequest
// Возвращаемый результат: сформированный запрос (при наличии), необработанные данные,
// успешность выполнения (если false - получен некорректный запрос)
func ParseOneReq(data []byte) (req *HttpRequest, unworked_data []byte, success bool) {
	unworked_data = data
	success = true

	endl := []byte(endline_str)                          // Конец строки
	req_identifier := []byte(http_version + endline_str) // Признак запроса (версия HTTP + символы конца строки)

	// Ищем заголовок
	p_req_id := bytes.Index(unworked_data, req_identifier)
	p_endl := -1
	if p_req_id >= 0 {
		p_endl = bytes.LastIndex(unworked_data[:p_req_id], endl)
	} else {
		p_endl = bytes.LastIndex(unworked_data, endl)
	}

	if p_endl >= 0 {
		// Обнаружен переход на следующую строку перед признаком запроса - косяк
		unworked_data = unworked_data[p_endl+len(endline_str):]
		success = false
		return
	} else if p_req_id >= 0 {
		// Найден признак запроса
		p_req_id += len(req_identifier)
		params := strings.Split(string(unworked_data[:p_req_id]), " ")
		unworked_data = unworked_data[p_req_id:]

		if len(params) != 3 {
			// Неверный заголовок
			success = false
			return
		} // if len(params) != 3

		req = &HttpRequest{Type: params[0], Host: params[1]}
	} else {
		// Не нашли ни признака запроса, ни перехода на следующую строку
		unworked_data = data
		return
	}

	// Формируем параметры и тело запроса
	var rr *req_resp
	rr, unworked_data, success = parse_one_req_resp(unworked_data)
	if rr != nil {
		req.req_resp = *rr
	} else {
		req = nil
		if success {
			unworked_data = data
		}
	}

	return
} // func ParseOneReq(data []byte) (req *HttpRequest, unworked_data []byte, success bool)

作者:ReinhardHs    项目:platfor   
func shortText(t []byte) []byte {
	if t == nil {
		return nil
	}

	// Cut signature.
	i := bytes.LastIndex(t, sigDash)
	j := bytes.LastIndex(t, quote)
	if i > j && bytes.Count(t[i+1:], nl) <= 10 {
		t = t[:i+1]
	}

	// Cut trailing quoted text.
	for {
		rest, last := lastLine(t)
		trim := bytes.TrimSpace(last)
		if len(rest) < len(t) && (len(trim) == 0 || trim[0] == '>') {
			t = rest
			continue
		}
		break
	}

	// Cut 'On foo.*wrote:' line.
	rest, last := lastLine(t)
	if onwrote.Match(last) {
		t = rest
	}

	// Cut trailing blank lines.
	for {
		rest, last := lastLine(t)
		trim := bytes.TrimSpace(last)
		if len(rest) < len(t) && len(trim) == 0 {
			t = rest
			continue
		}
		break
	}

	// Cut signature again.
	i = bytes.LastIndex(t, sigDash)
	j = bytes.LastIndex(t, quote)
	if i > j && bytes.Count(t[i+1:], nl) <= 10 {
		t = t[:i+1]
	}

	return t
}

作者:vro    项目:se   
// TODO:
func (f *File) OffsetLine(ln, start int) (offset int, e error) {
	if start < 0 || start > len(f.b) {
		return 0, memfile.OutOfBounds
	}
	if ln == 0 {
		i := bytes.LastIndex(f.b[:start], []byte("\n"))
		return i + 1, nil
	}
	if ln < 0 {
		i := 0
		return bytes.LastIndexFunc(f.b[:start], func(r rune) bool {
			if r == '\n' {
				if i == ln {
					return true
				}
				i--
			}
			return false
		}) + 1, nil
	}
	i := 0
	va := bytes.IndexFunc(f.b[start:], func(r rune) bool {
		if r == '\n' {
			i++
			if i == ln {
				return true
			}
		}
		return false
	})
	if va != -1 {
		return va + start + 1, nil
	}
	return len(f.b), nil
}

作者:magicalbanan    项目:pgmg   
// need access to the original query contents in order to print it out properly,
// unfortunately.
func formatPgErr(contents *[]byte, pgerr *pq.Error) string {
	pos, _ := strconv.Atoi(pgerr.Position)
	lineNo := bytes.Count((*contents)[:pos], []byte("\n")) + 1
	columnNo := pos - bytes.LastIndex((*contents)[:pos], []byte("\n")) - 1

	return fmt.Sprint("PGERROR: line ", lineNo, " pos ", columnNo, ": ", pgerr.Message, ". ", pgerr.Detail)
}

作者:vincecim    项目:just-instal   
// FileGetLastLine reads the last line from a file.
// In case of a network file, the whole file is read.
// In case of a local file, the last 64kb are read,
// so if the last line is longer than 64kb it is not returned completely.
// The first optional timeout is used for network files only.
func FileGetLastLine(filenameOrURL string, timeout ...time.Duration) (line string, err error) {
	if strings.Index(filenameOrURL, "file://") == 0 {
		return FileGetLastLine(filenameOrURL[len("file://"):])
	}

	var data []byte

	if strings.Contains(filenameOrURL, "://") {
		data, err = FileGetBytes(filenameOrURL, timeout...)
		if err != nil {
			return "", err
		}
	} else {
		file, err := os.Open(filenameOrURL)
		if err != nil {
			return "", err
		}
		defer file.Close()
		info, err := file.Stat()
		if err != nil {
			return "", err
		}
		if start := info.Size() - 64*1024; start > 0 {
			file.Seek(start, os.SEEK_SET)
		}
		data, err = ioutil.ReadAll(file)
		if err != nil {
			return "", err
		}
	}

	pos := bytes.LastIndex(data, []byte{'\n'})
	return string(data[pos+1:]), nil
}

作者:ZhuHangpen    项目:mi   
// FromBytes takes a byte slice corresponding to a MaxMind DB file and returns
// a Reader structure or an error.
func FromBytes(buffer []byte) (*Reader, error) {
	metadataStart := bytes.LastIndex(buffer, metadataStartMarker)

	if metadataStart == -1 {
		return nil, fmt.Errorf("error opening database file: invalid MaxMind DB file")
	}

	metadataStart += len(metadataStartMarker)
	metadataDecoder := decoder{buffer[metadataStart:]}

	var metadata Metadata

	rvMetdata := reflect.ValueOf(&metadata)
	_, err := metadataDecoder.decode(0, rvMetdata)
	if err != nil {
		return nil, err
	}

	searchTreeSize := metadata.NodeCount * metadata.RecordSize / 4
	decoder := decoder{
		buffer[searchTreeSize+dataSectionSeparatorSize : metadataStart-len(metadataStartMarker)],
	}

	reader := &Reader{
		buffer:    buffer,
		decoder:   decoder,
		Metadata:  metadata,
		ipv4Start: 0,
	}

	reader.ipv4Start, err = reader.startNode()

	return reader, err
}

作者:rainycap    项目:gondol   
// New returns a new Article by decoding it from the given io.Reader.
func New(r io.Reader) (*Article, error) {
	data, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}
	article := &Article{}
	var text, props []byte
	// Find last empty line
	sep := bytes.LastIndex(data, propertySeparator)
	if sep >= 0 {
		text = data[:sep]
		props = data[sep:]
	} else {
		text = data
	}
	for _, v := range propertyRe.FindAllSubmatch(props, -1) {
		key := string(v[1])
		value := string(v[2])
		if value != "" && value[0] == '"' && value[len(value)-1] == '"' {
			val, err := strconv.Unquote(value)
			if err != nil {
				return nil, fmt.Errorf("error unquoting %q: %s", value, err)
			}
			value = val
		}
		if err := article.add(key, value, false); err != nil {
			return nil, fmt.Errorf("error setting key %q with value %q: %s", key, string(value), err)
		}
	}
	text = append(text, propertyRe.ReplaceAll(props, nil)...)
	article.Text = bytes.TrimSpace(text)
	return article, nil
}

作者:nikhil-salgaonka    项目:csrf   
// Valid returns true if token is a valid, unexpired token returned by Generate.
func validAtTime(token, key, userID string, now time.Time) bool {
	// Decode the token.
	data, err := base64.URLEncoding.DecodeString(token)
	if err != nil {
		return false
	}

	// Extract the issue time of the token.
	sep := bytes.LastIndex(data, []byte{':'})
	if sep < 0 {
		return false
	}
	nanos, err := strconv.ParseInt(string(data[sep+1:]), 10, 64)
	if err != nil {
		return false
	}
	issueTime := time.Unix(0, nanos)

	// Check that the token is not expired.
	if now.Sub(issueTime) >= timeout {
		return false
	}

	// Check that the token is not from the future.
	// Allow 1 minute grace period in case the token is being verified on a
	// machine whose clock is behind the machine that issued the token.
	if issueTime.After(now.Add(1 * time.Minute)) {
		return false
	}

	// Check that the token matches the expected value.
	expected := generateAtTime(key, userID, issueTime)
	return token == expected
}

作者:JuanCarlos    项目:flee   
func (l *lexer) lexOptionValueFunc(section, name string) lexStep {
	return func() (lexStep, error) {
		var partial bytes.Buffer

		for {
			line, err := l.toEOL()
			if err != nil {
				return nil, err
			}

			// lack of continuation means this value has been exhausted
			idx := bytes.LastIndex(line, []byte{'\\'})
			if idx == -1 {
				partial.Write(line)
				break
			}

			partial.Write(line[0:idx])
			partial.WriteRune(' ')
		}

		val := strings.TrimSpace(partial.String())
		l.optchan <- &UnitOption{Section: section, Name: name, Value: val}

		return l.lexNextSectionOrOptionFunc(section), nil
	}
}

作者:xiaobago    项目:ech   
func Unmarshall(ns []byte) ([]byte, error) {
	if ns == nil {
		return nil, errors.New("ns is nil")
	}
	colonPos := bytes.Index(ns, []byte(":"))
	if colonPos == -1 {
		return nil, ErrNoColon
	}
	nsLenBytes := ns[:colonPos]
	nsLen, err := strconv.Atoi(string(nsLenBytes))
	if err != nil {
		return nil, ErrNsLenBytesConv
	}
	commaPos := bytes.LastIndex(ns, []byte(","))
	if commaPos == -1 {
		return nil, ErrNoComma
	}
	if commaPos-1 == colonPos {
		return nil, errors.New("org is nil")
	}

	if nsLen == commaPos-colonPos-1 {
		return ns[colonPos+1 : commaPos], nil
	} else {
		return nil, ErrNsLenNotEqaulOrgLen
	}
}

作者:CERN-Stage-    项目:vites   
//Main parse loop
func (blp *Bls) parseBinlogEvents(sendReply proto.SendBinlogResponse, binlogReader io.Reader) {
	// read over the stream and buffer up the transactions
	var err error
	var line []byte
	bigLine := make([]byte, 0, BINLOG_BLOCK_SIZE)
	lineReader := bufio.NewReaderSize(binlogReader, BINLOG_BLOCK_SIZE)
	readAhead := false
	var event *blsEventBuffer
	var delimIndex int

	for {
		line = line[:0]
		bigLine = bigLine[:0]
		line, err = blp.readBlsLine(lineReader, bigLine)
		if err != nil {
			if err == io.EOF {
				//end of stream
				blp.globalState.blsStats.parseStats.Add("EOFErrors."+blp.keyrangeTag, 1)
				panic(newBinlogServerError(fmt.Sprintf("EOF")))
			}
			panic(newBinlogServerError(fmt.Sprintf("ReadLine err: , %v", err)))
		}
		if len(line) == 0 {
			continue
		}

		if line[0] == '#' {
			//parse positional data
			line = bytes.TrimSpace(line)
			blp.currentLine = string(line)
			blp.parsePositionData(line)
		} else {
			//parse event data

			if readAhead {
				event.LogLine = append(event.LogLine, line...)
			} else {
				event = newBlsEventBuffer(blp.currentPosition, line)
			}

			delimIndex = bytes.LastIndex(event.LogLine, BINLOG_DELIMITER)
			if delimIndex != -1 {
				event.LogLine = event.LogLine[:delimIndex]
				readAhead = false
			} else {
				readAhead = true
				continue
			}

			event.LogLine = bytes.TrimSpace(event.LogLine)
			event.firstKw = string(bytes.ToLower(bytes.SplitN(event.LogLine, SPACE, 2)[0]))

			blp.currentLine = string(event.LogLine)

			//processes statements only for the dbname that it is subscribed to.
			blp.parseDbChange(event)
			blp.parseEventData(sendReply, event)
		}
	}
}

作者:xmak    项目:itsdangerou   
func (this *simpleSigner) bytesSplit(value []byte) ([]byte, []byte) {
	idx := bytes.LastIndex(value, this.options.Separator)
	if idx == -1 {
		return value, nil
	}
	return value[:idx], value[idx+len(this.options.Separator):]
}

作者:General-Bec    项目:snapp   
// Decode parses a serialized assertion.
//
// The expected serialisation format looks like:
//
//   HEADER ("\n\n" BODY?)? "\n\n" SIGNATURE
//
// where:
//
//    HEADER is a set of header lines separated by "\n"
//    BODY can be arbitrary,
//    SIGNATURE is the signature
//
// A header line looks like:
//
//   NAME ": " VALUE
//
// The following headers are mandatory:
//
//   type
//   authority-id (the signer id)
//
// The following headers expect integer values and if omitted
// otherwise are assumed to be 0:
//
//   revision (a positive int)
//   body-length (expected to be equal to the length of BODY)
//
func Decode(serializedAssertion []byte) (Assertion, error) {
	// copy to get an independent backstorage that can't be mutated later
	assertionSnapshot := make([]byte, len(serializedAssertion))
	copy(assertionSnapshot, serializedAssertion)
	contentSignatureSplit := bytes.LastIndex(assertionSnapshot, nlnl)
	if contentSignatureSplit == -1 {
		return nil, fmt.Errorf("assertion content/signature separator not found")
	}
	content := assertionSnapshot[:contentSignatureSplit]
	signature := assertionSnapshot[contentSignatureSplit+2:]

	headersBodySplit := bytes.Index(content, nlnl)
	var body, head []byte
	if headersBodySplit == -1 {
		head = content
	} else {
		body = content[headersBodySplit+2:]
		if len(body) == 0 {
			body = nil
		}
		head = content[:headersBodySplit]
	}

	headers, err := parseHeaders(head)
	if err != nil {
		return nil, fmt.Errorf("parsing assertion headers: %v", err)
	}

	if len(signature) == 0 {
		return nil, fmt.Errorf("empty assertion signature")
	}

	return buildAssertion(headers, body, content, signature)
}

作者:Debia    项目:dc   
func grep(re *Regexp, b []byte) []int {
	var m []int
	lineno := 1
	for {
		i := re.Match(b, true, true)
		if i < 0 {
			break
		}
		start := bytes.LastIndex(b[:i], nl) + 1
		end := i + 1
		if end > len(b) {
			end = len(b)
		}
		lineno += bytes.Count(b[:start], nl)
		m = append(m, lineno)
		if start < end && b[end-1] == '\n' {
			lineno++
		}
		b = b[end:]
		if len(b) == 0 {
			break
		}
	}
	return m
}

作者:naps    项目:la   
func snip(in []byte) []byte {
	raw := in
	// snip header
	if i := bytes.Index(raw, snips[0]); i > -1 {
		if j := bytes.IndexByte(raw[i:], '\n'); j > -1 {
			raw = raw[i+j+1:]
		}
	}
	// snip footer
	if i := bytes.LastIndex(raw, snips[1]); i > -1 {
		if j := bytes.LastIndex(raw[:i], []byte{'\n'}); j > -1 {
			raw = raw[:j]
		}
	}
	return raw
}

作者:ninqin    项目:vites   
func (evs *EventStreamer) buildDMLEvent(sql []byte, insertid int64) (dmlEvent *proto.StreamEvent, newinsertid int64, err error) {
	commentIndex := bytes.LastIndex(sql, STREAM_COMMENT_START)
	if commentIndex == -1 {
		return &proto.StreamEvent{Category: "ERR", Sql: string(sql)}, insertid, nil
	}
	streamComment := string(sql[commentIndex+len(STREAM_COMMENT_START):])
	eventNode, err := parseStreamComment(streamComment)
	if err != nil {
		return nil, insertid, err
	}

	dmlEvent = new(proto.StreamEvent)
	dmlEvent.Category = "DML"
	dmlEvent.TableName = eventNode.Table
	dmlEvent.PKColNames = eventNode.Columns
	dmlEvent.PKValues = make([][]interface{}, 0, len(eventNode.Tuples))

	for _, tuple := range eventNode.Tuples {
		if len(tuple) != len(eventNode.Columns) {
			return nil, insertid, fmt.Errorf("length mismatch in values")
		}
		var rowPk []interface{}
		rowPk, insertid, err = encodePKValues(tuple, insertid)
		if err != nil {
			return nil, insertid, err
		}
		dmlEvent.PKValues = append(dmlEvent.PKValues, rowPk)
	}
	return dmlEvent, insertid, nil
}

作者:blablu    项目:log-courie   
// Parse a *json.SyntaxError into a pretty error message
func (c *Config) parseSyntaxError(js []byte, err error) error {
	json_err, ok := err.(*json.SyntaxError)
	if !ok {
		return err
	}

	start := bytes.LastIndex(js[:json_err.Offset], []byte("\n")) + 1
	end := bytes.Index(js[start:], []byte("\n"))
	if end >= 0 {
		end += start
	} else {
		end = len(js)
	}

	line, pos := bytes.Count(js[:start], []byte("\n")), int(json_err.Offset)-start-1

	var posStr string
	if pos > 0 {
		posStr = strings.Repeat(" ", pos)
	} else {
		posStr = ""
	}

	return fmt.Errorf("%s on line %d\n%s\n%s^", err, line, js[start:end], posStr)
}


问题


面经


文章

微信
公众号

扫码关注公众号