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

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

作者:shruti    项目:vites   
func parseIndex(sql []byte) (indexName string, indexId interface{}, userId uint64) {
	var err error
	keyspaceIndex := bytes.Index(sql, KEYSPACE_ID_COMMENT)
	if keyspaceIndex == -1 {
		panic(NewBinlogParseError(fmt.Sprintf("Error parsing index comment, doesn't contain keyspace id %v", string(sql))))
	}
	keyspaceIdComment := sql[keyspaceIndex+len(KEYSPACE_ID_COMMENT):]
	indexCommentStart := bytes.Index(keyspaceIdComment, INDEX_COMMENT)
	if indexCommentStart != -1 {
		indexCommentParts := bytes.SplitN(keyspaceIdComment[indexCommentStart:], COLON_BYTE, 2)
		userId, err = strconv.ParseUint(string(bytes.SplitN(indexCommentParts[1], mysqlctl.SPACE, 2)[0]), 10, 64)
		if err != nil {
			panic(NewBinlogParseError(fmt.Sprintf("Error converting user_id %v", string(sql))))
		}
		indexNameId := bytes.Split(indexCommentParts[0], DOT_BYTE)
		indexName = string(indexNameId[1])
		if indexName == "username" {
			indexId = string(bytes.TrimRight(indexNameId[2], COLON))
		} else {
			indexId, err = strconv.ParseUint(string(bytes.TrimRight(indexNameId[2], COLON)), 10, 64)
			if err != nil {
				panic(NewBinlogParseError(fmt.Sprintf("Error converting index id %v %v", string(bytes.TrimRight(indexNameId[2], COLON)), string(sql))))
			}
		}
	}
	return
}

作者:kodin    项目:kodin   
// ParseKey reads the given RSA private key and create a public one for it.
func ParseKey(pem string) (*Key, error) {
	p, err := ioutil.ReadFile(pem)
	if err != nil {
		return nil, err
	}
	key, err := ssh.ParseRawPrivateKey(p)
	if err != nil {
		return nil, err
	}
	rsaKey, ok := key.(*rsa.PrivateKey)
	if !ok {
		return nil, fmt.Errorf("%q is not a RSA key", pem)
	}
	pub, err := ssh.NewPublicKey(&rsaKey.PublicKey)
	if err != nil {
		return nil, err
	}
	// Compute key fingerprint.
	var buf bytes.Buffer
	for _, b := range md5.Sum(pub.Marshal()) {
		fmt.Fprintf(&buf, "%0.2x:", b)
	}
	return &Key{
		Label:       strings.TrimSuffix(filepath.Base(pem), ".pem"),               // trim .pem file extension
		Key:         string(bytes.TrimRight(ssh.MarshalAuthorizedKey(pub), "\n")), // trim newline
		Fingerprint: string(bytes.TrimRight(buf.Bytes(), ":")),                    // trim dangling colon
		Note:        "{}",
		Tags:        make(Tags),
	}, nil
}

作者:akave    项目:vfm   
func DetectAtxHeader(first, second Line, detectors Detectors) Handler {
	if !bytes.HasPrefix(first.Bytes, []byte("#")) {
		return nil
	}
	done := false
	return HandlerFunc(func(line Line, ctx Context) (bool, error) {
		if done {
			return false, nil
		}
		done = true
		block := md.AtxHeaderBlock{
			Raw: md.Raw{md.Run(line)},
		}
		text := bytes.TrimRight(line.Bytes, "\n")
		text = bytes.Trim(text, "#")
		if len(text) > 0 {
			block.Level, _ = mdutils.OffsetIn(line.Bytes, text)
		} else {
			block.Level = len(bytes.TrimRight(line.Bytes, "\n"))
		}
		if block.Level > 6 {
			block.Level = 6
		}

		spanRegion := md.Raw{md.Run{
			Line:  line.Line,
			Bytes: bytes.Trim(text, mdutils.Whites),
		}}
		ctx.Emit(block)
		parseSpans(spanRegion, ctx)
		ctx.Emit(md.End{})
		return true, nil
	})
}

作者:kleopatra99    项目:kat   
func (p *parser) readLine() []byte {
	if !p.linenoFixed {
		p.lineno = p.elineno + 1
	}
	var line []byte
	for !p.done {
		buf, err := p.rd.ReadBytes('\n')
		if !p.linenoFixed {
			p.elineno++
		}
		if err == io.EOF {
			p.done = true
		} else if err != nil {
			p.err = fmt.Errorf("readline %s: %v", p.srcpos(), err)
			p.done = true
		}
		line = append(line, buf...)
		buf = bytes.TrimRight(buf, "\r\n")
		backslash := false
		for len(buf) > 1 && buf[len(buf)-1] == '\\' {
			buf = buf[:len(buf)-1]
			backslash = !backslash
		}
		if !backslash {
			break
		}
	}
	line = bytes.TrimRight(line, "\r\n")
	return line
}

