bytes

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: BSD-3-Clause Imports: 5 Imported by: 2

Documentation

Overview

Package bytes provide a library for working with byte or slice of bytes.

Index

Examples

Constants

View Source
const (
	// ASCIILetters contains list of lower and upper case characters in
	// ASCII.
	ASCIILetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

	// ASCIILettersNumber contains list of lower and upper case
	// characters in ASCII with numbers.
	ASCIILettersNumber = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"

	// HexaLETTERS contains list of hexadecimal characters in upper cases.
	HexaLETTERS = "0123456789ABCDEF"
	// HexaLetters contains list of hexadecimal characters in lower and
	// upper cases.
	HexaLetters = "0123456789abcedfABCDEF"
	// Hexaletters contains list of hexadecimal characters in lower cases.
	Hexaletters = "0123456789abcedf"
)

Variables

View Source
var (
	// ASCIISpaces contains list of white spaces in ASCII.
	ASCIISpaces = []byte{'\t', '\n', '\v', '\f', '\r', ' '} //nolint: gochecknoglobals
)

Functions

func AppendInt16

func AppendInt16(data *[]byte, v int16)

AppendInt16 into slice of byte.

func AppendInt32

func AppendInt32(data *[]byte, v int32)

AppendInt32 into slice of byte.

func AppendUint16

func AppendUint16(data *[]byte, v uint16)

AppendUint16 into slice of byte.

func AppendUint32

func AppendUint32(data *[]byte, v uint32)

AppendUint32 into slice of byte.

func Concat added in v0.5.0

func Concat(sb []byte, args ...interface{}) (out []byte)

Concat merge one or more slice of byte or string in arguments into slice of byte. Any type that is not []byte or string in arguments will be ignored.

func Copy

func Copy(src []byte) (dst []byte)

Copy slice of bytes from parameter.

func CutUntilToken

func CutUntilToken(line, token []byte, startAt int, checkEsc bool) ([]byte, int, bool)

CutUntilToken cut line until we found token.

If token found, it will return all cutted bytes before token, positition of byte after token, and boolean true.

If no token found, it will return false.

If `checkEsc` is true, token that is prefixed with escaped character '\' will be skipped.

Example
line := []byte(`abc \def ghi`)

cut, p, found := CutUntilToken(line, []byte("def"), 0, false)
fmt.Printf("'%s' %d %t\n", cut, p, found)

cut, p, found = CutUntilToken(line, []byte("def"), 0, true)
fmt.Printf("'%s' %d %t\n", cut, p, found)

cut, p, found = CutUntilToken(line, []byte("ef"), 0, true)
fmt.Printf("'%s' %d %t\n", cut, p, found)

cut, p, found = CutUntilToken(line, []byte("hi"), 0, true)
fmt.Printf("'%s' %d %t\n", cut, p, found)
Output:

'abc \' 8 true
'abc def ghi' 12 false
'abc \d' 8 true
'abc \def g' 12 true

func EncloseRemove

func EncloseRemove(line, leftcap, rightcap []byte) ([]byte, bool)

EncloseRemove given a line, remove all bytes inside it, starting from `leftcap` until the `rightcap` and return cutted line and status to true.

If no `leftcap` or `rightcap` is found, it will return line as is, and status will be false.

Example
line := []byte(`[[ ABC ]] DEF`)
leftcap := []byte(`[[`)
rightcap := []byte(`]]`)

got, changed := EncloseRemove(line, leftcap, rightcap)

fmt.Printf("'%s' %t\n", got, changed)
Output:

' DEF' true

func EncloseToken

func EncloseToken(line, token, leftcap, rightcap []byte) (
	newline []byte,
	status bool,
)

EncloseToken will find `token` in `line` and enclose it with bytes from `leftcap` and `rightcap`. If at least one token found, it will return modified line with true status. If no token is found, it will return the same line with false status.

