fix CopyStrings and ShuffleStrings for slice when slice is nil

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud
2017-06-23 11:41:18 +08:00
parent e4af9dccb0
commit f98bc7d454
2 changed files with 34 additions and 7 deletions

View File

@@ -26,6 +26,9 @@ import (
// CopyStrings copies the contents of the specified string slice
// into a new slice.
func CopyStrings(s []string) []string {
if s == nil {
return nil
}
c := make([]string, len(s))
copy(c, s)
return c
@@ -41,6 +44,9 @@ func SortStrings(s []string) []string {
// ShuffleStrings copies strings from the specified slice into a copy in random
// order. It returns a new slice.
func ShuffleStrings(s []string) []string {
if s == nil {
return nil
}
shuffled := make([]string, len(s))
perm := utilrand.Perm(len(s))
for i, j := range perm {