-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
67 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package utils | ||
|
||
// Clone returns a copy of b[:len(b)]. | ||
// The result may have additional unused capacity. | ||
// Clone(nil) returns nil. | ||
func Clone(b []byte) []byte { | ||
if b == nil { | ||
return nil | ||
} | ||
return append([]byte{}, b...) | ||
} | ||
|
||
// Equal reports whether a and b | ||
// are the same length and contain the same bytes. | ||
// A nil argument is equivalent to an empty slice. | ||
func Equal(a, b []byte) bool { | ||
// Neither cmd/compile nor gccgo allocates for these string conversions. | ||
return string(a) == string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package utils | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
func TestClone(t *testing.T) { | ||
var cloneTests = [][]byte{ | ||
[]byte(nil), | ||
[]byte{}, | ||
Clone([]byte{}), | ||
[]byte(strings.Repeat("a", 42))[:0], | ||
[]byte(strings.Repeat("a", 42))[:0:0], | ||
[]byte("short"), | ||
[]byte(strings.Repeat("a", 42)), | ||
} | ||
for _, input := range cloneTests { | ||
clone := Clone(input) | ||
if !Equal(clone, input) { | ||
t.Errorf("Clone(%q) = %q; want %q", input, clone, input) | ||
} | ||
|
||
if input == nil && clone != nil { | ||
t.Errorf("Clone(%#v) return value should be equal to nil slice.", input) | ||
} | ||
|
||
if input != nil && clone == nil { | ||
t.Errorf("Clone(%#v) return value should not be equal to nil slice.", input) | ||
} | ||
|
||
if cap(input) != 0 && unsafe.SliceData(input) == unsafe.SliceData(clone) { | ||
t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input) | ||
} | ||
} | ||
} |