From 424f4f0ff5427e9c3149e93abacf00956701e16f Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Thu, 4 Jun 2009 15:28:09 -0700 Subject: [PATCH] use the new bytes package R=rsc DELTA=61 (8 added, 31 deleted, 22 changed) OCL=29897 CL=29899 --- src/lib/Make.deps | 6 +++--- src/lib/bytes/bytes.go | 5 +++-- src/lib/io/io.go | 5 ++--- src/lib/strconv/decimal.go | 11 ++++++----- src/lib/tabwriter/tabwriter.go | 5 ++--- src/lib/utf8/utf8_test.go | 25 +++++++------------------ 6 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/lib/Make.deps b/src/lib/Make.deps index 50ba9c9e5ef..0445fbc6aeb 100644 --- a/src/lib/Make.deps +++ b/src/lib/Make.deps @@ -22,7 +22,7 @@ hash.install: io.install hash/adler32.install: hash.install os.install hash/crc32.install: hash.install os.install http.install: bufio.install fmt.install io.install log.install net.install os.install path.install strconv.install strings.install utf8.install -io.install: os.install sync.install +io.install: bytes.install os.install sync.install json.install: container/vector.install fmt.install io.install math.install reflect.install strconv.install strings.install utf8.install log.install: fmt.install io.install os.install runtime.install time.install malloc.install: @@ -36,11 +36,11 @@ reflect.install: strconv.install sync.install utf8.install regexp.install: container/vector.install os.install runtime.install utf8.install runtime.install: sort.install: -strconv.install: math.install os.install utf8.install +strconv.install: bytes.install math.install os.install utf8.install strings.install: utf8.install sync.install: syscall.install: sync.install -tabwriter.install: container/vector.install io.install os.install utf8.install +tabwriter.install: bytes.install container/vector.install io.install os.install utf8.install template.install: container/vector.install fmt.install io.install os.install reflect.install runtime.install strings.install testing.install: flag.install fmt.install os.install runtime.install testing/iotest.install: io.install log.install os.install diff --git a/src/lib/bytes/bytes.go b/src/lib/bytes/bytes.go index a64b07b74fe..dd299a82ee1 100644 --- a/src/lib/bytes/bytes.go +++ b/src/lib/bytes/bytes.go @@ -43,11 +43,12 @@ func Equal(a, b []byte) bool { // Copy copies the source to the destination, stopping when the source // is all transferred. The caller must guarantee that there is enough -// room in the destination. -func Copy(dst, src []byte) { +// room in the destination. It returns the number of bytes copied +func Copy(dst, src []byte) int { for i, x := range src { dst[i] = x } + return len(src) } // Explode splits s into an array of UTF-8 sequences, one per Unicode character (still arrays of bytes). diff --git a/src/lib/io/io.go b/src/lib/io/io.go index c120d8d4430..ba0449ac17d 100644 --- a/src/lib/io/io.go +++ b/src/lib/io/io.go @@ -10,6 +10,7 @@ package io import ( + "bytes"; "os"; ) @@ -210,9 +211,7 @@ func (r ByteReader) Read(p []byte) (int, os.Error) { if n > len(b) { n = len(b); } - for i := 0; i < n; i++ { - p[i] = b[i]; - } + bytes.Copy(p, b[0:n]); *b = b[n:len(b)]; return n, nil; } diff --git a/src/lib/strconv/decimal.go b/src/lib/strconv/decimal.go index 4808e93463d..38d9c47fb1e 100644 --- a/src/lib/strconv/decimal.go +++ b/src/lib/strconv/decimal.go @@ -11,6 +11,8 @@ package strconv +import "bytes" + type decimal struct { // TODO(rsc): Can make d[] a bit smaller and add // truncated bool; @@ -27,7 +29,6 @@ func (a *decimal) RoundDown(nd int) *decimal; func (a *decimal) RoundedInteger() uint64; -func copy(dst []byte, src []byte) int; func digitZero(dst []byte) int; func (a *decimal) String() string { @@ -52,18 +53,18 @@ func (a *decimal) String() string { buf[w] = '.'; w++; w += digitZero(buf[w:w+-a.dp]); - w += copy(buf[w:w+a.nd], a.d[0:a.nd]); + w += bytes.Copy(buf[w:w+a.nd], a.d[0:a.nd]); case a.dp < a.nd: // decimal point in middle of digits - w += copy(buf[w:w+a.dp], a.d[0:a.dp]); + w += bytes.Copy(buf[w:w+a.dp], a.d[0:a.dp]); buf[w] = '.'; w++; - w += copy(buf[w:w+a.nd-a.dp], a.d[a.dp:a.nd]); + w += bytes.Copy(buf[w:w+a.nd-a.dp], a.d[a.dp:a.nd]); default: // zeros fill space between digits and decimal point - w += copy(buf[w:w+a.nd], a.d[0:a.nd]); + w += bytes.Copy(buf[w:w+a.nd], a.d[0:a.nd]); w += digitZero(buf[w:w+a.dp-a.nd]); } return string(buf[0:w]); diff --git a/src/lib/tabwriter/tabwriter.go b/src/lib/tabwriter/tabwriter.go index 8179165bc55..88da7da2b67 100644 --- a/src/lib/tabwriter/tabwriter.go +++ b/src/lib/tabwriter/tabwriter.go @@ -10,6 +10,7 @@ package tabwriter import ( + "bytes"; "container/vector"; "io"; "os"; @@ -56,9 +57,7 @@ func (b *byteArray) append(s []byte) { n2 = m; } b := make([]byte, n2); - for i := 0; i < n; i++ { - b[i] = a[i]; - } + bytes.Copy(b, a); a = b; } diff --git a/src/lib/utf8/utf8_test.go b/src/lib/utf8/utf8_test.go index d6d20a135ac..f60b0b17ee6 100644 --- a/src/lib/utf8/utf8_test.go +++ b/src/lib/utf8/utf8_test.go @@ -5,6 +5,7 @@ package utf8 import ( + "bytes"; "fmt"; "io"; "testing"; @@ -45,7 +46,7 @@ var utf8map = []Utf8Map { } // io.StringBytes with one extra byte at end -func bytes(s string) []byte { +func makeBytes(s string) []byte { s += "\x00"; b := io.StringBytes(s); return b[0:len(s)-1]; @@ -54,7 +55,7 @@ func bytes(s string) []byte { func TestFullRune(t *testing.T) { for i := 0; i < len(utf8map); i++ { m := utf8map[i]; - b := bytes(m.str); + b := makeBytes(m.str); if !utf8.FullRune(b) { t.Errorf("FullRune(%q) (rune %04x) = false, want true", b, m.rune); } @@ -73,26 +74,14 @@ func TestFullRune(t *testing.T) { } } -func equalBytes(a, b []byte) bool { - if len(a) != len(b) { - return false; - } - for i := 0; i < len(a); i++ { - if a[i] != b[i] { - return false; - } - } - return true; -} - func TestEncodeRune(t *testing.T) { for i := 0; i < len(utf8map); i++ { m := utf8map[i]; - b := bytes(m.str); + b := makeBytes(m.str); var buf [10]byte; n := utf8.EncodeRune(m.rune, &buf); b1 := buf[0:n]; - if !equalBytes(b, b1) { + if !bytes.Equal(b, b1) { t.Errorf("EncodeRune(0x%04x) = %q want %q", m.rune, b1, b); } } @@ -101,7 +90,7 @@ func TestEncodeRune(t *testing.T) { func TestDecodeRune(t *testing.T) { for i := 0; i < len(utf8map); i++ { m := utf8map[i]; - b := bytes(m.str); + b := makeBytes(m.str); rune, size := utf8.DecodeRune(b); if rune != m.rune || size != len(b) { t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, m.rune, len(b)); @@ -172,7 +161,7 @@ func TestRuneCount(t *testing.T) { if out := utf8.RuneCountInString(tt.in); out != tt.out { t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out); } - if out := utf8.RuneCount(bytes(tt.in)); out != tt.out { + if out := utf8.RuneCount(makeBytes(tt.in)); out != tt.out { t.Errorf("RuneCount(%q) = %d, want %d", tt.in, out, tt.out); } }