Skip to content

Instantly share code, notes, and snippets.

@scottcagno
Created January 8, 2016 20:56
Show Gist options
  • Select an option

  • Save scottcagno/c6d83a6bc8cecf95a1ac to your computer and use it in GitHub Desktop.

Select an option

Save scottcagno/c6d83a6bc8cecf95a1ac to your computer and use it in GitHub Desktop.
Golang Slice Helper Functions
package byteslicer
func Copy(a []byte) []byte {
b := make([]byte, len(a))
copy(b, a)
return b
}
func Cut(a []byte, i, j int) []byte {
copy(a[i:], a[j:])
for k, n := len(a)-j+i, len(a); k < n; k++ {
a[k] = 0x00
}
a = a[:len(a)-j+i]
return a
}
func Delete(a []byte, i int) []byte {
copy(a[i:], a[i+1:])
a[len(a)-1] = 0x00
a = a[:len(a)-1]
return a
}
func Insert(a []byte, b byte, i int) []byte {
a = append(a, 0)
copy(a[i+1:], a[i:])
a[i] = b
return a
}
func InsertVector(a []byte, b []byte, i int) []byte {
a = append(a[:i], append(b, a[i:]...)...)
return a
}
func PushR(a []byte, b byte) []byte {
a = append(a, b)
return a
}
func PushL(a []byte, b byte) {
a = append([]byte{b}, a...)
}
func PopR(a []byte) byte {
b, a := a[len(a)-1], a[:len(a)-1]
return b
}
func PopL(a []byte) byte {
b, a := a[0], a[1:]
return b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment