byteslice

package module
v0.0.0-...-ea188ca Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2020 License: MIT Imports: 2 Imported by: 0

README

= byteslice

image:https://github.com/rlespinasse/byteslice/workflows/Test/badge.svg["Test Status", link="https://github.com/rlespinasse/byteslice/actions?query=workflow%3ATest"]
image:https://coveralls.io/repos/github/rlespinasse/byteslice/badge.svg?branch=v0["Coverage Status", link="https://coveralls.io/github/rlespinasse/byteslice?branch=v0"]
image:https://pkg.go.dev/badge/github.com/rlespinasse/byteslice["GoDoc", link="https://pkg.go.dev/github.com/rlespinasse/byteslice"]
image:https://goreportcard.com/badge/github.com/rlespinasse/byteslice["Go Report Card", link="https://goreportcard.com/report/github.com/rlespinasse/byteslice"]

== How to

Run `make help` to see the available commands.

=== test it with coverage

[source,shell]
-----
$ make test
ok  	github.com/rlespinasse/byteslice	0.007s	coverage: 100.0% of statements
-----

=== launch the benchmark

[source,shell]
-----
$ make bench
goos: ...
goarch: ...
pkg: github.com/rlespinasse/byteslice
BenchmarkRBit/get_low_bit_of_high_nibble-8              2000000000               1.76 ns/op
BenchmarkRBit/get_low_bit-8                             2000000000               1.76 ns/op
...
PASS
ok      github.com/rlespinasse/byteslice        154.701s
-----

Documentation

Overview

Package byteslice provides functions to manipulate byte slices with endianness support (little, and big endian order)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flip

func Flip(data []byte) []byte

Flip apply NOT operation to a byte slice to flip it.

Example
data := []byte{0xDA, 0x99, 0xBA}

output := Flip(data)
fmt.Printf("%x\n", output)
Output:

256645

func LPad

func LPad(data []byte, length int, filler byte) []byte

LPad pads the left-side of a byte slice with a filler byte.

Example
data := []byte{0x55, 0xDA, 0xBA}

fmt.Printf("%x\n", LPad(data, 5, 0x22))
Output:

222255daba

func LSet

func LSet(data, setData []byte) []byte

LSet apply OR operation on a byte slice with an "set" byte slice using big endian order.

Example
data := []byte{0xAA, 0xCA, 0x55}
setData := []byte{0x10, 0x12}

fmt.Printf("%x\n", LSet(data, setData))
Output:

bada55

func LShift

func LShift(data []byte, shift uint64) []byte

LShift apply left shift operation to an byte slice.

Example
data := []byte{0xDA, 0x99, 0xBA}

output := LShift(data, 8)
fmt.Printf("%x\n", output)
Output:

99ba00

func LSubset

func LSubset(data []byte, leastSignificantBit, mostSignificantBit uint64) []byte

LSubset get the byte slice of a subset of the big endian ordered data byte defined by the least significant bit and the most significant bit.

Example
data := []byte{0xDA, 0x99, 0xBA}

output := LSubset(data, 6, 17)
fmt.Printf("%x\n", output)
Output:

a660

func LToggle

func LToggle(data, toggleData []byte) []byte

LToggle apply XOR operation on a byte slice with an "toggle" byte slice using big endian order.

Example
data := []byte{0xAB, 0xCB, 0x44}
setData := []byte{0x11, 0x11, 0x11}

fmt.Printf("%x\n", LToggle(data, setData))
Output:

bada55

func LUnset

func LUnset(data, unsetData []byte) []byte

LUnset apply AND operation on a byte slice with an "unset" byte slice using big endian order.

Example
data := []byte{0x11, 0x11, 0x10}
unsetData := []byte{0x01, 0x01}

output := LUnset(data, unsetData)
fmt.Printf("%x\n", output)
Output:

010110

func RBit

func RBit(data byte, bit uint8) byte

RBit get a specific bit of the little endian ordered data byte.

Example
fmt.Printf("%x\n", RBit(0x55, 6))
Output:

