aboutsummaryrefslogtreecommitdiff
path: root/src/lib/bytes
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-06-04 15:00:15 -0700
committerRob Pike <r@golang.org>2009-06-04 15:00:15 -0700
commit52e5d061c792750bc37bb43e1f1dbcf3d8dcfb85 (patch)
treea1aca68b91bcee0876e890158a39b34a7f683651 /src/lib/bytes
parent78933226f114c5c9e6de5addd693d86a7724f2f4 (diff)
downloadgo-52e5d061c792750bc37bb43e1f1dbcf3d8dcfb85.tar.xz
bytes.Copy
R=rsc DELTA=38 (38 added, 0 deleted, 0 changed) OCL=29895 CL=29895
Diffstat (limited to 'src/lib/bytes')
-rw-r--r--src/lib/bytes/bytes.go9
-rw-r--r--src/lib/bytes/bytes_test.go27
2 files changed, 36 insertions, 0 deletions
diff --git a/src/lib/bytes/bytes.go b/src/lib/bytes/bytes.go
index fe97b04958..a64b07b74f 100644
--- a/src/lib/bytes/bytes.go
+++ b/src/lib/bytes/bytes.go
@@ -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 {
diff --git a/src/lib/bytes/bytes_test.go b/src/lib/bytes/bytes_test.go
index 26b3fc21d0..4e7cdfad65 100644
--- a/src/lib/bytes/bytes_test.go
+++ b/src/lib/bytes/bytes_test.go
@@ -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;
+ }
+ }
+}