]> Cypherpunks repositories - gostls13.git/commitdiff
bytes.Copy
authorRob Pike <r@golang.org>
Thu, 4 Jun 2009 22:00:15 +0000 (15:00 -0700)
committerRob Pike <r@golang.org>
Thu, 4 Jun 2009 22:00:15 +0000 (15:00 -0700)
R=rsc
DELTA=38  (38 added, 0 deleted, 0 changed)
OCL=29895
CL=29895

src/lib/Make.deps
src/lib/Makefile
src/lib/bytes/bytes.go
src/lib/bytes/bytes_test.go

index 538e17039b1ed441a7308a165d3afb39362286bd..50ba9c9e5efdbcf30ddddb287964ca9700491913 100644 (file)
@@ -1,5 +1,6 @@
 bignum.install: fmt.install
 bufio.install: io.install os.install utf8.install
+bytes.install: utf8.install
 container/list.install:
 container/vector.install:
 crypto/aes.install: os.install
index 0f76507a4c487b793f10878c55adc7e2f1db7d8b..bdcfa0194e9d4a68dbe838800ddff6051e1f5255 100644 (file)
@@ -16,6 +16,7 @@ GC=6g
 DIRS=\
        bignum\
        bufio\
+       bytes\
        container/list\
        container/vector\
        crypto/aes\
index fe97b0495844693eca5fb741de57e601bba7f973..a64b07b74fe0105e2019df29aa619cf129f52e67 100644 (file)
@@ -41,6 +41,15 @@ func Equal(a, b []byte) bool {
        return true
 }
 
+// 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) {
+       for i, x := range src {
+               dst[i] = x
+       }
+}
+
 // Explode splits s into an array of UTF-8 sequences, one per Unicode character (still arrays of bytes).
 // Invalid UTF-8 sequences become correct encodings of U+FFF8.
 func Explode(s []byte) [][]byte {
index 26b3fc21d0a0a7ac7e356b1e622e37481bc3c72c..4e7cdfad65285bc64b244bd3e94329b88b2bf8cd 100644 (file)
@@ -128,3 +128,30 @@ func TestSplit(t *testing.T) {
                }
        }
 }
+
+type CopyTest struct {
+       a       string;
+       b       string;
+       res     string;
+}
+var copytests = []CopyTest {
+       CopyTest{ "", "", "" },
+       CopyTest{ "a", "", "a" },
+       CopyTest{ "a", "a", "a" },
+       CopyTest{ "a", "b", "b" },
+       CopyTest{ "xyz", "abc", "abc" },
+       CopyTest{ "wxyz", "abc", "abcz" },
+}
+
+func TestCopy(t *testing.T) {
+       for i := 0; i < len(copytests); i++ {
+               tt := copytests[i];
+               dst := io.StringBytes(tt.a);
+               Copy(dst, io.StringBytes(tt.b));
+               result := string(dst);
+               if result != tt.res {
+                       t.Errorf(`Copy("%s", "%s") = "%s"; want "%s"`, tt.a, tt.b, result, tt.res);
+                       continue;
+               }
+       }
+}