作者:secumo    项目:themi   
func main() {
	input_buffer := bufio.NewReader(os.Stdin)
	fmt.Println("Type your settings from https://themis.cossacklabs.com/interactive-simulator/setup/")

	fmt.Println("JSON endpoint: ")
	endpoint, err := input_buffer.ReadString('\n')
	endpoint = strings.TrimRight(endpoint, "\n\r")

	fmt.Println("Your private key in base64 format:")
	client_private, err := input_buffer.ReadBytes('\n')
	client_private, err = base64.StdEncoding.DecodeString(string(client_private))
	if err != nil {
		fmt.Println("Incorrect base64 format for private key")
		return
	}

	fmt.Println("Server public key in base64 format:")
	server_public, err := input_buffer.ReadBytes('\n')
	server_public = bytes.TrimRight(server_public, "\r\n")
	server_public, err = base64.StdEncoding.DecodeString(string(server_public))
	secure_message := message.New(
		&keys.PrivateKey{bytes.TrimRight(client_private, "\r\n")},
		&keys.PublicKey{server_public})

	for {
		fmt.Println("Print message to send (or quit to stop):")
		line, _, err := input_buffer.ReadLine()
		if err != nil {
			fmt.Println(err)
			return
		}
		if bytes.Equal(line, []byte("quit")) {
			return
		}

		wrapped, err := secure_message.Wrap(line)
		if err != nil {
			fmt.Println("Error in wraping", err)
			return
		}
		data, err := send_message(wrapped, endpoint)
		if err != nil {
			fmt.Println("Error occured:", err)
			return
		}
		unwrapped, err := secure_message.Unwrap(data)
		fmt.Println(string(unwrapped))
	}
}

作者:jmptrade    项目:mu   
func formatQuoted(text []byte) []byte {
	if bytes.Equal(text, noOutputOld) {
		return noOutputNew
	}
	nonZero := bytes.HasSuffix(text, nonZeroOld)
	if nonZero {
		text = text[:len(text)-len(nonZeroOld)]
	}

	var buf bytes.Buffer
	buf.Grow(512)
	fmt.Fprintf(&buf, "%q", text)
	if buf.Len() > maxTextLen {
		truncateMaxLen(&buf, 1)
		buf.Truncate(len(bytes.TrimRight(buf.Bytes(), "\\")))
		buf.WriteString(`" + `)
		buf.WriteString(more)
	}

	if nonZero {
		buf.WriteString(split)
		buf.WriteString(nonZeroNew)
	}
	return buf.Bytes()
}

作者:jamesandaries    项目:iss   
func main() {
	flag.Parse()
	if *show_key {
		if flag.NArg() < 1 {
			flag.Usage()
			os.Exit(1)
		}
		for _, seed := range flag.Args() {
			key, err := issh.GetAuthorizedKey(seed)
			if err != nil {
				panic("Failed to create a key: " + err.Error())
			}
			fmt.Printf("public key for \"%s\"\n"+
				"command=\"exit 1\",no-port-forwarding,no-X11-forwarding,no-pty %s "+
				"issh generated from seed: '%s'\n",
				seed, bytes.TrimRight(key, "\r\n \t"), seed)
		}
		return
	}

	if flag.NArg() != 2 {
		flag.Usage()
		os.Exit(1)
	}

	user, host, port := parseUserHostPort(flag.Arg(1))
	stdout, exitcode, err := issh.Run(user, host, port, flag.Arg(0))
	if err != nil {
		panic("Failed to execute remote command: " + err.Error())
	}

	os.Stdout.Write(stdout)
	os.Exit(exitcode)
}

作者:SeaSunOpenSourc    项目:go-jxhtt   
func desDecode(b []byte) ([]byte, error) {
	td, err := des.NewTripleDESCipher(deskey)
	if err != nil {
		logger.Println(err)
		return nil, err
	}
	// blockMode := cipher.NewCBCDecrypter(block, key)
	// orig := make([]byte, len(b))
	// blockMode.CryptBlocks(orig, b)
	// logger.Println(string(orig))

	n := len(b) / td.BlockSize()
	var rb []byte
	for i := 0; i < n; i++ {
		dst := make([]byte, td.BlockSize())
		td.Decrypt(dst, b[i*8:(i+1)*8])
		rb = append(rb, dst[:]...)
	}

	lastValue := int(rb[len(rb)-1])
	logger.Println(string(rb[0 : len(rb)-lastValue]))

	// 移除最后的0
	return bytes.TrimRight(rb, string([]byte{0})), nil
}