40

func RBitsSubset

func RBitsSubset(data byte, leastSignificantBit, mostSignificantBit uint8) byte

RBitsSubset get the byte value of a subset of the little endian ordered data byte defined by the least significant bit and the most significant bit.

Example
fmt.Printf("%x\n", RBitsSubset(0x55, 2, 6))
Output:

15

func RPad

func RPad(data []byte, length int, filler byte) []byte

RPad pads the right-side of a byte slice with a filler byte.

Example
data := []byte{0x55, 0xDA, 0xBA}

fmt.Printf("%x\n", RPad(data, 5, 0x22))
Output:

55daba2222

func RSet

func RSet(data, setData []byte) []byte

RSet apply OR operation on a byte slice with an "set" byte slice using little endian order.

Example
data := []byte{0xBA, 0xCA, 0x44}
setData := []byte{0x12, 0x11}

fmt.Printf("%x\n", RSet(data, setData))
Output:

bada55

func RShift

func RShift(data []byte, shift uint64) []byte

RShift apply right shift operation to an byte slice.

Example
data := []byte{0xDA, 0x99, 0xBA}

output := RShift(data, 8)
fmt.Printf("%x\n", output)
Output:

00da99

func RSubset

func RSubset(data []byte, leastSignificantBit, mostSignificantBit uint64) []byte

RSubset get the byte slice of a subset of the little endian ordered data byte defined by the least significant bit and the most significant bit.

Example
data := []byte{0xDA, 0x99, 0xBA}
output := RSubset(data, 6, 17)
fmt.Printf("%x\n", output)
Output:

0a66

func RToggle

func RToggle(data, toggleData []byte) []byte

RToggle apply XOR operation on a byte slice with an "toggle" byte slice using little endian order.

Example
data := []byte{0xDA, 0x99, 0xBA}
toogleData := []byte{0x77, 0x88, 0x11, 0xAD, 0x11, 0xAB}

output := RToggle(data, toogleData)
fmt.Printf("%x\n", output)
Output:

778811778811

func RUnset

func RUnset(data, unsetData []byte) []byte

RUnset apply AND operation on a byte slice with an "unset" byte slice using little endian order.

Example
data := []byte{0x11, 0x11, 0x00}
unsetData := []byte{0x01, 0x01}

output := RUnset(data, unsetData)
fmt.Printf("%x\n", output)
Output:

110100

func Reverse

func Reverse(data []byte) []byte

Reverse change the order of the byte slice.

Example
data := []byte{0x55, 0xDA, 0xBA}

fmt.Printf("%x\n", Reverse(data))
Output:

bada55

func Set

func Set(data, setData []byte) ([]byte, error)

Set apply OR operation on a byte slice with an "set" byte slice (must have the same size).

Example
data := []byte{0xDA, 0x99, 0xBA}
setData := []byte{0x11, 0x22, 0x33}

output, _ := Set(data, setData)
fmt.Printf("%x\n", output)
Output:

dbbbbb

func Toggle

func Toggle(data, toggleData []byte) ([]byte, error)

Toggle apply XOR operation on a byte slice with an "toggle" byte slice (must have the same size).

Example
data := []byte{0xbb, 0xdb, 0x54}
toggle := []byte{0x01, 0x01, 0x01}

output, _ := Toggle(data, toggle)
fmt.Printf("%x\n", output)
Output:

bada55
Example (Simple)
data := []byte{0x00, 0x01, 0x00}
toggle := []byte{0x01, 0x00, 0x01}

output, _ := Toggle(data, toggle)
fmt.Printf("%x\n", output)
Output:

010101

func Unset

func Unset(data, unsetData []byte) ([]byte, error)

Unset apply AND operation on a byte slice with an "unset" byte slice (must have the same size).

Example
data := []byte{0x01, 0x11, 0x00}
unsetData := []byte{0x01, 0x01, 0x01}

output, _ := Unset(data, unsetData)
fmt.Printf("%x\n", output)
Output:

010100

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL