当前位置: 首页>>代码示例>>Golang>>正文


Golang bytes.TrimRightFunc函数代码示例

本文整理汇总了Golang中bytes.TrimRightFunc函数的典型用法代码示例。如果您正苦于以下问题:Golang TrimRightFunc函数的具体用法?Golang TrimRightFunc怎么用?Golang TrimRightFunc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了TrimRightFunc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Read

// Read reads and decodes quoted-printable data from the underlying reader.
func (r *Reader) Read(p []byte) (int, error) {
	// Deviations from RFC 2045:
	// 1. in addition to "=\r\n", "=\n" is also treated as soft line break.
	// 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
	//    with other broken QP encoders & decoders.
	var n int
	var err error
	for len(p) > 0 {
		if len(r.line) == 0 {
			if err = r.Fn(); err != nil {
				return n, err
			}
			r.line, r.rerr = r.br.ReadSlice('\n')
			r.gerr.addUnrecover(r.rerr)

			// Does the line end in CRLF instead of just LF?
			hasLF := bytes.HasSuffix(r.line, lf)
			hasCR := bytes.HasSuffix(r.line, crlf)
			wholeLine := r.line
			r.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace)
			if bytes.HasSuffix(r.line, softSuffix) {
				rightStripped := wholeLine[len(r.line):]
				r.line = r.line[:len(r.line)-1]
				if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) {
					r.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped)
					r.gerr.add(r.rerr)
				}
			} else if hasLF {
				if hasCR {
					r.line = append(r.line, '\r', '\n')
				} else {
					r.line = append(r.line, '\n')
				}
			}
			continue
		}
		b := r.line[0]

		switch {
		case b == '=':
			b, err = readHexByte(r.line[1:])
			if err != nil {
				b = '='
				r.gerr.add(err)
				break // this modification allow bad email to be parsed too
				//return n, err
			}
			r.line = r.line[2:] // 2 of the 3; other 1 is done below
		case b == '\t' || b == '\r' || b == '\n':
		case b < ' ' || b > '~':
			//return n, fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", b)
			r.gerr.add(fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", b))
		}
		p[0] = b
		p = p[1:]
		r.line = r.line[1:]
		n++
	}
	return n, r.Fn()
}
开发者ID:cention-sany,项目名称:mime,代码行数:61,代码来源:reader.go

示例2: main

func main() {
	s := []byte("123456789")
	f := func(r rune) bool {
		return r > '7'
	}
	fmt.Println(string(bytes.TrimRightFunc(s, f)))
}
开发者ID:cwen-coder,项目名称:study-gopkg,代码行数:7,代码来源:TrimRightFunc.go

示例3: EncodeKey

func EncodeKey(key []byte) string {
	// we do sloppy work and process safe bytes only at the beginning
	// and end; this avoids many false positives in large binary data

	var left, middle, right string

	{
		mid := bytes.TrimLeftFunc(key, isSafe)
		if len(key)-len(mid) > prettyTheshold {
			left = string(key[:len(key)-len(mid)]) + string(FragSeparator)
			key = mid
		}
	}

	{
		mid := bytes.TrimRightFunc(key, isSafe)
		if len(key)-len(mid) > prettyTheshold {
			right = string(FragSeparator) + string(key[len(mid):])
			key = mid
		}
	}

	if len(key) > 0 {
		middle = "@" + hex.EncodeToString(key)
	}

	return strings.Trim(left+middle+right, string(FragSeparator))
}
开发者ID:voidException,项目名称:bazil,代码行数:28,代码来源:util.go

示例4: Listen

func (g *gobot) Listen() (err error) {
	start, err := g.slackApi.startRTM()
	if err != nil {
		return
	}
	if !start.Okay {
		return fmt.Errorf("Real-Time Messaging failed to start, aborting: %s", start.Error)
	}

	if g.setupFunc != nil {
		g.setupFunc(g.slackApi)
	}

	conn := start.openWebSocket()

	healthChecks(conn)

	for {
		_, msg, err := conn.ReadMessage()
		if err != nil {
			return err
		}
		var msgType unmarshalled
		if err = json.Unmarshal(bytes.TrimRightFunc(msg, func(r rune) bool { return r == '\x00' }), &msgType); err != nil {
			return err
		}
		go g.delegate(msgType.Type, msg)
	}
}
开发者ID:adufrene,项目名称:gobot,代码行数:29,代码来源:gobot.go

示例5: sanitizeText