作者:ably-fork    项目:flyn   
func (S) TestPatterns(c *C) {
	const sep = " "
	var input bytes.Buffer

	key := genPublicKey(c)
	keyBytes := bytes.TrimRight(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)), "\n")

	// format: pattern
	input.WriteString("*.example")
	input.WriteString(sep)
	input.Write(keyBytes)
	input.WriteString("\n")

	// format: negated pattern
	input.WriteString("!*.example.or?")
	input.WriteString(sep)
	input.Write(keyBytes)
	input.WriteString("\n")

	k, err := Unmarshal(bytes.NewReader(input.Bytes()))
	c.Assert(err, IsNil)

	// Test HostKeyCallback
	addr := &net.TCPAddr{
		Port: 22,
	}
	c.Assert(k.HostKeyCallback("foo.example:22", addr, key), IsNil)                         // pattern match
	c.Assert(k.HostKeyCallback("foo.example.org:22", addr, key), Equals, HostNotFoundError) // negated pattern match
	c.Assert(k.HostKeyCallback("anything.example.com:22", addr, key), IsNil)                // negated pattern miss

	// Make sure output is the same as input
	var output bytes.Buffer
	c.Assert(k.Marshal(&output), IsNil)
	c.Assert(output.String(), Equals, input.String())
}

作者:Codzar    项目:go-ethereu   
// read reads events from an inotify file descriptor. It does not handle errors
// returned from read(2) function since they are not critical to watcher logic.
func (i *inotify) read() (es []*event) {
	n, err := syscall.Read(int(i.fd), i.buffer[:])
	if err != nil || n < syscall.SizeofInotifyEvent {
		return
	}
	var sys *syscall.InotifyEvent
	nmin := n - syscall.SizeofInotifyEvent
	for pos, path := 0, ""; pos <= nmin; {
		sys = (*syscall.InotifyEvent)(unsafe.Pointer(&i.buffer[pos]))
		pos += syscall.SizeofInotifyEvent
		if path = ""; sys.Len > 0 {
			endpos := pos + int(sys.Len)
			path = string(bytes.TrimRight(i.buffer[pos:endpos], "\x00"))
			pos = endpos
		}
		es = append(es, &event{
			sys: syscall.InotifyEvent{
				Wd:     sys.Wd,
				Mask:   sys.Mask,
				Cookie: sys.Cookie,
			},
			path: path,
		})
	}
	return
}

作者:ENTGamin    项目:goirc   
// Client processor blockingly reads everything remote client sends,
// splits messages by CRLF and send them to Daemon gorouting for processing
// it futher. Also it can signalize that client is unavailable (disconnected).
func (client *Client) Processor(sink chan<- ClientEvent) {
	var bufNet []byte
	buf := make([]byte, 0)
	log.Println(client, "New client")
	sink <- ClientEvent{client, EventNew, ""}
	for {
		bufNet = make([]byte, BufSize)
		_, err := client.conn.Read(bufNet)
		if err != nil {
			sink <- ClientEvent{client, EventDel, ""}
			break
		}
		bufNet = bytes.TrimRight(bufNet, "\x00")
		buf = append(buf, bufNet...)
		if !bytes.HasSuffix(buf, []byte(CRLF)) {
			continue
		}
		for _, msg := range bytes.Split(buf[:len(buf)-2], []byte(CRLF)) {
			if len(msg) > 0 {
				sink <- ClientEvent{client, EventMsg, string(msg)}
			}
		}
		buf = []byte{}
	}
}

