Commit 1d812515 authored by Joe Tsai's avatar Joe Tsai Committed by Joe Tsai

archive/tar: simplify toASCII and parseString

Use a simple []byte instead of bytes.Buffer to create a string.
Use bytes.IndexByte instead of our own for loop.

Change-Id: Ic4a1161d79017fd3af086a05c53d5f20a5f09326
Reviewed-on: https://go-review.googlesource.com/54752Reviewed-by: default avatarAvelino <t@avelino.xxx>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
parent 23cd87eb
......@@ -27,13 +27,13 @@ func toASCII(s string) string {
if isASCII(s) {
return s
}
var buf bytes.Buffer
b := make([]byte, 0, len(s))
for _, c := range s {
if c < 0x80 && c != 0x00 {
buf.WriteByte(byte(c))
b = append(b, byte(c))
}
}
return buf.String()
return string(b)
}
type parser struct {
......@@ -47,11 +47,10 @@ type formatter struct {
// parseString parses bytes as a NUL-terminated C-style string.
// If a NUL byte is not found then the whole slice is returned as a string.
func (*parser) parseString(b []byte) string {
n := 0
for n < len(b) && b[n] != 0 {
n++
if i := bytes.IndexByte(b, 0); i >= 0 {
return string(b[:i])
}
return string(b[0:n])
return string(b)
}
// Write s into b, terminating it with a NUL if there is room.
......@@ -75,7 +74,7 @@ func (f *formatter) formatString(b []byte, s string) {
// that the first byte can only be either 0x80 or 0xff. Thus, the first byte is
// equivalent to the sign bit in two's complement form.
func fitsInBase256(n int, x int64) bool {
var binBits = uint(n-1) * 8
binBits := uint(n-1) * 8
return n >= 9 || (x >= -1<<binBits && x < 1<<binBits)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment