]> Cypherpunks repositories - gostls13.git/commitdiff
use the new bytes package
authorRob Pike <r@golang.org>
Thu, 4 Jun 2009 22:28:09 +0000 (15:28 -0700)
committerRob Pike <r@golang.org>
Thu, 4 Jun 2009 22:28:09 +0000 (15:28 -0700)
R=rsc
DELTA=61  (8 added, 31 deleted, 22 changed)
OCL=29897
CL=29899

src/lib/Make.deps
src/lib/bytes/bytes.go
src/lib/io/io.go
src/lib/strconv/decimal.go
src/lib/tabwriter/tabwriter.go
src/lib/utf8/utf8_test.go

index 50ba9c9e5efdbcf30ddddb287964ca9700491913..0445fbc6aeb9b6b74f3b7f519122e99f8daa0b96 100644 (file)
@@ -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
index a64b07b74fe0105e2019df29aa619cf129f52e67..dd299a82ee12c7bd8f8e2ef35d968e030cd06f56 100644 (file)
@@ -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).
index c120d8d443045f472e4df59f7e2373c4142b8c44..ba0449ac17dde7126db5d6d5a50997fcee32311e 100644 (file)
@@ -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;
 }
index 4808e93463d6e02b2b1343fb909853c42d821710..38d9c47fb1e78710a9eddce1f78ec8e43791d61f 100644 (file)
@@ -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]);
index 8179165bc55e489b9265321efd640527aa7ade9e..88da7da2b67df2c6c13addaa03d693c62ae36569 100644 (file)
@@ -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;
        }
 
index d6d20a135acd0d7f442567e48c4297a3b6564557..f60b0b17ee63432322cc995ae3bf1d57f6bbc18b 100644 (file)
@@ -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);
                }
        }