github.com/goki/ki@v1.1.11/sliceclone/sliceclone.go (about) 1 // Copyright (c) 2019, The GoKi Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 Package sliceclone provides those basic slice cloning methods that I finally got tired of 7 rewriting all the time. 8 */ 9 package sliceclone 10 11 // String returns a cloned copy of the given string slice -- returns nil 12 // if slice has zero length 13 func String(sl []string) []string { 14 sz := len(sl) 15 if sz == 0 { 16 return nil 17 } 18 cp := make([]string, sz) 19 copy(cp, sl) 20 return cp 21 } 22 23 // StringExclude returns a cloned copy of the given string slice 24 // while excluding the list of specific items 25 func StringExclude(sl, exclude []string) []string { 26 sz := len(sl) 27 if sz == 0 { 28 return nil 29 } 30 if len(exclude) == 0 { 31 return String(sl) 32 } 33 var cp []string 34 for _, s := range sl { 35 ex := false 36 for _, e := range exclude { 37 if e == s { 38 ex = true 39 break 40 } 41 } 42 if ex { 43 continue 44 } 45 cp = append(cp, s) 46 } 47 return cp 48 } 49 50 // Byte returns a cloned copy of the given byte slice -- returns nil 51 // if slice has zero length 52 func Byte(sl []byte) []byte { 53 sz := len(sl) 54 if sz == 0 { 55 return nil 56 } 57 cp := make([]byte, sz) 58 copy(cp, sl) 59 return cp 60 } 61 62 // Rune returns a cloned copy of the given rune slice -- returns nil 63 // if slice has zero length 64 func Rune(sl []rune) []rune { 65 sz := len(sl) 66 if sz == 0 { 67 return nil 68 } 69 cp := make([]rune, sz) 70 copy(cp, sl) 71 return cp 72 } 73 74 // Bool returns a cloned copy of the given bool slice -- returns nil 75 // if slice has zero length 76 func Bool(sl []bool) []bool { 77 sz := len(sl) 78 if sz == 0 { 79 return nil 80 } 81 cp := make([]bool, sz) 82 copy(cp, sl) 83 return cp 84 } 85 86 // Int returns a cloned copy of the given int slice -- returns nil 87 // if slice has zero length 88 func Int(sl []int) []int { 89 sz := len(sl) 90 if sz == 0 { 91 return nil 92 } 93 cp := make([]int, sz) 94 copy(cp, sl) 95 return cp 96 } 97 98 // Int32 returns a cloned copy of the given int32 slice -- returns nil 99 // if slice has zero length 100 func Int32(sl []int32) []int32 { 101 sz := len(sl) 102 if sz == 0 { 103 return nil 104 } 105 cp := make([]int32, sz) 106 copy(cp, sl) 107 return cp 108 } 109 110 // Int64 returns a cloned copy of the given int64 slice -- returns nil 111 // if slice has zero length 112 func Int64(sl []int64) []int64 { 113 sz := len(sl) 114 if sz == 0 { 115 return nil 116 } 117 cp := make([]int64, sz) 118 copy(cp, sl) 119 return cp 120 } 121 122 // Float64 returns a cloned copy of the given float64 slice -- returns nil 123 // if slice has zero length 124 func Float64(sl []float64) []float64 { 125 sz := len(sl) 126 if sz == 0 { 127 return nil 128 } 129 cp := make([]float64, sz) 130 copy(cp, sl) 131 return cp 132 } 133 134 // Float32 returns a cloned copy of the given float32 slice -- returns nil 135 // if slice has zero length 136 func Float32(sl []float32) []float32 { 137 sz := len(sl) 138 if sz == 0 { 139 return nil 140 } 141 cp := make([]float32, sz) 142 copy(cp, sl) 143 return cp 144 }