Example
line := []byte(`// Copyright 2016-2018 "Shulhan <ms@kilabit.info>". All rights reserved.`)
token := []byte(`"`)
leftcap := []byte(`\`)
rightcap := []byte(`_`)

got, changed := EncloseToken(line, token, leftcap, rightcap)

fmt.Printf("'%s' %t\n", got, changed)
Output:

'// Copyright 2016-2018 \"_Shulhan <ms@kilabit.info>\"_. All rights reserved.' true

func InReplace

func InReplace(in, allowed []byte, c byte) (out []byte)

InReplace do a reverse replace on input, any characters that is not on allowed, will be replaced with character c.

func IsAlnum

func IsAlnum(b byte) bool

IsAlnum will return true if byte is ASCII alphanumeric character, otherwise it will return false.

func IsAlpha

func IsAlpha(b byte) bool

IsAlpha will return true if byte is ASCII alphabet character, otherwise it will return false.

func IsDigit

func IsDigit(b byte) bool

IsDigit will return true if byte is ASCII digit, otherwise it will return false.

func IsDigits

func IsDigits(data []byte) bool

IsDigits will return true if all bytes are ASCII digit, otherwise it will return false.

func IsHex

func IsHex(b byte) bool

IsHex will return true if byte is hexadecimal number, otherwise it will return false.

func IsSpace

func IsSpace(b byte) bool

IsSpace will return true if byte is ASCII white spaces character, otherwise it will return false.

func IsTokenAt

func IsTokenAt(line, token []byte, p int) bool

IsTokenAt return true if `line` at index `p` match with `token`, otherwise it will return false. Empty token always return false.

Example
line := []byte("Hello, world")
token := []byte("world")
token2 := []byte("worlds")
tokenEmpty := []byte{}

fmt.Printf("%t\n", IsTokenAt(line, tokenEmpty, 6))
fmt.Printf("%t\n", IsTokenAt(line, token, 6))
fmt.Printf("%t\n", IsTokenAt(line, token, 7))
fmt.Printf("%t\n", IsTokenAt(line, token, 8))
fmt.Printf("%t\n", IsTokenAt(line, token2, 8))
Output:

false
false
true
false
false

func JSONEscape

func JSONEscape(in []byte) []byte

JSONEscape escape the following character: `"` (quotation mark), `\` (reverse solidus), `/` (solidus), `\b` (backspace), `\f` (formfeed), `\n` (newline), `\r` (carriage return`), `\t` (horizontal tab), and control character from 0 - 31.

References:

* https://tools.ietf.org/html/rfc7159#page-8

func JSONUnescape

func JSONUnescape(in []byte, strict bool) ([]byte, error)

JSONUnescape unescape JSON bytes, reversing what BytesJSONEscape do.

If strict is true, any unknown control character will be returned as error. For example, in string "\x", "x" is not valid control character, and the function will return empty string and error. If strict is false, it will return "x".

func PrintHex

func PrintHex(title string, data []byte, col int)

PrintHex will print each byte in slice as hexadecimal value into N column length.

func Random

func Random(seed []byte, n int) []byte

Random generate random sequence of value from seed with fixed length.

This function assume that random generator has been seeded.

func ReadHexByte

func ReadHexByte(data []byte, x int) (b byte, ok bool)

ReadHexByte read two characters from data start from index "x" and convert them to byte.

func ReadInt16

func ReadInt16(data []byte, x uint) int16

ReadInt16 will convert two bytes from data start at `x` into int16 and return it.

func ReadInt32

func ReadInt32(data []byte, x uint) int32

ReadInt32 will convert four bytes from data start at `x` into int32 and return it.

func ReadUint16

func ReadUint16(data []byte, x uint) uint16

ReadUint16 will convert two bytes from data start at `x` into uint16 and return it.

func ReadUint32

func ReadUint32(data []byte, x uint) uint32

ReadUint32 will convert four bytes from data start at `x` into uint32 and return it.

func SkipAfterToken

func SkipAfterToken(line, token []byte, startAt int, checkEsc bool) (int, bool)

SkipAfterToken skip all bytes until matched token is found and return the index after the token and boolean true.

If `checkEsc` is true, token that is prefixed with escaped character '\' will be considered as non-match token.

If no token found it will return -1 and boolean false.

Example
line := []byte(`abc \def ghi`)

p, found := SkipAfterToken(line, []byte("def"), 0, false)
fmt.Printf("%d %t\n", p, found)

p, found = SkipAfterToken(line, []byte("def"), 0, true)
fmt.Printf("%d %t\n", p, found)

p, found = SkipAfterToken(line, []byte("ef"), 0, true)
fmt.Printf("%d %t\n", p, found)

p, found = SkipAfterToken(line, []byte("hi"), 0, true)
fmt.Printf("%d %t\n", p, found)
Output:

8 true
12 false
8 true
12 true

func ToLower

func ToLower(data *[]byte)

ToLower convert slice of bytes to lower cases, in places.

func ToUpper

func ToUpper(data *[]byte)

ToUpper convert slice of bytes to upper cases, in places.

func TokenFind

func TokenFind(line, token []byte, startat int) (at int)

TokenFind return the first index of matched token in line, start at custom index. If "startat" parameter is less than 0, then it will be set to 0. If token is empty or no token found it will return -1.

Example
line := []byte("// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.")
token := []byte("right")

at := TokenFind(line, token, 0)

fmt.Printf("%d\n", at)
Output:

7

func WriteUint16

func WriteUint16(data *[]byte, x uint, v uint16)

WriteUint16 into slice of byte.

func WriteUint32

func WriteUint32(data *[]byte, x uint, v uint32)

WriteUint32 into slice of byte.

Types

This section is empty.

Jump to

Keyboard shortcuts

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