// sanitizeText tries to make the given string easier to read when presented
// as a single line. It squashes each run of white space into a single
// space, trims leading and trailing white space and trailing full
// stops. If newlineSemi is true, any newlines will be replaced with a
// semicolon.
func sanitizeText(s string, newlineSemi bool) []byte {
	out := make([]byte, 0, len(s))
	prevWhite := false
	for _, r := range s {
		if newlineSemi && r == '\n' && len(out) > 0 {
			out = append(out, ';')
			prevWhite = true
			continue
		}
		if unicode.IsSpace(r) {
			if len(out) > 0 {
				prevWhite = true
			}
			continue
		}
		if prevWhite {
			out = append(out, ' ')
			prevWhite = false
		}
		out = append(out, string(r)...)
	}
	// Remove final space, any full stops and any final semicolon
	// we might have added.
	out = bytes.TrimRightFunc(out, func(r rune) bool {
		return r == '.' || r == ' ' || r == ';'
	})
	return out
}
开发者ID:bz2,项目名称:httprequest,代码行数:33,代码来源:checkisjson.go

示例6: main

func main() {
	whitespace := " \t\r\n"

	padded := []byte("  \t\r\n\r\n\r\n  hello!!!    \t\t\t\t")
	trimmed := bytes.Trim(padded, whitespace)
	log.Printf("Trim removed runes in %q from the ends of %q to produce %q", whitespace, padded, trimmed)

	rhyme := []byte("aabbccddee")
	trimFunced := bytes.TrimFunc(rhyme, trimOdd)
	log.Printf("TrimFunc removed 'odd' runes from %q to produce %q", rhyme, trimFunced)

	leftTrimmed := bytes.TrimLeft(padded, whitespace)
	log.Printf("TrimLeft removed runes in %q from the left side of %q to produce %q", whitespace, padded, leftTrimmed)

	leftTrimFunced := bytes.TrimLeftFunc(rhyme, trimOdd)
	log.Printf("TrimLeftFunc removed 'odd' runes from the left side of %q to produce %q", rhyme, leftTrimFunced)

	rightTrimmed := bytes.TrimRight(padded, whitespace)
	log.Printf("TrimRight removed runes in %q from the right side of %q to produce %q", whitespace, padded, rightTrimmed)

	rightTrimFunced := bytes.TrimRightFunc(rhyme, trimOdd)
	log.Printf("TrimRightFunc removed 'odd' runes from the right side of %q to produce %q", rhyme, rightTrimFunced)

	spaceTrimmed := bytes.TrimSpace(padded)
	log.Printf("TrimSpace trimmed all whitespace from the ends of %q to produce %q", padded, spaceTrimmed)
}
开发者ID:rif,项目名称:golang-stuff,代码行数:26,代码来源:trimming.go

示例7: Read

// Read reads and decodes quoted-printable data from the underlying reader.
func (r *Reader) Read(p []byte) (n int, err error) {
	// Deviations from RFC 2045:
	// 1. in addition to "=\r\n", "=\n" is also treated as soft line break.
	// 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
	//    with other broken QP encoders & decoders.
	// 3. it accepts soft line-break (=) at end of message (issue 15486); i.e.
	//    the final byte read from the underlying reader is allowed to be '=',
	//    and it will be silently ignored.
	for len(p) > 0 {
		if len(r.line) == 0 {
			if r.rerr != nil {
				return n, r.rerr
			}
			r.line, r.rerr = r.br.ReadSlice('\n')

			// Does the line end in CRLF instead of just LF?
			hasLF := bytes.HasSuffix(r.line, lf)
			hasCR := bytes.HasSuffix(r.line, crlf)
			wholeLine := r.line
			r.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace)
			if bytes.HasSuffix(r.line, softSuffix) {
				rightStripped := wholeLine[len(r.line):]
				r.line = r.line[:len(r.line)-1]
				if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) &&
					!(len(rightStripped) == 0 && len(r.line) > 0 && r.rerr == io.EOF) {
					r.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped)
				}
			} else if hasLF {
				if hasCR {
					r.line = append(r.line, '\r', '\n')
				} else {
					r.line = append(r.line, '\n')
				}
			}
			continue
		}
		b := r.line[0]

		switch {
		case b == '=':
			b, err = readHexByte(r.line[1:])
			if err != nil {
				return n, err
			}
			r.line = r.line[2:] // 2 of the 3; other 1 is done below
		case b == '\t' || b == '\r' || b == '\n':
			break
		case b < ' ' || b > '~':
			return n, fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", b)
		}
		p[0] = b
		p = p[1:]
		r.line = r.line[1:]
		n++
	}
	return n, nil
}
开发者ID:kuangchanglang,项目名称:go,代码行数:58,代码来源:reader.go