作者:JRaspas    项目:go-statsd-clien   
func TestFlushOnClose(t *testing.T) {
	l, err := newUDPListener("127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer l.Close()

	c, err := NewBufferedClient(l.LocalAddr().String(), "test", 1*time.Second, 1024)
	if err != nil {
		t.Fatal(err)
	}

	c.Inc("count", int64(1), 1.0)
	c.Close()

	expected := "test.count:1|c"

	data := make([]byte, 1024)
	_, _, err = l.ReadFrom(data)
	if err != nil {
		t.Fatal(err)
	}

	data = bytes.TrimRight(data, "\x00")
	if bytes.Equal(data, []byte(expected)) != true {
		fmt.Println(data)
		fmt.Println([]byte(expected))
		t.Fatalf("got '%s' expected '%s'", data, expected)
	}
}

作者:aelnaie    项目:torront   
func (peer Peer) downloadFile(file File, conn *net.TCPConn) {
	if f, ok := status.status["local"].files[file.FileName]; ok {
		if f.Chunks[file.Chunks[1]] == 1 {
			return
		}
	} else {
		chunks := make([]int, file.Chunks[0])
		for chunk := range chunks {
			chunks[chunk] = 0
		}
		chunks[file.Chunks[1]] = 1
		status.status["local"].files[file.FileName] = File{
			FileName: file.FileName,
			Chunks:   chunks,
		}
	}
	status.status["local"].files[file.FileName].Chunks[file.Chunks[1]] = 1
	incrementChunkReplication(file.FileName, file.Chunks[1], file.Chunks[0])

	err := conn.SetReadBuffer(ChunkSize)
	checkError(err)

	readBuffer := make([]byte, ChunkSize)
	_, err = conn.Read(readBuffer)
	checkError(err)
	conn.Close()

	status.mu.Lock()
	basepath := path.Dir(file.FileName)
	fileName := path.Base(file.FileName)
	err = os.MkdirAll(basepath, 0777)
	checkError(err)

	filePath := path.Join(basepath, fileName)

	localFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0777)
	if err != nil {
		for {
			localFile, err = os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0777)
			if err == nil {
				break
			}
		}
	}

	writeOffset := int64(file.Chunks[1] * ChunkSize)
	_, err = localFile.WriteAt(bytes.TrimRight(readBuffer, "\x00"), writeOffset)
	checkError(err)

	err = localFile.Close()
	checkError(err)

	status.mu.Unlock()
	fmt.Printf("Downloaded file %s:%d \n\n", file.FileName, file.Chunks[1])

	fileList := []File{file}
	haveMessage := encodeMessage(peer.host, peer.port, Have, fileList)
	sendToAll(haveMessage)
	return
}

作者:akave    项目:vfm   
func DetectSetextHeader(first, second Line, detectors Detectors) Handler {
	if second.EOF() {
		return nil
	}
	if !reSetextHeader.Match(bytes.TrimRight(second.Bytes, "\n")) {
		return nil
	}
	block := md.SetextHeaderBlock{}
	switch second.Bytes[0] {
	case '=':
		block.Level = 1
	case '-':
		block.Level = 2
	}
	done := 0
	return HandlerFunc(func(next Line, ctx Context) (bool, error) {
		if done == 2 {
			ctx.Emit(block)
			parseSpans(trim(md.Raw{block.Raw[0]}), ctx)
			ctx.Emit(md.End{})
			return false, nil
		}
		done++
		block.Raw = append(block.Raw, md.Run(next))
		return true, nil
	})
}

作者:Pieter    项目:cra   
func main() {
	flag.Parse()
	if *fPeers == "" {
		flagbad("-peers is empty\n")
	}
	if *fTopic == "" {
		flagbad("-topic is empty\n")
	}

	kfk, err := kafka.New("kafka-producer", logger, strings.Split(*fPeers, ","))
	if err != nil {
		logger.Panicf("Failed to start kafka: %v", err)
	}
	defer kfk.Close()

	br := bufio.NewReader(os.Stdin)
	for {
		line, err := br.ReadBytes('\n')
		if err == io.EOF && len(line) == 0 {
			break
		}
		if err != nil {
			logger.Panicf("Reading from stdin: %v", err)
		}
		line = bytes.TrimRight(line, "\n")
		part, offset, err := kfk.Send(nil, line, *fTopic)
		if err != nil {
			logger.Panicf("Sending message: %v", err)
		}
		if *fVerbose {
			fmt.Printf("send (len=%d, part=%d, offset=%d)\n", len(line), part, offset)
		}
	}
}

作者:voxada    项目:cascades-socket   
// Serve a connection by reading and writing what was read.  That's right, this
// is an echo service.  Stop reading and writing if anything is received on the
// service's channel but only after writing what was read.
func (self *Service) serve(connection *Connection) {
	defer connection.Close()
	defer self.waitGroup.Done()
	for {
		select {
		case <-self.done:
			log.Println("Disconnecting", connection.TCPConn.RemoteAddr())
			return
		default:
		}

		connection.TCPConn.SetDeadline(time.Now().Add(30 * time.Second))
		buf := make([]byte, 4096)
		if _, err := connection.TCPConn.Read(buf); err != nil {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				continue
			}
			log.Println("Error reading from connection:", err)
			delete(self.dataMap, connection.Id)
			break
		}

		payload := bytes.TrimRight(buf, string([]byte{0x00, '\n', '\r'}))
		self.Output <- [][]byte{[]byte(connection.Id), payload}

		data := <-connection.Input
		data = append(data, '\n', '\r')
		if _, err := connection.TCPConn.Write(data); err != nil {
			log.Println(err)
			break
		}
	}
}

