1
0
mirror of https://github.com/golang/go synced 2024-11-26 00:57:56 -07:00

use buf.String() instead of string(buf.Bytes())

use strings.Buffer instead of bytes.Buffer in some places

R=rsc
DELTA=40  (0 added, 3 deleted, 37 changed)
OCL=34770
CL=34775
This commit is contained in:
Rob Pike 2009-09-17 23:51:06 -07:00
parent a0a965fbcf
commit 7be770071f
12 changed files with 27 additions and 29 deletions

View File

@ -73,7 +73,7 @@ func TestEncoder(t *testing.T) {
encoder := NewEncoder(StdEncoding, bb);
encoder.Write(strings.Bytes(p.decoded));
encoder.Close();
testEqual(t, "Encode(%q) = %q, want %q", p.decoded, string(bb.Bytes()), p.encoded);
testEqual(t, "Encode(%q) = %q, want %q", p.decoded, bb.String(), p.encoded);
}
}
@ -93,7 +93,7 @@ func TestEncoderBuffering(t *testing.T) {
}
err := encoder.Close();
testEqual(t, "Close gave error %v, want %v", err, os.Error(nil));
testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, string(bb.Bytes()), bigtest.encoded);
testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, bb.String(), bigtest.encoded);
}
}
@ -112,7 +112,7 @@ func TestDecode(t *testing.T) {
func TestDecoder(t *testing.T) {
for _, p := range pairs {
decoder := NewDecoder(StdEncoding, bytes.NewBuffer(strings.Bytes(p.encoded)));
decoder := NewDecoder(StdEncoding, strings.NewBuffer(p.encoded));
dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)));
count, err := decoder.Read(dbuf);
if err != nil && err != os.EOF {
@ -128,9 +128,8 @@ func TestDecoder(t *testing.T) {
}
func TestDecoderBuffering(t *testing.T) {
input := strings.Bytes(bigtest.encoded);
for bs := 1; bs <= 12; bs++ {
decoder := NewDecoder(StdEncoding, bytes.NewBuffer(input));
decoder := NewDecoder(StdEncoding, strings.NewBuffer(bigtest.encoded));
buf := make([]byte, len(bigtest.decoded) + 12);
var total int;
for total = 0; total < len(bigtest.decoded); {

View File

@ -60,13 +60,13 @@ func readBytes(buf *Reader) string {
}
func TestReaderSimple(t *testing.T) {
data := strings.Bytes("hello world");
b := NewReader(bytes.NewBuffer(data));
data := "hello world";
b := NewReader(strings.NewBuffer(data));
if s := readBytes(b); s != "hello world" {
t.Errorf("simple hello world test failed: got %q", s);
}
b = NewReader(newRot13Reader(bytes.NewBuffer(data)));
b = NewReader(newRot13Reader(strings.NewBuffer(data)));
if s := readBytes(b); s != "uryyb jbeyq" {
t.Error("rot13 hello world test failed: got %q", s);
}
@ -148,14 +148,13 @@ func TestReader(t *testing.T) {
for h := 0; h < len(texts); h++ {
text := texts[h];
textbytes := strings.Bytes(text);
for i := 0; i < len(readMakers); i++ {
for j := 0; j < len(bufreaders); j++ {
for k := 0; k < len(bufsizes); k++ {
readmaker := readMakers[i];
bufreader := bufreaders[j];
bufsize := bufsizes[k];
read := readmaker.fn(bytes.NewBuffer(textbytes));
read := readmaker.fn(strings.NewBuffer(text));
buf, _ := NewReaderSize(read, bufsize);
s := bufreader.fn(buf);
if s != text {
@ -309,7 +308,7 @@ func TestWriteErrors(t *testing.T) {
func TestNewReaderSizeIdempotent(t *testing.T) {
const BufSize = 1000;
b, err := NewReaderSize(bytes.NewBuffer(strings.Bytes("hello world")), BufSize);
b, err := NewReaderSize(strings.NewBuffer("hello world"), BufSize);
if err != nil {
t.Error("NewReaderSize create fail", err);
}

View File

@ -297,7 +297,7 @@ func TestInflater(t *testing.T) {
if err != tt.err {
t.Errorf("%s: io.Copy: %v want %v", tt.name, err, tt.err);
}
s := string(b.Bytes());
s := b.String();
if s != tt.raw {
t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.name, n, s, len(tt.raw), tt.raw);
}

View File

@ -95,7 +95,7 @@ func TestInflater(t *testing.T) {
}
continue;
}
s := string(b.Bytes());
s := b.String();
if s != tt.raw {
t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw);
}

View File

@ -729,5 +729,5 @@ func (f Format) Sprint(args ...) string {
if err != nil {
fmt.Fprintf(&buf, "--- Sprint(%s) failed: %v", fmt.Sprint(args), err);
}
return string(buf.Bytes());
return buf.String();
}

View File

@ -63,7 +63,7 @@ func (v *Map) String() string {
first = false;
}
fmt.Fprintf(b, "}");
return string(b.Bytes())
return b.String()
}
func (v *Map) Init() *Map {

View File

@ -228,7 +228,7 @@ func TestWrongTypeDecoder(t *testing.T) {
}
func corruptDataCheck(s string, err os.Error, t *testing.T) {
b := bytes.NewBuffer(strings.Bytes(s));
b := strings.NewBuffer(s);
dec := NewDecoder(b);
dec.Decode(new(ET2));
if dec.state.err != err {

View File

@ -43,7 +43,7 @@ func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
case "POST":
buf := new(bytes.Buffer);
io.Copy(req.Body, buf);
body := string(buf.Bytes());
body := buf.String();
if n, err := strconv.Atoi(body); err != nil {
fmt.Fprintf(c, "bad POST: %v\nbody: [%v]\n", err, body);
} else {

View File

@ -166,7 +166,7 @@ func Quote(s string) string {
}
chr[0] = '"';
b.Write(chr0);
return string(b.Bytes());
return b.String();
}

View File

@ -326,7 +326,7 @@ func TestForkExec(t *testing.T) {
var b bytes.Buffer;
io.Copy(r, &b);
output := string(b.Bytes());
output := b.String();
expect := "/\n";
if output != expect {
t.Errorf("exec /bin/pwd returned %q wanted %q", output, expect);
@ -605,7 +605,7 @@ func run(t *testing.T, cmd []string) string {
var b bytes.Buffer;
io.Copy(r, &b);
Wait(pid, 0);
output := string(b.Bytes());
output := b.String();
if n := len(output); n > 0 && output[n-1] == '\n' {
output = output[0:n-1];
}

View File

@ -883,7 +883,7 @@ func (re *Regexp) ReplaceAllString(src, repl string) string {
// Copy the unmatched characters after the last match.
io.WriteString(buf, src[lastMatchEnd:len(src)]);
return string(buf.Bytes());
return buf.String();
}
// ReplaceAll returns a copy of src in which all matches for the Regexp

View File

@ -312,8 +312,8 @@ func TestAll(t *testing.T) {
t.Errorf("expected execute error %q, got %q", test.err, err.String());
}
}
if string(buf.Bytes()) != test.out {
t.Errorf("for %q: expected %q got %q", test.in, test.out, string(buf.Bytes()));
if buf.String() != test.out {
t.Errorf("for %q: expected %q got %q", test.in, test.out, buf.String());
}
}
}
@ -328,7 +328,7 @@ func TestStringDriverType(t *testing.T) {
if err != nil {
t.Error("unexpected execute error:", err)
}
s := string(b.Bytes());
s := b.String();
if s != "template: hello" {
t.Errorf("failed passing string as data: expected %q got %q", "template: hello", s)
}
@ -344,7 +344,7 @@ func TestTwice(t *testing.T) {
if err != nil {
t.Error("unexpected parse error:", err)
}
s := string(b.Bytes());
s := b.String();
text := "template: hello";
if s != text {
t.Errorf("failed passing string as data: expected %q got %q", text, s);
@ -353,7 +353,7 @@ func TestTwice(t *testing.T) {
if err != nil {
t.Error("unexpected parse error:", err)
}
s = string(b.Bytes());
s = b.String();
text += text;
if s != text {
t.Errorf("failed passing string as data: expected %q got %q", text, s);
@ -386,7 +386,7 @@ func TestCustomDelims(t *testing.T) {
}
var b bytes.Buffer;
err = tmpl.Execute("hello", &b);
s := string(b.Bytes());
s := b.String();
if s != "template: hello" + ldelim + rdelim {
t.Errorf("failed delim check(%q %q) %q got %q", ldelim, rdelim, text, s)
}
@ -411,7 +411,7 @@ func TestVarIndirection(t *testing.T) {
t.Fatal("unexpected execute error:", err)
}
expect := fmt.Sprintf("%v", &t1); // output should be hex address of t1
if string(buf.Bytes()) != expect {
t.Errorf("for %q: expected %q got %q", input, expect, string(buf.Bytes()));
if buf.String() != expect {
t.Errorf("for %q: expected %q got %q", input, expect, buf.String());
}
}