示例8: ZeroUnPadding

func ZeroUnPadding(origData []byte, blockSize int) []byte {
	return bytes.TrimRightFunc(origData,
		func(r rune) bool {
			if r == rune(0) && blockSize > 0 {
				blockSize--
				return true
			}
			return false
		})
}
开发者ID:jackwanger,项目名称:cloud-base,代码行数:10,代码来源:des.go

示例9: Read

func (q *qpReader) Read(p []byte) (n int, err error) {
	for len(p) > 0 {
		if len(q.line) == 0 {
			if q.rerr != nil {
				return n, q.rerr
			}
			q.skipWhite = true
			q.line, q.rerr = q.br.ReadSlice('\n')

			// Does the line end in CRLF instead of just LF?
			hasLF := bytes.HasSuffix(q.line, lf)
			hasCR := bytes.HasSuffix(q.line, crlf)
			wholeLine := q.line
			q.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace)
			if bytes.HasSuffix(q.line, softSuffix) {
				rightStripped := wholeLine[len(q.line):]
				q.line = q.line[:len(q.line)-1]
				if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) {
					q.rerr = fmt.Errorf("multipart: invalid bytes after =: %q", rightStripped)
				}
			} else if hasLF {
				if hasCR {
					q.line = append(q.line, '\r', '\n')
				} else {
					q.line = append(q.line, '\n')
				}
			}
			continue
		}
		b := q.line[0]
		if q.skipWhite && isQPSkipWhiteByte(b) {
			q.line = q.line[1:]
			continue
		}
		q.skipWhite = false

		switch {
		case b == '=':
			b, err = q.readHexByte(q.line[1:])
			if err != nil {
				return n, err
			}
			q.line = q.line[2:] // 2 of the 3; other 1 is done below
		case b == '\t' || b == '\r' || b == '\n':
			break
		case b < ' ' || b > '~':
			return n, fmt.Errorf("multipart: invalid unescaped byte 0x%02x in quoted-printable body", b)
		}
		p[0] = b
		p = p[1:]
		q.line = q.line[1:]
		n++
	}
	return n, nil
}
开发者ID:serge-hulne,项目名称:golang,代码行数:55,代码来源:quotedprintable.go

示例10: NextXAsString

func (b *bufferHelper) NextXAsString(fieldName string, x int, remainingBytes *int) string {
	bString := b.NextX(fieldName, x, remainingBytes)
	if b.Error != nil {
		return ""
	}

	// trim nulls from end
	return string(bytes.TrimRightFunc(bString, func(r rune) bool {
		return r == 0x0
	}))
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:11,代码来源:decoder.go

示例11: minifyReadFile

func minifyReadFile(file string) []byte {
	f, err := os.Open(file)
	if err != nil {
		log.Fatalln(err)
	}
	defer f.Close()
	var lastLineEnd byte
	var partMark int
	var r = bufio.NewReader(f)
	var buf = new(bytes.Buffer)
	for {
		line, part, err := r.ReadLine()
		if part {
			partMark++
		} else if partMark > 0 {
			partMark = -1
		} else {
			partMark = 0
		}
		if len(line) > 0 {
			switch partMark {
			case 0:
				line = bytes.TrimSpace(line)
			case 1:
				line = bytes.TrimLeftFunc(line, unicode.IsSpace)
			default:
				if partMark < 0 {
					partMark = 0
					line = bytes.TrimRightFunc(line, unicode.IsSpace)
				}
			}
			buf.Write(line)
			lastLineEnd = line[len(line)-1]
		}
		if err != nil && r.Buffered() == 0 {
			break
		}
	}
	// make sure line end with \n
	if lastLineEnd != '\n' {
		buf.WriteByte('\n')
	}
	return buf.Bytes()
}
开发者ID:noscripter,项目名称:deblocus,代码行数:44,代码来源:update-webpanel.go

示例12: Parse

// Parse a debian changelog from r for any changes happening later than afterVersion
func Parse(r io.Reader, afterVersion string) (Changelog, error) {
	scanner := bufio.NewScanner(r)
	changelog := make(Changelog, 0, 5)
	change := Change{}
	inside := false

	for scanner.Scan() {
		b := bytes.TrimRightFunc(scanner.Bytes(), unicode.IsSpace)
		if b2 := bytes.TrimSpace(b); len(b2) < len(b) && !inside {
			b = b2
		}
		if len(b) == 0 {
			if inside {
				change.Changes = append(change.Changes, '\n')
			}
			continue
		}

		if !inside && change.parseVersionLine(b) {
			if len(afterVersion) > 0 && change.Version == afterVersion {
				break
			}
			inside = true
			continue
		}

		if inside && change.parseChangedByLine(b) {
			changelog = append(changelog, change)
			change = Change{}
			inside = false
			continue
		}

		change.Changes = append(change.Changes, b...)
		change.Changes = append(change.Changes, '\n')
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}
	return changelog, nil
}
开发者ID:nightlyone,项目名称:deb,代码行数:43,代码来源:changelog.go

示例13: decrypt

func decrypt(key, src []byte) (dst []byte, err error) {

	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	if len(src) < aes.BlockSize {
		return nil, fmt.Errorf("invalid source, too short")
	}

	iv := src[:ivLength]
	cipherText := src[ivLength:]
	mode := cipher.NewCBCDecrypter(block, iv)
	mode.CryptBlocks(cipherText, cipherText)

	// handle padding
	lastByte := cipherText[len(cipherText)-1:]
	buf := bytes.NewBuffer(lastByte)
	uVal, err := binary.ReadUvarint(buf)
	if err != nil {
		return nil, fmt.Errorf("Invalid padding, %s", err)
	}
	paddingLength := int(uVal)

	if paddingLength > aes.BlockSize && paddingLength != 32 {
		return nil, fmt.Errorf("Decription failed")
	}
	if paddingLength == 32 {
		return bytes.TrimRightFunc(cipherText, unicode.IsSpace), nil
	}
	paddingIndex := len(cipherText) - paddingLength - 1
	if bytes.Compare(cipherText[paddingIndex:], bytes.Repeat(lastByte, paddingLength)) == 0 {
		return nil, fmt.Errorf("Decription failed")
	}

	return cipherText[:len(cipherText)-paddingLength], nil
}
开发者ID:yulrizka,项目名称:jrnl,代码行数:38,代码来源:journal.go

示例14: EncodeKey

func EncodeKey(key []byte) string {
	// we do sloppy work and process safe bytes only at the beginning
	// and end; this avoids many false positives in large binary data

	var left, right []byte
	var middle string

	if key[0] != '.' {
		mid := bytes.TrimLeftFunc(key, isSafe)
		if len(key)-len(mid) > prettyTheshold {
			left = key[:len(key)-len(mid)]
			key = mid
		}
	}

	{
		mid := bytes.TrimRightFunc(key, isSafe)
		if len(mid) == 0 && len(key) > 0 && key[0] == '.' {
			// don't let right safe zone reach all the way to leading dot
			mid = key[:1]
		}
		if len(key)-len(mid) > prettyTheshold {
			right = key[len(mid):]
			key = mid
		}
	}

	if len(key) > 0 {
		middle = "@" + hex.EncodeToString(key)
	}

	return strings.Trim(
		string(left)+string(FragSeparator)+middle+string(FragSeparator)+string(right),
		string(FragSeparator),
	)
}
开发者ID:jostillmanns,项目名称:bolt-mount,代码行数:36,代码来源:encode.go

示例15: getValue

// IF allowEOF is true it won't return io.EOF as an error, but see it as the end
// of the value.
func getValue(buf *buffer, end byte, allowEOF bool) ([]byte, error) {
	var started, isQouted, qoutedClosed bool
	var value []byte
	var er error

	for {
		c, err := buf.ReadByte()
		if err != nil {
			if allowEOF && err == io.EOF {
				er = err
				break
			}

			return []byte{}, err
		}

		if !started {
			if isSpace(c) {
				continue
			}

			if c == qouteByte {
				isQouted = true
			} else {
				value = append(value, c)
			}

			started = true
			continue
		}

		if qoutedClosed {
			if isSpace(c) {
				continue
			} else if c != end {
				return []byte{}, fmt.Errorf("unexpected %s after closed qoute", string(c))
			}
		}

		if c == qouteByte {
			if isQouted && value[len(value)-1] != escapeByte {
				qoutedClosed = true
				continue
			} else if value[len(value)-1] == escapeByte {
				// replace the escape byte with the byte being escaped.
				value[len(value)-1] = c
				continue
			}
		}

		if c == end && (!isQouted || qoutedClosed) {
			break
		}

		value = append(value, c)
	}

	if !isQouted {
		value = bytes.TrimRightFunc(value, unicode.IsSpace)
	}

	// todo: trim left space.
	return value, er
}
开发者ID:Thomasdezeeuw,项目名称:syslog,代码行数:66,代码来源:parsers.go


注:本文中的bytes.TrimRightFunc函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。