作者:ashleyconno    项目:nnt   
func (y *yencReader) nextLine() error {
	if y.eof {
		return io.EOF
	}

	// read a whole line
	line, err := y.r.ReadBytes('\n')
	if err != nil {
		return err
	}

	// chomp the line ending
	line = bytes.TrimRight(line, "\r\n")

	if !y.readHeader {
		// expect a =ybegin line
		if len(line) >= 7 && string(line[:7]) == "=ybegin" {
			y.readHeader = true
			return nil
		} else {
			return fmt.Errorf("expected =ybegin, got %q", string(line))
		}
	}

	// are we at the end of the yenc blob?
	if len(line) >= 5 && string(line[:5]) == "=yend" {
		// remember this and signal the caller
		y.eof = true
		return io.EOF
	}

	y.buf = y.decode(line)
	return nil
}

作者:arip3    项目:go-ir   
func ParseMessage(message string) *Message {
	// :<prefix> <command> <params> :<trailing>
	msg := []byte(message)
	var prefix, params, trailing []byte

	if bytes.HasPrefix(msg, []byte(":")) {
		index := bytes.Index(msg, []byte(" "))
		prefix = msg[1:index]
		msg = msg[index+1:]
	}

	cmdEndIndex := bytes.Index(msg, []byte(" "))
	command := msg[:cmdEndIndex]
	msg = msg[cmdEndIndex+1:]
	trailingStartIndex := bytes.Index(msg, []byte(":"))
	if trailingStartIndex < 0 {
		params = msg
	} else {
		params = bytes.TrimRight(msg[:trailingStartIndex], " ")
		trailing = msg[trailingStartIndex+1:]
	}

	return &Message{
		raw:      message,
		Prefix:   prefix,
		Command:  command,
		Params:   params,
		Trailing: trailing,
	}
}

作者:joeljesk    项目:sync_gatewa   
// Adds a new part to the given multipart writer, containing the given revision.
// The revision will be written as a nested multipart body if it has attachments.
func (db *Database) WriteRevisionAsPart(revBody Body, isError bool, compressPart bool, writer *multipart.Writer) error {
	partHeaders := textproto.MIMEHeader{}
	docID, _ := revBody["_id"].(string)
	revID, _ := revBody["_rev"].(string)
	if len(docID) > 0 {
		partHeaders.Set("X-Doc-ID", docID)
		partHeaders.Set("X-Rev-ID", revID)
	}

	if hasInlineAttachments(revBody) {
		// Write as multipart, including attachments:
		// OPT: Find a way to do this w/o having to buffer the MIME body in memory!
		var buffer bytes.Buffer
		docWriter := multipart.NewWriter(&buffer)
		contentType := fmt.Sprintf("multipart/related; boundary=%q",
			docWriter.Boundary())
		partHeaders.Set("Content-Type", contentType)
		db.WriteMultipartDocument(revBody, docWriter, compressPart)
		docWriter.Close()
		content := bytes.TrimRight(buffer.Bytes(), "\r\n")

		part, err := writer.CreatePart(partHeaders)
		if err == nil {
			_, err = part.Write(content)
		}
		return err
	} else {
		// Write as JSON:
		contentType := "application/json"
		if isError {
			contentType += `; error="true"`
		}
		return writeJSONPart(writer, contentType, revBody, compressPart)
	}
}

作者:paulharte    项目:sync_gatewa   
func (h *handler) writeMultipart(subtype string, callback func(*multipart.Writer) error) error {
	if !h.requestAccepts("multipart/") {
		return base.HTTPErrorf(http.StatusNotAcceptable, "Response is multipart")
	}

	// Get the output stream. Due to a CouchDB bug, if we're sending to it we need to buffer the
	// output in memory so we can trim the final bytes.
	var output io.Writer
	var buffer bytes.Buffer
	if h.userAgentIs("CouchDB") {
		output = &buffer
	} else {
		output = h.response
	}

	writer := multipart.NewWriter(output)
	h.setHeader("Content-Type",
		fmt.Sprintf("multipart/%s; boundary=%q", subtype, writer.Boundary()))

	err := callback(writer)
	writer.Close()

	if err == nil && output == &buffer {
		// Trim trailing newline; CouchDB is allergic to it:
		_, err = h.response.Write(bytes.TrimRight(buffer.Bytes(), "\r\n"))
	}
	return err
}


问题


面经


文章

微信
公众号

扫码关注公众号